/
bridge.go
157 lines (139 loc) · 4.18 KB
/
bridge.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
package btc
import (
"math/big"
"sync"
"time"
"github.com/anyswap/CrossChain-Router/v3/log"
"github.com/anyswap/CrossChain-Router/v3/tokens"
"github.com/btcsuite/btcd/chaincfg"
)
var (
// ensure Bridge impl tokens.CrossChainBridge
_ tokens.IBridge = &Bridge{}
supportedChainIDs = make(map[string]bool)
supportedChainIDsInit sync.Once
)
const (
mainnetNetWork = "mainnet"
testnetNetWork = "testnet"
)
// Bridge near bridge
type Bridge struct {
*tokens.CrossChainBridgeBase
}
// SupportsChainID supports chainID
func SupportsChainID(chainID *big.Int) bool {
supportedChainIDsInit.Do(func() {
supportedChainIDs[GetStubChainID(mainnetNetWork).String()] = true
supportedChainIDs[GetStubChainID(testnetNetWork).String()] = true
})
return supportedChainIDs[chainID.String()]
}
// NewCrossChainBridge new bridge
func NewCrossChainBridge() *Bridge {
return &Bridge{CrossChainBridgeBase: tokens.NewCrossChainBridgeBase()}
}
// GetStubChainID get stub chainID
func GetStubChainID(network string) *big.Int {
stubChainID := new(big.Int).SetBytes([]byte("BTC"))
switch network {
case mainnetNetWork:
case testnetNetWork:
stubChainID.Add(stubChainID, big.NewInt(1))
default:
log.Fatalf("unknown network %v", network)
}
stubChainID.Mod(stubChainID, tokens.StubChainIDBase)
stubChainID.Add(stubChainID, tokens.StubChainIDBase)
return stubChainID
}
// VerifyTokenConfig verify token config
func (b *Bridge) VerifyTokenConfig(tokenCfg *tokens.TokenConfig) error {
return nil
}
// GetLatestBlockNumber gets latest block number
func (b *Bridge) GetLatestBlockNumber() (result uint64, err error) {
urls := append(b.GatewayConfig.APIAddress, b.GatewayConfig.APIAddressExt...)
for _, url := range urls {
result, err = GetLatestBlockNumber(url)
if err == nil {
return result, nil
}
}
return 0, tokens.ErrTxNotFound
}
// GetLatestBlockNumberOf gets latest block number from single api
func (b *Bridge) GetLatestBlockNumberOf(apiAddress string) (uint64, error) {
return GetLatestBlockNumber(apiAddress)
}
// GetTransaction impl
func (b *Bridge) GetTransaction(txHash string) (tx interface{}, err error) {
return b.GetTransactionByHash(txHash)
}
// GetTransactionByHash get tx response by hash
func (b *Bridge) GetTransactionByHash(txHash string) (result *ElectTx, err error) {
urls := append(b.GatewayConfig.APIAddress, b.GatewayConfig.APIAddressExt...)
for _, url := range urls {
result, err = GetTransactionByHash(url, txHash)
if err == nil {
return result, nil
}
}
return nil, tokens.ErrTxNotFound
}
// GetElectTransactionStatus impl
func (b *Bridge) GetElectTransactionStatus(txHash string) (result *ElectTxStatus, err error) {
urls := append(b.GatewayConfig.APIAddress, b.GatewayConfig.APIAddressExt...)
for _, url := range urls {
result, err = GetElectTransactionStatus(url, txHash)
if err == nil {
return result, nil
}
}
return nil, tokens.ErrTxNotFound
}
// GetTransactionStatus impl
func (b *Bridge) GetTransactionStatus(txHash string) (status *tokens.TxStatus, err error) {
txStatus := &tokens.TxStatus{}
electStatus, err := b.GetElectTransactionStatus(txHash)
if err != nil {
log.Trace(b.ChainConfig.BlockChain+" Bridge::GetElectTransactionStatus fail", "tx", txHash, "err", err)
return txStatus, err
}
if electStatus.BlockHash != nil {
txStatus.BlockHash = *electStatus.BlockHash
}
if electStatus.BlockTime != nil {
txStatus.BlockTime = *electStatus.BlockTime
}
if electStatus.BlockHeight != nil {
txStatus.BlockHeight = *electStatus.BlockHeight
latest, errt := b.GetLatestBlockNumber()
if errt == nil {
if latest > txStatus.BlockHeight {
txStatus.Confirmations = latest - txStatus.BlockHeight
}
}
}
return txStatus, nil
}
func (b *Bridge) getTransactionByHashWithRetry(txid string) (tx *ElectTx, err error) {
for i := 0; i < retryCount; i++ {
tx, err = b.GetTransactionByHash(txid)
if err == nil {
break
}
time.Sleep(retryInterval)
}
return tx, err
}
// GetChainParams get chain config (net params)
func (b *Bridge) GetChainParams(chainID *big.Int) *chaincfg.Params {
chainId := chainID.String()
switch chainId {
case (*GetStubChainID(mainnetNetWork)).String():
return &chaincfg.MainNetParams
default:
return &chaincfg.TestNet3Params
}
}