Skip to content

Commit

Permalink
Merge pull request #94 from onflow/fix-contract-field-access
Browse files Browse the repository at this point in the history
Fix contract field access
  • Loading branch information
sisyphusSmiling authored Jul 1, 2024
2 parents 8e04119 + 160c682 commit ae9e49e
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 54 deletions.
11 changes: 10 additions & 1 deletion cadence/contracts/bridge/FlowEVMBridgeUtils.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ access(all)
contract FlowEVMBridgeUtils {

/// Address of the bridge factory Solidity contract
access(all)
access(self)
var bridgeFactoryEVMAddress: EVM.EVMAddress
/// Delimeter used to derive contract names
access(self)
Expand Down Expand Up @@ -91,6 +91,15 @@ contract FlowEVMBridgeUtils {
Public Bridge Utils
**************************/

/// Retrieves the bridge factory contract address
///
/// @returns The EVMAddress of the bridge factory contract in EVM
///
access(all)
view fun getBridgeFactoryEVMAddress(): EVM.EVMAddress {
return self.bridgeFactoryEVMAddress
}

/// Calculates the fee bridge fee based on the given storage usage + the current base fee.
///
/// @param used: The amount of storage used by the asset
Expand Down
2 changes: 1 addition & 1 deletion cadence/scripts/utils/get_factory_address.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import "FlowEVMBridgeUtils"
/// @return The EVM address of the FlowEVMBridgeFactory contract as hex string (without 0x prefix)
///
access(all) fun main(): String {
return FlowEVMBridgeUtils.bridgeFactoryEVMAddress.toString()
return FlowEVMBridgeUtils.getBridgeFactoryEVMAddress().toString()
}
3 changes: 1 addition & 2 deletions cadence/scripts/utils/token_uri.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import "FlowEVMBridgeUtils"

/// Returns the tokenURI of the given tokenID from the given EVM contract address
///
/// @param coaHost: The Flow account Address of the account that hosts the COA used to make the call
/// @param id: The ID of the ERC721 token
/// @param contractAddressHex: The hex string of the contract address of the ERC721 token
/// @param tokenID: The ID of the ERC721 token
///
/// @return The tokenURI of the given tokenID from the given EVM contract address. Reverts if the call is unsuccessful
///
Expand Down
30 changes: 30 additions & 0 deletions cadence/tests/scripts/get_registry.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "EVM"

import "FlowEVMBridgeUtils"

access(all)
fun main(): String {
let coa = getAuthAccount<auth(BorrowValue) &Account>(0xdfc20aee650fcbdf)
.storage
.borrow<auth(EVM.Call) &EVM.CadenceOwnedAccount>(
from: /storage/evm
) ?? panic("Problem borrowing COA")
// Confirm the registry address was set
let postRegistryResult = coa.call(
to: FlowEVMBridgeUtils.getBridgeFactoryEVMAddress(),
data: EVM.encodeABIWithSignature("owner()", []),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
)
assert(
postRegistryResult.status == EVM.Status.successful,
message: "Failed to get registry address from FlowBridgeFactory contract"
)

let decodedResult = EVM.decodeABI(
types: [Type<EVM.EVMAddress>()],
data: postRegistryResult.data
)
assert(decodedResult.length == 1, message: "Invalid response from getRegistry() call to FlowBridgeFactory contract")
return (decodedResult[0] as! EVM.EVMAddress).toString()
}
14 changes: 7 additions & 7 deletions cadence/transactions/bridge/admin/evm/add_deployer.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import "FlowEVMBridgeUtils"
///
transaction(deployerTag: String, deployerEVMAddressHex: String) {

let targetDeployerEVMAddress: EVM.EVMAddress
let coa: auth(EVM.Call) &EVM.CadenceOwnedAccount
var postDeployer: EVM.EVMAddress?

prepare(signer: auth(BorrowValue) &Account) {
self.targetDeployerEVMAddress = EVM.addressFromString(deployerEVMAddressHex)
self.coa = signer.storage.borrow<auth(EVM.Call) &EVM.CadenceOwnedAccount>(from: /storage/evm)
?? panic("Could not borrow COA from provided gateway address")

self.postDeployer = nil
}

execute {
// Execute the call
let deployerEVMAddress = EVM.addressFromString(deployerEVMAddressHex)
let callResult = self.coa.call(
to: FlowEVMBridgeUtils.bridgeFactoryEVMAddress,
to: FlowEVMBridgeUtils.getBridgeFactoryEVMAddress(),
data: EVM.encodeABIWithSignature(
"addDeployer(string,address)",
[deployerTag, deployerEVMAddress]
[deployerTag, self.targetDeployerEVMAddress]
),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
Expand All @@ -36,7 +36,7 @@ transaction(deployerTag: String, deployerEVMAddressHex: String) {

// Confirm the deployer was added under the tag
let postDeployerResult = self.coa.call(
to: FlowEVMBridgeUtils.bridgeFactoryEVMAddress,
to: FlowEVMBridgeUtils.getBridgeFactoryEVMAddress(),
data: EVM.encodeABIWithSignature(
"getDeployer(string)",
[deployerTag]
Expand All @@ -49,12 +49,12 @@ transaction(deployerTag: String, deployerEVMAddressHex: String) {
let decodedResult = EVM.decodeABI(
types: [Type<EVM.EVMAddress>()],
data: postDeployerResult.data
) as! [AnyStruct]
)
assert(decodedResult.length == 1, message: "Invalid response from getDeployer call")
self.postDeployer = decodedResult[0] as! EVM.EVMAddress
}

post {
self.postDeployer!.toString() == deployerEVMAddressHex: "Deployer was not properly configured"
self.postDeployer!.equals(self.targetDeployerEVMAddress): "Deployer was not properly configured"
}
}
14 changes: 7 additions & 7 deletions cadence/transactions/bridge/admin/evm/set_delegated_deployer.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import "FlowEVMBridgeUtils"
///
transaction(deployerEVMAddressHex: String) {

let targetDeployerEVMAddress: EVM.EVMAddress
let coa: auth(EVM.Call) &EVM.CadenceOwnedAccount
var postDelegatedDeployer: EVM.EVMAddress?

prepare(signer: auth(BorrowValue) &Account) {
self.targetDeployerEVMAddress = EVM.addressFromString(deployerEVMAddressHex)
self.coa = signer.storage.borrow<auth(EVM.Call) &EVM.CadenceOwnedAccount>(from: /storage/evm)
?? panic("Could not borrow COA from provided gateway address")

self.postDelegatedDeployer = nil
}

execute {
// Execute the call
let deployerEVMAddress = EVM.addressFromString(deployerEVMAddressHex)
let callResult = self.coa.call(
to: deployerEVMAddress,
to: self.targetDeployerEVMAddress,
data: EVM.encodeABIWithSignature(
"setDelegatedDeployer(address)",
[FlowEVMBridgeUtils.bridgeFactoryEVMAddress]
[FlowEVMBridgeUtils.getBridgeFactoryEVMAddress()]
),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
Expand All @@ -35,7 +35,7 @@ transaction(deployerEVMAddressHex: String) {

// Confirm the delegated deployer was set
let postDelegatedDeployerResult = self.coa.call(
to: deployerEVMAddress,
to: self.targetDeployerEVMAddress,
data: EVM.encodeABIWithSignature("delegatedDeployer()", []),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
Expand All @@ -51,9 +51,9 @@ transaction(deployerEVMAddressHex: String) {
}

post {
self.postDelegatedDeployer!.toString() == FlowEVMBridgeUtils.bridgeFactoryEVMAddress.toString():
self.postDelegatedDeployer!.equals(FlowEVMBridgeUtils.getBridgeFactoryEVMAddress()):
"FlowBridgeFactory address "
.concat(FlowEVMBridgeUtils.bridgeFactoryEVMAddress.toString())
.concat(FlowEVMBridgeUtils.getBridgeFactoryEVMAddress().toString())
.concat(" was not set as the delegated deployer in the deployer contract ")
.concat(deployerEVMAddressHex)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ import "FlowEVMBridgeUtils"
///
transaction(registryEVMAddressHex: String) {

let targetRegistryEVMAddress: EVM.EVMAddress
let coa: auth(EVM.Call) &EVM.CadenceOwnedAccount
var postRegistry: EVM.EVMAddress?

prepare(signer: auth(BorrowValue) &Account) {
self.targetRegistryEVMAddress = EVM.addressFromString(registryEVMAddressHex)
self.coa = signer.storage.borrow<auth(EVM.Call) &EVM.CadenceOwnedAccount>(from: /storage/evm)
?? panic("Could not borrow COA from provided gateway address")
self.postRegistry = nil
}

execute {
// Execute call
let registryEVMAddress = EVM.addressFromString(registryEVMAddressHex)
let callResult = self.coa.call(
to: FlowEVMBridgeUtils.bridgeFactoryEVMAddress,
to: FlowEVMBridgeUtils.getBridgeFactoryEVMAddress(),
data: EVM.encodeABIWithSignature(
"setDeploymentRegistry(address)",
[registryEVMAddress]
[self.targetRegistryEVMAddress]
),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
Expand All @@ -38,7 +39,7 @@ transaction(registryEVMAddressHex: String) {

// Confirm the registry address was set
let postRegistryResult = self.coa.call(
to: FlowEVMBridgeUtils.bridgeFactoryEVMAddress,
to: FlowEVMBridgeUtils.getBridgeFactoryEVMAddress(),
data: EVM.encodeABIWithSignature("getRegistry()", []),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
Expand All @@ -51,16 +52,16 @@ transaction(registryEVMAddressHex: String) {
let decodedResult = EVM.decodeABI(
types: [Type<EVM.EVMAddress>()],
data: postRegistryResult.data
) as! [AnyStruct]
)
assert(decodedResult.length == 1, message: "Invalid response from getRegistry() call to FlowBridgeFactory contract")
self.postRegistry = decodedResult[0] as! EVM.EVMAddress
}

post {
self.postRegistry!.toString() == registryEVMAddressHex:
self.postRegistry!.equals(self.targetRegistryEVMAddress):
"Registry address "
.concat(registryEVMAddressHex)
.concat(" was not set in the FlowBridgeFactory contract ")
.concat(FlowEVMBridgeUtils.bridgeFactoryEVMAddress.toString())
.concat(FlowEVMBridgeUtils.getBridgeFactoryEVMAddress().toString())
}
}
15 changes: 8 additions & 7 deletions cadence/transactions/bridge/admin/evm/set_registrar.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import "FlowEVMBridgeUtils"
///
transaction(registryEVMAddressHex: String) {

let targetRegistryEVMAddress: EVM.EVMAddress
let coa: auth(EVM.Call) &EVM.CadenceOwnedAccount
var postRegistrar: EVM.EVMAddress?

prepare(signer: auth(BorrowValue) &Account) {
self.targetRegistryEVMAddress = EVM.addressFromString(registryEVMAddressHex)
self.coa = signer.storage.borrow<auth(EVM.Call) &EVM.CadenceOwnedAccount>(from: /storage/evm)
?? panic("Could not borrow COA from provided gateway address")
self.postRegistrar = nil
}

execute {
let registryEVMAddress = EVM.addressFromString(registryEVMAddressHex)
let callResult = self.coa.call(
to: registryEVMAddress,
to: self.targetRegistryEVMAddress,
data: EVM.encodeABIWithSignature(
"setRegistrar(address)",
[FlowEVMBridgeUtils.bridgeFactoryEVMAddress]
[FlowEVMBridgeUtils.getBridgeFactoryEVMAddress()]
),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
Expand All @@ -33,7 +34,7 @@ transaction(registryEVMAddressHex: String) {

// Confirm the registrar was set
let postRegistrarResult = self.coa.call(
to: registryEVMAddress,
to: self.targetRegistryEVMAddress,
data: EVM.encodeABIWithSignature("registrar()", []),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
Expand All @@ -43,15 +44,15 @@ transaction(registryEVMAddressHex: String) {
let decodedResult = EVM.decodeABI(
types: [Type<EVM.EVMAddress>()],
data: postRegistrarResult.data
) as! [AnyStruct]
)
assert(decodedResult.length == 1, message: "Invalid response from registrar() call to registry contract")
self.postRegistrar = decodedResult[0] as! EVM.EVMAddress
}

post {
self.postRegistrar!.toString() == FlowEVMBridgeUtils.bridgeFactoryEVMAddress.toString():
self.postRegistrar!.equals(FlowEVMBridgeUtils.getBridgeFactoryEVMAddress()):
"FlowBridgeFactory address "
.concat(FlowEVMBridgeUtils.bridgeFactoryEVMAddress.toString())
.concat(FlowEVMBridgeUtils.getBridgeFactoryEVMAddress().toString())
.concat(" was not set as the registrar in the registry contract ")
.concat(registryEVMAddressHex)
}
Expand Down
39 changes: 17 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func main() {
checkNoErr(registryDeployment.Err)
registryAddr := getContractAddressFromEVMEvent(registryDeployment)

log.Printf("Registry deployed to address: %s", factoryAddr)
log.Printf("Registry deployed to address: %s", registryAddr)

/// Deploy ERC20 deployer ///
//
Expand All @@ -148,7 +148,7 @@ func main() {
checkNoErr(erc20DeployerDeployment.Err)
erc20DeployerAddr := getContractAddressFromEVMEvent(erc20DeployerDeployment)

log.Printf("ERC20 Deployer deployed to address: %s", factoryAddr)
log.Printf("ERC20 Deployer deployed to address: %s", erc20DeployerAddr)

/// Deploy ERC721 deployer ///
//
Expand All @@ -163,7 +163,7 @@ func main() {
checkNoErr(erc721DeployerDeployment.Err)
erc721DeployerAddr := getContractAddressFromEVMEvent(erc721DeployerDeployment)

log.Printf("ERC721 Deployer deployed to address: %s", factoryAddr)
log.Printf("ERC721 Deployer deployed to address: %s", erc721DeployerAddr)

/* --- Cadence Configuration --- */

Expand All @@ -179,6 +179,16 @@ func main() {
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)}
Expand All @@ -197,23 +207,8 @@ func main() {
WithArg("pause", true),
)
checkNoErr(pauseResult.Err)
log.Printf("Bridge paused, configuring token handlers...")

// TODO: Blocked on FiatToken staging - uncomment once the updated contract is staged & migrated
// Add TokenHandler for specified Types
// fiatToken, err := o.State.Config().Contracts.ByName("FiatToken")
// checkNoErr(err)
// fiatTokenAddress := fiatToken.Aliases.ByNetwork(o.GetNetwork()).Address
// fiatTokenVaultIdentifier := "A." + fiatTokenAddress.String() + ".FiatToken.Vault"
// fiatTokenMinterIdentifier := "A." + fiatTokenAddress.String() + ".FiatToken.MinterResource"
// handlerCreationResult := o.Tx("bridge/admin/token-handler/create_cadence_native_token_handler",
// WithSigner("flow-evm-bridge"),
// WithArg("vaultIdentifier", fiatTokenVaultIdentifier),
// WithArg("minterIdentifier", fiatTokenMinterIdentifier),
// )
// checkNoErr(handlerCreationResult.Err)

log.Printf("Token handlers configured...continuing EVM setup...")

// TODO: Add any TokenHandlers here if needed

/* --- Finish EVM Contract Setup --- */

Expand Down Expand Up @@ -294,12 +289,12 @@ func main() {

onboardFeeResult := o.Tx("bridge/admin/fee/update_onboard_fee",
WithSigner("flow-evm-bridge"),
WithArg("newFee", 0.0),
WithArg("newFee", 1.0),
)
checkNoErr(onboardFeeResult.Err)
baseFeeResult := o.Tx("bridge/admin/fee/update_base_fee",
WithSigner("flow-evm-bridge"),
WithArg("newFee", 0.0),
WithArg("newFee", 0.001),
)
checkNoErr(baseFeeResult.Err)

Expand Down

0 comments on commit ae9e49e

Please sign in to comment.