forked from ChainSafe/ChainBridge
/
config.go
194 lines (172 loc) 路 6.23 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
package ethereum
import (
"errors"
"fmt"
"math/big"
"github.com/PolkaFoundry/ChainBridge/connections/ethereum/egs"
utils "github.com/PolkaFoundry/ChainBridge/shared/ethereum"
"github.com/PolkaFoundry/chainbridge-utils/core"
"github.com/PolkaFoundry/chainbridge-utils/msg"
"github.com/ethereum/go-ethereum/common"
)
const DefaultGasLimit = 6721975
const DefaultGasPrice = 20000000000
const DefaultBlockConfirmations = 10
const DefaultGasMultiplier = 1
// Chain specific options
var (
BridgeOpt = "bridge"
Erc20HandlerOpt = "erc20Handler"
Erc721HandlerOpt = "erc721Handler"
GenericHandlerOpt = "genericHandler"
MaxGasPriceOpt = "maxGasPrice"
GasLimitOpt = "gasLimit"
GasMultiplier = "gasMultiplier"
HttpOpt = "http"
StartBlockOpt = "startBlock"
BlockConfirmationsOpt = "blockConfirmations"
EGSApiKey = "egsApiKey"
EGSSpeed = "egsSpeed"
)
// 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
gasMultiplier *big.Float
http bool // Config for type of connection
startBlock *big.Int
blockConfirmations *big.Int
egsApiKey string // API key for ethgasstation to query gas prices
egsSpeed string // The speed which a transaction should be processed: average, fast, fastest. Default: fast
}
// 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),
gasMultiplier: big.NewFloat(DefaultGasMultiplier),
http: false,
startBlock: big.NewInt(0),
blockConfirmations: big.NewInt(0),
egsApiKey: "",
egsSpeed: "",
}
if contract, ok := chainCfg.Opts[BridgeOpt]; ok && contract != "" {
config.bridgeContract = common.HexToAddress(contract)
delete(chainCfg.Opts, BridgeOpt)
} else {
return nil, fmt.Errorf("must provide opts.bridge field for ethereum config")
}
if contract, ok := chainCfg.Opts[Erc20HandlerOpt]; ok {
config.erc20HandlerContract = common.HexToAddress(contract)
delete(chainCfg.Opts, Erc20HandlerOpt)
}
if contract, ok := chainCfg.Opts[Erc721HandlerOpt]; ok {
config.erc721HandlerContract = common.HexToAddress(contract)
delete(chainCfg.Opts, Erc721HandlerOpt)
}
if contract, ok := chainCfg.Opts[GenericHandlerOpt]; ok {
config.genericHandlerContract = common.HexToAddress(contract)
delete(chainCfg.Opts, GenericHandlerOpt)
}
if gasPrice, ok := chainCfg.Opts[MaxGasPriceOpt]; ok {
price := big.NewInt(0)
_, pass := price.SetString(gasPrice, 10)
if pass {
config.maxGasPrice = price
delete(chainCfg.Opts, MaxGasPriceOpt)
} else {
return nil, errors.New("unable to parse max gas price")
}
}
if gasLimit, ok := chainCfg.Opts[GasLimitOpt]; ok {
limit := big.NewInt(0)
_, pass := limit.SetString(gasLimit, 10)
if pass {
config.gasLimit = limit
delete(chainCfg.Opts, GasLimitOpt)
} else {
return nil, errors.New("unable to parse gas limit")
}
}
if gasMultiplier, ok := chainCfg.Opts[GasMultiplier]; ok {
multilier := big.NewFloat(1)
_, pass := multilier.SetString(gasMultiplier)
if pass {
config.gasMultiplier = multilier
delete(chainCfg.Opts, GasMultiplier)
} else {
return nil, errors.New("unable to parse gasMultiplier to float")
}
}
if HTTP, ok := chainCfg.Opts[HttpOpt]; ok && HTTP == "true" {
config.http = true
delete(chainCfg.Opts, HttpOpt)
} else if HTTP, ok := chainCfg.Opts[HttpOpt]; ok && HTTP == "false" {
config.http = false
delete(chainCfg.Opts, HttpOpt)
}
if startBlock, ok := chainCfg.Opts[StartBlockOpt]; ok && startBlock != "" {
block := big.NewInt(0)
_, pass := block.SetString(startBlock, 10)
if pass {
config.startBlock = block
delete(chainCfg.Opts, StartBlockOpt)
} else {
return nil, fmt.Errorf("unable to parse %s", StartBlockOpt)
}
}
if blockConfirmations, ok := chainCfg.Opts[BlockConfirmationsOpt]; ok && blockConfirmations != "" {
val := big.NewInt(DefaultBlockConfirmations)
_, pass := val.SetString(blockConfirmations, 10)
if pass {
config.blockConfirmations = val
delete(chainCfg.Opts, BlockConfirmationsOpt)
} else {
return nil, fmt.Errorf("unable to parse %s", BlockConfirmationsOpt)
}
} else {
config.blockConfirmations = big.NewInt(DefaultBlockConfirmations)
delete(chainCfg.Opts, BlockConfirmationsOpt)
}
if gsnApiKey, ok := chainCfg.Opts[EGSApiKey]; ok && gsnApiKey != "" {
config.egsApiKey = gsnApiKey
delete(chainCfg.Opts, EGSApiKey)
}
if speed, ok := chainCfg.Opts[EGSSpeed]; ok && speed == egs.Average || speed == egs.Fast || speed == egs.Fastest {
config.egsSpeed = speed
delete(chainCfg.Opts, EGSSpeed)
} else {
// Default to "fast"
config.egsSpeed = egs.Fast
delete(chainCfg.Opts, EGSSpeed)
}
if len(chainCfg.Opts) != 0 {
return nil, fmt.Errorf("unknown Opts Encountered: %#v", chainCfg.Opts)
}
return config, nil
}