/
nft_test_helpers.go
152 lines (130 loc) · 4.87 KB
/
nft_test_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package test
import (
"testing"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-emulator/adapters"
"github.com/onflow/flow-emulator/emulator"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/onflow/flow-go-sdk/test"
"github.com/stretchr/testify/assert"
"github.com/onflow/flow-nft/lib/go/contracts"
"github.com/onflow/flow-nft/lib/go/templates"
)
// Mints a single NFT from the ExampleNFT contract
// with standard metadata fields and royalty cuts
func mintExampleNFT(
t *testing.T,
b emulator.Emulator,
accountKeys *test.AccountKeys,
nftAddress, metadataAddress, exampleNFTAddress flow.Address,
exampleNFTAccountKey *flow.AccountKey,
exampleNFTSigner crypto.Signer,
) {
// Create two new accounts to act as beneficiaries for royalties
beneficiaryAddress1, _, beneficiarySigner1 := newAccountWithAddress(b, accountKeys)
setupRoyaltyReceiver(t, b,
metadataAddress,
beneficiaryAddress1,
beneficiarySigner1,
)
beneficiaryAddress2, _, beneficiarySigner2 := newAccountWithAddress(b, accountKeys)
setupRoyaltyReceiver(t, b,
metadataAddress,
beneficiaryAddress2,
beneficiarySigner2,
)
// Generate the script that mints a new NFT and deposits it into the recipient's account
// whose address is the first argument to the transaction
script := templates.GenerateMintNFTScript(nftAddress, exampleNFTAddress, metadataAddress, flow.HexToAddress(emulatorFTAddress))
// Create the transaction object with the generated script and authorizer
tx := createTxWithTemplateAndAuthorizer(b, script, exampleNFTAddress)
// Assemble the cut information for royalties
cut1 := CadenceUFix64("0.25")
cut2 := CadenceUFix64("0.40")
cuts := []cadence.Value{cut1, cut2}
// Assemble the royalty description and beneficiary addresses to get their receivers
royaltyDescriptions := []cadence.Value{cadence.String("Minter royalty"), cadence.String("Creator royalty")}
royaltyBeneficiaries := []cadence.Value{cadence.NewAddress(beneficiaryAddress1), cadence.NewAddress(beneficiaryAddress2)}
// First argument is the recipient of the newly minted NFT
tx.AddArgument(cadence.NewAddress(exampleNFTAddress))
tx.AddArgument(cadence.String("Example NFT 0"))
tx.AddArgument(cadence.String("This is an example NFT"))
tx.AddArgument(cadence.String("example.jpeg"))
tx.AddArgument(cadence.NewArray(cuts))
tx.AddArgument(cadence.NewArray(royaltyDescriptions))
tx.AddArgument(cadence.NewArray(royaltyBeneficiaries))
serviceSigner, _ := b.ServiceKey().Signer()
signAndSubmit(
t, b, tx,
[]flow.Address{
b.ServiceKey().Address,
exampleNFTAddress,
},
[]crypto.Signer{
serviceSigner,
exampleNFTSigner,
},
false,
)
}
// Deploys the NonFungibleToken, MetadataViews, and ExampleNFT contracts to new accounts
// and returns their addresses
func deployNFTContracts(
t *testing.T,
b emulator.Emulator,
adapter *adapters.SDKAdapter,
exampleNFTAccountKey *flow.AccountKey,
) (flow.Address, flow.Address, flow.Address, flow.Address) {
nftAddress := deploy(t, b, adapter, "NonFungibleToken", contracts.NonFungibleToken())
metadataAddress := deploy(t, b, adapter, "MetadataViews", contracts.MetadataViews(flow.HexToAddress(emulatorFTAddress), nftAddress))
resolverAddress := deploy(t, b, adapter, "ViewResolver", contracts.Resolver())
exampleNFTAddress := deploy(
t, b, adapter,
"ExampleNFT",
contracts.ExampleNFT(nftAddress, metadataAddress, resolverAddress),
exampleNFTAccountKey,
)
return nftAddress, metadataAddress, exampleNFTAddress, resolverAddress
}
// Assers that the ExampleNFT collection in the specified user's account
// is the expected length
func assertCollectionLength(
t *testing.T,
b emulator.Emulator,
nftAddress flow.Address, exampleNFTAddress flow.Address,
collectionAddress flow.Address,
expectedLength int,
) {
script := templates.GenerateGetCollectionLengthScript(nftAddress, exampleNFTAddress)
actualLength := executeScriptAndCheck(t, b, script, [][]byte{jsoncdc.MustEncode(cadence.NewAddress(collectionAddress))})
assert.Equal(t, cadence.NewInt(expectedLength), actualLength)
}
// Sets up an account with the generic royalty receiver in place of their Flow token receiver
func setupRoyaltyReceiver(
t *testing.T,
b emulator.Emulator,
metadataAddress flow.Address,
authorizerAddress flow.Address,
authorizerSigner crypto.Signer,
) {
script := templates.GenerateSetupAccountToReceiveRoyaltyScript(metadataAddress, flow.HexToAddress(emulatorFTAddress))
tx := createTxWithTemplateAndAuthorizer(b, script, authorizerAddress)
vaultPath := cadence.Path{Domain: common.PathDomainStorage, Identifier: "flowTokenVault"}
tx.AddArgument(vaultPath)
serviceSigner, _ := b.ServiceKey().Signer()
signAndSubmit(
t, b, tx,
[]flow.Address{
b.ServiceKey().Address,
authorizerAddress,
},
[]crypto.Signer{
serviceSigner,
authorizerSigner,
},
false,
)
}