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

Batch onboarding scripts & txns #35

Merged
merged 8 commits into from
Apr 16, 2024
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
24 changes: 7 additions & 17 deletions cadence/contracts/bridge/FlowEVMBridge.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "FlowToken"

import "EVM"

import "EVMUtils"
import "BridgePermissions"
import "ICrossVM"
import "IEVMBridgeNFTMinter"
Expand Down Expand Up @@ -140,7 +141,7 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
emit Onboarded(
type: type,
cadenceContractAddress: FlowEVMBridgeUtils.getContractAddress(fromType: type)!,
evmContractAddress: FlowEVMBridgeUtils.getEVMAddressAsHexString(address: onboardingValues.evmContractAddress)
evmContractAddress: EVMUtils.getEVMAddressAsHexString(address: onboardingValues.evmContractAddress)
)
}

Expand Down Expand Up @@ -581,14 +582,7 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
if !FlowEVMBridgeUtils.isValidFlowAsset(type: type) {
return nil
}
if type == Type<@FlowToken.Vault>() {
return false
} else if type.isSubtype(of: Type<@{NonFungibleToken.NFT}>()) {
return !FlowEVMBridgeNFTEscrow.isInitialized(forType: type)
} else if type.isSubtype(of: Type<@{FungibleToken.Vault}>()) {
return !FlowEVMBridgeTokenEscrow.isInitialized(forType: type)
}
return nil
return FlowEVMBridgeConfig.getEVMAddressAssociated(with: type) == nil
}

/// Returns whether an EVM-native asset needs to be onboarded to the bridge
Expand All @@ -599,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 Expand Up @@ -812,7 +802,7 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
assetName: name,
symbol: symbol,
isERC721: isERC721,
evmContractAddress: FlowEVMBridgeUtils.getEVMAddressAsHexString(address: evmContractAddress)
evmContractAddress: EVMUtils.getEVMAddressAsHexString(address: evmContractAddress)
)
}
}
29 changes: 25 additions & 4 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,10 @@ 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}

/* --- Path Constants --- */
//
Expand Down Expand Up @@ -51,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 @@ -60,6 +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 @@ -100,11 +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]
)
}
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
51 changes: 3 additions & 48 deletions cadence/contracts/bridge/FlowEVMBridgeUtils.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "FlowStorageFees"

import "EVM"

import "EVMUtils"
import "FlowEVMBridgeConfig"
import "BridgePermissions"

Expand All @@ -29,52 +30,6 @@ contract FlowEVMBridgeUtils {
Public Bridge Utils
**************************/

/// Returns an EVMAddress as a hex string without a 0x prefix
///
/// @param address: The EVMAddress to convert to a hex string
///
/// @return The hex string representation of the EVMAddress without 0x prefix
///
// TODO: Remove once EVMAddress.toString() is available
access(all)
view fun getEVMAddressAsHexString(address: EVM.EVMAddress): String {
let bytes = address.bytes
// Iterating & appending to an array is not allowed in a `view` method and this method must be `view` for
// certain use cases in the bridge contracts - namely for emitting values in pre- & post-conditions
let addressBytes: [UInt8] = [
bytes[0], bytes[1], bytes[2], bytes[3],
bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11],
bytes[12], bytes[13], bytes[14], bytes[15],
bytes[16], bytes[17], bytes[18], bytes[19]
]
return String.encodeHex(addressBytes)
}

/// Returns an EVMAddress as a hex string without a 0x prefix, truncating the string's last 20 bytes if exceeded
///
/// @param address: The hex string to convert to an EVMAddress without the 0x prefix
///
/// @return The EVMAddress representation of the hex string
///
access(all)
fun getEVMAddressFromHexString(address: String): EVM.EVMAddress? {
if address.length != 40 {
return nil
}
var addressBytes: [UInt8] = address.decodeHex()
if addressBytes.length != 20 {
return nil
}
return EVM.EVMAddress(bytes: [
addressBytes[0], addressBytes[1], addressBytes[2], addressBytes[3],
addressBytes[4], addressBytes[5], addressBytes[6], addressBytes[7],
addressBytes[8], addressBytes[9], addressBytes[10], addressBytes[11],
addressBytes[12], addressBytes[13], addressBytes[14], addressBytes[15],
addressBytes[16], addressBytes[17], addressBytes[18], addressBytes[19]
])
}

/// Calculates the fee bridge fee based on the given storage usage. If includeBase is true, the base fee is included
/// in the resulting calculation.
///
Expand Down Expand Up @@ -529,7 +484,7 @@ contract FlowEVMBridgeUtils {
view fun deriveBridgedNFTContractName(from evmContract: EVM.EVMAddress): String {
return self.contractNamePrefixes[Type<@{NonFungibleToken.NFT}>()]!["bridged"]!
.concat(self.delimiter)
.concat("0x".concat(self.getEVMAddressAsHexString(address: evmContract)))
.concat("0x".concat(EVMUtils.getEVMAddressAsHexString(address: evmContract)))
}

/// Derives the Cadence contract name for a given EVM fungible token of the form
Expand All @@ -543,7 +498,7 @@ contract FlowEVMBridgeUtils {
view fun deriveBridgedTokenContractName(from evmContract: EVM.EVMAddress): String {
return self.contractNamePrefixes[Type<@{FungibleToken.Vault}>()]!["bridged"]!
.concat(self.delimiter)
.concat("0x".concat(self.getEVMAddressAsHexString(address: evmContract)))
.concat("0x".concat(EVMUtils.getEVMAddressAsHexString(address: evmContract)))
}

/****************
Expand Down
10 changes: 5 additions & 5 deletions cadence/contracts/bridge/IFlowEVMNFTBridge.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import "NonFungibleToken"

import "EVM"

import "EVMUtils"
import "FlowEVMBridgeConfig"
import "FlowEVMBridgeUtils"
import "CrossVMNFT"

access(all) contract interface IFlowEVMNFTBridge {
Expand Down Expand Up @@ -69,8 +69,8 @@ access(all) contract interface IFlowEVMNFTBridge {
type: token.getType(),
id: token.id,
evmID: CrossVMNFT.getEVMID(from: &token as &{NonFungibleToken.NFT}) ?? UInt256(token.id),
to: FlowEVMBridgeUtils.getEVMAddressAsHexString(address: to),
evmContractAddress: FlowEVMBridgeUtils.getEVMAddressAsHexString(
to: EVMUtils.getEVMAddressAsHexString(address: to),
evmContractAddress: EVMUtils.getEVMAddressAsHexString(
address: self.getAssociatedEVMAddress(with: token.getType())
?? panic("Could not find EVM Contract address associated with provided NFT")
), bridgeAddress: self.account.address
Expand Down Expand Up @@ -104,8 +104,8 @@ access(all) contract interface IFlowEVMNFTBridge {
type: result.getType(),
id: result.id,
evmID: id,
caller: FlowEVMBridgeUtils.getEVMAddressAsHexString(address: owner),
evmContractAddress: FlowEVMBridgeUtils.getEVMAddressAsHexString(
caller: EVMUtils.getEVMAddressAsHexString(address: owner),
evmContractAddress: EVMUtils.getEVMAddressAsHexString(
address: self.getAssociatedEVMAddress(with: result.getType())
?? panic("Could not find EVM Contract address associated with provided NFT")
), bridgeAddress: self.account.address
Expand Down
10 changes: 5 additions & 5 deletions cadence/contracts/bridge/IFlowEVMTokenBridge.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "NonFungibleToken"

import "EVM"

import "FlowEVMBridgeUtils"
import "EVMUtils"

access(all) contract interface IFlowEVMTokenBridge {

Expand Down Expand Up @@ -64,8 +64,8 @@ access(all) contract interface IFlowEVMTokenBridge {
emit BridgedTokensToEVM(
type: vault.getType(),
amount: vault.balance,
to: FlowEVMBridgeUtils.getEVMAddressAsHexString(address: to),
evmContractAddress: FlowEVMBridgeUtils.getEVMAddressAsHexString(
to: EVMUtils.getEVMAddressAsHexString(address: to),
evmContractAddress: EVMUtils.getEVMAddressAsHexString(
address: self.getAssociatedEVMAddress(with: vault.getType())
?? panic("Could not find EVM Contract address associated with provided NFT")
), bridgeAddress: self.account.address
Expand Down Expand Up @@ -98,8 +98,8 @@ access(all) contract interface IFlowEVMTokenBridge {
emit BridgedTokensFromEVM(
type: result.getType(),
amount: amount,
caller: FlowEVMBridgeUtils.getEVMAddressAsHexString(address: owner),
evmContractAddress: FlowEVMBridgeUtils.getEVMAddressAsHexString(
caller: EVMUtils.getEVMAddressAsHexString(address: owner),
evmContractAddress: EVMUtils.getEVMAddressAsHexString(
address: self.getAssociatedEVMAddress(with: result.getType())
?? panic("Could not find EVM Contract address associated with provided Vault")
), bridgeAddress: self.account.address
Expand Down
51 changes: 51 additions & 0 deletions cadence/contracts/utils/EVMUtils.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import "EVM"

/// Contract containing EVM-related utility methods
///
access(all) contract EVMUtils {
/// Returns an EVMAddress as a hex string without a 0x prefix
///
/// @param address: The EVMAddress to convert to a hex string
///
/// @return The hex string representation of the EVMAddress without 0x prefix
///
// TODO: Remove once EVMAddress.toString() is available
access(all)
view fun getEVMAddressAsHexString(address: EVM.EVMAddress): String {
let bytes = address.bytes
// Iterating & appending to an array is not allowed in a `view` method and this method must be `view` for
// certain use cases in the bridge contracts - namely for emitting values in pre- & post-conditions
let addressBytes: [UInt8] = [
bytes[0], bytes[1], bytes[2], bytes[3],
bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11],
bytes[12], bytes[13], bytes[14], bytes[15],
bytes[16], bytes[17], bytes[18], bytes[19]
]
return String.encodeHex(addressBytes)
}

/// Returns an EVMAddress as a hex string without a 0x prefix, truncating the string's last 20 bytes if exceeded
///
/// @param address: The hex string to convert to an EVMAddress without the 0x prefix
///
/// @return The EVMAddress representation of the hex string
///
access(all)
fun getEVMAddressFromHexString(address: String): EVM.EVMAddress? {
if address.length != 40 {
return nil
}
var addressBytes: [UInt8] = address.decodeHex()
if addressBytes.length != 20 {
return nil
}
return EVM.EVMAddress(bytes: [
addressBytes[0], addressBytes[1], addressBytes[2], addressBytes[3],
addressBytes[4], addressBytes[5], addressBytes[6], addressBytes[7],
addressBytes[8], addressBytes[9], addressBytes[10], addressBytes[11],
addressBytes[12], addressBytes[13], addressBytes[14], addressBytes[15],
addressBytes[16], addressBytes[17], addressBytes[18], addressBytes[19]
])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import "EVMUtils"
import "FlowEVMBridge"

/// Returns whether a EVM contract needs to be onboarded to the FlowEVMBridge
///
/// @param evmAddresses: Array of hex-encoded address of the EVM contract as a String without 0x prefix to check for
/// onboarding status
///
/// @return Whether the contract requires onboarding to the FlowEVMBridge if the type is bridgeable, otherwise nil
/// indexed on the hex-encoded address of the EVM contract without 0x prefix
///
access(all) fun main(evmAddresses: [String]): {String: Bool?} {
let results: {String: Bool?} = {}
for addressHex in evmAddresses {
if results[addressHex] != nil {
continue
}
if let address = EVMUtils.getEVMAddressFromHexString(address: addressHex) {
let requiresOnboarding = FlowEVMBridge.evmAddressRequiresOnboarding(address)
results.insert(key: addressHex, requiresOnboarding)
}
}
return results
}
19 changes: 19 additions & 0 deletions cadence/scripts/bridge/batch_type_requires_onboarding.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "FlowEVMBridge"

/// Returns whether a type needs to be onboarded to the FlowEVMBridge
///
/// @param Types: The array of types to check for onboarding status
///
/// @return Whether the type requires onboarding to the FlowEVMBridge if the type is bridgeable, otherwise nil indexed
/// on the type
///
access(all) fun main(types: [Type]): {Type: Bool?} {
let results: {Type: Bool?} = {}
for type in types {
if results[type] != nil {
continue
}
Comment on lines +13 to +15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this ever be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, but covering a case where there are non-unique values in the provided array

results.insert(key: type, FlowEVMBridge.typeRequiresOnboarding(type))
}
return results
}
4 changes: 2 additions & 2 deletions cadence/scripts/bridge/evm_address_requires_onboarding.cdc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "FlowEVMBridgeUtils"
import "EVMUtils"
import "FlowEVMBridge"

/// Returns whether a EVM contract needs to be onboarded to the FlowEVMBridge
Expand All @@ -8,7 +8,7 @@ import "FlowEVMBridge"
/// @return Whether the contract requires onboarding to the FlowEVMBridge if the type is bridgeable, otherwise nil
///
access(all) fun main(evmAddressHex: String): Bool? {
if let address = FlowEVMBridgeUtils.getEVMAddressFromHexString(address: evmAddressHex) {
if let address = EVMUtils.getEVMAddressFromHexString(address: evmAddressHex) {
return FlowEVMBridge.evmAddressRequiresOnboarding(address)
}
return nil
Expand Down
Loading
Loading