-
Notifications
You must be signed in to change notification settings - Fork 42
/
deploy.go
167 lines (139 loc) · 4.97 KB
/
deploy.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
package feeoraclev1
import (
"context"
"math/big"
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/e2e/app/eoa"
"github.com/omni-network/omni/lib/contracts"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient/ethbackend"
"github.com/omni-network/omni/lib/netconf"
"github.com/omni-network/omni/lib/tokens/coingecko"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
type DeploymentConfig struct {
Owner common.Address
Manager common.Address // manager is the address that can set fee parameters (gas price, conversion rates)
Deployer common.Address
ProxyAdmin common.Address
BaseGasLimit uint64
ProtocolFee *big.Int
}
func isDeadOrEmpty(addr common.Address) bool {
return addr == common.Address{} || addr == common.HexToAddress(eoa.ZeroXDead)
}
func (cfg DeploymentConfig) Validate() error {
if isDeadOrEmpty(cfg.Owner) {
return errors.New("owner is zero")
}
if isDeadOrEmpty(cfg.Manager) {
return errors.New("manager is zero")
}
if isDeadOrEmpty(cfg.Deployer) {
return errors.New("deployer is zero")
}
if (cfg.ProxyAdmin == common.Address{}) {
return errors.New("proxy admin is zero")
}
return nil
}
func getDeployCfg(chainID uint64, network netconf.ID) (DeploymentConfig, error) {
if network == netconf.Devnet {
return devnetCfg(), nil
}
if network == netconf.Mainnet {
return mainnetCfg(), nil
}
if network == netconf.Omega {
return testnetCfg(), nil
}
if network == netconf.Staging {
return stagingCfg(), nil
}
return DeploymentConfig{}, errors.New("unsupported chain for network", "chain_id", chainID, "network", network)
}
// NOTE: monitor is owner of fee oracle contracts, because monitor manages on chain gas prices / conversion rates
func mainnetCfg() DeploymentConfig {
return DeploymentConfig{
Owner: eoa.MustAddress(netconf.Mainnet, eoa.RolePortalAdmin),
Manager: eoa.MustAddress(netconf.Mainnet, eoa.RoleMonitor),
Deployer: eoa.MustAddress(netconf.Mainnet, eoa.RoleDeployer),
ProxyAdmin: contracts.MainnetProxyAdmin(),
BaseGasLimit: 50_000,
ProtocolFee: big.NewInt(0),
}
}
func testnetCfg() DeploymentConfig {
return DeploymentConfig{
Owner: eoa.MustAddress(netconf.Omega, eoa.RolePortalAdmin),
Manager: eoa.MustAddress(netconf.Omega, eoa.RoleMonitor),
Deployer: eoa.MustAddress(netconf.Omega, eoa.RoleDeployer),
ProxyAdmin: contracts.TestnetProxyAdmin(),
BaseGasLimit: 50_000,
ProtocolFee: big.NewInt(0),
}
}
func devnetCfg() DeploymentConfig {
return DeploymentConfig{
Owner: eoa.MustAddress(netconf.Devnet, eoa.RolePortalAdmin),
Manager: eoa.MustAddress(netconf.Devnet, eoa.RoleMonitor),
Deployer: eoa.MustAddress(netconf.Devnet, eoa.RoleDeployer),
ProxyAdmin: contracts.DevnetProxyAdmin(),
BaseGasLimit: 50_000,
ProtocolFee: big.NewInt(0),
}
}
func stagingCfg() DeploymentConfig {
return DeploymentConfig{
Owner: eoa.MustAddress(netconf.Staging, eoa.RolePortalAdmin),
Manager: eoa.MustAddress(netconf.Staging, eoa.RoleMonitor),
Deployer: eoa.MustAddress(netconf.Staging, eoa.RoleDeployer),
ProxyAdmin: contracts.StagingProxyAdmin(),
BaseGasLimit: 50_000,
ProtocolFee: big.NewInt(0),
}
}
func Deploy(ctx context.Context, network netconf.ID, chainID uint64, destChainIDs []uint64, backends ethbackend.Backends) (common.Address, *ethtypes.Receipt, error) {
cfg, err := getDeployCfg(chainID, network)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "get deployment config")
}
backend, err := backends.Backend(chainID)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "get backend")
}
txOpts, err := backend.BindOpts(ctx, cfg.Deployer)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "bind opts")
}
feeparams, err := feeParams(ctx, network, chainID, destChainIDs, backends, coingecko.New())
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "fee params")
}
feeOracleAbi, err := bindings.FeeOracleV1MetaData.GetAbi()
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "get fee oracle abi")
}
initializer, err := feeOracleAbi.Pack("initialize", cfg.Owner, cfg.Manager, cfg.BaseGasLimit, cfg.ProtocolFee, feeparams)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "pack initialize")
}
impl, tx, _, err := bindings.DeployFeeOracleV1(txOpts, backend)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "deploy fee oracle")
}
_, err = backend.WaitMined(ctx, tx)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "wait mined")
}
proxy, tx, _, err := bindings.DeployTransparentUpgradeableProxy(txOpts, backend, impl, cfg.ProxyAdmin, initializer)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "deploy proxy")
}
receipt, err := backend.WaitMined(ctx, tx)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "wait mined")
}
return proxy, receipt, nil
}