/
rpcclient.go
68 lines (60 loc) · 1.57 KB
/
rpcclient.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
package btc
import (
"sort"
"github.com/anyswap/CrossChain-Router/v3/rpc/client"
)
// PostTransaction call post to /tx
func PostTransaction(url, txHex string) (txHash string, err error) {
restApi := url + "/tx"
txHash, err = client.RPCRawPost(restApi, txHex)
if err == nil {
return txHash, nil
}
return "", err
}
// GetTransactionByHash get tx by hash
func GetTransactionByHash(url, txHash string) (*ElectTx, error) {
var result ElectTx
var err error
restApi := url + "/tx/" + txHash
err = client.RPCGet(&result, restApi)
if err == nil {
return &result, nil
}
return nil, err
}
func EstimateFeePerKb(url string, blocks int) (fee int64, err error) {
var result map[int]float64
restApi := url + "/fee-estimates"
err = client.RPCGet(&result, restApi)
if err != nil {
return 0, err
}
return int64(result[blocks] * 1000), nil
}
func FindUtxos(url string, addr string) (result []*ElectUtxo, err error) {
restApi := url + "/address/" + addr + "/utxo"
err = client.RPCGet(&result, restApi)
if err == nil {
sort.Sort(SortableElectUtxoSlice(result))
return result, nil
}
return nil, err
}
// GetElectTransactionStatus call /tx/{txHash}/status
func GetElectTransactionStatus(url, txHash string) (result *ElectTxStatus, err error) {
restApi := url + "/tx/" + txHash + "/status"
err = client.RPCGet(&result, restApi)
if err == nil {
return result, nil
}
return nil, err
}
func GetLatestBlockNumber(url string) (result uint64, err error) {
restApi := url + "/blocks/tip/height"
err = client.RPCGet(&result, restApi)
if err == nil {
return result, nil
}
return 0, err
}