Created
January 10, 2018 18:57
-
-
Save donovank/105b3eb05963c692ab25999ef0ce8d84 to your computer and use it in GitHub Desktop.
Shows Bitcoin transactions and updates when a new block is added to the blockchain.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"strconv" | |
"time" | |
) | |
type Block struct { | |
Hash string `json:"hash"` | |
Time int `json:"time"` | |
BlockIndex int `json:"block_index"` | |
Height int `json:"height"` | |
TxIndexes []int `json:"txIndexes"` | |
} | |
type RawTransaction struct { | |
Out []Transaction `json:"out"` | |
} | |
type Transaction struct { | |
Addr string `json:"addr"` | |
Value int `json:"value"` | |
} | |
type BTCPrice struct { | |
Last string `json:"last"` | |
} | |
var lasthash string | |
var currentex float64 | |
var bltrans int | |
func reqFromUrl(url string) []byte { | |
res, err := http.Get(url) | |
if err != nil { | |
panic(err) | |
} | |
b, err := ioutil.ReadAll(res.Body) | |
defer res.Body.Close() | |
if err != nil { | |
panic(err) | |
} | |
return b | |
} | |
func main() { | |
go func() { | |
for { | |
btcp := new(BTCPrice) | |
err := json.Unmarshal(reqFromUrl("https://www.bitstamp.net/api/ticker/"), btcp) | |
if err != nil { | |
panic(err) | |
} | |
currentex, err = strconv.ParseFloat(btcp.Last, 64) | |
if err != nil { | |
panic(err) | |
} | |
time.Sleep(time.Second * 60) | |
} | |
}() | |
time.Sleep(time.Second * 3) | |
for { | |
bl := new(Block) | |
err := json.Unmarshal(reqFromUrl("https://blockchain.info/latestblock"), bl) | |
if err != nil { | |
panic(err) | |
} | |
if bl.Hash == lasthash { | |
time.Sleep(time.Minute) | |
continue | |
} else { | |
lasthash = bl.Hash | |
} | |
for _, x := range bl.TxIndexes { | |
tr := new(RawTransaction) | |
err = json.Unmarshal(reqFromUrl(fmt.Sprintf("https://blockchain.info/rawtx/%d", x)), tr) | |
if err != nil { | |
panic(err) | |
} | |
for _, a := range tr.Out { | |
if a.Addr == "" { | |
continue | |
} | |
btc := float64(a.Value) / float64(100000000) | |
fmt.Printf("\033[1;37m①\033[0;32m #%-4d \033[1;37m②\033[0;32m %-7d \033[1;37m③\033[0;32m %-34s \033[1;37m④\033[0;32m ฿%-12f \033[1;37m⑤\033[0;32m $%-12.2f\n", | |
bltrans, bl.Height, a.Addr, btc, currentex*btc) | |
bltrans++ | |
} | |
} | |
bltrans = 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment