1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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)
}
}
|