forked from eoscanada/eos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
762 lines (632 loc) · 22.4 KB
/
api.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
package eos
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"strings"
"sync"
"time"
"github.com/eoscanada/eos-go/ecc"
)
type API struct {
HttpClient *http.Client
BaseURL string
Signer Signer
Debug bool
Compress CompressionType
// Header is one or more headers to be added to all outgoing calls
Header http.Header
DefaultMaxCPUUsageMS uint8
DefaultMaxNetUsageWords uint32 // in 8-bytes words
lastGetInfo *InfoResp
lastGetInfoStamp time.Time
lastGetInfoLock sync.Mutex
customGetRequiredKeys func(ctx context.Context, tx *Transaction) ([]ecc.PublicKey, error)
enablePartialRequiredKeys bool
}
func New(baseURL string) *API {
api := &API{
HttpClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableKeepAlives: true, // default behavior, because of `nodeos`'s lack of support for Keep alives.
},
},
BaseURL: strings.TrimRight(baseURL, "/"),
Compress: CompressionZlib,
Header: make(http.Header),
}
return api
}
// FixKeepAlives tests the remote server for keepalive support (the
// main `nodeos` software doesn't in the version from March 22nd
// 2018). Some endpoints front their node with a keep-alive
// supporting web server. Adjust the `KeepAlive` support of the
// client accordingly.
func (api *API) FixKeepAlives(ctx context.Context) bool {
// Yeah, to provoke a keep alive, you need to query twice.
for i := 0; i < 5; i++ {
_, err := api.GetInfo(ctx)
if api.Debug {
log.Println("err", err)
}
if err == io.EOF {
if tr, ok := api.HttpClient.Transport.(*http.Transport); ok {
tr.DisableKeepAlives = true
return true
}
}
_, err = api.GetNetConnections(ctx)
if api.Debug {
log.Println("err", err)
}
if err == io.EOF {
if tr, ok := api.HttpClient.Transport.(*http.Transport); ok {
tr.DisableKeepAlives = true
return true
}
}
}
return false
}
func (api *API) EnableKeepAlives() bool {
if tr, ok := api.HttpClient.Transport.(*http.Transport); ok {
tr.DisableKeepAlives = false
return true
}
return false
}
func (api *API) SetCustomGetRequiredKeys(f func(ctx context.Context, tx *Transaction) ([]ecc.PublicKey, error)) {
api.customGetRequiredKeys = f
}
func (api *API) UsePartialRequiredKeys() {
api.enablePartialRequiredKeys = true
}
func (api *API) getPartialRequiredKeys(ctx context.Context, tx *Transaction) ([]ecc.PublicKey, error) {
// loop to get all the authorizers, and dedupe
var authorizers []PermissionLevel
for _, act := range tx.Actions {
for _, pl := range act.Authorization {
authorizers = append(authorizers, pl)
}
}
ourKeys, err := api.Signer.AvailableKeys(ctx)
if err != nil {
return nil, err
}
resp, err := api.GetAccountsByAuthorizers(ctx, authorizers, ourKeys)
if err != nil {
return nil, err
}
var out []ecc.PublicKey
seen := make(map[string]bool)
for _, acct := range resp.Accounts {
if acct.AuthorizingKey == nil {
continue
}
stringKey := acct.AuthorizingKey.String()
if seen[stringKey] {
continue
}
out = append(out, *acct.AuthorizingKey)
seen[stringKey] = true
}
return out, nil
}
func (api *API) SetSigner(s Signer) {
api.Signer = s
}
// ProducerPause will pause block production on a nodeos with
// `producer_api` plugin loaded.
func (api *API) ProducerPause(ctx context.Context) error {
return api.call(ctx, "producer", "pause", nil, nil)
}
// CreateSnapshot will write a snapshot file on a nodeos with
// `producer_api` plugin loaded.
func (api *API) CreateSnapshot(ctx context.Context) (out *CreateSnapshotResp, err error) {
err = api.call(ctx, "producer", "create_snapshot", nil, &out)
return
}
// GetIntegrityHash will produce a hash corresponding to current
// state. Requires `producer_api` and useful when loading
// from a snapshot
func (api *API) GetIntegrityHash(ctx context.Context) (out *GetIntegrityHashResp, err error) {
err = api.call(ctx, "producer", "get_integrity_hash", nil, &out)
return
}
// ProducerResume will resume block production on a nodeos with
// `producer_api` plugin loaded. Obviously, this needs to be a
// producing node on the producers schedule for it to do anything.
func (api *API) ProducerResume(ctx context.Context) error {
return api.call(ctx, "producer", "resume", nil, nil)
}
// IsProducerPaused queries the blockchain for the pause statement of
// block production.
func (api *API) IsProducerPaused(ctx context.Context) (out bool, err error) {
err = api.call(ctx, "producer", "paused", nil, &out)
return
}
func (api *API) GetProducerProtocolFeatures(ctx context.Context) (out []ProtocolFeature, err error) {
err = api.call(ctx, "producer", "get_supported_protocol_features", nil, &out)
return
}
func (api *API) ScheduleProducerProtocolFeatureActivations(ctx context.Context, protocolFeaturesToActivate []Checksum256) error {
return api.call(ctx, "producer", "schedule_protocol_feature_activations", M{"protocol_features_to_activate": protocolFeaturesToActivate}, nil)
}
type GetAccountOption interface {
apply(body M)
}
type getAccountOptionFunc func(body M)
func (f getAccountOptionFunc) apply(body M) {
f(body)
}
func WithCoreSymbol(symbol Symbol) GetAccountOption {
return getAccountOptionFunc(func(body M) {
body["expected_core_symbol"] = symbol.String()
})
}
func (api *API) GetAccount(ctx context.Context, name AccountName, opts ...GetAccountOption) (out *AccountResp, err error) {
body := M{"account_name": name}
for _, opt := range opts {
opt.apply(body)
}
err = api.call(ctx, "chain", "get_account", body, &out)
return
}
func (api *API) GetRawCodeAndABI(ctx context.Context, account AccountName) (out *GetRawCodeAndABIResp, err error) {
err = api.call(ctx, "chain", "get_raw_code_and_abi", M{"account_name": account}, &out)
return
}
func (api *API) GetCode(ctx context.Context, account AccountName) (out *GetCodeResp, err error) {
err = api.call(ctx, "chain", "get_code", M{"account_name": account, "code_as_wasm": true}, &out)
return
}
func (api *API) GetCodeHash(ctx context.Context, account AccountName) (out Checksum256, err error) {
resp := GetCodeHashResp{}
if err = api.call(ctx, "chain", "get_code_hash", M{"account_name": account}, &resp); err != nil {
return
}
buffer, err := hex.DecodeString(resp.CodeHash)
return Checksum256(buffer), err
}
func (api *API) GetABI(ctx context.Context, account AccountName) (out *GetABIResp, err error) {
err = api.call(ctx, "chain", "get_abi", M{"account_name": account}, &out)
return
}
func (api *API) ABIJSONToBin(ctx context.Context, code AccountName, action Name, payload M) (out HexBytes, err error) {
resp := ABIJSONToBinResp{}
err = api.call(ctx, "chain", "abi_json_to_bin", M{"code": code, "action": action, "args": payload}, &resp)
if err != nil {
return
}
buffer, err := hex.DecodeString(resp.Binargs)
return HexBytes(buffer), err
}
func (api *API) ABIBinToJSON(ctx context.Context, code AccountName, action Name, payload HexBytes) (out M, err error) {
resp := ABIBinToJSONResp{}
err = api.call(ctx, "chain", "abi_bin_to_json", M{"code": code, "action": action, "binargs": payload}, &resp)
if err != nil {
return
}
return resp.Args, nil
}
func (api *API) WalletCreate(ctx context.Context, walletName string) (err error) {
return api.call(ctx, "wallet", "create", walletName, nil)
}
func (api *API) WalletOpen(ctx context.Context, walletName string) (err error) {
return api.call(ctx, "wallet", "open", walletName, nil)
}
func (api *API) WalletLock(ctx context.Context, walletName string) (err error) {
return api.call(ctx, "wallet", "lock", walletName, nil)
}
func (api *API) WalletLockAll(ctx context.Context) (err error) {
return api.call(ctx, "wallet", "lock_all", nil, nil)
}
func (api *API) WalletUnlock(ctx context.Context, walletName, password string) (err error) {
return api.call(ctx, "wallet", "unlock", []string{walletName, password}, nil)
}
// WalletImportKey loads a new WIF-encoded key into the wallet.
func (api *API) WalletImportKey(ctx context.Context, walletName, wifPrivKey string) (err error) {
return api.call(ctx, "wallet", "import_key", []string{walletName, wifPrivKey}, nil)
}
func (api *API) WalletPublicKeys(ctx context.Context) (out []ecc.PublicKey, err error) {
var textKeys []string
err = api.call(ctx, "wallet", "get_public_keys", nil, &textKeys)
if err != nil {
return nil, err
}
for _, k := range textKeys {
newKey, err := ecc.NewPublicKey(k)
if err != nil {
return nil, err
}
out = append(out, newKey)
}
return
}
func (api *API) ListWallets(ctx context.Context, walletName ...string) (out []string, err error) {
err = api.call(ctx, "wallet", "list_wallets", walletName, &out)
if err != nil {
return nil, err
}
return
}
func (api *API) ListKeys(ctx context.Context, walletNames ...string) (out []*ecc.PrivateKey, err error) {
var textKeys []string
err = api.call(ctx, "wallet", "list_keys", walletNames, &textKeys)
if err != nil {
return nil, err
}
for _, k := range textKeys {
newKey, err := ecc.NewPrivateKey(k)
if err != nil {
return nil, err
}
out = append(out, newKey)
}
return
}
func (api *API) GetPublicKeys(ctx context.Context) (out []*ecc.PublicKey, err error) {
var textKeys []string
err = api.call(ctx, "wallet", "get_public_keys", nil, &textKeys)
if err != nil {
return nil, err
}
for _, k := range textKeys {
newKey, err := ecc.NewPublicKey(k)
if err != nil {
return nil, err
}
out = append(out, &newKey)
}
return
}
func (api *API) WalletSetTimeout(ctx context.Context, timeout int32) (err error) {
return api.call(ctx, "wallet", "set_timeout", timeout, nil)
}
func (api *API) WalletSignTransaction(ctx context.Context, tx *SignedTransaction, chainID []byte, pubKeys ...ecc.PublicKey) (out *WalletSignTransactionResp, err error) {
var textKeys []string
for _, key := range pubKeys {
textKeys = append(textKeys, key.String())
}
err = api.call(ctx, "wallet", "sign_transaction", []interface{}{
tx,
textKeys,
hex.EncodeToString(chainID),
}, &out)
return
}
// SignPushActions will create a transaction, fill it with default
// values, sign it and submit it to the chain. It is the highest
// level function on top of the `/v1/chain/push_transaction` endpoint.
func (api *API) SignPushActions(ctx context.Context, a ...*Action) (out *PushTransactionFullResp, err error) {
return api.SignPushActionsWithOpts(ctx, a, nil)
}
func (api *API) SignPushActionsWithOpts(ctx context.Context, actions []*Action, opts *TxOptions) (out *PushTransactionFullResp, err error) {
if opts == nil {
opts = &TxOptions{}
}
if err := opts.FillFromChain(ctx, api); err != nil {
return nil, err
}
tx := NewTransaction(actions, opts)
return api.SignPushTransaction(ctx, tx, opts.ChainID, opts.Compress)
}
// SignPushTransaction will sign a transaction and submit it to the
// chain.
func (api *API) SignPushTransaction(ctx context.Context, tx *Transaction, chainID Checksum256, compression CompressionType) (out *PushTransactionFullResp, err error) {
_, packed, err := api.SignTransaction(ctx, tx, chainID, compression)
if err != nil {
return nil, err
}
return api.PushTransaction(ctx, packed)
}
// SignTransaction will sign and pack a transaction, but not submit to
// the chain. It lives on the `api` object because it might query the
// blockchain to learn which keys are required to sign this particular
// transaction.
//
// You can override the way we request keys (which defaults to
// `api.GetRequiredKeys()`) with SetCustomGetRequiredKeys().
//
// To sign a transaction, you need a Signer defined on the `API`
// object. See SetSigner.
func (api *API) SignTransaction(ctx context.Context, tx *Transaction, chainID Checksum256, compression CompressionType) (*SignedTransaction, *PackedTransaction, error) {
if api.Signer == nil {
return nil, nil, fmt.Errorf("no Signer configured")
}
stx := NewSignedTransaction(tx)
var err error
var requiredKeys []ecc.PublicKey
if api.customGetRequiredKeys != nil {
requiredKeys, err = api.customGetRequiredKeys(ctx, tx)
if err != nil {
return nil, nil, fmt.Errorf("custom_get_required_keys: %w", err)
}
} else {
if api.enablePartialRequiredKeys {
requiredKeys, err = api.getPartialRequiredKeys(ctx, tx)
if err != nil {
return nil, nil, fmt.Errorf("get_accounts_by_authorizers: %w", err)
}
} else {
resp, err := api.GetRequiredKeys(ctx, tx)
if err != nil {
return nil, nil, fmt.Errorf("get_required_keys: %w", err)
}
requiredKeys = resp.RequiredKeys
}
}
signedTx, err := api.Signer.Sign(ctx, stx, chainID, requiredKeys...)
if err != nil {
return nil, nil, fmt.Errorf("signing through wallet: %w", err)
}
packed, err := signedTx.Pack(compression)
if err != nil {
return nil, nil, err
}
return signedTx, packed, nil
}
// PushTransaction submits a properly filled (tapos), packed and
// signed transaction to the blockchain.
func (api *API) PushTransaction(ctx context.Context, tx *PackedTransaction) (out *PushTransactionFullResp, err error) {
err = api.call(ctx, "chain", "push_transaction", tx, &out)
return
}
func (api *API) SendTransaction(ctx context.Context, tx *PackedTransaction) (out *PushTransactionFullResp, err error) {
err = api.call(ctx, "chain", "send_transaction", tx, &out)
return
}
func (api *API) PushTransactionRaw(ctx context.Context, tx *PackedTransaction) (out json.RawMessage, err error) {
err = api.call(ctx, "chain", "push_transaction", tx, &out)
return
}
func (api *API) SendTransactionRaw(ctx context.Context, tx *PackedTransaction) (out json.RawMessage, err error) {
err = api.call(ctx, "chain", "send_transaction", tx, &out)
return
}
func (api *API) GetInfo(ctx context.Context) (out *InfoResp, err error) {
err = api.call(ctx, "chain", "get_info", nil, &out)
return
}
func (api *API) cachedGetInfo(ctx context.Context) (*InfoResp, error) {
api.lastGetInfoLock.Lock()
defer api.lastGetInfoLock.Unlock()
var info *InfoResp
var err error
if !api.lastGetInfoStamp.IsZero() && time.Now().Add(-1*time.Second).Before(api.lastGetInfoStamp) {
info = api.lastGetInfo
} else {
info, err = api.GetInfo(ctx)
if err != nil {
return nil, err
}
api.lastGetInfoStamp = time.Now()
api.lastGetInfo = info
}
if err != nil {
return nil, err
}
return api.lastGetInfo, nil
}
func (api *API) GetNetConnections(ctx context.Context) (out []*NetConnectionsResp, err error) {
err = api.call(ctx, "net", "connections", nil, &out)
return
}
func (api *API) NetConnect(ctx context.Context, host string) (out NetConnectResp, err error) {
err = api.call(ctx, "net", "connect", host, &out)
return
}
func (api *API) NetDisconnect(ctx context.Context, host string) (out NetDisconnectResp, err error) {
err = api.call(ctx, "net", "disconnect", host, &out)
return
}
func (api *API) GetNetStatus(ctx context.Context, host string) (out *NetStatusResp, err error) {
err = api.call(ctx, "net", "status", M{"host": host}, &out)
return
}
func (api *API) GetBlockByID(ctx context.Context, id string) (out *BlockResp, err error) {
err = api.call(ctx, "chain", "get_block", M{"block_num_or_id": id}, &out)
return
}
// GetScheduledTransactionsWithBounds returns scheduled transactions within specified bounds
func (api *API) GetScheduledTransactionsWithBounds(ctx context.Context, lower_bound string, limit uint32) (out *ScheduledTransactionsResp, err error) {
err = api.call(ctx, "chain", "get_scheduled_transactions", M{"json": true, "lower_bound": lower_bound, "limit": limit}, &out)
return
}
// GetScheduledTransactions returns the Top 100 scheduled transactions
func (api *API) GetScheduledTransactions(ctx context.Context) (out *ScheduledTransactionsResp, err error) {
return api.GetScheduledTransactionsWithBounds(ctx, "", 100)
}
func (api *API) GetProducers(ctx context.Context) (out *ProducersResp, err error) {
/*
+FC_REFLECT( eosio::chain_apis::read_only::get_producers_params, (json)(lower_bound)(limit) )
+FC_REFLECT( eosio::chain_apis::read_only::get_producers_result, (rows)(total_producer_vote_weight)(more) ); */
err = api.call(ctx, "chain", "get_producers", M{"json": true}, &out)
return
}
func (api *API) GetBlockByNum(ctx context.Context, num uint32) (out *BlockResp, err error) {
err = api.call(ctx, "chain", "get_block", M{"block_num_or_id": fmt.Sprintf("%d", num)}, &out)
//err = api.call("chain", "get_block", M{"block_num_or_id": num}, &out)
return
}
func (api *API) GetBlockByNumOrID(ctx context.Context, query string) (out *SignedBlock, err error) {
err = api.call(ctx, "chain", "get_block", M{"block_num_or_id": query}, &out)
return
}
func (api *API) GetBlockByNumOrIDRaw(ctx context.Context, query string) (out interface{}, err error) {
err = api.call(ctx, "chain", "get_block", M{"block_num_or_id": query}, &out)
return
}
func (api *API) GetDBSize(ctx context.Context) (out *DBSizeResp, err error) {
err = api.call(ctx, "db_size", "get", nil, &out)
return
}
func (api *API) GetTransaction(ctx context.Context, id string) (out *TransactionResp, err error) {
err = api.call(ctx, "history", "get_transaction", M{"id": id}, &out)
return
}
func (api *API) GetTransactionRaw(ctx context.Context, id string) (out json.RawMessage, err error) {
err = api.call(ctx, "history", "get_transaction", M{"id": id}, &out)
return
}
func (api *API) GetActions(ctx context.Context, params GetActionsRequest) (out *ActionsResp, err error) {
err = api.call(ctx, "history", "get_actions", params, &out)
return
}
func (api *API) GetKeyAccounts(ctx context.Context, publicKey string) (out *KeyAccountsResp, err error) {
err = api.call(ctx, "history", "get_key_accounts", M{"public_key": publicKey}, &out)
return
}
func (api *API) GetControlledAccounts(ctx context.Context, controllingAccount string) (out *ControlledAccountsResp, err error) {
err = api.call(ctx, "history", "get_controlled_accounts", M{"controlling_account": controllingAccount}, &out)
return
}
func (api *API) GetTransactions(ctx context.Context, name AccountName) (out *TransactionsResp, err error) {
err = api.call(ctx, "account_history", "get_transactions", M{"account_name": name}, &out)
return
}
func (api *API) GetTableByScope(ctx context.Context, params GetTableByScopeRequest) (out *GetTableByScopeResp, err error) {
err = api.call(ctx, "chain", "get_table_by_scope", params, &out)
return
}
func (api *API) GetTableRows(ctx context.Context, params GetTableRowsRequest) (out *GetTableRowsResp, err error) {
err = api.call(ctx, "chain", "get_table_rows", params, &out)
return
}
func (api *API) GetRawABI(ctx context.Context, params GetRawABIRequest) (out *GetRawABIResp, err error) {
err = api.call(ctx, "chain", "get_raw_abi", params, &out)
return
}
func (api *API) GetRequiredKeys(ctx context.Context, tx *Transaction) (out *GetRequiredKeysResp, err error) {
keys, err := api.Signer.AvailableKeys(ctx)
if err != nil {
return nil, err
}
err = api.call(ctx, "chain", "get_required_keys", M{"transaction": tx, "available_keys": keys}, &out)
return
}
func (api *API) GetAccountsByAuthorizers(ctx context.Context, authorizations []PermissionLevel, keys []ecc.PublicKey) (out *GetAccountsByAuthorizersResp, err error) {
err = api.call(ctx, "chain", "get_accounts_by_authorizers", M{"accounts": authorizations, "keys": keys}, &out)
return
}
func (api *API) GetCurrencyBalance(ctx context.Context, account AccountName, symbol string, code AccountName) (out []Asset, err error) {
params := M{"account": account, "code": code}
if symbol != "" {
params["symbol"] = symbol
}
err = api.call(ctx, "chain", "get_currency_balance", params, &out)
return
}
func (api *API) GetCurrencyStats(ctx context.Context, code AccountName, symbol string) (out *GetCurrencyStatsResp, err error) {
params := M{"code": code, "symbol": symbol}
outWrapper := make(map[string]*GetCurrencyStatsResp)
err = api.call(ctx, "chain", "get_currency_stats", params, &outWrapper)
out = outWrapper[symbol]
return
}
// See more here: libraries/chain/contracts/abi_serializer.cpp:58...
func (api *API) call(ctx context.Context, baseAPI string, endpoint string, body interface{}, out interface{}) error {
jsonBody, err := enc(body)
if err != nil {
return err
}
targetURL := fmt.Sprintf("%s/v1/%s/%s", api.BaseURL, baseAPI, endpoint)
req, err := http.NewRequest("POST", targetURL, jsonBody)
if err != nil {
return fmt.Errorf("NewRequest: %w", err)
}
for k, v := range api.Header {
if req.Header == nil {
req.Header = http.Header{}
}
req.Header[k] = append(req.Header[k], v...)
}
if api.Debug {
// Useful when debugging API calls
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Println(err)
}
fmt.Println("-------------------------------")
fmt.Println(string(requestDump))
fmt.Println("")
}
resp, err := api.HttpClient.Do(req.WithContext(ctx))
if err != nil {
return fmt.Errorf("%s: %w", req.URL.String(), err)
}
defer resp.Body.Close()
var cnt bytes.Buffer
_, err = io.Copy(&cnt, resp.Body)
if err != nil {
return fmt.Errorf("Copy: %w", err)
}
if resp.StatusCode == 404 {
var apiErr APIError
if err := json.Unmarshal(cnt.Bytes(), &apiErr); err != nil {
return ErrNotFound
}
return apiErr
}
if resp.StatusCode > 299 {
var apiErr APIError
if err := json.Unmarshal(cnt.Bytes(), &apiErr); err != nil {
return fmt.Errorf("%s: status code=%d, body=%s", req.URL.String(), resp.StatusCode, cnt.String())
}
// Handle cases where some API calls (/v1/chain/get_account for example) returns a 500
// error when retrieving data that does not exist.
if apiErr.IsUnknownKeyError() {
return ErrNotFound
}
return apiErr
}
if api.Debug {
fmt.Println("RESPONSE:")
responseDump, err := httputil.DumpResponse(resp, true)
if err != nil {
fmt.Println(err)
}
fmt.Println("-------------------------------")
fmt.Println(cnt.String())
fmt.Println("-------------------------------")
fmt.Printf("%q\n", responseDump)
fmt.Println("")
}
if err := json.Unmarshal(cnt.Bytes(), &out); err != nil {
return fmt.Errorf("Unmarshal: %w", err)
}
return nil
}
var ErrNotFound = errors.New("resource not found")
type M map[string]interface{}
func enc(v interface{}) (io.Reader, error) {
if v == nil {
return nil, nil
}
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
return nil, err
}
return buffer, nil
}