From 2f2fba9a304ca1c6e98445f31cb12d36a5e7f7e5 Mon Sep 17 00:00:00 2001 From: ashlee <100004665+web3ashlee@users.noreply.github.com> Date: Thu, 21 Dec 2023 09:33:24 -0600 Subject: [PATCH] `TestBorrowNFT`, `TestEditionResolveView` (#29) Co-authored-by: ash --- CLI_SCRIPTS.md | 21 +++++++++-- contracts/FlowtyWrapped.cdc | 6 +++ contracts/WrappedEditions.cdc | 4 ++ scripts/borrow_nft.cdc | 17 +++++++++ scripts/get_total_edition_supply.cdc | 15 ++++++++ test/FlowtyWrapped_tests.cdc | 53 +++++++++++++++++++++++++++ transactions/setup_flowty_wrapped.cdc | 6 +-- 7 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 scripts/borrow_nft.cdc create mode 100644 scripts/get_total_edition_supply.cdc diff --git a/CLI_SCRIPTS.md b/CLI_SCRIPTS.md index 67c82bf..be5896e 100644 --- a/CLI_SCRIPTS.md +++ b/CLI_SCRIPTS.md @@ -1,6 +1,5 @@ Flow CLI ready to paste scripts for Flowty Wrapper transactions and scripts - 1 - Create new emulator account (if you already made this step before but restarted emulator, remove emulator-1 object from flow.json) `flow accounts create` @@ -10,10 +9,24 @@ On account name you can add: `emulator-1` 2 - Setup FlowtyWrapped Collection `flow transactions send --signer=emulator-1 transactions/setup_flowty_wrapped.cdc` -3 - Mint FlowtyWrapped +3 - Register Edition + +`flow transactions send --signer=emulator-account transactions/register_edition.cdc 'true' '0' '100' '""' '""'` -`flow transactions send --signer=emulator-account transactions/mint_flowty_wrapped.cdc '0x01cf0e2f2f715450'` +4 - Mint FlowtyWrapped + +`flow transactions send --signer=emulator-account transactions/mint_flowty_wrapped.cdc '0x01cf0e2f2f715450' 'testSingleMint' '1' '1' '1' '[""]' '[""]'` 4 - Check NFT Edition -`flow scripts execute scripts/get_editions_flowty_wrapped.cdc '0x01cf0e2f2f715450' '2'` +`flow scripts execute scripts/get_editions_flowty_wrapped.cdc '0x01cf0e2f2f715450' '1'` + +Optional + +Borrow NFT + +`flow scripts execute scripts/borrow_nft.cdc '0x01cf0e2f2f715450' '1'` + +Get nftIDs + +`flow scripts execute scripts/get_nft_ids.cdc '0x01cf0e2f2f715450'` diff --git a/contracts/FlowtyWrapped.cdc b/contracts/FlowtyWrapped.cdc index f3f084b..318752a 100644 --- a/contracts/FlowtyWrapped.cdc +++ b/contracts/FlowtyWrapped.cdc @@ -34,6 +34,7 @@ pub contract FlowtyWrapped: NonFungibleToken, ViewResolver { pub struct interface WrappedEdition { pub fun getName(): String pub fun resolveView(_ t: Type, _ nft: &NFT): AnyStruct? + pub fun getEditionSupply(): UInt64 access(account) fun setStatus(_ s: String) access(account) fun mint(address: Address, data: {String: AnyStruct}): @NFT @@ -264,6 +265,11 @@ pub contract FlowtyWrapped: NonFungibleToken, ViewResolver { return <- nft } + pub fun getEdition(editionName: String): AnyStruct{ + let edition = FlowtyWrapped.getEditionRef(editionName) + return edition + } + pub fun registerEdition(_ edition: {WrappedEdition}) { pre { FlowtyWrapped.editions[edition.getName()] == nil: "edition name already exists" diff --git a/contracts/WrappedEditions.cdc b/contracts/WrappedEditions.cdc index 6df5451..0258ea7 100644 --- a/contracts/WrappedEditions.cdc +++ b/contracts/WrappedEditions.cdc @@ -79,6 +79,10 @@ pub contract WrappedEditions { return nil } + pub fun getEditionSupply(): UInt64 { + return self.supply + } + access(account) fun mint(address: Address, data: {String: AnyStruct}): @FlowtyWrapped.NFT { pre { self.mintedAddresses[address] == nil: "address has already been minted" diff --git a/scripts/borrow_nft.cdc b/scripts/borrow_nft.cdc new file mode 100644 index 0000000..d20fd9d --- /dev/null +++ b/scripts/borrow_nft.cdc @@ -0,0 +1,17 @@ +import "NonFungibleToken" +import "MetadataViews" + +import "FlowtyWrapped" + +pub fun main(addr: Address, nftID: UInt64): Bool{ + let cp = getAccount(addr).getCapability<&{NonFungibleToken.CollectionPublic}>(FlowtyWrapped.CollectionPublicPath).borrow() + ?? panic("collection not found") + + let nft = cp.borrowNFT(id: nftID) + + if (nft != nil) { + return true + } else { + return false + } +} \ No newline at end of file diff --git a/scripts/get_total_edition_supply.cdc b/scripts/get_total_edition_supply.cdc new file mode 100644 index 0000000..699fae5 --- /dev/null +++ b/scripts/get_total_edition_supply.cdc @@ -0,0 +1,15 @@ +import "FlowtyWrapped" +import "WrappedEditions" + +pub fun main(addr: Address, editionName: String): UInt64 { + + let acct = getAuthAccount(addr) + let admin = acct.borrow<&FlowtyWrapped.Admin>(from: FlowtyWrapped.AdminStoragePath)! + + + let edition = admin.getEdition(editionName: editionName) as! &AnyStruct{FlowtyWrapped.WrappedEdition} + + let editionSupply = edition.getEditionSupply() + + return editionSupply +} \ No newline at end of file diff --git a/test/FlowtyWrapped_tests.cdc b/test/FlowtyWrapped_tests.cdc index 49c31f1..a89ed13 100644 --- a/test/FlowtyWrapped_tests.cdc +++ b/test/FlowtyWrapped_tests.cdc @@ -3,6 +3,7 @@ import "test_helpers.cdc" import "FlowtyWrapped" import "MetadataViews" +import "WrappedEditions" pub let rafflesAcct = Test.getAccount(Address(0x0000000000000007)) pub let minterAccount = Test.getAccount(Address(0x0000000000000007)) @@ -61,6 +62,35 @@ pub fun testGetEditions() { scriptExecutor("get_editions_flowty_wrapped.cdc", [acct.address, nftID1]) } +pub fun testEditionResolveView() { + let acct = Test.createAccount() + + let currentEditionNumber = getEditionNumber() + + let expectedEditionName = "Flowty Wrapped 2023" + let expectedEditionNumber: UInt64 = currentEditionNumber + 1 + let expectedEditionMax = nil + + setupForMint(acct: acct) + + let result = scriptExecutor("get_nft_ids.cdc", [acct.address]) + + let castedResult = result! as! [UInt64] + var nftID1 = castedResult[0] + + let res: AnyStruct? = scriptExecutor("get_editions_flowty_wrapped.cdc", [acct.address, nftID1]) + let castedTest = res! as! MetadataViews.Editions + let Edition = castedTest.infoList[0] + + let name = Edition.name! + let number = Edition.number + let max = Edition.max + + assert(name == expectedEditionName, message: "Edition name is not expected result") + assert(number == expectedEditionNumber, message: "NFT serial is not expected result") + assert(max == expectedEditionMax, message: "max should be nil") +} + pub fun testDepositToWrongAddressFails() { let acct = Test.createAccount() let wrongAccount = Test.createAccount() @@ -81,6 +111,20 @@ pub fun testDepositToWrongAddressFails() { } + + +pub fun testBorrowNFT() { + let acct = Test.createAccount() + setupForMint(acct: acct) + + let result = scriptExecutor("get_nft_ids.cdc", [acct.address]) + + let castedResult = result! as! [UInt64] + var nftID1 = castedResult[0] + + scriptExecutor("borrow_nft.cdc", [acct.address, nftID1]) +} + pub fun testSingleMint() { let acct = Test.createAccount() @@ -135,6 +179,15 @@ pub fun getMedias(addr: Address, nftID: UInt64): MetadataViews.Medias { return scriptExecutor("get_medias.cdc", [addr, nftID])! as! MetadataViews.Medias } +pub fun getEditionNumber(): UInt64{ + let editionName = "Flowty Wrapped 2023" + let res = scriptExecutor("get_total_edition_supply.cdc", [minterAccount.address, editionName]) + + let editionNumber = res! as! UInt64 + return editionNumber + +} + pub fun setupForMint(acct: Test.Account) { txExecutor("setup_flowty_wrapped.cdc", [acct], [], nil) diff --git a/transactions/setup_flowty_wrapped.cdc b/transactions/setup_flowty_wrapped.cdc index 9cfbcad..45f059f 100644 --- a/transactions/setup_flowty_wrapped.cdc +++ b/transactions/setup_flowty_wrapped.cdc @@ -6,15 +6,15 @@ transaction { prepare(signer: AuthAccount) { - // Return early if the account already stores a ExampleToken Vault + // Return early if the account already stores a FlowtyWrapped Collection if signer.borrow<&FlowtyWrapped.Collection>(from: FlowtyWrapped.CollectionStoragePath) == nil { - // Create a new ExampleToken Vault and put it in storage + // Create a new FlowtyWrapped Collection and put it in storage signer.save( <-FlowtyWrapped.createEmptyCollection(), to: FlowtyWrapped.CollectionStoragePath ) - // Create a public capability to the Vault that only exposes + // Create a public capability to the Collection that only exposes // the balance field through the Balance interface signer.link<&FlowtyWrapped.Collection{NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection}>( FlowtyWrapped.CollectionPublicPath,