Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor go package #170

Merged
merged 5 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: test
test:
sh local/run_cadence_tests.sh
go test gotests/contracts_test.go
go test

.PHONY: ci
ci: check-tidy test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
transaction(name: String, code: String, evmAddress: Address) {
prepare(signer: auth(AddContract) &Account) {
signer.contracts.add(name: name, code: code.utf8, publishToEVMAccount: evmAddress)
}
}
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ toolchain go1.23.1

require (
github.com/onflow/flow-core-contracts/lib/go/templates v1.6.0
github.com/onflow/flow-evm-bridge/bridge v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.9.0
)

Expand Down Expand Up @@ -69,5 +68,3 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/onflow/flow-evm-bridge/bridge => ./
330 changes: 14 additions & 316 deletions main.go → templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ import (
//go:embed cadence/scripts/utils/token_uri.cdc
//go:embed cadence/scripts/utils/total_supply.cdc

//go:embed cadence/transactions/bridge/admin/deploy_bridge_utils.cdc
//go:embed cadence/transactions/bridge/admin/deploy_bridge_accessor.cdc

//go:embed cadence/transactions/bridge/admin/blocklist/block_cadence_type.cdc
//go:embed cadence/transactions/bridge/admin/blocklist/block_evm_address.cdc
//go:embed cadence/transactions/bridge/admin/blocklist/unblock_cadence_type.cdc
Expand Down Expand Up @@ -155,6 +158,9 @@ import (
//go:embed cadence/args/bridged-nft-code-chunks-args-emulator.json
//go:embed cadence/args/bridged-token-code-chunks-args-emulator.json
//go:embed cadence/args/deploy-factory-args.json
//go:embed cadence/args/deploy-deployment-registry-args.json
//go:embed cadence/args/deploy-erc20-deployer-args.json
//go:embed cadence/args/deploy-erc721-deployer-args.json
var content embed.FS

var (
Expand Down Expand Up @@ -230,7 +236,7 @@ func ReplaceAddress(code, placeholder, replacement string) string {
code = strings.ReplaceAll(
code,
placeholder,
placeholderWithoutQuotes+"from "+withHexPrefix(replacement),
placeholderWithoutQuotes+" from "+withHexPrefix(replacement),
)
}
return code
Expand Down Expand Up @@ -297,11 +303,13 @@ func ReplaceAddresses(code string, bridgeEnv Environment, coreEnv coreContracts.
placeholderFlowEVMBridgeAddress,
bridgeEnv.FlowEVMBridgeAddress,
)
code = ReplaceAddress(
code,
placeholderFlowEVMBridgeAccessorAddress,
bridgeEnv.FlowEVMBridgeAccessorAddress,
)
if !strings.Contains(code, "contract FlowEVMBridgeAccessor") {
code = ReplaceAddress(
code,
placeholderFlowEVMBridgeAccessorAddress,
bridgeEnv.FlowEVMBridgeAccessorAddress,
)
}
code = ReplaceAddress(
code,
placeholderFlowEVMBridgeConfigAddress,
Expand Down Expand Up @@ -620,316 +628,6 @@ func GetSolidityContractCode(contractName string) (string, error) {
}
}

/**
* 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() {

// // 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 registry 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 {

// 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...")

// // 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 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)
Expand Down
Loading
Loading