forked from blocknetdx/go-xrouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (87 loc) · 2.46 KB
/
main.go
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
// Copyright (c) 2020 The Blocknet developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/hex"
"fmt"
"log"
"os"
"time"
"github.com/blocknetdx/go-xrouter/blockcfg"
"github.com/blocknetdx/go-xrouter/xrouter"
)
func main() {
log.SetOutput(os.Stdout)
config := blockcfg.MainnetParams
// Manually set seed node (via ip or dns)
//config.DNSSeeds = []chaincfg.DNSSeed{
// {"seed1.blocknet.co", false}, // optional direct connect to trusted node
//}
// Instantiate the xrouter client
client, err := xrouter.NewClient(config)
if err != nil {
log.Println(err.Error())
return
}
// Start xrouter (this will begin querying the network)
client.Start()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
defer shutdown(client)
// Wait for xrouter to be ready
if ready, err := client.WaitForXRouter(ctx); err != nil || !ready {
errStr := ""
if err != nil {
errStr = err.Error()
}
log.Println("XRouter failed to connect and obtain service nodes", errStr)
return
}
log.Printf("XRouter is ready")
// List all network services
//for _, service := range client.ListNetworkServices() {
// log.Printf(service)
//}
ctx2, cancel2 := context.WithTimeout(ctx, 5*time.Second)
defer cancel2()
queryCount := 1
if err := client.WaitForServices(ctx2, []string{"xrs::CCSinglePrice", "xr::BTC"}, queryCount); err != nil {
log.Printf("error: %v", err)
return
}
{
// Query the price oracle to obtain Bitcoin's price in USD
var params []interface{}
params = append(params, "BTC", "USD")
if reply, flag, err := client.CallService("xrs::CCSinglePrice", params, queryCount); err != nil {
log.Printf("error: %v", err)
return
} else {
if reply == nil {
log.Printf("No replies found. %v\n", flag)
} else {
log.Printf("Result from %v: %v. %v", hex.EncodeToString(reply.Pubkey), string(reply.Reply), flag)
}
}
}
{
// Query the BTC oracle to obtain the chain height
if reply, flag, err := client.GetBlockCount("xr::BTC", queryCount); err != nil {
log.Printf("error: %v", err)
return
} else {
if reply == nil {
log.Printf("No replies found. %v\n", flag)
} else {
log.Printf("Result from %v: %v. %v", hex.EncodeToString(reply.Pubkey), string(reply.Reply), flag)
}
}
}
}
func shutdown(client *xrouter.Client) {
if err := client.Stop(); err != nil {
fmt.Printf("error shutdown: %v", err)
}
}