This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
nfts.eg.ts
118 lines (103 loc) · 3.23 KB
/
nfts.eg.ts
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
/**
* @title NFTs
* @description An example using the upcoming NFTs pallet to create an NFT collection,
* mint, list and purchase an NFT, as well as lock the collection and NFT as to prevent.
* @test_skip
*/
import {
CollectionConfig,
MintSettings,
MintType,
rococoDevWestmint,
} from "@capi/rococo-dev-westmint"
import { assertEquals } from "asserts"
import { $, createDevUsers, is, Rune } from "capi"
import { signature } from "capi/patterns/signature/statemint"
import { DefaultCollectionSetting, DefaultItemSetting } from "capi/patterns/unstable/nfts"
/// Create two dev users. Alexa will mint and list the NFT. Billy will purchase it.
const { alexa, billy } = await createDevUsers()
/// Create a collection and get the resulting collection ID.
const collection = await rococoDevWestmint.Nfts
.create({
config: CollectionConfig({
settings: DefaultCollectionSetting.AllOff,
mintSettings: MintSettings({
mintType: MintType.Issuer(),
defaultItemSettings: DefaultItemSetting.AllOff,
}),
}),
admin: alexa.address,
})
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Create collection:")
.finalizedEvents("Nfts", "Created")
.access(0, "event", "value", "collection")
.run()
/// Ensure the collection id is a number.
$.assert($.u32, collection)
console.log("Collection id:", collection)
/// We'll create a single NFT with the id of 46
const item = 46
/// Mint an item to the collection.
await rococoDevWestmint.Nfts
.mint({
collection,
item,
mintTo: alexa.address,
})
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Mint the NFT:")
.finalized()
.run()
const owner = rococoDevWestmint.Nfts.Item
.value([collection, item])
.unhandle(is(undefined))
.access("owner")
/// Retrieve the final owner.
const initialOwner = await owner.run()
/// Ensure Alexa is the initial owner.
console.log("Initial owner:", initialOwner)
assertEquals(initialOwner, alexa.publicKey)
/// Submit a batch, which reverts if any calls fail. The contained calls do the following:
///
/// 1. Set the price.
/// 2. Prevent further minting.
/// 3. Lock the collection to prevent changes.
const price = 1000000n
await rococoDevWestmint.Utility
.batchAll({
calls: Rune.array([
rococoDevWestmint.Nfts.setPrice({ collection, item, price }),
rococoDevWestmint.Nfts.setCollectionMaxSupply({ collection, maxSupply: 1 }),
rococoDevWestmint.Nfts.lockCollection({ collection, lockSettings: 8n }), /// TODO: enum helper
]),
})
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Sale prep:")
.finalized()
.run()
/// Retrieve the price of the NFT.
const bidPrice = await rococoDevWestmint.Nfts.ItemPriceOf
.value([collection, item])
.unhandle(is(undefined))
.access(0)
.run()
/// Ensure the `bidPrice` is the expected value.
console.log(bidPrice)
assertEquals(price, bidPrice)
/// Buy the NFT as Billy.
await rococoDevWestmint.Nfts
.buyItem({ collection, item, bidPrice })
.signed(signature({ sender: billy }))
.sent()
.dbgStatus("Purchase:")
.finalized()
.run()
/// Retrieve the final owner.
const finalOwner = await owner.run()
/// Ensure Billy is the final owner.
console.log("Final owner:", finalOwner)
assertEquals(finalOwner, billy.publicKey)