Using go-bitdb you can query a bitdb node and connect to a bitsocket node in golang.
BitDb is an autonomous database that continuously synchronizes itself with Bitcoin. BitDB stores every bitcoin transaction in a structured document format that can be queried against like a regular database. With a simple MongoDB query, anyone can easily query, filter, and build powerful decentralized applications on Bitcoin.
Bitsocket is a bitcoin notification service. By constructing a bitquery, you can subscribe to the bitcoin blockchain and recieve transactions that meet a certain criteria.
go get github.com/dylankilkenny/go-bitdb
The following code creates a BitDb instance and queries a bitdb node for a transaction by its TxID.
The jq
string is the processing function which will return an object containing the amount in the first output of the transaction
version := 3 // API version
bitdbURL := "https://bitdb.network/q/" // API url
apiKey := "qq54zc33pttdp6l8ycnnj99ahan8a2hfrygqyz0fc3"
BitDb := bitdb.New(version, bitdbURL, apiKey) // Create new instance
txHash := BitDb.TxHash{Hash: "ffff5c6d0660068381b26fe3546eb2a51faf1a0a1a707db1ca32a5b168a7301b"}
jq := ".[] | .out[0] | {amount: .e.v}"
response, err := BitDb.Request(txHash, jq)
if err != nil {
fmt.Println(err)
}
confirmed, _ := response.Confirmed.(map[string]interface{})
fmt.Println(confirmed["amount"])
Output:
7051
Rather than using structs you can query with a json string:
var bitquery = []byte(`
{
"v": 3,
"q": {
"find": {
"tx.h": "ffff5c6d0660068381b26fe3546eb2a51faf1a0a1a707db1ca32a5b168a7301b"
}
},
"r": {
"f": ".[] | .out[0] | {amount: .e.v}"
}
}
`)
response, err := BitDb.RawRequest(bitquery)
if err != nil {
fmt.Println(err)
}
confirmed, _ := response.Confirmed.(map[string]interface{})
fmt.Println(confirmed["amount"])
version := 3
bitdbURL := "https://bitdb.network/q/"
bitsocket := bitdb.NewSocket(version, bitdbURL)
events, err := bitsocket.Stream("", ".[] | .out[0] | {amount: .e.v}")
if err != nil {
t.Error(err)
}
for tx := range events {
// do something with transaction
}