-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNftCollection.ts
More file actions
145 lines (132 loc) · 4.55 KB
/
NftCollection.ts
File metadata and controls
145 lines (132 loc) · 4.55 KB
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
import {
Address,
beginCell,
Cell,
Contract,
contractAddress,
ContractProvider,
Sender,
SendMode,
TupleItemInt,
} from '@ton/core';
export type RoyaltyParams = {
royaltyFactor: number;
royaltyBase: number;
royaltyAddress: Address;
};
export type NftCollectionConfig = {
ownerAddress: Address;
nextItemIndex: number;
collectionContent: Cell;
nftItemCode: Cell;
royaltyParams: RoyaltyParams;
};
export function nftCollectionConfigToCell(config: NftCollectionConfig): Cell {
return beginCell()
.storeAddress(config.ownerAddress)
.storeUint(config.nextItemIndex, 64)
.storeRef(config.collectionContent)
.storeRef(config.nftItemCode)
.storeRef(
beginCell()
.storeUint(config.royaltyParams.royaltyFactor, 16)
.storeUint(config.royaltyParams.royaltyBase, 16)
.storeAddress(config.royaltyParams.royaltyAddress)
)
.endCell();
}
export class NftCollection implements Contract {
constructor(readonly address: Address, readonly init?: { code: Cell; data: Cell }) {}
static createFromAddress(address: Address) {
return new NftCollection(address);
}
static createFromConfig(config: NftCollectionConfig, code: Cell, workchain = 0) {
const data = nftCollectionConfigToCell(config);
const init = { code, data };
return new NftCollection(contractAddress(workchain, init), init);
}
async sendDeploy(provider: ContractProvider, via: Sender, value: bigint) {
await provider.internal(via, {
value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell().endCell(),
});
}
async sendMintNft(provider: ContractProvider, via: Sender,
opts: {
value: bigint;
queryId: number;
amount: bigint; // to send with nft
itemIndex: number;
itemOwnerAddress: Address;
itemContent: Cell;
}
) {
const nftMessage = beginCell();
nftMessage.storeAddress(opts.itemOwnerAddress)
nftMessage.storeRef(opts.itemContent)
await provider.internal(via, {
value: opts.value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeUint(1,32) // operation
.storeUint(opts.queryId,64)
.storeUint(opts.itemIndex,64)
.storeCoins(opts.amount)
.storeRef(nftMessage) // body
.endCell()
})
}
async sendChangeOwner(provider: ContractProvider, via: Sender,
opts: {
value: bigint;
queryId: bigint;
newOwnerAddress: Address;
}
) {
await provider.internal(via, {
value: opts.value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeUint(3,32) //operation
.storeUint(opts.queryId, 64)
.storeAddress(opts.newOwnerAddress)
.endCell()
})
}
// GETTERS
async getCollectionData(provider: ContractProvider): Promise<{
nextItemId: bigint,
ownerAddress: Address,
collectionContent: Cell
}>
{
const collection_data = await provider.get("get_collection_data", []);
const stack = await collection_data.stack;
let nextItem: bigint = stack.readBigNumber();
let collectionContent = await stack.readCell();
let ownerAddress = await stack.readAddress();
return {
nextItemId: nextItem,
collectionContent: collectionContent,
ownerAddress: ownerAddress
};
}
async getItemAddressByIndex(provider: ContractProvider, index: TupleItemInt){
const res = await provider.get("get_nft_address_by_index", [index]);
const itemAddress = await res.stack.readAddress()
return itemAddress;
}
async getRoyaltyParams(provider: ContractProvider) {
const royaltyParams = await provider.get("royalty_params", []);
const stack = royaltyParams.stack;
let numerator: bigint = stack.readBigNumber();
let denominator: bigint = stack.readBigNumber();
let destination: Address = stack.readAddress();
return {
royaltyFactor: numerator,
royaltyBase: denominator,
royaltyAddress: destination,
}
}
}