From 5ab3bd66ed9ec28443ee564bb0161602e7756345 Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:20:01 -0500 Subject: [PATCH] add reverse mapping to config & refactor evmAddressRequiresOnboarding for batch onboarding --- cadence/contracts/bridge/FlowEVMBridge.cdc | 10 ++----- .../contracts/bridge/FlowEVMBridgeConfig.cdc | 30 ++++++++++++++----- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/cadence/contracts/bridge/FlowEVMBridge.cdc b/cadence/contracts/bridge/FlowEVMBridge.cdc index e2a31668..e68cb9eb 100644 --- a/cadence/contracts/bridge/FlowEVMBridge.cdc +++ b/cadence/contracts/bridge/FlowEVMBridge.cdc @@ -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 } diff --git a/cadence/contracts/bridge/FlowEVMBridgeConfig.cdc b/cadence/contracts/bridge/FlowEVMBridgeConfig.cdc index 82c52eab..295b7932 100644 --- a/cadence/contracts/bridge/FlowEVMBridgeConfig.cdc +++ b/cadence/contracts/bridge/FlowEVMBridgeConfig.cdc @@ -2,6 +2,8 @@ import "EVM" import "FlowToken" +import "EVMUtils" + /// This contract is used to store configuration information shared by FlowEVMBridge contracts /// access(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} @@ -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 ****************************/ @@ -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 } /***************** @@ -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