-
Notifications
You must be signed in to change notification settings - Fork 3
/
horizon.go
299 lines (261 loc) · 7.17 KB
/
horizon.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package xlm
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/pkg/errors"
utils "github.com/Varunram/essentials/utils"
protocols "github.com/stellar/go/protocols/horizon"
)
// GetLedgerData gets the latest data from the ledger
func GetLedgerData(blockNumberx int) ([]byte, error) {
var err error
var data []byte
blockNumber, err := utils.ToString(blockNumberx)
if err != nil {
return data, err
}
resp, err := http.Get(TestNetClient.HorizonURL + "ledgers/" + blockNumber)
if err != nil || resp.Status != "200 OK" {
return data, errors.New("API Request did not succeed")
}
defer func() {
if ferr := resp.Body.Close(); ferr != nil {
err = ferr
}
}()
data, err = ioutil.ReadAll(resp.Body)
return data, err
}
// GetBlockHash gets the block hash corresponding to a block number
func GetBlockHash(blockNumber int) (string, error) {
var err error
var hash string
b, err := GetLedgerData(blockNumber)
if err != nil {
return hash, errors.Wrap(err, "could not get updated ledger data")
}
var x protocols.Ledger
err = json.Unmarshal(b, &x)
hash = x.Hash
log.Printf("The block hash for block %d is: %s and the prev hash is %s", x.Sequence, hash, x.PrevHash)
return hash, err
}
// GetLatestBlockHash gets the lastest block hash
func GetLatestBlockHash() (string, error) {
url := TestNetClient.HorizonURL + "ledgers?cursor=now&order=desc&limit=1"
resp, err := http.Get(url)
if err != nil || resp.Status != "200 OK" {
return "", errors.New("API Request did not succeed")
}
defer func() {
if ferr := resp.Body.Close(); ferr != nil {
err = ferr
}
}()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "could not read response body")
}
// hacks below follow because of stellar's incomplete go sdk support
var x map[string]*json.RawMessage
err = json.Unmarshal(data, &x)
if err != nil {
return "", errors.Wrap(err, "could not unmarshal json")
}
var y map[string]*json.RawMessage
err = json.Unmarshal(*x["_embedded"], &y)
if err != nil {
return "", errors.Wrap(err, "could not unmarshal json")
}
var z []protocols.Ledger
err = json.Unmarshal(*y["records"], &z)
if err != nil {
return "", errors.Wrap(err, "could not unmarshal json")
}
return z[0].Hash, nil
}
// GetLatestBlock fetches the latest block from the blockchain
func GetLatestBlock() (int32, error) {
url := TestNetClient.HorizonURL + "ledgers?cursor=now&order=desc&limit=1"
resp, err := http.Get(url)
if err != nil || resp.Status != "200 OK" {
return -1, errors.New("API Request did not succeed")
}
defer func() {
if ferr := resp.Body.Close(); ferr != nil {
err = ferr
}
}()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -1, errors.Wrap(err, "could not read response body")
}
// hacks below follow because of stellar's incomplete go sdk support
var x map[string]*json.RawMessage
err = json.Unmarshal(data, &x)
if err != nil {
return -1, errors.Wrap(err, "could not unmarshal json")
}
var y map[string]*json.RawMessage
err = json.Unmarshal(*x["_embedded"], &y)
if err != nil {
return -1, errors.Wrap(err, "could not unmarshal json")
}
var z []protocols.Ledger
err = json.Unmarshal(*y["records"], &z)
if err != nil {
return -1, errors.Wrap(err, "could not unmarshal json")
}
return z[0].Sequence, nil
}
// GetAccountData gets the account data
func GetAccountData(a string) ([]byte, error) {
var err error
var data []byte
resp, err := http.Get(TestNetClient.HorizonURL + "accounts/" + a)
if err != nil {
return data, errors.Wrap(err, "could not get /accounts/ endpoint from API")
}
if resp.Status != "200 OK" {
return data, errors.New("Request did not succeed")
}
defer func() {
if ferr := resp.Body.Close(); ferr != nil {
err = ferr
}
}()
data, err = ioutil.ReadAll(resp.Body)
return data, err
}
// GetNativeBalance gets the xlm balance of a specific account
func GetNativeBalance(publicKey string) float64 {
var err error
b, err := GetAccountData(publicKey)
if err != nil {
log.Println(err)
return -1
}
var x protocols.Account
err = json.Unmarshal(b, &x)
if err != nil {
log.Println(err)
return -1
}
for _, balance := range x.Balances {
if balance.Asset.Type == "native" {
balance, err := utils.ToFloat(balance.Balance)
if err != nil {
return 0
}
return balance
}
}
log.Println("Native balance not found")
return 0
}
// GetAssetBalance gets the balance of the user in the specific asset
func GetAssetBalance(publicKey string, assetName string) float64 {
var err error
b, err := GetAccountData(publicKey)
if err != nil {
log.Println(err)
return -1
}
var x protocols.Account
err = json.Unmarshal(b, &x)
if err != nil {
log.Println(err)
return -1
}
for _, balance := range x.Balances {
if balance.Asset.Code == assetName {
balance, err := utils.ToFloat(balance.Balance)
if err != nil {
return 0
}
return balance
}
}
// if the asset does not exist, the balance associated is zero
// but to differentiate between not having the asset and never
// having had the asset, we return -1
log.Println("Asset: ", assetName, " publickey: ", publicKey, " balance not found")
return 0
}
// GetAssetTrustLimit gets the trust limit that the user has with an issuer
func GetAssetTrustLimit(publicKey string, assetName string) float64 {
var err error
b, err := GetAccountData(publicKey)
if err != nil {
return -1
}
var x protocols.Account
err = json.Unmarshal(b, &x)
if err != nil {
return -1
}
for _, balance := range x.Balances {
if balance.Asset.Code == assetName {
balance, err := utils.ToFloat(balance.Balance)
if err != nil {
return 0
}
return balance
}
}
return 0
}
// GetAllBalances gets all the balances associated with a certain account.
func GetAllBalances(publicKey string) ([]protocols.Balance, error) {
account, err := ReturnSourceAccountPubkey(publicKey)
if err != nil {
log.Println(err)
return nil, errors.Wrap(err, "could not load account")
}
return account.Balances, nil
}
// HasStableCoin checks whether the PublicKey has a stablecoin balance
func HasStableCoin(publicKey string) bool {
account, err := ReturnSourceAccountPubkey(publicKey)
if err != nil {
return false
}
for _, balance := range account.Balances {
if balance.Asset.Code == "STABLEUSD" {
return true
}
}
return false
}
// GetTransactionData gets tx data
func GetTransactionData(txhash string) ([]byte, error) {
var err error
var data []byte
resp, err := http.Get(TestNetClient.HorizonURL + "transactions/" + txhash)
if err != nil || resp.Status != "200 OK" {
// check here since if we don't, we need to check the body of the unmarshalled
// response to see if we have 0
return data, errors.New("API Request did not succeed")
}
defer func() {
if ferr := resp.Body.Close(); ferr != nil {
err = ferr
}
}()
data, err = ioutil.ReadAll(resp.Body)
return data, err
}
// GetTransactionHeight gets height at which a tx was confirmed
func GetTransactionHeight(txhash string) (int, error) {
var err error
var txheight int
b, err := GetTransactionData(txhash)
if err != nil {
return txheight, errors.Wrap(err, "could not get transaction data")
}
var x protocols.Transaction
err = json.Unmarshal(b, &x)
return int(x.Ledger), err
}