Skip to content

Commit

Permalink
TestBorrowNFT, TestEditionResolveView (#29)
Browse files Browse the repository at this point in the history
Co-authored-by: ash <[email protected]>
  • Loading branch information
web3ashlee and ash authored Dec 21, 2023
1 parent b61e995 commit 2f2fba9
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 7 deletions.
21 changes: 17 additions & 4 deletions CLI_SCRIPTS.md
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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'`
6 changes: 6 additions & 0 deletions contracts/FlowtyWrapped.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions contracts/WrappedEditions.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions scripts/borrow_nft.cdc
Original file line number Diff line number Diff line change
@@ -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
}
}
15 changes: 15 additions & 0 deletions scripts/get_total_edition_supply.cdc
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 53 additions & 0 deletions test/FlowtyWrapped_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions transactions/setup_flowty_wrapped.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2f2fba9

Please sign in to comment.