-
Notifications
You must be signed in to change notification settings - Fork 6
/
bridge.go
68 lines (57 loc) · 1.78 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
package btc
import (
"strings"
"time"
"github.com/fsn-dev/crossChain-Bridge/log"
"github.com/fsn-dev/crossChain-Bridge/tokens"
)
const (
netMainnet = "mainnet"
netTestnet3 = "testnet3"
netCustom = "custom"
)
// BridgeInstance btc bridge instance
var BridgeInstance *Bridge
// Bridge btc bridge
type Bridge struct {
*tokens.CrossChainBridgeBase
}
// NewCrossChainBridge new btc bridge
func NewCrossChainBridge(isSrc bool) *Bridge {
if !isSrc {
panic(tokens.ErrBridgeDestinationNotSupported)
}
BridgeInstance = &Bridge{tokens.NewCrossChainBridgeBase(isSrc)}
return BridgeInstance
}
// SetTokenAndGateway set token and gateway config
func (b *Bridge) SetTokenAndGateway(tokenCfg *tokens.TokenConfig, gatewayCfg *tokens.GatewayConfig) {
b.CrossChainBridgeBase.SetTokenAndGateway(tokenCfg, gatewayCfg)
networkID := strings.ToLower(tokenCfg.NetID)
switch networkID {
case netMainnet, netTestnet3:
case netCustom:
return
default:
log.Fatal("unsupported bitcoin network", "netID", tokenCfg.NetID)
}
if !b.IsP2pkhAddress(tokenCfg.DcrmAddress) {
log.Fatal("invalid dcrm address (not p2pkh)", "address", tokenCfg.DcrmAddress)
}
if strings.EqualFold(tokenCfg.Symbol, "BTC") && *tokenCfg.Decimals != 8 {
log.Fatal("invalid decimals for BTC", "configed", *tokenCfg.Decimals, "want", 8)
}
var latest uint64
var err error
for {
latest, err = b.GetLatestBlockNumber()
if err == nil {
tokens.SetLatestBlockHeight(latest, b.IsSrc)
log.Info("get latst block number succeed.", "number", latest, "BlockChain", tokenCfg.BlockChain, "NetID", tokenCfg.NetID)
break
}
log.Error("get latst block number failed.", "BlockChain", tokenCfg.BlockChain, "NetID", tokenCfg.NetID, "err", err)
log.Println("retry query gateway", gatewayCfg.APIAddress)
time.Sleep(3 * time.Second)
}
}