Skip to content

Commit

Permalink
add reverse mapping to config & refactor evmAddressRequiresOnboarding…
Browse files Browse the repository at this point in the history
… for batch onboarding
  • Loading branch information
sisyphusSmiling committed Apr 16, 2024
1 parent c987f43 commit 5ab3bd6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
10 changes: 3 additions & 7 deletions cadence/contracts/bridge/FlowEVMBridge.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -593,19 +593,15 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
///
access(all)
fun evmAddressRequiresOnboarding(_ address: EVM.EVMAddress): Bool? {
// If the address was deployed by the bridge or a Cadence contract has been deployed to define the
// corresponding NFT, it's already been onboarded
let nftContractName = FlowEVMBridgeUtils.deriveBridgedNFTContractName(from: address)
let tokenContractName = FlowEVMBridgeUtils.deriveBridgedTokenContractName(from: address)
if FlowEVMBridgeUtils.isEVMContractBridgeOwned(evmContractAddress: address) ||
self.account.contracts.get(name: nftContractName) != nil ||
self.account.contracts.get(name: tokenContractName) != nil {
// See if the bridge already has a known type associated with the given address
if FlowEVMBridgeConfig.getTypeAssociated(with: address) != nil {
return false
}
// Dealing with EVM-native asset, check if it's NFT or FT exclusively
if FlowEVMBridgeUtils.isValidEVMAsset(evmContractAddress: address) {
return true
}
// Not onboarded and not a valid asset, so return nil
return nil
}

Expand Down
30 changes: 23 additions & 7 deletions cadence/contracts/bridge/FlowEVMBridgeConfig.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "EVM"

import "FlowToken"

import "EVMUtils"

/// This contract is used to store configuration information shared by FlowEVMBridge contracts
///
access(all)
Expand All @@ -21,6 +23,8 @@ contract FlowEVMBridgeConfig {
/// Mapping of Type to its associated EVMAddress as relevant to the bridge
access(self)
let typeToEVMAddress: {Type: EVM.EVMAddress}
/// Reverse mapping of typeToEVMAddress. Note the EVMAddress is stored as a hex string since the EVMAddress type
/// as of contract development is not a hashable or equatable type and making it so is not supported by Cadence
access(self)
let evmAddressHexToType: {String: Type}

Expand Down Expand Up @@ -53,6 +57,14 @@ contract FlowEVMBridgeConfig {
return self.typeToEVMAddress[type]
}

/// Retrieves the type associated with a given EVMAddress if it has been onboarded to the bridge
///
access(all)
view fun getTypeAssociated(with evmAddress: EVM.EVMAddress): Type? {
let evmAddressHex = EVMUtils.getEVMAddressAsHexString(address: evmAddress)
return self.evmAddressHexToType[evmAddressHex]
}

/****************************
Bridge Account Methods
****************************/
Expand All @@ -62,7 +74,8 @@ contract FlowEVMBridgeConfig {
access(account)
fun associateType(_ type: Type, with evmAddress: EVM.EVMAddress) {
self.typeToEVMAddress[type] = evmAddress

let evmAddressHex = EVMUtils.getEVMAddressAsHexString(address: evmAddress)
self.evmAddressHexToType[evmAddressHex] = type
}

/*****************
Expand Down Expand Up @@ -103,13 +116,16 @@ contract FlowEVMBridgeConfig {
self.onboardFee = 0.0
self.baseFee = 0.0
self.defaultDecimals = 18

self.typeToEVMAddress = {
Type<@FlowToken.Vault>(): EVM.EVMAddress(
bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
// Although $FLOW does not have ERC20 address, we associate the the Vault with the EVM address from which
// EVM transfers originate
// See FLIP #223 - https://github.com/onflow/flips/pull/225
let flowOriginationAddress = EVM.EVMAddress(
bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
)
}
self.evmAddressHexToType = {}
let flowVaultType = Type<@FlowToken.Vault>()
let flowOriginationAddressHex = EVMUtils.getEVMAddressAsHexString(address: flowOriginationAddress)
self.typeToEVMAddress = { flowVaultType: flowOriginationAddress }
self.evmAddressHexToType = { flowOriginationAddressHex: flowVaultType}
self.adminStoragePath = /storage/flowEVMBridgeConfigAdmin
self.coaStoragePath = /storage/evm
self.providerCapabilityStoragePath = /storage/bridgeFlowVaultProvider
Expand Down

0 comments on commit 5ab3bd6

Please sign in to comment.