-
Notifications
You must be signed in to change notification settings - Fork 39
/
chain.go
115 lines (98 loc) · 2.69 KB
/
chain.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
package btc
import (
"encoding/json"
"fmt"
"net/http"
"github.com/coming-chat/wallet-SDK/core/base"
"github.com/coming-chat/wallet-SDK/pkg/httpUtil"
)
type Chain struct {
*Util
}
func NewChainWithChainnet(chainnet string) (*Chain, error) {
util, err := NewUtilWithChainnet(chainnet)
if err != nil {
return nil, err
}
return &Chain{Util: util}, nil
}
// MARK - Implement the protocol Chain
func (c *Chain) MainToken() base.Token {
return c
}
func (c *Chain) BalanceOfAddress(address string) (*base.Balance, error) {
b, err := queryBalance(address, c.Chainnet)
if err != nil {
return nil, err
}
return &base.Balance{
Total: b,
Usable: b,
}, nil
}
func (c *Chain) BalanceOfPublicKey(publicKey string) (*base.Balance, error) {
b, err := queryBalancePubkey(publicKey, c.Chainnet)
if err != nil {
return nil, err
}
return &base.Balance{
Total: b,
Usable: b,
}, nil
}
func (c *Chain) BalanceOfAccount(account base.Account) (*base.Balance, error) {
return c.BalanceOfPublicKey(account.PublicKeyHex())
}
// Send the raw transaction on-chain
// @return the hex hash string
func (c *Chain) SendRawTransaction(signedTx string) (string, error) {
return sendRawTransaction(signedTx, c.Chainnet)
}
// Fetch transaction details through transaction hash
// Note: The input parsing of bitcoin is very complex and the network cost is relatively high,
// So only the status and timestamp can be queried.
func (c *Chain) FetchTransactionDetail(hash string) (*base.TransactionDetail, error) {
return fetchTransactionDetail(hash, c.Chainnet)
}
func (c *Chain) FetchTransactionStatus(hash string) base.TransactionStatus {
return fetchTransactionStatus(hash, c.Chainnet)
}
func (c *Chain) BatchFetchTransactionStatus(hashListString string) string {
return sdkBatchTransactionStatus(hashListString, c.Chainnet)
}
type FeeRate struct {
Low int64
Average int64
High int64
}
func SuggestFeeRate() (*FeeRate, error) {
url := "https://mempool-mainnet.coming.chat/api/v1/fees/recommended"
response, err := httpUtil.Request(http.MethodGet, url, nil, nil)
if err != nil {
return nil, base.MapAnyToBasicError(err)
}
if response.Code != http.StatusOK {
return nil, fmt.Errorf("code: %d, body: %s", response.Code, string(response.Body))
}
respDict := make(map[string]interface{})
err = json.Unmarshal(response.Body, &respDict)
if err != nil {
return nil, err
}
var low, avg, high float64
var ok bool
if low, ok = respDict["minimumFee"].(float64); !ok {
low = 1
}
if avg, ok = respDict["halfHourFee"].(float64); !ok {
avg = low
}
if high, ok = respDict["fastestFee"].(float64); !ok {
high = avg
}
return &FeeRate{
Low: int64(low),
Average: int64(avg),
High: int64(high),
}, nil
}