-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
486 lines (405 loc) · 15.9 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"slices"
"strings"
. "github.com/bjartek/overflow/v2"
"github.com/onflow/cadence"
)
/**
* This script is used to configure the bridge contracts on various networks.
* The following assumptions about the bridge account are made:
* - The bridge account is named "NETWORK-flow-evm-bridge" in the flow.json
* - The bridge account has a balance to cover funding a COA with 1.0 FLOW tokens (if necessary)
* - The bridge account has enough FLOW to cover the storage required for the deployed contracts
*/
// Overflow prefixes signer names with the current network - e.g. "crescendo-flow-evm-bridge"
// Ensure accounts in flow.json are named accordingly
var networks = []string{"crescendo", "emulator", "mainnet", "previewnet", "testnet"}
// Pulled from flow.json deployments. Specified here as some contracts have init arg values that
// are emitted in transaction events and cannot be hardcoded in the deployment config section
var contracts = []string{
"FlowEVMBridgeHandlerInterfaces",
"ArrayUtils",
"StringUtils",
"ScopedFTProviders",
"Serialize",
"SerializeMetadata",
"IBridgePermissions",
"ICrossVM",
"ICrossVMAsset",
"CrossVMNFT",
"CrossVMToken",
"FlowEVMBridgeConfig",
"FlowEVMBridgeUtils",
"FlowEVMBridgeResolver",
"FlowEVMBridgeHandlers",
"FlowEVMBridgeNFTEscrow",
"FlowEVMBridgeTokenEscrow",
"FlowEVMBridgeTemplates",
"IEVMBridgeNFTMinter",
"IEVMBridgeTokenMinter",
"IFlowEVMNFTBridge",
"IFlowEVMTokenBridge",
"FlowEVMBridge",
}
// To run, execute the following command:
// go run main.go $NETWORK
func main() {
network := getSpecifiedNetwork()
dir, err := os.Getwd()
checkNoErr(err)
ctx := context.Background()
o := Overflow(
WithNetwork(network),
WithTransactionFolderName("cadence/transactions"),
WithScriptFolderName("cadence/scripts"),
WithGlobalPrintOptions(WithTransactionUrl()),
)
// Check if the script is a dry run
if checkDryRun() {
log.Printf("Dry run detected...running setup script in dry run mode")
// Run the dry run transaction
serviceDryRunResult := o.Tx("bridge/admin/dry_run", WithSigner("service-account"))
checkNoErr(serviceDryRunResult.Err)
log.Printf("Service Account dry run run successful...")
bridgeDryRunResult := o.Tx("bridge/admin/dry_run", WithSigner("flow-evm-bridge"))
checkNoErr(bridgeDryRunResult.Err)
log.Printf("Bridge Account dry run run successful...")
log.Printf("Dry run complete...exiting script")
return
}
// Create a COA in the bridge account if one does not already exist
bridgeCOAHex, err := o.Script("evm/get_evm_address_string", WithArg("flowAddress", o.Address("flow-evm-bridge"))).GetAsInterface()
checkNoErr(err)
if bridgeCOAHex == nil {
// no COA found, create a COA in the bridge account
coaCreationTxn := o.Tx("evm/create_account", WithSigner("flow-evm-bridge"), WithArg("amount", 1.0))
checkNoErr(coaCreationTxn.Err)
bridgeCOAHex, err = o.Script("evm/get_evm_address_string", WithArg("flowAddress", o.Address("flow-evm-bridge"))).GetAsInterface()
checkNoErr(err)
}
log.Printf("Bridge COA has EVM address: %s", bridgeCOAHex)
/* --- EVM Configuration --- */
gasLimit := 15000000
deploymentValue := 0.0
/// Deploy factory ///
//
// Get the Cadence args json for the factory deployment args
factoryArgsPath := filepath.Join(dir, "cadence/args/deploy-factory-args.json")
// Retrieve the bytecode from the JSON args
// Future implementations should use flowkit to handle this after fixing dependency issues
factoryBytecode := getBytecodeFromArgsJSON(factoryArgsPath)
factoryDeployment := o.Tx("evm/deploy",
WithSigner("flow-evm-bridge"),
WithArg("bytecode", factoryBytecode),
WithArg("gasLimit", gasLimit),
WithArg("value", deploymentValue),
)
checkNoErr(factoryDeployment.Err)
factoryAddr := getContractAddressFromEVMEvent(factoryDeployment)
log.Printf("Factory deployed to address: %s", factoryAddr)
/// Deploy registry ///
//
// Get the Cadence args json for the factory deployment args
registryArgsPath := filepath.Join(dir, "cadence/args/deploy-deployment-registry-args.json")
// Retrieve the bytecode from the JSON args
// Future implementations should use flowkit to handle this after fixing dependency issues
registryBytecode := getBytecodeFromArgsJSON(registryArgsPath)
registryDeployment := o.Tx("evm/deploy",
WithSigner("flow-evm-bridge"),
WithArg("bytecode", registryBytecode),
WithArg("gasLimit", gasLimit),
WithArg("value", deploymentValue),
)
checkNoErr(registryDeployment.Err)
registryAddr := getContractAddressFromEVMEvent(registryDeployment)
log.Printf("Registry deployed to address: %s", registryAddr)
/// Deploy ERC20 deployer ///
//
erc20DeployerArgsPath := filepath.Join(dir, "cadence/args/deploy-erc20-deployer-args.json")
erc20DeployerBytecode := getBytecodeFromArgsJSON(erc20DeployerArgsPath)
erc20DeployerDeployment := o.Tx("evm/deploy",
WithSigner("flow-evm-bridge"),
WithArg("bytecode", erc20DeployerBytecode),
WithArg("gasLimit", gasLimit),
WithArg("value", deploymentValue),
)
checkNoErr(erc20DeployerDeployment.Err)
erc20DeployerAddr := getContractAddressFromEVMEvent(erc20DeployerDeployment)
log.Printf("ERC20 Deployer deployed to address: %s", erc20DeployerAddr)
/// Deploy ERC721 deployer ///
//
erc721DeployerArgsPath := filepath.Join(dir, "cadence/args/deploy-erc721-deployer-args.json")
erc721DeployerBytecode := getBytecodeFromArgsJSON(erc721DeployerArgsPath)
erc721DeployerDeployment := o.Tx("evm/deploy",
WithSigner("flow-evm-bridge"),
WithArg("bytecode", erc721DeployerBytecode),
WithArg("gasLimit", gasLimit),
WithArg("value", deploymentValue),
)
checkNoErr(erc721DeployerDeployment.Err)
erc721DeployerAddr := getContractAddressFromEVMEvent(erc721DeployerDeployment)
log.Printf("ERC721 Deployer deployed to address: %s", erc721DeployerAddr)
/* --- Cadence Configuration --- */
log.Printf("Deploying Cadence contracts...")
// Iterate over contracts in the contracts map
for _, name := range contracts {
log.Printf("Deploying contract: %s...", name)
contract, err := o.State.Config().Contracts.ByName(name)
checkNoErr(err)
contractPath := filepath.Join(dir, contract.Location)
contractCode, err := os.ReadFile(contractPath)
checkNoErr(err)
// If the contract is already deployed as-is, skip deployment
a, err := o.GetAccount(ctx, "flow-evm-bridge")
checkNoErr(err)
log.Printf("Checking if contract %s is already deployed...", name)
if a.Contracts[name] != nil {
log.Printf("Contract %s already deployed, skipping...", name)
continue
}
log.Printf("Contract %s not found on %s, deploying...", name, network)
var args []cadence.Value
if name == "FlowEVMBridgeUtils" {
args = []cadence.Value{cadence.String(factoryAddr)}
} else {
args = []cadence.Value{}
}
err = o.AddContract(ctx, "flow-evm-bridge", contractCode, args, contractPath, true)
checkNoErr(err)
}
log.Printf("Cadence contracts deployed...Pausing bridge for setup...")
// If emulator, deploy USDCFlow contract
if network == "emulator" {
log.Printf("Emulator detected...deploying USDCFlow contract...")
usdcContract, err := o.State.Config().Contracts.ByName("USDCFlow")
checkNoErr(err)
usdcPath := filepath.Join(dir, usdcContract.Location)
usdcCode, err := os.ReadFile(usdcPath)
checkNoErr(err)
err = o.AddContract(ctx, "flow-evm-bridge", usdcCode, []cadence.Value{}, usdcPath, true)
checkNoErr(err)
log.Printf("USDCFlow contract deployed...")
}
// Pause the bridge for setup
var pauseResult = o.Tx("bridge/admin/pause/update_bridge_pause_status",
WithSigner("flow-evm-bridge"),
WithArg("pause", true),
)
checkNoErr(pauseResult.Err)
log.Printf("Bridge deployed and paused...")
/* --- USDCFlow TokenHandler Configuration --- */
// Add USDCFlow TokenHandler
// usdcFlowAddr := o.Address("USDCFlow")
// usdcFlowVaultIdentifier := buildUSDCFlowVaultIdentifier(usdcFlowAddr)
// usdcFlowMinterIdentifier := buildUSDCFlowMinterIdentifier(usdcFlowAddr)
// log.Printf("Bridge pause confirmed...configuring USDCFlow TokenHandler with vault=" + usdcFlowVaultIdentifier + " and minter=" + usdcFlowMinterIdentifier)
// // execute create_cadence_native_token_handler transaction
// createTokenHandlerResult := o.Tx("bridge/admin/token-handler/create_cadence_native_token_handler",
// WithSigner("flow-evm-bridge"),
// WithArg("vaultIdentifier", usdcFlowVaultIdentifier),
// WithArg("minterIdentifier", usdcFlowMinterIdentifier),
// )
// checkNoErr(createTokenHandlerResult.Err)
// log.Printf("USDCFlow TokenHandler configured...")
/* --- Finish EVM Contract Setup --- */
log.Printf("Integrating EVM-side bridge contracts...")
// Set the factory as registrar in the registry
setRegistrarResult := o.Tx("bridge/admin/evm/set_registrar",
WithSigner("flow-evm-bridge"),
WithArg("registryEVMAddressHex", registryAddr),
)
checkNoErr(setRegistrarResult.Err)
// Add the registry to the factory
setRegistryResult := o.Tx("bridge/admin/evm/set_deployment_registry",
WithSigner("flow-evm-bridge"),
WithArg("registryEVMAddressHex", registryAddr),
)
checkNoErr(setRegistryResult.Err)
// Set the factory as delegated deployer in the ERC20 deployer
setDelegatedDeployerResult := o.Tx("bridge/admin/evm/set_delegated_deployer",
WithSigner("flow-evm-bridge"),
WithArg("deployerEVMAddressHex", erc20DeployerAddr),
)
checkNoErr(setDelegatedDeployerResult.Err)
// Set the factory as delegated deployer in the ERC721 deployer
setDelegatedDeployerResult = o.Tx("bridge/admin/evm/set_delegated_deployer",
WithSigner("flow-evm-bridge"),
WithArg("deployerEVMAddressHex", erc721DeployerAddr),
)
checkNoErr(setDelegatedDeployerResult.Err)
// Add the ERC20 Deployer as a deployer in the factory
addDeployerResult := o.Tx("bridge/admin/evm/add_deployer",
WithSigner("flow-evm-bridge"),
WithArg("deployerTag", "ERC20"),
WithArg("deployerEVMAddressHex", erc20DeployerAddr),
)
checkNoErr(addDeployerResult.Err)
// Add the ERC721 Deployer as a deployer in the factory
addDeployerResult = o.Tx("bridge/admin/evm/add_deployer",
WithSigner("flow-evm-bridge"),
WithArg("deployerTag", "ERC721"),
WithArg("deployerEVMAddressHex", erc721DeployerAddr),
)
checkNoErr(addDeployerResult.Err)
log.Printf("Cross-VM bridge contract integration complete...integrating with EVM contract...")
/* --- EVM Contract Integration --- */
// Deploy FlowEVMBridgeAccessor, providing EVM contract host (network service account) as argument
accessorContract, err := o.State.Config().Contracts.ByName("FlowEVMBridgeAccessor")
checkNoErr(err)
// accessorPath := filepath.Join(projectRoot, accessorContract.Location)
accessorPath := filepath.Join(dir, accessorContract.Location)
accessorCode, err := os.ReadFile(accessorPath)
checkNoErr(err)
evmConfigAddr, err := o.State.Config().Contracts.ByName("EVM")
checkNoErr(err)
evmAddr := evmConfigAddr.Aliases.ByNetwork(o.GetNetwork()).Address
log.Printf("EVM contract address: %s", evmAddr)
err = o.AddContract(ctx, "flow-evm-bridge", accessorCode, []cadence.Value{cadence.NewAddress(evmAddr)}, accessorPath, false)
checkNoErr(err)
// Integrate the EVM contract with the BridgeAccessor
integrateResult := o.Tx("bridge/admin/evm-integration/claim_accessor_capability_and_save_router",
WithSigner("service-account"),
WithArg("name", "FlowEVMBridgeAccessor"),
WithArg("provider", o.Address("FlowEVMBridge")),
)
checkNoErr(integrateResult.Err)
log.Printf("EVM integration complete...setting fees...")
/* --- Set Bridge Fees --- */
onboardFeeResult := o.Tx("bridge/admin/fee/update_onboard_fee",
WithSigner("flow-evm-bridge"),
WithArg("newFee", 1.0),
)
checkNoErr(onboardFeeResult.Err)
baseFeeResult := o.Tx("bridge/admin/fee/update_base_fee",
WithSigner("flow-evm-bridge"),
WithArg("newFee", 0.001),
)
checkNoErr(baseFeeResult.Err)
/* --- COMPLETE --- */
log.Printf("Bridge setup complete...Adding bridged Token & NFT templates")
// TODO: Try to pull args JSON from local file once flowkit ParseJSON is fixed
tokenChunkPath := filepath.Join(
dir,
"cadence/args/bridged-token-code-chunks-args-"+network+".json",
)
nftChunkPath := filepath.Join(dir, "cadence/args/bridged-nft-code-chunks-args-"+network+".json")
tokenChunks := getCodeChunksFromArgsJSON(tokenChunkPath)
nftChunks := getCodeChunksFromArgsJSON(nftChunkPath)
tokenChunkUpsert := o.Tx("bridge/admin/templates/upsert_contract_code_chunks",
WithSigner("flow-evm-bridge"),
WithArg("forTemplate", "bridgedToken"),
WithArg("newChunks", tokenChunks),
)
checkNoErr(tokenChunkUpsert.Err)
nftChunkUpsert := o.Tx("bridge/admin/templates/upsert_contract_code_chunks",
WithSigner("flow-evm-bridge"),
WithArg("forTemplate", "bridgedNFT"),
WithArg("newChunks", nftChunks),
)
checkNoErr(nftChunkUpsert.Err)
log.Printf("Templates have been added...Unpausing bridge...")
// Unpause the bridge
// unpauseResult := o.Tx("bridge/admin/pause/update_bridge_pause_status",
// WithSigner("flow-evm-bridge"),
// WithArg("pause", false),
// )
// checkNoErr(unpauseResult.Err)
// log.Printf("SETUP COMPLETE! Bridge is now unpaused and ready for use.")
log.Printf("SETUP COMPLETE! Bridge is still paused - be sure to unpause before use.")
}
func checkDryRun() bool {
if len(os.Args) < 3 {
return false
}
return os.Args[2] == "--dry-run"
}
// Parses the network argument from the command line
// e.g. go run main.go $NETWORK
func getSpecifiedNetwork() string {
if len(os.Args) < 2 {
log.Fatal("Please provide a network as an argument: ", networks)
}
network := os.Args[1]
if !slices.Contains(networks, network) {
log.Fatal("Please provide a valid network as an argument: ", networks)
}
return network
}
// Extracts the deployed contract address from the TransactionExecuted event
func getContractAddressFromEVMEvent(res *OverflowResult) string {
evts := res.GetEventsWithName("TransactionExecuted")
contractAddr := evts[0].Fields["contractAddress"]
if contractAddr == nil {
log.Fatal("Contract address not found in event")
}
return strings.ToLower(strings.Split(contractAddr.(string), "x")[1])
}
// Reads the JSON file at the specified path and returns the compiled solidity bytecode where the
// bytecode is the first element in the JSON array as a Cadence JSON string
func getBytecodeFromArgsJSON(path string) string {
argsData, err := os.ReadFile(path)
checkNoErr(err)
var args []map[string]string
err = json.Unmarshal(argsData, &args)
checkNoErr(err)
return args[0]["value"]
}
type Element struct {
Type string `json:"type"`
Value interface{} `json:"value"`
}
// Reads the JSON file at the specified path and returns the code chunks where the code chunks are
// the second element in the JSON array as Cadence JSON string array
func getCodeChunksFromArgsJSON(path string) []string {
file, err := os.Open(path)
if err != nil {
log.Fatalf("Failed opening file: %s", err)
}
defer file.Close()
byteValue, _ := ioutil.ReadAll(file)
var elements []Element
json.Unmarshal(byteValue, &elements)
secondElement := elements[1]
// Check if the second element is of type "Array"
if secondElement.Type != "Array" {
log.Fatalf("Second element is not of type Array")
}
// Assert that the value is a slice of interfaces
values, ok := secondElement.Value.([]interface{})
if !ok {
log.Fatalf("Failed to assert value to []interface{}")
}
var strArr []string
for _, v := range values {
// Assert that the value is a map
valueMap, ok := v.(map[string]interface{})
if !ok {
log.Fatalf("Failed to assert value to map[string]interface{}")
}
// Get the "value" from the map and assert it to string
str, ok := valueMap["value"].(string)
if !ok {
log.Fatalf("Failed to assert value to string")
}
strArr = append(strArr, str)
}
return strArr
}
func buildUSDCFlowVaultIdentifier(addrString string) string {
return "A." + strings.Split(addrString, "x")[1] + ".USDCFlow.Vault"
}
func buildUSDCFlowMinterIdentifier(addrString string) string {
return "A." + strings.Split(addrString, "x")[1] + ".USDCFlow.Minter"
}
func checkNoErr(err error) {
if err != nil {
log.Fatal(err)
}
}