forked from ChainSafe/ChainBridge
/
config.go
122 lines (105 loc) 路 3.82 KB
/
config.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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
package ethereum
import (
"errors"
"fmt"
"math/big"
utils "github.com/kaelnew/chainbridge/shared/ethereum"
"github.com/kaelnew/chainbridge-utils/core"
"github.com/kaelnew/chainbridge-utils/msg"
"github.com/ethereum/go-ethereum/common"
)
const DefaultGasLimit = 6721975
const DefaultGasPrice = 20000000000
// Config encapsulates all necessary parameters in ethereum compatible forms
type Config struct {
name string // Human-readable chain name
id msg.ChainId // ChainID
endpoint string // url for rpc endpoint
from string // address of key to use
keystorePath string // Location of keyfiles
blockstorePath string
freshStart bool // Disables loading from blockstore at start
bridgeContract common.Address
erc20HandlerContract common.Address
erc721HandlerContract common.Address
genericHandlerContract common.Address
gasLimit *big.Int
maxGasPrice *big.Int
http bool // Config for type of connection
startBlock *big.Int
}
// parseChainConfig uses a core.ChainConfig to construct a corresponding Config
func parseChainConfig(chainCfg *core.ChainConfig) (*Config, error) {
config := &Config{
name: chainCfg.Name,
id: chainCfg.Id,
endpoint: chainCfg.Endpoint,
from: chainCfg.From,
keystorePath: chainCfg.KeystorePath,
blockstorePath: chainCfg.BlockstorePath,
freshStart: chainCfg.FreshStart,
bridgeContract: utils.ZeroAddress,
erc20HandlerContract: utils.ZeroAddress,
erc721HandlerContract: utils.ZeroAddress,
genericHandlerContract: utils.ZeroAddress,
gasLimit: big.NewInt(DefaultGasLimit),
maxGasPrice: big.NewInt(DefaultGasPrice),
http: false,
startBlock: big.NewInt(0),
}
if contract, ok := chainCfg.Opts["bridge"]; ok && contract != "" {
config.bridgeContract = common.HexToAddress(contract)
delete(chainCfg.Opts, "bridge")
} else {
return nil, fmt.Errorf("must provide opts.bridge field for ethereum config")
}
config.erc20HandlerContract = common.HexToAddress(chainCfg.Opts["erc20Handler"])
delete(chainCfg.Opts, "erc20Handler")
config.erc721HandlerContract = common.HexToAddress(chainCfg.Opts["erc721Handler"])
delete(chainCfg.Opts, "erc721Handler")
config.genericHandlerContract = common.HexToAddress(chainCfg.Opts["genericHandler"])
delete(chainCfg.Opts, "genericHandler")
if gasPrice, ok := chainCfg.Opts["maxGasPrice"]; ok {
price := big.NewInt(0)
_, pass := price.SetString(gasPrice, 10)
if pass {
config.maxGasPrice = price
delete(chainCfg.Opts, "maxGasPrice")
} else {
return nil, errors.New("unable to parse max gas price")
}
}
if gasLimit, ok := chainCfg.Opts["gasLimit"]; ok {
limit := big.NewInt(0)
_, pass := limit.SetString(gasLimit, 10)
if pass {
config.gasLimit = limit
delete(chainCfg.Opts, "gasLimit")
} else {
return nil, errors.New("unable to parse gas limit")
}
}
if HTTP, ok := chainCfg.Opts["http"]; ok && HTTP == "true" {
config.http = true
delete(chainCfg.Opts, "http")
} else if HTTP, ok := chainCfg.Opts["http"]; ok && HTTP == "false" {
config.http = false
delete(chainCfg.Opts, "http")
}
if startBlock, ok := chainCfg.Opts["startBlock"]; ok && startBlock != "" {
block := big.NewInt(0)
_, pass := block.SetString(startBlock, 10)
if pass {
config.startBlock = block
delete(chainCfg.Opts, "startBlock")
} else {
return nil, errors.New("unable to parse start block")
}
}
if len(chainCfg.Opts) != 0 {
return nil, fmt.Errorf("unknown Opts Encountered: %#v", chainCfg.Opts)
}
return config, nil
}