-
Notifications
You must be signed in to change notification settings - Fork 114
/
collection.go
272 lines (225 loc) · 7.58 KB
/
collection.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
package nft
import (
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"math/big"
"github.com/xssnick/tonutils-go/ton"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
"github.com/xssnick/tonutils-go/tvm/cell"
)
type TonApi interface {
WaitForBlock(seqno uint32) ton.APIClientWrapped
CurrentMasterchainInfo(ctx context.Context) (_ *ton.BlockIDExt, err error)
RunGetMethod(ctx context.Context, blockInfo *ton.BlockIDExt, addr *address.Address, method string, params ...any) (*ton.ExecutionResult, error)
}
type ItemMintPayload struct {
_ tlb.Magic `tlb:"#00000001"`
QueryID uint64 `tlb:"## 64"`
Index *big.Int `tlb:"## 64"`
TonAmount tlb.Coins `tlb:"."`
Content *cell.Cell `tlb:"^"`
}
type CollectionChangeOwner struct {
_ tlb.Magic `tlb:"#00000003"`
QueryID uint64 `tlb:"## 64"`
NewOwner *address.Address `tlb:"addr"`
}
type CollectionData struct {
NextItemIndex *big.Int
Content ContentAny
OwnerAddress *address.Address
}
type CollectionRoyaltyParams struct {
Factor uint16
Base uint16
Address *address.Address
}
type CollectionClient struct {
addr *address.Address
api TonApi
}
func NewCollectionClient(api TonApi, collectionAddr *address.Address) *CollectionClient {
return &CollectionClient{
addr: collectionAddr,
api: api,
}
}
func (c *CollectionClient) GetNFTAddressByIndex(ctx context.Context, index *big.Int) (*address.Address, error) {
b, err := c.api.CurrentMasterchainInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get masterchain info: %w", err)
}
return c.GetNFTAddressByIndexAtBlock(ctx, index, b)
}
func (c *CollectionClient) GetNFTAddressByIndexAtBlock(ctx context.Context, index *big.Int, b *ton.BlockIDExt) (*address.Address, error) {
res, err := c.api.WaitForBlock(b.SeqNo).RunGetMethod(ctx, b, c.addr, "get_nft_address_by_index", index)
if err != nil {
return nil, fmt.Errorf("failed to run get_nft_address_by_index method: %w", err)
}
x, err := res.Slice(0)
if err != nil {
return nil, fmt.Errorf("result get err: %w", err)
}
addr, err := x.LoadAddr()
if err != nil {
return nil, fmt.Errorf("failed to load address from result slice: %w", err)
}
return addr, nil
}
func (c *CollectionClient) RoyaltyParams(ctx context.Context) (*CollectionRoyaltyParams, error) {
b, err := c.api.CurrentMasterchainInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get masterchain info: %w", err)
}
return c.RoyaltyParamsAtBlock(ctx, b)
}
func (c *CollectionClient) RoyaltyParamsAtBlock(ctx context.Context, b *ton.BlockIDExt) (*CollectionRoyaltyParams, error) {
res, err := c.api.WaitForBlock(b.SeqNo).RunGetMethod(ctx, b, c.addr, "royalty_params")
if err != nil {
return nil, fmt.Errorf("failed to run royalty_params method: %w", err)
}
factor, err := res.Int(0)
if err != nil {
return nil, fmt.Errorf("factor get err: %w", err)
}
base, err := res.Int(1)
if err != nil {
return nil, fmt.Errorf("base get err: %w", err)
}
addrSlice, err := res.Slice(2)
if err != nil {
return nil, fmt.Errorf("addr slice get err: %w", err)
}
addr, err := addrSlice.LoadAddr()
if err != nil {
return nil, fmt.Errorf("failed to load address from result slice: %w", err)
}
return &CollectionRoyaltyParams{
Factor: uint16(factor.Uint64()),
Base: uint16(base.Uint64()),
Address: addr,
}, nil
}
func (c *CollectionClient) GetNFTContent(ctx context.Context, index *big.Int, individualNFTContent ContentAny) (ContentAny, error) {
b, err := c.api.CurrentMasterchainInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get masterchain info: %w", err)
}
return c.GetNFTContentAtBlock(ctx, index, individualNFTContent, b)
}
func (c *CollectionClient) GetNFTContentAtBlock(ctx context.Context, index *big.Int, individualNFTContent ContentAny, b *ton.BlockIDExt) (ContentAny, error) {
con, err := toNftContent(individualNFTContent)
if err != nil {
return nil, fmt.Errorf("failed to convert nft content to cell: %w", err)
}
res, err := c.api.WaitForBlock(b.SeqNo).RunGetMethod(ctx, b, c.addr, "get_nft_content", index, con)
if err != nil {
return nil, fmt.Errorf("failed to run get_nft_content method: %w", err)
}
x, err := res.Cell(0)
if err != nil {
return nil, fmt.Errorf("result get err: %w", err)
}
cnt, err := ContentFromCell(x)
if err != nil {
return nil, fmt.Errorf("failed to parse content: %w", err)
}
return cnt, nil
}
func (c *CollectionClient) GetCollectionData(ctx context.Context) (*CollectionData, error) {
b, err := c.api.CurrentMasterchainInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get masterchain info: %w", err)
}
return c.GetCollectionDataAtBlock(ctx, b)
}
func (c *CollectionClient) GetCollectionDataAtBlock(ctx context.Context, b *ton.BlockIDExt) (*CollectionData, error) {
res, err := c.api.WaitForBlock(b.SeqNo).RunGetMethod(ctx, b, c.addr, "get_collection_data")
if err != nil {
return nil, fmt.Errorf("failed to run get_collection_data method: %w", err)
}
nextIndex, err := res.Int(0)
if err != nil {
return nil, fmt.Errorf("next index get err: %w", err)
}
content, err := res.Cell(1)
if err != nil {
return nil, fmt.Errorf("content get err: %w", err)
}
ownerRes, err := res.Slice(2)
if err != nil {
return nil, fmt.Errorf("owner get err: %w", err)
}
addr, err := ownerRes.LoadAddr()
if err != nil {
return nil, fmt.Errorf("failed to load owner address from result slice: %w", err)
}
cnt, err := ContentFromCell(content)
if err != nil {
return nil, fmt.Errorf("failed to parse content: %w", err)
}
return &CollectionData{
NextItemIndex: nextIndex,
Content: cnt,
OwnerAddress: addr,
}, nil
}
func (c *CollectionClient) BuildMintPayload(index *big.Int, owner *address.Address, amountForward tlb.Coins, content ContentAny) (_ *cell.Cell, err error) {
con, err := toNftContent(content)
if err != nil {
return nil, fmt.Errorf("failed to convert nft content to cell: %w", err)
}
con = cell.BeginCell().MustStoreAddr(owner).MustStoreRef(con).EndCell()
buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return nil, err
}
rnd := binary.LittleEndian.Uint64(buf)
body, err := tlb.ToCell(ItemMintPayload{
QueryID: rnd,
Index: index,
TonAmount: amountForward,
Content: con,
})
if err != nil {
return nil, fmt.Errorf("failed to convert ItemMintPayload to cell: %w", err)
}
return body, nil
}
func (c *CollectionClient) BuildMintEditablePayload(index *big.Int, owner, editor *address.Address, amountForward tlb.Coins, content ContentAny) (_ *cell.Cell, err error) {
con, err := toNftContent(content)
if err != nil {
return nil, fmt.Errorf("failed to convert nft content to cell: %w", err)
}
con = cell.BeginCell().MustStoreAddr(owner).MustStoreRef(con).MustStoreAddr(editor).EndCell()
buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return nil, err
}
rnd := binary.LittleEndian.Uint64(buf)
body, err := tlb.ToCell(ItemMintPayload{
QueryID: rnd,
Index: index,
TonAmount: amountForward,
Content: con,
})
if err != nil {
return nil, fmt.Errorf("failed to convert ItemMintPayload to cell: %w", err)
}
return body, nil
}
func toNftContent(content ContentAny) (*cell.Cell, error) {
if content == nil {
return cell.BeginCell().EndCell(), nil
}
if off, ok := content.(*ContentOffchain); ok {
// https://github.com/ton-blockchain/TIPs/issues/64
// Standard says that prefix should be 0x01, but looks like it was misunderstanding in other implementations and 0x01 was dropped
// so, we make compatibility
return cell.BeginCell().MustStoreStringSnake(off.URI).EndCell(), nil
}
return content.ContentCell()
}