-
Notifications
You must be signed in to change notification settings - Fork 264
/
main.go
300 lines (264 loc) · 8.93 KB
/
main.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"context"
"flag"
"fmt"
"math/big"
"os"
"strings"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum-optimism/optimism/op-node/bindings"
bindingspreview "github.com/ethereum-optimism/optimism/op-node/bindings/preview"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/base-org/withdrawer/signer"
"github.com/base-org/withdrawer/withdraw"
)
type network struct {
l2RPC string
portalAddress string
l2OOAddress string
disputeGameFactory string
faultProofs bool
}
var networks = map[string]network{
"base-mainnet": {
l2RPC: "https://mainnet.base.org",
portalAddress: "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e",
l2OOAddress: "0x0000000000000000000000000000000000000000",
disputeGameFactory: "0x43edB88C4B80fDD2AdFF2412A7BebF9dF42cB40e",
faultProofs: true,
},
"base-sepolia": {
l2RPC: "https://sepolia.base.org",
portalAddress: "0x49f53e41452C74589E85cA1677426Ba426459e85",
l2OOAddress: "0x0000000000000000000000000000000000000000",
disputeGameFactory: "0xd6E6dBf4F7EA0ac412fD8b65ED297e64BB7a06E1",
faultProofs: true,
},
"op-mainnet": {
l2RPC: "https://mainnet.optimism.io",
portalAddress: "0xbEb5Fc579115071764c7423A4f12eDde41f106Ed",
l2OOAddress: "0x0000000000000000000000000000000000000000",
disputeGameFactory: "0xe5965Ab5962eDc7477C8520243A95517CD252fA9",
faultProofs: true,
},
"op-sepolia": {
l2RPC: "https://sepolia.optimism.io",
portalAddress: "0x16Fc5058F25648194471939df75CF27A2fdC48BC",
l2OOAddress: "0x0000000000000000000000000000000000000000",
disputeGameFactory: "0x05F9613aDB30026FFd634f38e5C4dFd30a197Fa1",
faultProofs: true,
},
}
func main() {
var networkKeys []string
for n := range networks {
networkKeys = append(networkKeys, n)
}
var rpcFlag string
var networkFlag string
var l2RpcFlag string
var faultProofs bool
var portalAddress string
var l2OOAddress string
var dgfAddress string
var withdrawalFlag string
var privateKey string
var ledger bool
var mnemonic string
var hdPath string
flag.StringVar(&rpcFlag, "rpc", "", "Ethereum L1 RPC url")
flag.StringVar(&networkFlag, "network", "base-mainnet", fmt.Sprintf("op-stack network to withdraw.go from (one of: %s)", strings.Join(networkKeys, ", ")))
flag.StringVar(&l2RpcFlag, "l2-rpc", "", "Custom network L2 RPC url")
flag.BoolVar(&faultProofs, "fault-proofs", false, "Use fault proofs")
flag.StringVar(&portalAddress, "portal-address", "", "Custom network OptimismPortal address")
flag.StringVar(&l2OOAddress, "l2oo-address", "", "Custom network L2OutputOracle address")
flag.StringVar(&dgfAddress, "dfg-address", "", "Custom network DisputeGameFactory address")
flag.StringVar(&withdrawalFlag, "withdrawal", "", "TX hash of the L2 withdrawal transaction")
flag.StringVar(&privateKey, "private-key", "", "Private key to use for signing transactions")
flag.BoolVar(&ledger, "ledger", false, "Use ledger device for signing transactions")
flag.StringVar(&mnemonic, "mnemonic", "", "Mnemonic to use for signing transactions")
flag.StringVar(&hdPath, "hd-path", "m/44'/60'/0'/0/0", "Hierarchical deterministic derivation path for mnemonic or ledger")
flag.Parse()
log.SetDefault(oplog.NewLogger(os.Stderr, oplog.DefaultCLIConfig()))
n, ok := networks[networkFlag]
if !ok {
log.Crit("Unknown network", "network", networkFlag)
}
// check for non-compatible networks with given flags
if faultProofs {
if n.faultProofs == false {
log.Crit("Fault proofs are not supported on this network")
}
} else {
if n.faultProofs == true {
log.Crit("Fault proofs are required on this network, please provide the --fault-proofs flag")
}
}
// check for non-empty flags for non-fault proof networks
if !faultProofs && (l2RpcFlag != "" || portalAddress != "" || l2OOAddress != "") {
if l2RpcFlag == "" {
log.Crit("Missing --l2-rpc flag")
}
if portalAddress == "" {
log.Crit("Missing --portal-address flag")
}
if l2OOAddress == "" {
log.Crit("Missing --l2oo-address flag")
}
n = network{
l2RPC: l2RpcFlag,
portalAddress: portalAddress,
l2OOAddress: l2OOAddress,
faultProofs: faultProofs,
}
}
// check for non-empty flags for fault proof networks
if faultProofs && (l2RpcFlag != "" || dgfAddress != "" || portalAddress != "") {
if l2RpcFlag == "" {
log.Crit("Missing --l2-rpc flag")
}
if dgfAddress == "" {
log.Crit("Missing --dfg-address flag")
}
if portalAddress == "" {
log.Crit("Missing --portal-address flag")
}
n = network{
l2RPC: l2RpcFlag,
portalAddress: portalAddress,
disputeGameFactory: dgfAddress,
faultProofs: faultProofs,
}
}
if rpcFlag == "" {
log.Crit("Missing --rpc flag")
}
if withdrawalFlag == "" {
log.Crit("Missing --withdrawal flag")
}
withdrawal := common.HexToHash(withdrawalFlag)
options := 0
if privateKey != "" {
options++
}
if ledger {
options++
}
if mnemonic != "" {
options++
}
if options != 1 {
log.Crit("One (and only one) of --private-key, --ledger, --mnemonic must be set")
}
// instantiate shared variables
s, err := signer.CreateSigner(privateKey, mnemonic, hdPath)
if err != nil {
log.Crit("Error creating signer", "error", err)
}
withdrawer, err := CreateWithdrawHelper(rpcFlag, withdrawal, n, s)
if err != nil {
log.Crit("Error creating withdrawer", "error", err)
}
// handle withdrawals with or without the fault proofs withdrawer
isFinalized, err := withdrawer.IsProofFinalized()
if err != nil {
log.Crit("Error querying withdrawal finalization status", "error", err)
}
if isFinalized {
fmt.Println("Withdrawal already finalized")
return
}
// TODO: Add functionality to generate output root proposal and prove to that proposal for FPs
err = withdrawer.CheckIfProvable()
if err != nil {
log.Crit("Withdrawal is not provable", "error", err)
}
proofTime, err := withdrawer.GetProvenWithdrawalTime()
if err != nil {
log.Crit("Error querying withdrawal proof", "error", err)
}
if proofTime == 0 {
err = withdrawer.ProveWithdrawal()
if err != nil {
log.Crit("Error proving withdrawal", "error", err)
}
if faultProofs {
fmt.Println("The withdrawal has been successfully proven, finalization of the withdrawal can be done once the dispute game has finished and the finalization period has elapsed")
} else {
fmt.Println("The withdrawal has been successfully proven, finalization of the withdrawal can be done once the finalization period has elapsed")
}
return
}
// TODO: Add edge-case handling for FPs if a withdrawal needs to be re-proven due to blacklisted / failed dispute game resolution
err = withdrawer.FinalizeWithdrawal()
if err != nil {
log.Crit("Error completing withdrawal", "error", err)
}
}
func CreateWithdrawHelper(l1Rpc string, withdrawal common.Hash, n network, s signer.Signer) (withdraw.WithdrawHelper, error) {
ctx := context.Background()
l1Client, err := ethclient.DialContext(ctx, l1Rpc)
if err != nil {
return nil, fmt.Errorf("Error dialing L1 client: %w", err)
}
l1ChainID, err := l1Client.ChainID(ctx)
if err != nil {
return nil, fmt.Errorf("Error querying chain ID: %w", err)
}
l1Nonce, err := l1Client.PendingNonceAt(ctx, s.Address())
if err != nil {
return nil, fmt.Errorf("Error querying nonce: %w", err)
}
l1opts := &bind.TransactOpts{
From: s.Address(),
Signer: s.SignerFn(l1ChainID),
Context: ctx,
Nonce: big.NewInt(int64(l1Nonce)),
}
l2Client, err := rpc.DialContext(ctx, n.l2RPC)
if err != nil {
return nil, fmt.Errorf("Error dialing L2 client: %w", err)
}
if n.faultProofs {
portal, err := bindingspreview.NewOptimismPortal2(common.HexToAddress(n.portalAddress), l1Client)
if err != nil {
return nil, fmt.Errorf("Error binding OptimismPortal2 contract: %w", err)
}
dgf, err := bindings.NewDisputeGameFactory(common.HexToAddress(n.disputeGameFactory), l1Client)
if err != nil {
return nil, fmt.Errorf("Error binding DisputeGameFactory contract: %w", err)
}
return &withdraw.FPWithdrawer{
Ctx: ctx,
L1Client: l1Client,
L2Client: l2Client,
L2TxHash: withdrawal,
Portal: portal,
Factory: dgf,
Opts: l1opts,
}, nil
} else {
portal, err := bindings.NewOptimismPortal(common.HexToAddress(n.portalAddress), l1Client)
if err != nil {
return nil, fmt.Errorf("Error binding OptimismPortal contract: %w", err)
}
l2oo, err := bindings.NewL2OutputOracle(common.HexToAddress(n.l2OOAddress), l1Client)
if err != nil {
return nil, fmt.Errorf("Error binding L2OutputOracle contract: %w", err)
}
return &withdraw.Withdrawer{
Ctx: ctx,
L1Client: l1Client,
L2Client: l2Client,
L2TxHash: withdrawal,
Portal: portal,
Oracle: l2oo,
Opts: l1opts,
}, nil
}
}