Skip to content

Commit

Permalink
update NFT & FT bridge txn interfaces to accept type identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Jun 27, 2024
1 parent 8667f8a commit dbd804d
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 114 deletions.
43 changes: 30 additions & 13 deletions cadence/tests/flow_evm_bridge_handler_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,11 @@ fun testBridgeHandledCadenceNativeTokenToEVMFails() {
// Execute bridge to EVM - should fail since Handler is not enabled
bridgeTokensToEVM(
signer: alice,
contractAddr: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
vaultIdentifier: buildTypeIdentifier(
address: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
resourceName: "Vault"
),
amount: cadenceBalance,
beFailed: true
)
Expand All @@ -474,8 +477,11 @@ fun testBridgeHandledCadenceNativeTokenFromEVMFails() {
// Execute bridge from EVM
bridgeTokensFromEVM(
signer: alice,
contractAddr: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
vaultIdentifier: buildTypeIdentifier(
address: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
resourceName: "Vault"
),
amount: evmBalance,
beFailed: true
)
Expand Down Expand Up @@ -518,8 +524,11 @@ fun testBridgeHandledCadenceNativeTokenToEVMFirstSucceeds() {
// Execute bridge to EVM
bridgeTokensToEVM(
signer: alice,
contractAddr: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
vaultIdentifier: buildTypeIdentifier(
address: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
resourceName: "Vault"
),
amount: cadenceBalance,
beFailed: false
)
Expand Down Expand Up @@ -557,8 +566,11 @@ fun testBridgeHandledCadenceNativeTokenFromEVMSecondSucceeds() {
let ufixEVMbalance = uint256ToUFix64(evmBalance, decimals: defaultDecimals)
bridgeTokensFromEVM(
signer: alice,
contractAddr: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
vaultIdentifier: buildTypeIdentifier(
address: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
resourceName: "Vault"
),
amount: evmBalance,
beFailed: false
)
Expand Down Expand Up @@ -617,8 +629,11 @@ fun testBridgeHandledCadenceNativeTokenFromEVMFirstSucceeds() {
// Execute bridge from EVM
bridgeTokensFromEVM(
signer: alice,
contractAddr: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
vaultIdentifier: buildTypeIdentifier(
address: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
resourceName: "Vault"
),
amount: aliceEVMBalanceBefore,
beFailed: false
)
Expand Down Expand Up @@ -666,9 +681,11 @@ fun testBridgeHandledCadenceNativeTokenToEVMSecondSucceeds() {
// Execute bridge to EVM
bridgeTokensToEVM(
signer: alice,
contractAddr: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
amount: aliceCadenceBalanceBefore,
vaultIdentifier: buildTypeIdentifier(
address: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
resourceName: "Vault"
), amount: aliceCadenceBalanceBefore,
beFailed: false
)

Expand Down
72 changes: 37 additions & 35 deletions cadence/tests/flow_evm_bridge_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,11 @@ fun testBridgeFlowToEVMSucceeds() {
let bridgeAmount = 100.0
bridgeTokensToEVM(
signer: alice,
contractAddr: Address(0x03),
contractName: "FlowToken",
amount: bridgeAmount,
vaultIdentifier: buildTypeIdentifier(
address: Address(0x03),
contractName: "FlowToken",
resourceName: "Vault"
), amount: bridgeAmount,
beFailed: false
)

Expand Down Expand Up @@ -605,8 +607,7 @@ fun testOnboardAndBridgeNFTToEVMSucceeds() {
// Execute bridge NFT to EVM - should also onboard the NFT type
bridgeNFTToEVM(
signer: alice,
contractAddr: exampleNFTAccount.address,
contractName: "ExampleNFT",
nftIdentifier: exampleNFTIdentifier,
nftID: aliceID,
bridgeAccountAddr: bridgeAccount.address,
beFailed: false
Expand Down Expand Up @@ -658,7 +659,7 @@ fun testOnboardAndCrossVMTransferNFTToEVMSucceeds() {
// Execute bridge NFT to EVM recipient - should also onboard the NFT type
let crossVMTransferResult = executeTransaction(
"../transactions/bridge/nft/bridge_nft_to_any_evm_address.cdc",
[ exampleNFTAccount.address, "ExampleNFT", aliceID, recipient ],
[ exampleNFTIdentifier, aliceID, recipient ],
alice
)
Test.expect(crossVMTransferResult, Test.beSucceeded())
Expand Down Expand Up @@ -731,8 +732,7 @@ fun testOnboardAndBridgeTokensToEVMSucceeds() {
// Execute bridge to EVM - should also onboard the token type
bridgeTokensToEVM(
signer: alice,
contractAddr: exampleTokenAccount.address,
contractName: "ExampleToken",
vaultIdentifier: exampleTokenIdentifier,
amount: cadenceBalance,
beFailed: false
)
Expand Down Expand Up @@ -780,7 +780,7 @@ fun testOnboardAndCrossVMTransferTokensToEVMSucceeds() {
// Execute bridge to EVM - should also onboard the token type
let crossVMTransferResult = executeTransaction(
"../transactions/bridge/tokens/bridge_tokens_to_any_evm_address.cdc",
[ exampleTokenAccount.address, "ExampleToken", cadenceBalance, recipient ],
[ exampleTokenIdentifier, cadenceBalance, recipient ],
alice
)
Test.expect(crossVMTransferResult, Test.beSucceeded())
Expand Down Expand Up @@ -973,8 +973,7 @@ fun testPauseBridgeSucceeds() {
// Execute bridge to EVM - should fail after pausing
bridgeNFTToEVM(
signer: alice,
contractAddr: exampleNFTAccount.address,
contractName: "ExampleNFT",
nftIdentifier: exampleNFTIdentifier,
nftID: aliceOwnedIDs[0],
bridgeAccountAddr: bridgeAccount.address,
beFailed: true
Expand Down Expand Up @@ -1006,8 +1005,7 @@ fun testBridgeCadenceNativeNFTToEVMSucceeds() {
// Execute bridge to EVM
bridgeNFTToEVM(
signer: alice,
contractAddr: exampleNFTAccount.address,
contractName: "ExampleNFT",
nftIdentifier: exampleNFTIdentifier,
nftID: aliceOwnedIDs[0],
bridgeAccountAddr: bridgeAccount.address,
beFailed: false
Expand Down Expand Up @@ -1052,7 +1050,7 @@ fun testCrossVMTransferCadenceNativeNFTFromEVMSucceeds() {
// Execute bridge NFT from EVM to Cadence recipient (Bob in this case)
let crossVMTransferResult = executeTransaction(
"../transactions/bridge/nft/bridge_nft_to_any_cadence_address.cdc",
[ exampleNFTAccount.address, "ExampleNFT", UInt256(mintedNFTID), bob.address ],
[ exampleNFTIdentifier, UInt256(mintedNFTID), bob.address ],
alice
)
Test.expect(crossVMTransferResult, Test.beSucceeded())
Expand Down Expand Up @@ -1082,8 +1080,7 @@ fun testBridgeCadenceNativeNFTFromEVMSucceeds() {
// Execute bridge from EVM
bridgeNFTFromEVM(
signer: alice,
contractAddr: exampleNFTAccount.address,
contractName: "ExampleNFT",
nftIdentifier: exampleNFTIdentifier,
erc721ID: UInt256(mintedNFTID),
bridgeAccountAddr: bridgeAccount.address,
beFailed: false
Expand All @@ -1108,9 +1105,11 @@ fun testBridgeEVMNativeNFTFromEVMSucceeds() {

bridgeNFTFromEVM(
signer: alice,
contractAddr: bridgeAccount.address,
contractName: derivedERC721ContractName,
erc721ID: erc721ID,
nftIdentifier: buildTypeIdentifier(
address: bridgeAccount.address,
contractName: derivedERC721ContractName,
resourceName: "NFT"
), erc721ID: erc721ID,
bridgeAccountAddr: bridgeAccount.address,
beFailed: false
)
Expand Down Expand Up @@ -1152,8 +1151,7 @@ fun testPauseByTypeSucceeds() {
// Execute bridge to EVM - should fail after pausing
bridgeNFTToEVM(
signer: alice,
contractAddr: exampleNFTAccount.address,
contractName: "ExampleNFT",
nftIdentifier: exampleNFTIdentifier,
nftID: aliceOwnedIDs[0],
bridgeAccountAddr: bridgeAccount.address,
beFailed: true
Expand Down Expand Up @@ -1187,9 +1185,11 @@ fun testBridgeEVMNativeNFTToEVMSucceeds() {

bridgeNFTToEVM(
signer: alice,
contractAddr: bridgeAccount.address,
contractName: derivedERC721ContractName,
nftID: aliceOwnedIDs[0],
nftIdentifier: buildTypeIdentifier(
address: bridgeAccount.address,
contractName: derivedERC721ContractName,
resourceName: "NFT"
), nftID: aliceOwnedIDs[0],
bridgeAccountAddr: bridgeAccount.address,
beFailed: false
)
Expand All @@ -1214,8 +1214,7 @@ fun testBridgeCadenceNativeTokenToEVMSucceeds() {
// Execute bridge to EVM
bridgeTokensToEVM(
signer: alice,
contractAddr: exampleTokenAccount.address,
contractName: "ExampleToken",
vaultIdentifier: exampleTokenIdentifier,
amount: cadenceBalance,
beFailed: false
)
Expand Down Expand Up @@ -1266,7 +1265,7 @@ fun testCrossVMTransferCadenceNativeTokenFromEVMSucceeds() {
// Execute bridge tokens from EVM to Cadence recipient (Bob in this case)
let crossVMTransferResult = executeTransaction(
"../transactions/bridge/tokens/bridge_tokens_to_any_cadence_address.cdc",
[ exampleTokenAccount.address, "ExampleToken", evmBalance, bob.address ],
[ exampleTokenIdentifier, evmBalance, bob.address ],
alice
)
Test.expect(crossVMTransferResult, Test.beSucceeded())
Expand Down Expand Up @@ -1305,8 +1304,7 @@ fun testBridgeCadenceNativeTokenFromEVMSucceeds() {
// Execute bridge from EVM
bridgeTokensFromEVM(
signer: alice,
contractAddr: exampleTokenAccount.address,
contractName: "ExampleToken",
vaultIdentifier: exampleTokenIdentifier,
amount: evmBalance,
beFailed: false
)
Expand Down Expand Up @@ -1339,9 +1337,11 @@ fun testBridgeEVMNativeTokenFromEVMSucceeds() {
// Execute bridge from EVM
bridgeTokensFromEVM(
signer: alice,
contractAddr: bridgeAccount.address,
contractName: derivedERC20ContractName,
amount: evmBalance,
vaultIdentifier: buildTypeIdentifier(
address: bridgeAccount.address,
contractName: derivedERC20ContractName,
resourceName: "Vault"
), amount: evmBalance,
beFailed: false
)

Expand Down Expand Up @@ -1388,9 +1388,11 @@ fun testBridgeEVMNativeTokenToEVMSucceeds() {
// Execute bridge from EVM
bridgeTokensToEVM(
signer: alice,
contractAddr: bridgeAccount.address,
contractName: derivedERC20ContractName,
amount: cadenceBalance,
vaultIdentifier: buildTypeIdentifier(
address: bridgeAccount.address,
contractName: derivedERC20ContractName,
resourceName: "Vault"
), amount: cadenceBalance,
beFailed: false
)

Expand Down
29 changes: 17 additions & 12 deletions cadence/tests/test_helpers.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ fun getEVMAddressHexFromEvents(_ evts: [AnyStruct], idx: Int): String {
return hexAddress
}

/* --- Derivation Helpers --- */

access(all)
fun buildTypeIdentifier(address: Address, contractName: String, resourceName: String): String {
return "A.".concat(address.toString().split(separator: "x")[1]).concat(".")
.concat(contractName).concat(".")
.concat(resourceName)
}

/* --- Script Helpers --- */

access(all)
Expand Down Expand Up @@ -367,15 +376,14 @@ fun createCOA(signer: Test.TestAccount, fundingAmount: UFix64) {
access(all)
fun bridgeNFTToEVM(
signer: Test.TestAccount,
contractAddr: Address,
contractName: String,
nftIdentifier: String,
nftID: UInt64,
bridgeAccountAddr: Address,
beFailed: Bool
) {
let bridgeResult = _executeTransaction(
"../transactions/bridge/nft/bridge_nft_to_evm.cdc",
[contractAddr, contractName, nftID],
[nftIdentifier, nftID],
signer
)
if beFailed {
Expand All @@ -399,15 +407,14 @@ fun bridgeNFTToEVM(
access(all)
fun bridgeNFTFromEVM(
signer: Test.TestAccount,
contractAddr: Address,
contractName: String,
nftIdentifier: String,
erc721ID: UInt256,
bridgeAccountAddr: Address,
beFailed: Bool
) {
let bridgeResult = _executeTransaction(
"../transactions/bridge/nft/bridge_nft_from_evm.cdc",
[contractAddr, contractName, erc721ID],
[nftIdentifier, erc721ID],
signer
)
if beFailed {
Expand All @@ -429,14 +436,13 @@ fun bridgeNFTFromEVM(
access(all)
fun bridgeTokensToEVM(
signer: Test.TestAccount,
contractAddr: Address,
contractName: String,
vaultIdentifier: String,
amount: UFix64,
beFailed: Bool
) {
let bridgeResult = _executeTransaction(
"../transactions/bridge/tokens/bridge_tokens_to_evm.cdc",
[contractAddr, contractName, amount],
[vaultIdentifier, amount],
signer
)
Test.expect(bridgeResult, beFailed ? Test.beFailed() : Test.beSucceeded())
Expand All @@ -448,14 +454,13 @@ fun bridgeTokensToEVM(
access(all)
fun bridgeTokensFromEVM(
signer: Test.TestAccount,
contractAddr: Address,
contractName: String,
vaultIdentifier: String,
amount: UInt256,
beFailed: Bool
) {
let bridgeResult = _executeTransaction(
"../transactions/bridge/tokens/bridge_tokens_from_evm.cdc",
[contractAddr, contractName, amount],
[vaultIdentifier, amount],
signer
)
Test.expect(bridgeResult, beFailed ? Test.beFailed() : Test.beSucceeded())
Expand Down
21 changes: 12 additions & 9 deletions cadence/transactions/bridge/nft/bridge_nft_from_evm.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import "FlowEVMBridgeUtils"
/// NOTE: The ERC721 must have first been onboarded to the bridge. This can be checked via the method
/// FlowEVMBridge.evmAddressRequiresOnboarding(address: self.evmContractAddress)
///
/// @param nftContractAddress: The Flow account address hosting the NFT-defining Cadence contract
/// @param nftContractName: The name of the NFT-defining Cadence contract
/// @param nftIdentifier: The Cadence type identifier of the NFT to bridge - e.g. nft.getType().identifier
/// @param id: The ERC721 id of the NFT to bridge to Cadence from EVM
///
transaction(nftContractAddress: Address, nftContractName: String, id: UInt256) {
transaction(nftIdentifier: String, id: UInt256) {

let nftType: Type
let collection: &{NonFungibleToken.Collection}
Expand All @@ -34,12 +33,16 @@ transaction(nftContractAddress: Address, nftContractName: String, id: UInt256) {
self.coa = signer.storage.borrow<auth(EVM.Bridge) &EVM.CadenceOwnedAccount>(from: /storage/evm)
?? panic("Could not borrow COA from provided gateway address")

// Get the ERC721 contract address for the given NFT type
self.nftType = FlowEVMBridgeUtils.buildCompositeType(
address: nftContractAddress,
contractName: nftContractName,
resourceName: "NFT"
) ?? panic("Could not construct NFT type")
/* --- Construct the NFT type --- */
//
// Construct the NFT type from the provided identifier
self.nftType = CompositeType(nftIdentifier)
?? panic("Could not construct NFT type from identifier: ".concat(nftIdentifier))
// Parse the NFT identifier into its components
let nftContractAddress = FlowEVMBridgeUtils.getContractAddress(fromType: self.nftType)
?? panic("Could not get contract address from identifier: ".concat(nftIdentifier))
let nftContractName = FlowEVMBridgeUtils.getContractName(fromType: self.nftType)
?? panic("Could not get contract name from identifier: ".concat(nftIdentifier))

/* --- Reference the signer's NFT Collection --- */
//
Expand Down
Loading

0 comments on commit dbd804d

Please sign in to comment.