Skip to content

Commit

Permalink
fix test cases to correspond with new contract implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Oct 24, 2023
1 parent 92526d4 commit 42b00b3
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 35 deletions.
6 changes: 3 additions & 3 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.

12 changes: 6 additions & 6 deletions lib/go/templates/transaction_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ func GenerateMintNFTScript(nftAddress, exampleNFTAddress, metadataViewsAddress,

// GenerateTransferNFTScript returns a script that withdraws an NFT token
// from a collection and deposits it into another collection.
func GenerateTransferNFTScript(nftAddress, exampleNFTAddress flow.Address) []byte {
func GenerateTransferNFTScript(nftAddress, exampleNFTAddress, metadataAddress, viewResolverAddress flow.Address) []byte {
code := assets.MustAssetString(filenameTransferNFT)
return replaceAddresses(code, nftAddress, exampleNFTAddress, flow.EmptyAddress, flow.EmptyAddress, flow.EmptyAddress)
return replaceAddresses(code, nftAddress, exampleNFTAddress, metadataAddress, flow.EmptyAddress, viewResolverAddress)
}

// GenerateDestroyNFTScript creates a script that withdraws an NFT token
// from a collection and destroys it.
func GenerateDestroyNFTScript(nftAddress, exampleNFTAddress flow.Address) []byte {
func GenerateDestroyNFTScript(nftAddress, exampleNFTAddress, metadataAddress flow.Address) []byte {
code := assets.MustAssetString(filenameDestroyNFT)
return replaceAddresses(code, nftAddress, exampleNFTAddress, flow.EmptyAddress, flow.EmptyAddress, flow.EmptyAddress)
return replaceAddresses(code, nftAddress, exampleNFTAddress, metadataAddress, flow.EmptyAddress, flow.EmptyAddress)
}

// GenerateSetupAccountToReceiveRoyaltyScript returns a script that
Expand All @@ -62,7 +62,7 @@ func GenerateSetupAccountToReceiveRoyaltyScript(metadataViewsAddress, ftAddress
// GenerateSetupAccountFromNftReferenceScript returns a script that instantiates a new
// NFT collection instance, saves the collection in storage, then stores a
// reference to the collection.
func GenerateSetupAccountFromNftReferenceScript(nftAddress flow.Address, exampleNFTAddress flow.Address, metadataViewsAddress flow.Address) []byte {
func GenerateSetupAccountFromNftReferenceScript(nftAddress, metadataViewsAddress flow.Address) []byte {
code := assets.MustAssetString(filenameSetupAccountFromNftReference)
return replaceAddresses(code, nftAddress, exampleNFTAddress, metadataViewsAddress, flow.EmptyAddress, flow.EmptyAddress)
return replaceAddresses(code, nftAddress, flow.EmptyAddress, metadataViewsAddress, flow.EmptyAddress, flow.EmptyAddress)
}
23 changes: 18 additions & 5 deletions lib/go/test/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,27 @@ func TestSetupCollectionFromNFTReference(t *testing.T) {
exampleNFTSigner)

t.Run("Should be able to setup an account using the NFTCollectionData metadata view of a referenced NFT", func(t *testing.T) {
// Ideally, the exampleNFTAddress would not be needed in order to perform the full setup, but it is required
// until the following issue is supported in cadence: https://github.com/onflow/cadence/issues/1617
script := templates.GenerateSetupAccountFromNftReferenceScript(nftAddress, exampleNFTAddress, metadataAddress)
const (
pathName = "cadenceExampleNFTCollection"
)

idsScript := templates.GenerateGetCollectionIDsScript(nftAddress, exampleNFTAddress)
idsResult := executeScriptAndCheck(
t, b,
idsScript,
[][]byte{
jsoncdc.MustEncode(cadence.NewAddress(exampleNFTAddress)),
jsoncdc.MustEncode(cadence.Path{Domain: common.PathDomainPublic, Identifier: pathName}),
},
)
mintedID := idsResult.(cadence.Array).Values[0].(cadence.UInt64)

script := templates.GenerateSetupAccountFromNftReferenceScript(nftAddress, metadataAddress)
tx := createTxWithTemplateAndAuthorizer(b, script, aAddress)

tx.AddArgument(cadence.NewAddress(exampleNFTAddress))
tx.AddArgument(cadence.Path{Domain: common.PathDomainPublic, Identifier: "exampleNFTCollection"})
tx.AddArgument(cadence.NewUInt64(0))
tx.AddArgument(cadence.Path{Domain: common.PathDomainPublic, Identifier: pathName})
tx.AddArgument(mintedID)

serviceSigner, _ := b.ServiceKey().Signer()

Expand Down
34 changes: 17 additions & 17 deletions lib/go/test/nft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestTransferNFT(t *testing.T) {
// Create new keys for the NFT contract account
// and deploy all the NFT contracts
exampleNFTAccountKey, exampleNFTSigner := accountKeys.NewWithSigner()
nftAddress, metadataAddress, exampleNFTAddress, _ := deployNFTContracts(t, b, adapter, accountKeys, exampleNFTAccountKey)
nftAddress, metadataAddress, exampleNFTAddress, viewResolverAddress := deployNFTContracts(t, b, adapter, accountKeys, exampleNFTAccountKey)

// Create a new account to test transfers
joshAddress, _, joshSigner := newAccountWithAddress(b, accountKeys)
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestTransferNFT(t *testing.T) {

// This transaction tries to withdraw an NFT from a collection
// and deposit it to another collection
script := templates.GenerateTransferNFTScript(nftAddress, exampleNFTAddress)
script := templates.GenerateTransferNFTScript(nftAddress, exampleNFTAddress, metadataAddress, viewResolverAddress)
tx := createTxWithTemplateAndAuthorizer(b, script, exampleNFTAddress)

// Specify ExampleNFT contract address & name
Expand Down Expand Up @@ -199,9 +199,12 @@ func TestTransferNFT(t *testing.T) {
// Transfer an NFT correctly
t.Run("Should be able to withdraw an NFT and deposit to another accounts collection", func(t *testing.T) {

// Same transaction as before
script := templates.GenerateTransferNFTScript(nftAddress, exampleNFTAddress)
tx := createTxWithTemplateAndAuthorizer(b, script, exampleNFTAddress)
// // Mint a single NFT with standard royalty cuts and metadata
// mintExampleNFT(t, b,
// accountKeys,
// nftAddress, metadataAddress, exampleNFTAddress,
// exampleNFTAccountKey,
// exampleNFTSigner)

idsScript := templates.GenerateGetCollectionIDsScript(nftAddress, exampleNFTAddress)
idsResult := executeScriptAndCheck(
Expand All @@ -214,12 +217,9 @@ func TestTransferNFT(t *testing.T) {
)
mintedID := idsResult.(cadence.Array).Values[0].(cadence.UInt64)

// Mint a single NFT with standard royalty cuts and metadata
mintExampleNFT(t, b,
accountKeys,
nftAddress, metadataAddress, exampleNFTAddress,
exampleNFTAccountKey,
exampleNFTSigner)
// Same transaction as before
script := templates.GenerateTransferNFTScript(nftAddress, exampleNFTAddress, metadataAddress, viewResolverAddress)
tx := createTxWithTemplateAndAuthorizer(b, script, exampleNFTAddress)

// Specify ExampleNFT contract address & name
tx.AddArgument(cadence.NewAddress(exampleNFTAddress))
Expand Down Expand Up @@ -270,12 +270,6 @@ func TestTransferNFT(t *testing.T) {

t.Run("Should be able to withdraw an NFT and destroy it, not reducing the supply", func(t *testing.T) {

// This transaction withdraws the specifed NFT from the authorizers account
// and calls `destroy NFT`
script := templates.GenerateDestroyNFTScript(nftAddress, exampleNFTAddress)

tx := createTxWithTemplateAndAuthorizer(b, script, joshAddress)

idsScript := templates.GenerateGetCollectionIDsScript(nftAddress, exampleNFTAddress)
idsResult := executeScriptAndCheck(
t, b,
Expand All @@ -287,6 +281,12 @@ func TestTransferNFT(t *testing.T) {
)
mintedID := idsResult.(cadence.Array).Values[0].(cadence.UInt64)

// This transaction withdraws the specifed NFT from the authorizers account
// and calls `destroy NFT`
script := templates.GenerateDestroyNFTScript(nftAddress, exampleNFTAddress, metadataAddress)

tx := createTxWithTemplateAndAuthorizer(b, script, joshAddress)

// Destroy the only NFT in the collection
tx.AddArgument(mintedID)

Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/get_nft_metadata.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ access(all) fun main(address: Address, id: UInt64): Bool {
assert(nft.getID() == nftMetadata.serialNumber)
assert(/public/cadenceExampleNFTCollection == nftMetadata.collectionPublicPath)
assert(/storage/cadenceExampleNFTCollection == nftMetadata.collectionStoragePath)
assert(/private/exampleNFTCollection == nftMetadata.collectionProviderPath)
assert(/private/cadenceExampleNFTCollection == nftMetadata.collectionProviderPath)
// assert("&A.01cf0e2f2f715450.ExampleNFT.Collection" == nftMetadata.collectionPublic)
// assert("&A.01cf0e2f2f715450.ExampleNFT.Collection" == nftMetadata.collectionPublicLinkedType)
// assert("&A.01cf0e2f2f715450.ExampleNFT.Collection" == nftMetadata.collectionProviderLinkedType)
Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/get_nft_view.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ access(all) fun main(address: Address, id: UInt64): Bool {
assert("https://example-nft.onflow.org/".concat(id.toString()) == nftViewResult.externalURL)
assert(/public/cadenceExampleNFTCollection == nftViewResult.collectionPublicPath)
assert(/storage/cadenceExampleNFTCollection == nftViewResult.collectionStoragePath)
assert(/private/exampleNFTCollection == nftViewResult.collectionProviderPath)
assert(/private/cadenceExampleNFTCollection == nftViewResult.collectionProviderPath)
// assert("&A.01cf0e2f2f715450.ExampleNFT.Collection{A.01cf0e2f2f715450.ExampleNFT.ExampleNFTCollectionPublic}" == nftViewResult.collectionPublic)
// assert("&A.01cf0e2f2f715450.ExampleNFT.Collection{A.01cf0e2f2f715450.ExampleNFT.ExampleNFTCollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.CollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.Receiver,A.f8d6e0586b0a20c7.MetadataViews.ResolverCollection}" == nftViewResult.collectionPublicLinkedType)
// assert("auth(A.f8d6e0586b0a20c7.NonFungibleToken.Withdrawable)&A.01cf0e2f2f715450.ExampleNFT.Collection" == nftViewResult.collectionProviderLinkedType)
Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/resolve_nft_views.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ access(all) fun main(): Bool {

assert(/storage/cadenceExampleNFTCollection == collectionData.storagePath)
assert(/public/cadenceExampleNFTCollection == collectionData.publicPath)
assert(/private/exampleNFTCollection == collectionData.providerPath)
assert(/private/cadenceExampleNFTCollection == collectionData.providerPath)
assert(Type<&ExampleNFT.Collection>() == collectionData.publicCollection)
assert(Type<&ExampleNFT.Collection>() == collectionData.publicLinkedType)
assert(Type<auth(NonFungibleToken.Withdrawable) &ExampleNFT.Collection>() == collectionData.providerLinkedType)
Expand Down
2 changes: 1 addition & 1 deletion transactions/setup_account_from_nft_reference.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import MetadataViews from "MetadataViews"
transaction(address: Address, publicPath: PublicPath, id: UInt64) {

prepare(signer: auth(IssueStorageCapabilityController, PublishCapability, SaveValue) &Account) {
let collection = getAccount(address).capabilties.borrow<&{NonFungibleToken.Collection}>(publicPath)
let collection = getAccount(address).capabilities.borrow<&{NonFungibleToken.Collection}>(publicPath)
?? panic("Could not borrow a reference to the collection")

let resolver = collection.borrowViewResolver(id: id)!
Expand Down

0 comments on commit 42b00b3

Please sign in to comment.