Skip to content

Commit

Permalink
refactor tests as much as possible - blocked by issue in EVM.Result
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed May 3, 2024
1 parent 869f545 commit 245f4b3
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 162 deletions.
10 changes: 9 additions & 1 deletion cadence/args/deploy-factory-args.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions cadence/contracts/bridge/FlowEVMBridgeNFTEscrow.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ access(all) contract FlowEVMBridgeNFTEscrow {
access(all) let erc721Address: EVM.EVMAddress
/// The type of NFTs this Locker escrows
access(all) let lockedType: Type
/// Count of locked NFTs as lockedNFTs.length may exceed computation limits
/// Count of locked NFTs as ownedNFTs.length may exceed computation limits
access(self) var lockedNFTCount: Int
/// Indexed on NFT UUID to prevent collisions
access(self) let lockedNFTs: @{UInt64: {NonFungibleToken.NFT}}
access(all) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}}
/// Maps EVM NFT ID to Flow NFT ID, covering cross-VM project NFTs
access(self) let evmIDToFlowID: {UInt256: UInt64}

Expand All @@ -167,7 +167,7 @@ access(all) contract FlowEVMBridgeNFTEscrow {
self.lockedType = lockedType
self.erc721Address = erc721Address
self.lockedNFTCount = 0
self.lockedNFTs <- {}
self.ownedNFTs <- {}
self.evmIDToFlowID = {}
}

Expand All @@ -192,7 +192,7 @@ access(all) contract FlowEVMBridgeNFTEscrow {
///
access(all)
view fun getIDs(): [UInt64] {
return self.lockedNFTs.keys
return self.ownedNFTs.keys
}

/// Returns all the EVM IDs of the locked NFTs if the locked token implements CrossVMNFT.EVMNFT
Expand Down Expand Up @@ -238,7 +238,7 @@ access(all) contract FlowEVMBridgeNFTEscrow {
///
access(all)
view fun borrowNFT(_ id: UInt64): &{NonFungibleToken.NFT}? {
return &self.lockedNFTs[id]
return &self.ownedNFTs[id]
}

/// Returns a map of supported NFT types - at the moment Lockers only support the lockedNFTType defined by
Expand Down Expand Up @@ -276,17 +276,17 @@ access(all) contract FlowEVMBridgeNFTEscrow {
self.evmIDToFlowID[evmID] = token.id
}
self.lockedNFTCount = self.lockedNFTCount + 1
self.lockedNFTs[token.id] <-! token
self.ownedNFTs[token.id] <-! token
}

/// Withdraws the NFT from this locker, removing it from the collection and returning it
///
access(NonFungibleToken.Withdraw | NonFungibleToken.Owner)
access(NonFungibleToken.Withdraw)
fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
// Should not happen, but prevent potential underflow
assert(self.lockedNFTCount > 0, message: "No NFTs to withdraw")
self.lockedNFTCount = self.lockedNFTCount - 1
let token <- self.lockedNFTs.remove(key: withdrawID)!
let token <- self.ownedNFTs.remove(key: withdrawID)!
if let evmID = CrossVMNFT.getEVMID(from: &token as &{NonFungibleToken.NFT}) {
self.evmIDToFlowID.remove(key: evmID)
}
Expand Down
20 changes: 3 additions & 17 deletions cadence/contracts/bridge/FlowEVMBridgeUtils.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ contract FlowEVMBridgeUtils {
return decodedResult[0] as! EVM.EVMAddress
}

init(bridgeFactoryBytecodeHex: String) {
init(bridgeFactoryAddressHex: String) {
self.delimiter = "_"
self.contractNamePrefixes = {
Type<@{NonFungibleToken.NFT}>(): {
Expand All @@ -1265,21 +1265,7 @@ contract FlowEVMBridgeUtils {
"bridged": "EVMVMBridgedToken"
}
}

// Deploy the FlowBridgeFactory.sol contract from provided bytecode and capture the deployed address
let coa = self.account.storage.borrow<auth(EVM.Deploy) &EVM.CadenceOwnedAccount>(from: /storage/evm)
?? panic("Could not borrow COA reference")
let evmResult = coa.deploy(
code: bridgeFactoryBytecodeHex.decodeHex(),
gasLimit: 15_000_000,
value: EVM.Balance(attoflow: 0)
)

assert(
evmResult.status == EVM.Status.successful,
message: "Bridge factory deployment failed"
)

self.bridgeFactoryEVMAddress = evmResult.deployedContract ?? panic("Deployed contract address is nil")
self.bridgeFactoryEVMAddress = EVMUtils.getEVMAddressFromHexString(address: bridgeFactoryAddressHex)
?? panic("Invalid EVM address hex")
}
}
42 changes: 0 additions & 42 deletions cadence/tests/contracts/EVMDeployer.cdc

This file was deleted.

51 changes: 27 additions & 24 deletions cadence/tests/flow_evm_bridge_handler_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BlockchainHelpers
import "FungibleToken"
import "NonFungibleToken"
import "FlowStorageFees"
import "EVM"

import "test_helpers.cdc"

Expand All @@ -20,6 +21,7 @@ access(all) let exampleTokenMinterIdentifier = "A.0000000000000011.ExampleHandle
access(all) let exampleTokenMintAmount = 100.0

// ERC20 values
access(all) var erc20AddressHex: String = ""
access(all) let erc20MintAmount: UInt256 = 100_000_000_000_000_000_000

// Fee initialiazation values
Expand Down Expand Up @@ -113,10 +115,23 @@ fun setup() {
arguments: []
)
Test.expect(err, Test.beNil())

// Deploy FlowBridgeFactory.sol
let deploymentResult = executeTransaction(
"../transactions/evm/deploy.cdc",
[getCompiledFactoryBytecode(), 15_000_000, 0.0],
bridgeAccount
)
// Get the deployed contract address from the latest EVM event
let evts = Test.eventsOfType(Type<EVM.TransactionExecuted>())
Test.assertEqual(2, evts.length)
let factoryDeploymentEvent = evts[0] as! EVM.TransactionExecuted
let factoryAddressHex = factoryDeploymentEvent.contractAddress

err = Test.deployContract(
name: "FlowEVMBridgeUtils",
path: "../contracts/bridge/FlowEVMBridgeUtils.cdc",
arguments: [getCompiledFactoryBytecode()]
arguments: [factoryAddressHex.slice(from: 2, upTo: factoryAddressHex.length)]
)
Test.expect(err, Test.beNil())
err = Test.deployContract(
Expand Down Expand Up @@ -200,15 +215,6 @@ fun setup() {
transferFlow(signer: serviceAccount, recipient: exampleERCAccount.address, amount: 1_000.0)
createCOA(signer: exampleERCAccount, fundingAmount: 10.0)

// Deploy the ERC20 from EVMDeployer (simply to capture deploye EVM contract address)
// TODO: Replace this contract with the `deployedContractAddress` value emitted on deployment
// once `evm` events Types are available
err = Test.deployContract(
name: "EVMDeployer",
path: "./contracts/EVMDeployer.cdc",
arguments: []
)
Test.expect(err, Test.beNil())
err = Test.deployContract(
name: "ExampleHandledToken",
path: "../contracts/example-assets/ExampleHandledToken.cdc",
Expand Down Expand Up @@ -331,21 +337,26 @@ fun testEnableTokenHandlerFails() {
access(all)
fun testDeployERC20Succeeds() {
let erc20DeployResult = executeTransaction(
"./transactions/deploy_using_evm_deployer.cdc",
["erc20", getCompiledERC20Bytecode(), 0 as UInt],
"../transactions/evm/deploy.cdc",
[getCompiledERC20Bytecode(), UInt64(15_000_000), 0.0],
exampleERCAccount
)
Test.expect(erc20DeployResult, Test.beSucceeded())

let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")
let evts = Test.eventsOfType(Type<EVM.TransactionExecuted>())
Test.assertEqual(7, evts.length)
let erc20DeploymentEvent = evts[6] as! EVM.TransactionExecuted
// remove 0x prefix
erc20AddressHex = erc20DeploymentEvent.contractAddress.slice(
from: 2,
upTo: erc20DeploymentEvent.contractAddress.length
).toLower()
}

// Set the TokenHandler's targetEVMAddress to the deployed ERC20 contract address
// This will filter requests to onboard the ERC20 to the bridge as the Cadence-nat
access(all)
fun testSetHandlerTargetEVMAddressSucceeds() {
let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")

let setHandlerTargetResult = executeTransaction(
"../transactions/bridge/admin/token-handler/set_handler_target_evm_address.cdc",
[exampleTokenIdentifier, erc20AddressHex],
Expand All @@ -368,7 +379,6 @@ fun testMintERC20ToBridgeEscrowSucceeds() {
contractName: "ExampleHandledToken",
vaultIdentifier: exampleTokenIdentifier
) ?? panic("Problem getting total supply of Cadence tokens")
let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")

// Convert total supply UFix64 to UInt256 for ERC20 minting
let uintTotalSupply = ufix64ToUInt256(exampleTokenTotalSupply, decimals: defaultDecimals)
Expand All @@ -388,7 +398,6 @@ fun testMintERC20ToBridgeEscrowSucceeds() {
access(all)
fun testMintERC20ToArbitraryRecipientSucceeds() {
let aliceCOAAddressHex = getCOAAddressHex(atFlowAddress: alice.address)
let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")

let mintERC20Result = executeTransaction(
"../transactions/example-assets/evm-assets/mint_erc20.cdc",
Expand Down Expand Up @@ -426,7 +435,6 @@ fun testOnboardHandledTokenByTypeFails() {
// Since the erc20 Address has a TokenHandler, onboarding should fail
access(all)
fun testOnboardHandledERC20ByEVMAddressFails() {
let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")

var onboaringRequiredResult = executeScript(
"../scripts/bridge/evm_address_requires_onboarding.cdc",
Expand Down Expand Up @@ -469,7 +477,6 @@ fun testBridgeHandledCadenceNativeTokenToEVMFails() {
// Bridging frrom EVM before TokenHandler is enabled should fail
access(all)
fun testBridgeHandledCadenceNativeTokenFromEVMFails() {
let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")
let aliceCOAAddressHex = getCOAAddressHex(atFlowAddress: alice.address)

// Confirm ownership on EVM side with Alice COA as owner of ERC721 representation
Expand Down Expand Up @@ -503,7 +510,6 @@ access(all)
fun testBridgeHandledCadenceNativeTokenToEVMFirstSucceeds() {
snapshot = getCurrentBlockHeight()

let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")
let erc20TotalSupplyBefore = getEVMTotalSupply(erc20AddressHex: erc20AddressHex)

// Alice was the only recipient, so their balance should be the total supply
Expand Down Expand Up @@ -554,7 +560,6 @@ fun testBridgeHandledCadenceNativeTokenToEVMFirstSucceeds() {
// With all funds now in EVM, we can test bridging back to Cadence
access(all)
fun testBridgeHandledCadenceNativeTokenFromEVMSecondSucceeds() {
let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")
let aliceCOAAddressHex = getCOAAddressHex(atFlowAddress: alice.address)

let erc20TotalSupplyBefore = getEVMTotalSupply(erc20AddressHex: erc20AddressHex)
Expand Down Expand Up @@ -594,10 +599,9 @@ fun testBridgeHandledCadenceNativeTokenFromEVMFirstSucceeds() {
// Reset to snapshot before bridging between VMs
Test.reset(to: snapshot)

let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")
let aliceCOAAddressHex = getCOAAddressHex(atFlowAddress: alice.address)

var erc20TotalSupplyBefore = getEVMTotalSupply(erc20AddressHex: getDeployedAddressFromDeployer(name: "erc20"))
var erc20TotalSupplyBefore = getEVMTotalSupply(erc20AddressHex: erc20AddressHex)
let cadenceTotalSupplyBefore = getCadenceTotalSupply(
contractAddress: exampleHandledTokenAccount.address,
contractName: "ExampleHandledToken",
Expand Down Expand Up @@ -658,7 +662,6 @@ fun testBridgeHandledCadenceNativeTokenFromEVMFirstSucceeds() {
// Now return all liquidity back to EVM
access(all)
fun testBridgeHandledCadenceNativeTokenToEVMSecondSucceeds() {
let erc20AddressHex = getDeployedAddressFromDeployer(name: "erc20")
let aliceCOAAddressHex = getCOAAddressHex(atFlowAddress: alice.address)

let erc20TotalSupplyBefore = getEVMTotalSupply(erc20AddressHex: erc20AddressHex)
Expand Down
Loading

0 comments on commit 245f4b3

Please sign in to comment.