Skip to content

Commit

Permalink
add test coverage - found issue with test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Mar 21, 2024
1 parent da96b31 commit db6f166
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 10 deletions.
22 changes: 22 additions & 0 deletions cadence/scripts/utils/is_owner.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "EVM"

import "FlowEVMBridgeUtils"

/// Returns whether the given owner (hex-encoded EVM address - minus 0x prefix) is the owner of the given ERC721 NFT
/// defined at the hex-encoded EVM contract address
///
/// @param ofNFT: The ERC721 ID of the NFT
/// @param owner: The hex-encoded EVM address of the owner without the 0x prefix
/// @param evmContractAddress: The hex-encoded EVM contract address of the ERC721 contract without the 0x prefix
///
/// @return Whether the given owner is the owner of the given ERC721 NFT. Reverts on call failure.
///
access(all) fun main(ofNFT: UInt256, owner: String, evmContractAddress: String): Bool {
return FlowEVMBridgeUtils.isOwner(
ofNFT: ofNFT,
owner: FlowEVMBridgeUtils.getEVMAddressFromHexString(address: owner)
?? panic("Invalid owner address"),
evmContractAddress: FlowEVMBridgeUtils.getEVMAddressFromHexString(address: evmContractAddress)
?? panic("Invalid EVM contract address")
)
}
73 changes: 65 additions & 8 deletions cadence/tests/flow_evm_bridge_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fun setup() {
arguments: []
)
Test.expect(err, Test.beNil())

// Update EVM contract with proposed bridge-supporting COA integration
let updateResult = executeTransaction(
"../transactions/test/update_contract.cdc",
Expand Down Expand Up @@ -145,12 +145,15 @@ fun setup() {
arguments: []
)
Test.expect(err, Test.beNil())
err = Test.deployContract(
name: "EVMBridgeRouter",
path: "../contracts/bridge/EVMBridgeRouter.cdc",
arguments: [serviceAccount.address, "FlowEVMBridge"]

// Deploy EVMBridgeRouter manually to service account for COA -> bridge integration
let deployResult = executeTransaction(
"../transactions/test/add_contract.cdc",
["EVMBridgeRouter", getEVMBridgeRouterCode(), bridgeAccount.address, "FlowEVMBridge"],
serviceAccount
)
Test.expect(err, Test.beNil())
Test.expect(updateResult, Test.beSucceeded())
Test.assertEqual(true, getAccount(serviceAccount.address).contracts.names.contains("EVMBridgeRouter"))

// Transfer ERC721 deployer some $FLOW
let fundERC721AccountResult = executeTransaction(
Expand Down Expand Up @@ -183,6 +186,18 @@ fun setup() {
Test.expect(err, Test.beNil())
}

// TODO: Figure out how to test EVMBridgeRouter given it needs to be deployed to the service account
// and we can't seem to alter service account storage from the test suite
access(all)
fun testIsBridgeRouterConfiguredSucceeds() {
let isConfiguredResult = executeScript(
"../scripts/test/is_bridge_router_configured.cdc",
[]
)
Test.expect(isConfiguredResult, Test.beSucceeded())
Test.assertEqual(true, isConfiguredResult.returnValue as! Bool? ?? panic("Problem getting Router"))
}

access(all)
fun testCreateCOASucceeds() {
let transferFlowResult = executeTransaction(
Expand Down Expand Up @@ -274,7 +289,7 @@ fun testOnboardByTypeSucceeds() {
alice
)
Test.expect(onboardingResult, Test.beSucceeded())

onboaringRequiredResult = executeScript(
"../scripts/bridge/type_requires_onboarding.cdc",
[exampleNFTIdentifier]
Expand Down Expand Up @@ -315,7 +330,7 @@ fun testOnboardByEVMAddressSucceeds() {
alice
)
Test.expect(onboardingResult, Test.beSucceeded())

onboaringRequiredResult = executeScript(
"../scripts/bridge/evm_address_requires_onboarding.cdc",
[erc721AddressString]
Expand All @@ -330,4 +345,46 @@ fun testOnboardByEVMAddressSucceeds() {
alice
)
Test.expect(onboardingResult, Test.beFailed())
}

access(all)
fun testBridgeCadenceNativeNFTToEVMSucceeds() {
let aliceIDResult = executeScript(
"../scripts/nft/get_ids.cdc",
[alice.address, "cadenceExampleNFTCollection"]
)
Test.expect(aliceIDResult, Test.beSucceeded())
let aliceOwnedIDs = aliceIDResult.returnValue as! [UInt64]? ?? panic("Problem getting ExampleNFT IDs")
Test.assertEqual(1, aliceOwnedIDs.length)

let aliceCOAAddressResult = executeScript(
"../scripts/evm/get_evm_address_string.cdc",
[alice.address]
)
Test.expect(aliceCOAAddressResult, Test.beSucceeded())
let aliceCOAAddressString = aliceCOAAddressResult.returnValue as! String? ?? panic("Problem getting COA address as String")
Test.assertEqual(40, aliceCOAAddressString.length)

// TODO: This fails because EVMBridgeRouter.Router does not configure a resource in the service account
let bridgeToEVMResult = executeTransaction(
"../transactions/bridge/bridge_nft_to_evm.cdc",
[exampleNFTAccount.address, "ExampleNFT", aliceOwnedIDs[0]],
alice
)
Test.expect(bridgeToEVMResult, Test.beSucceeded())

var associatedEVMAddressResult = executeScript(
"../scripts/bridge/get_associated_evm_address.cdc",
[exampleNFTIdentifier]
)
Test.expect(associatedEVMAddressResult, Test.beSucceeded())
let associatedEVMAddressString = associatedEVMAddressResult.returnValue as! String? ?? panic("Problem getting EVM Address as String")
Test.assertEqual(40, associatedEVMAddressString.length)

var isOwnerResult = executeScript(
"../scripts/utils/is_owner.cdc",
[UInt256(aliceOwnedIDs[0]), aliceCOAAddressString, associatedEVMAddressString]
)
Test.expect(isOwnerResult, Test.beSucceeded())
Test.assertEqual(true, isOwnerResult.returnValue as! Bool? ?? panic("Problem getting owner status"))
}
13 changes: 12 additions & 1 deletion cadence/tests/test_helpers.cdc

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions cadence/transactions/test/add_contract.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
transaction(name: String, codeHex: String, arg0: AnyStruct, arg1: AnyStruct) {
prepare(signer: auth(AddContract) &Account) {
signer.contracts.add(name: name, code: codeHex.decodeHex(), arg0, arg1)
}
}
2 changes: 1 addition & 1 deletion flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"source": "./cadence/contracts/bridge/EVMBridgeRouter.cdc",
"aliases": {
"emulator": "f8d6e0586b0a20c7",
"testing": "0000000000000007"
"testing": "0000000000000001"
}
},
"EVMDeployer": {
Expand Down

0 comments on commit db6f166

Please sign in to comment.