-
Notifications
You must be signed in to change notification settings - Fork 36
/
deploy.go
188 lines (157 loc) · 5.45 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package l1bridge
import (
"context"
"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/create3"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient/ethbackend"
"github.com/omni-network/omni/lib/netconf"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
type DeploymentConfig struct {
Create3Factory common.Address
Create3Salt string
ProxyAdmin common.Address
Owner common.Address
Portal common.Address
Token common.Address
Deployer common.Address
ExpectedAddr common.Address
}
func isDeadOrEmpty(addr common.Address) bool {
return addr == common.Address{} || addr == common.HexToAddress(eoa.ZeroXDead)
}
func (cfg DeploymentConfig) Validate() error {
if (cfg.Create3Factory == common.Address{}) {
return errors.New("create3 factory is zero")
}
if cfg.Create3Salt == "" {
return errors.New("create3 salt is empty")
}
if (cfg.ProxyAdmin == common.Address{}) {
return errors.New("proxy admin is zero")
}
if isDeadOrEmpty(cfg.Deployer) {
return errors.New("deployer is not set")
}
if isDeadOrEmpty(cfg.Owner) {
return errors.New("owner is not set")
}
if (cfg.Token == common.Address{}) {
return errors.New("token is zero")
}
if (cfg.Portal == common.Address{}) {
return errors.New("portal is zero")
}
if (cfg.ExpectedAddr == common.Address{}) {
return errors.New("expected address is zero")
}
return nil
}
func getDeployCfg(network netconf.ID) (DeploymentConfig, error) {
if network == netconf.Devnet {
return devnetCfg(), nil
}
if network == netconf.Staging {
return stagingCfg(), nil
}
return DeploymentConfig{}, errors.New("unsupported network", "network", network)
}
func stagingCfg() DeploymentConfig {
return DeploymentConfig{
Create3Factory: contracts.StagingCreate3Factory(),
Create3Salt: contracts.L1BridgeSalt(netconf.Staging),
Owner: mustAdmin(netconf.Staging),
Deployer: eoa.MustAddress(netconf.Staging, eoa.RoleDeployer),
ProxyAdmin: contracts.StagingProxyAdmin(),
Portal: contracts.StagingPortal(),
Token: contracts.StagingToken(),
ExpectedAddr: contracts.StagingL1Bridge(),
}
}
func devnetCfg() DeploymentConfig {
return DeploymentConfig{
Create3Factory: contracts.DevnetCreate3Factory(),
Create3Salt: contracts.L1BridgeSalt(netconf.Devnet),
Owner: mustAdmin(netconf.Devnet),
Deployer: eoa.MustAddress(netconf.Devnet, eoa.RoleDeployer),
ProxyAdmin: contracts.DevnetProxyAdmin(),
Portal: contracts.DevnetPortal(),
Token: contracts.DevnetToken(),
ExpectedAddr: contracts.DevnetL1Bridge(),
}
}
func mustAdmin(network netconf.ID) common.Address {
addr, err := eoa.Admin(network)
if err != nil {
panic(err)
}
return addr
}
// Deploy deploys a new L1Bridge contract and returns the address and receipt.
func Deploy(ctx context.Context, network netconf.ID, backend *ethbackend.Backend) (common.Address, *ethtypes.Receipt, error) {
cfg, err := getDeployCfg(network)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "get deployment config")
}
return deploy(ctx, cfg, backend)
}
func deploy(ctx context.Context, cfg DeploymentConfig, backend *ethbackend.Backend) (common.Address, *ethtypes.Receipt, error) {
if err := cfg.Validate(); err != nil {
return common.Address{}, nil, errors.Wrap(err, "validate config")
}
txOpts, err := backend.BindOpts(ctx, cfg.Deployer)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "bind opts")
}
factory, err := bindings.NewCreate3(cfg.Create3Factory, backend)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "new create3")
}
salt := create3.HashSalt(cfg.Create3Salt)
addr, err := factory.GetDeployed(nil, txOpts.From, salt)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "get deployed")
} else if (cfg.ExpectedAddr != common.Address{}) && addr != cfg.ExpectedAddr {
return common.Address{}, nil, errors.New("unexpected address", "expected", cfg.ExpectedAddr, "actual", addr)
}
impl, tx, _, err := bindings.DeployOmniBridgeL1(txOpts, backend, cfg.Token)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "deploy impl")
}
_, err = backend.WaitMined(ctx, tx)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "wait mined impl")
}
initCode, err := packInitCode(cfg, impl)
if err != nil {
return common.Address{}, nil, errors.Wrap(err, "pack init code")
}
tx, err = factory.Deploy(txOpts, salt, initCode)
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 proxy")
}
return addr, receipt, nil
}
func packInitCode(cfg DeploymentConfig, impl common.Address) ([]byte, error) {
bridgeAbi, err := bindings.OmniBridgeL1MetaData.GetAbi()
if err != nil {
return nil, errors.Wrap(err, "get abi")
}
proxyAbi, err := bindings.TransparentUpgradeableProxyMetaData.GetAbi()
if err != nil {
return nil, errors.Wrap(err, "get proxy abi")
}
initializer, err := bridgeAbi.Pack("initialize", cfg.Owner, cfg.Portal)
if err != nil {
return nil, errors.Wrap(err, "encode initializer")
}
return contracts.PackInitCode(proxyAbi, bindings.TransparentUpgradeableProxyBin, impl, cfg.ProxyAdmin, initializer)
}