-
Notifications
You must be signed in to change notification settings - Fork 8
/
e2e_test.go
396 lines (333 loc) · 11.5 KB
/
e2e_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"context"
"math"
"os"
"strings"
"testing"
"time"
"github.com/bjartek/go-with-the-flow/v2/gwtf"
"github.com/flow-hydraulics/flow-pds/go-contracts/util"
"github.com/flow-hydraulics/flow-pds/service/app"
"github.com/flow-hydraulics/flow-pds/service/common"
"github.com/flow-hydraulics/flow-pds/service/flow_helpers"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/client"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)
func TestE2E(t *testing.T) {
cfg := getTestCfg(t, nil)
a, cleanup := getTestApp(cfg, true)
defer cleanup()
no_packs := cfg.TestPackCount
no_collectibles_per_pack := 5
no_collectibles := no_packs * no_collectibles_per_pack
g := gwtf.NewGoWithTheFlow([]string{"./flow.json"}, "emulator", false, 0)
flowClient, err := client.New("localhost:3569", grpc.WithInsecure())
if err != nil {
t.Fatal(err)
}
issuer := common.FlowAddress(g.Account("issuer").Address())
t.Log("Setting up collectible NFT (ExampleNFT) collection for owner")
// The caller wishing to create the collection will choose which Private Path they would like to link the
// the Collection Provider Capability (when shared, to withdraw from their collection)
// The Private Path string in this case is "NFTCollectionProvider"
setupExampleNFT := "./cadence-transactions/exampleNFT/setup_exampleNFT.cdc"
setupExampleNFTCode := util.ParseCadenceTemplate(setupExampleNFT)
_, err = g.
TransactionFromFile(setupExampleNFT, setupExampleNFTCode).
SignProposeAndPayAs("owner").
RunE()
if err != nil {
t.Fatal(err)
}
t.Log("Issuer link NFT collection capability to share when create dist")
linkScript := "./cadence-transactions/exampleNFT/link_providerCap_exampleNFT.cdc"
linkCode := util.ParseCadenceTemplate(linkScript)
_, err = g.TransactionFromFile(linkScript, linkCode).
SignProposeAndPayAs("issuer").
Argument(cadence.Path{Domain: "private", Identifier: "NFTCollectionProvider"}).
RunE()
assert.NoError(t, err)
t.Log("Issuer create PackIssuer resource to store DistCap")
createPackIssuer := "./cadence-transactions/pds/create_new_pack_issuer.cdc"
createPackIssuerCode := util.ParseCadenceTemplate(createPackIssuer)
_, err = g.
TransactionFromFile(createPackIssuer, createPackIssuerCode).
SignProposeAndPayAs("issuer").
RunE()
if err != nil {
t.Fatal(err)
}
t.Log("Issuer create PackNFT collection resource to store minted PackNFT")
createPackNFTCollection := "./cadence-transactions/packNFT/create_new_packNFT_collection.cdc"
createPackNFTCollectionCode := util.ParseCadenceTemplate(createPackNFTCollection)
_, err = g.
TransactionFromFile(createPackNFTCollection, createPackNFTCollectionCode).
SignProposeAndPayAs("issuer").
RunE()
if err != nil {
t.Fatal(err)
}
t.Log("Owner create PackNFT collection resource to store PackNFT after purchase")
_, err = g.
TransactionFromFile(createPackNFTCollection, createPackNFTCollectionCode).
SignProposeAndPayAs("owner").
RunE()
if err != nil {
t.Fatal(err)
}
// -- Check if issuer needs more collectible NFTs --
t.Log("Check if issuer needs more collectible NFTs")
balance, err := getExampleNFTBalance(flowClient, flow.Address(issuer))
if err != nil {
t.Fatal(err)
}
if balance < uint64(no_collectibles) {
// Mint so we have enough collectible NFTs
mintBatchSize := int(math.Min(100, float64(no_collectibles)))
mintBatchCount := int(math.Ceil(float64(no_collectibles-int(balance)) / float64(mintBatchSize)))
t.Logf("Need to mint %d collectibles \n", mintBatchCount*mintBatchSize)
mintExampleNFT := "./cadence-transactions/exampleNFT/mint_exampleNFTBatched.cdc"
mintExampleNFTCode := util.ParseCadenceTemplate(mintExampleNFT)
for i := 0; i < mintBatchCount; i++ {
_, err := g.
TransactionFromFile(mintExampleNFT, mintExampleNFTCode).
SignProposeAndPayAs("issuer").
AccountArgument("issuer").
IntArgument(mintBatchSize).
RunE()
if err != nil {
t.Fatal(err)
}
}
}
balance, err = getExampleNFTBalance(flowClient, flow.Address(issuer))
if err != nil {
t.Fatal(err)
}
collection, err := getExampleNFTIDs(flowClient, flow.Address(issuer), balance)
if err != nil {
t.Fatal(err)
}
t.Log("PDS share DistCap to PackIssuer (owned by Issuer)")
err = a.SetDistCap(context.Background(), issuer)
if err != nil {
t.Fatal(err)
}
t.Log("Issuer creates distribution on chain")
pdsDistId := "./cadence-scripts/pds/get_next_dist_id.cdc"
pdsDistIdCode := util.ParseCadenceTemplate(pdsDistId)
currentDistId, err := g.ScriptFromFile(pdsDistId, pdsDistIdCode).RunReturns()
if err != nil {
t.Fatal(err)
}
keyPair := cadence.KeyValuePair{Key: cadence.NewString("metadataKey"), Value: cadence.NewString("metadataValue")}
stringifiedKeyPair := "{\"metadataKey\": \"metadataValue\"}"
var keypairArr []cadence.KeyValuePair
keypairArr = append(keypairArr, keyPair)
expMetadata := cadence.NewDictionary(keypairArr)
expTitle := "ExampleDistTitle"
createDist := "./cadence-transactions/pds/create_distribution.cdc"
createDistCode, err := flow_helpers.ParseCadenceTemplate(
createDist,
&flow_helpers.CadenceTemplateVars{
PackNFTName: "PackNFT",
PackNFTAddress: os.Getenv("PACKNFT_ADDRESS"),
},
)
if err != nil {
t.Fatal(err)
}
// Private path must match the PackNFT contract
e, err := g.
TransactionFromFile(createDist, createDistCode).
SignProposeAndPayAs("issuer").
Argument(cadence.Path{Domain: "private", Identifier: "NFTCollectionProvider"}).
StringArgument(expTitle).
Argument(expMetadata).
RunE()
if err != nil {
t.Fatal(err)
}
events := util.ParseTestEvents(e)
util.NewExpectedPDSEvent("DistributionCreated").
AddField("DistId", currentDistId.String()).
AddField("state", "0").
AddField("title", expTitle).
AddField("metadata", stringifiedKeyPair).
AssertEqual(t, events[0])
// -- Create distribution --
t.Log("Use available NFTs to create a distribution in backend")
distId, err := common.FlowIDFromCadence(currentDistId)
if err != nil {
t.Fatal(err)
}
start := time.Now()
distribution := app.Distribution{
State: common.DistributionStateInit,
FlowID: distId,
Issuer: issuer,
PackTemplate: app.PackTemplate{
PackReference: app.AddressLocation{
Name: "PackNFT",
Address: issuer,
},
PackCount: uint(no_packs),
Buckets: []app.Bucket{
{
CollectibleReference: app.AddressLocation{
Name: "ExampleNFT",
Address: issuer,
},
CollectibleCount: uint(no_collectibles_per_pack),
CollectibleCollection: collection,
},
},
},
}
if err := a.CreateDistribution(context.Background(), &distribution); err != nil {
t.Fatal(err)
}
collectibleCount, err := distribution.TemplateCollectibleCount()
if err != nil {
t.Fatal(err)
}
resolved := make(app.Collectibles, 0, collectibleCount)
for _, pack := range distribution.Packs {
resolved = append(resolved, pack.Collectibles...)
}
resolvedStr := make([]string, len(resolved))
for i, c := range resolved {
resolvedStr[i] = c.String()
}
if len(resolved) > 20 {
t.Logf("Distribution created with collectibles: %d\n", len(resolved))
} else {
t.Logf("Distribution created with collectibles:\n%s\n", strings.Join(resolvedStr, "\n"))
}
// -- Resolve, settle and mint --
t.Log("Wait for the distribution to complete")
for {
state, err := a.GetDistributionState(context.Background(), distribution.ID)
if err != nil {
if strings.Contains(err.Error(), "database is locked") {
continue
}
t.Fatal(err)
}
if state == common.DistributionStateComplete {
d, err := a.GetDistribution(context.Background(), distribution.ID)
if err != nil {
if strings.Contains(err.Error(), "database is locked") {
continue
}
t.Fatal(err)
}
distribution = *d
break
}
time.Sleep(time.Second)
}
t.Logf("Distribution settled and minted, took %s\n", time.Since(start))
balanceExampleNFT := "./cadence-scripts/exampleNFT/balance_exampleNFT.cdc"
balanceExampleNFTCode := util.ParseCadenceTemplate(balanceExampleNFT)
ownerCollectibleNFTsBefore, err := g.
ScriptFromFile(balanceExampleNFT, balanceExampleNFTCode).
AccountArgument("owner").
RunReturns()
if err != nil {
t.Fatal(err)
}
t.Log("Picking one pack")
// Pick one pack
randomPack := distribution.Packs[0]
randomPackID := cadence.UInt64(randomPack.FlowID.Int64)
if len(randomPack.Collectibles) != no_collectibles_per_pack {
t.Fatalf("expected pack to contain %d collectibles", no_collectibles_per_pack)
}
collectibles := randomPack.Collectibles
collectiblesStr := make([]string, len(collectibles))
for i, c := range collectibles {
collectiblesStr[i] = c.String()
}
t.Logf("Collectible NFTs in the pack:\n%s\n", strings.Join(collectiblesStr, "\n"))
// -- Transfer --
t.Log("Transferring a pack to owner")
// Issuer transfer PackNFT to owner
transferPackNFT := "./cadence-transactions/packNFT/transfer_packNFT.cdc"
transferPackNFTCode := util.ParseCadenceTemplate(transferPackNFT)
_, err = g.
TransactionFromFile(transferPackNFT, transferPackNFTCode).
SignProposeAndPayAs("issuer").
AccountArgument("owner").
Argument(randomPackID).
RunE()
if err != nil {
t.Fatal(err)
}
// -- Reveal & open --
t.Log("Owner requests to reveal and open the pack")
revealRequest := "./cadence-transactions/packNFT/reveal_request.cdc"
revealRequestCode := util.ParseCadenceTemplate(revealRequest)
e, err = g.
TransactionFromFile(revealRequest, revealRequestCode).
SignProposeAndPayAs("owner").
Argument(randomPackID).
BooleanArgument(true).
RunE()
if err != nil {
t.Fatal(err)
}
events = util.ParseTestEvents(e)
// Owner withdraw PackNFT from the collection, calls reveal & open on it and deposits back into their collection
util.NewExpectedPackNFTEvent("RevealRequest").AddField("id", randomPackID.String()).AddField("openRequest", "true").AssertEqual(t, events[0])
t.Log("PDS backend submits reveal transaction w/ openRequest=true")
t.Log("Wait for the reveal & open to happen")
for {
p, err := a.GetPack(context.Background(), randomPack.ID)
if err != nil {
if strings.Contains(err.Error(), "database is locked") {
continue
}
t.Fatal(err)
}
if p.State == common.PackStateRevealed || p.State == common.PackStateOpened {
break
}
time.Sleep(time.Second)
}
// Wait a bit more as the blocktime might be 1s if run from the test script
time.Sleep(time.Second * 2)
ownerCollectibleNFTsAfter, err := g.
ScriptFromFile(balanceExampleNFT, balanceExampleNFTCode).
AccountArgument("owner").
RunReturns()
if err != nil {
t.Fatal(err)
}
ownerCollectibleIDs, err := common.FlowIDListFromCadence(ownerCollectibleNFTsAfter)
if err != nil {
t.Fatal(err)
}
randomPackCollectibleIDs := make(common.FlowIDList, len(randomPack.Collectibles))
for i, c := range randomPack.Collectibles {
randomPackCollectibleIDs[i] = c.FlowID
}
for _, id := range randomPackCollectibleIDs {
if _, ok := ownerCollectibleIDs.Contains(id); !ok {
t.Errorf("expected owner to have collectible NFT: %s", id)
}
}
t.Logf("Owner collectible NFTs before: %s\n", ownerCollectibleNFTsBefore.String())
t.Logf("Owner collectible NFTs after: %s\n", ownerCollectibleNFTsAfter.String())
distStateScript := "./cadence-scripts/pds/get_dist_state.cdc"
distStateCode := util.ParseCadenceTemplate(distStateScript)
distStateR, err := g.ScriptFromFile(distStateScript, distStateCode).UInt64Argument(uint64(distribution.FlowID.Int64)).RunReturns()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, uint8(2), distStateR.ToGoValue().(uint8), "Expected distribution to be in state 2 (complete)")
}