-
Notifications
You must be signed in to change notification settings - Fork 1
/
explorer.go
105 lines (83 loc) · 2.68 KB
/
explorer.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
102
103
104
105
package erg
import (
"encoding/json"
"fmt"
"io"
"net/url"
"strconv"
"github.com/hashicorp/go-retryablehttp"
"github.com/nightowlcasino/nightowl/config"
"github.com/spf13/viper"
)
var (
oracleAddress = "4FC5xSYb7zfRdUhm6oRmE11P2GJqSMY8UARPbHkmXEq6hTinXq4XNWdJs73BEV44MdmJ49Qo"
getErgTxsEndpoint = "/api/v1/transactions/"
)
type Explorer struct {
client *retryablehttp.Client
url *url.URL
user string
pass string
}
func NewExplorer(client *retryablehttp.Client) (*Explorer, error) {
var node *Explorer
config.SetExplorerDefaults()
var u = &url.URL{
Scheme: viper.Get("explorer_node.scheme").(string),
Host: viper.Get("explorer_node.fqdn").(string)+":"+strconv.Itoa(viper.Get("explorer_node.port").(int)),
}
node = &Explorer{
client: client,
url: u,
user: viper.Get("ergo_node.user").(string),
pass: viper.Get("ergo_node.password").(string),
}
return node, nil
}
func (e *Explorer) GetOracleTxs(minHeight, maxHeight, limit, offset int) (ErgBoxIds, error) {
var ergTxs ErgBoxIds
endpoint := fmt.Sprintf("%s/api/v1/addresses/%s/transactions?fromHeight=%d&toHeight=%d&limit=%d&offset=%d", e.url.String(), oracleAddress, minHeight, maxHeight, limit, offset)
req, err := retryablehttp.NewRequest("GET", endpoint, nil)
if err != nil {
return ergTxs, fmt.Errorf("failed to build oracle transactions request - %s", err.Error())
}
resp, err := e.client.Do(req)
if err != nil {
return ergTxs, fmt.Errorf("error calling ergo api explorer - %s", err.Error())
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return ergTxs, fmt.Errorf("error reading erg txs body - %s", err.Error())
}
err = json.Unmarshal(body, &ergTxs)
if err != nil {
return ergTxs, fmt.Errorf("error unmarshalling erg txs - %s", err.Error())
}
return ergTxs, nil
}
func (e *Explorer) GetErgTx(unconfirmedTx string) (ErgTx, error) {
var ergTx ErgTx
endpoint := fmt.Sprintf("%s%s%s", e.url.String(), getErgTxsEndpoint, unconfirmedTx)
req, err := retryablehttp.NewRequest("GET", endpoint, nil)
if err != nil {
return ergTx, fmt.Errorf("failed to build explorer transaction request - %s", err.Error())
}
resp, err := e.client.Do(req)
if err != nil {
return ergTx, fmt.Errorf("error calling ergo api explorer - %s", err.Error())
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return ergTx, fmt.Errorf("error reading erg tx body - %s", err.Error())
}
if resp.StatusCode != 200 {
return ergTx, fmt.Errorf("http status code != 200 - %s", resp.Status)
}
err = json.Unmarshal(body, &ergTx)
if err != nil {
return ergTx, fmt.Errorf("error unmarshalling erg tx - %s", err.Error())
}
return ergTx, nil
}