Skip to content

Commit

Permalink
add NFTForwarding Cadence tests & fix txn bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Oct 24, 2023
1 parent cf9a423 commit c7aa593
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 9 deletions.
12 changes: 6 additions & 6 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions tests/nft_forwarding_tests.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import Test
import "test_helpers.cdc"

access(all) let admin = blockchain.createAccount()
access(all) let forwarder = blockchain.createAccount()
access(all) let recipient = blockchain.createAccount()

access(all) let collectionPublicPath = /public/cadenceExampleNFTCollection

access(all) fun setup() {

blockchain.useConfiguration(
Test.Configuration(
addresses: {
"ViewResolver": admin.address,
"NonFungibleToken": admin.address,
"MetadataViews": admin.address,
"MultipleNFT": admin.address,
"ExampleNFT": admin.address,
"NFTForwarding": admin.address
}
)
)

deploy("ViewResolver", admin, "../contracts/ViewResolver.cdc")
deploy("NonFungibleToken", admin, "../contracts/NonFungibleToken-v2.cdc")
deploy("MetadataViews", admin, "../contracts/MetadataViews.cdc")
deploy("MultipleNFT", admin, "../contracts/MultipleNFT.cdc")
deploy("ExampleNFT", admin, "../contracts/ExampleNFT-v2.cdc")
deploy("NFTForwarding", admin, "../contracts/utility/NFTForwarding.cdc")
}

access(all) fun testCreateForwarderFails() {

let expectedErrorMessage = "Recipient is not configured with NFT Collection at the given path"
let expectedErrorType = ErrorType.TX_PANIC

// Create forwarder in forwarding account should fail since recipient doesn't have Collection configured
let forwarderSetupSuccess: Bool = txExecutor(
"nft-forwarding/create_forwarder.cdc",
[forwarder],
[recipient.address, collectionPublicPath],
expectedErrorMessage,
expectedErrorType
)
}

access(all) fun testCreateForwarder() {
// Setup Collection in recipient
let recipientSetupSuccess: Bool = txExecutor("setup_account.cdc", [recipient], [], nil, nil)

// Create forwarder in forwarding account
let forwarderSetupSuccess: Bool = txExecutor(
"nft-forwarding/create_forwarder.cdc",
[forwarder],
[recipient.address, collectionPublicPath],
nil,
nil
)

Test.assertEqual(true, recipientSetupSuccess)
Test.assertEqual(true, forwarderSetupSuccess)
}

access(all) fun testMintNFT() {

let expectedCollectionLength: Int = 1

let royaltySetupSuccess: Bool = txExecutor(
"setup_account_to_receive_royalty.cdc",
[admin],
[/storage/flowTokenVault],
nil,
nil
)
Test.assertEqual(true, royaltySetupSuccess)

// Minting to forwarder should forward minted NFT to recipient
let mintSuccess: Bool = txExecutor(
"mint_nft.cdc",
[admin],
[
forwarder.address,
"NFT Name",
"NFT Description",
"NFT Thumbnail",
[0.05],
["Creator Royalty"],
[admin.address]
],
nil,
nil
)
Test.assertEqual(true, mintSuccess)

// TODO: Uncomment once TestAccount bug fixed
// let forwardEventType = CompositeType(buildTypeIdentifier(admin, "NFTForwarding", "ForwardedNFTDeposit"))!
// Test.assertEqual(1, blockchain.eventsOfType(forwardEventType).length)
let actualCollectionLength = scriptExecutor(
"get_collection_length.cdc",
[recipient.address],
) as! Int? ?? panic("problem retrieving NFT IDs from recipient at expected path")

Test.assertEqual(expectedCollectionLength, actualCollectionLength)
}
4 changes: 2 additions & 2 deletions transactions/mint_nft.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ transaction(
let minter: &ExampleNFT.NFTMinter

/// Reference to the receiver's collection
let recipientCollectionRef: &{NonFungibleToken.Collection}
let recipientCollectionRef: &{NonFungibleToken.Receiver}

prepare(signer: auth(BorrowValue) &Account) {

Expand All @@ -33,7 +33,7 @@ transaction(
?? panic("Account does not store an object at the specified path")

// Borrow the recipient's public NFT collection reference
self.recipientCollectionRef = getAccount(recipient).capabilities.borrow<&{NonFungibleToken.Collection}>(
self.recipientCollectionRef = getAccount(recipient).capabilities.borrow<&{NonFungibleToken.Receiver}>(
collectionData.publicPath
) ?? panic("Could not get receiver reference to the NFT Collection")
}
Expand Down
Loading

0 comments on commit c7aa593

Please sign in to comment.