summaryrefslogtreecommitdiff
path: root/blockmon.go
diff options
context:
space:
mode:
Diffstat (limited to 'blockmon.go')
-rw-r--r--blockmon.go122
1 files changed, 122 insertions, 0 deletions
diff --git a/blockmon.go b/blockmon.go
new file mode 100644
index 0000000..0556e8f
--- /dev/null
+++ b/blockmon.go
@@ -0,0 +1,122 @@
+package main
+
+import "encoding/json"
+import "fmt"
+//import "os"
+import "net/http"
+import "io/ioutil"
+import "time"
+
+type Block struct {
+ Hash string
+ Tx []string
+ Time int64
+ PreviousBlockHash string
+}
+
+type Tx struct {
+ Vout []Vout
+ Confirmations int
+ Time int64
+}
+
+type Vout struct {
+ Value float64
+ ScriptPubKey ScriptPubKey
+}
+
+type ScriptPubKey struct {
+ Addresses []string
+}
+
+func getContent(url string) ([]byte, error) {
+ // Build the request
+ req, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ return nil, err
+ }
+ // Send the request via a client
+ client := &http.Client{}
+ resp, err := client.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ // Defer the closing of the body
+ defer resp.Body.Close()
+ // Read the content into a byte array
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+ // At this point we're done - simply return the bytes
+ return body, nil
+}
+var latestBlock Block
+var previousHash string
+
+func main() {
+
+for {
+
+content, err := getContent("http://ws.ltcchain.com/latestblock")
+
+if err != nil {
+ // Uh-oh!
+} else {
+ // Note that this will require you add fmt to your list of imports.
+}
+
+err = json.Unmarshal(content, &latestBlock)
+fmt.Println("Latest hash: " + latestBlock.Hash)
+if (previousHash != latestBlock.Hash) {
+
+txid := "http://ws.ltcchain.com/tx/" + latestBlock.Tx[0]
+content, err = getContent(txid)
+
+var tx Tx
+err = json.Unmarshal(content, &tx)
+
+if err != nil {
+ fmt.Println(err)
+}
+
+if (tx.Vout[0].ScriptPubKey.Addresses[0] == "LWjXQsWXZnsoNgfwxG65EEm1291UKag96H") {
+ fmt.Println("***WE FOUND A BLOCK!!****")
+ fmt.Println("Block found by " + tx.Vout[0].ScriptPubKey.Addresses[0])
+ fmt.Println("****WE FOUND A BLOCK!!***")
+} else {
+ fmt.Println("Block found by " + tx.Vout[0].ScriptPubKey.Addresses[0])
+}
+
+originalBlock := latestBlock
+
+for {
+ if (previousHash == "" || previousHash == latestBlock.PreviousBlockHash) {
+ previousHash = originalBlock.Hash
+ break;
+ }
+ blockUrl := "http://ws.ltcchain.com/block/" + latestBlock.PreviousBlockHash
+ content, err = getContent(blockUrl)
+ err = json.Unmarshal(content, &latestBlock)
+ fmt.Println("Recent hash: " + latestBlock.Hash)
+ if (latestBlock.Hash != previousHash) {
+ txid := "http://ws.ltcchain.com/tx/" + latestBlock.Tx[0]
+ content, err = getContent(txid)
+
+ var tx Tx
+ err = json.Unmarshal(content, &tx)
+if (tx.Vout[0].ScriptPubKey.Addresses[0] == "LWjXQsWXZnsoNgfwxG65EEm1291UKag96H") {
+ fmt.Println("***WE FOUND A BLOCK!!****")
+ fmt.Println("Block found by " + tx.Vout[0].ScriptPubKey.Addresses[0])
+ fmt.Println("****WE FOUND A BLOCK!!***")
+} else {
+ fmt.Println("Block found by " + tx.Vout[0].ScriptPubKey.Addresses[0])
+}
+ }
+
+}
+previousHash = originalBlock.Hash
+}
+time.Sleep(300*time.Second)
+}
+}