forked from hashgraph/hedera-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hts-nftP2-kyc-upate-schedule.js
384 lines (346 loc) · 13.3 KB
/
hts-nftP2-kyc-upate-schedule.js
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
import {
AccountBalanceQuery,
AccountId,
Client,
CustomFixedFee,
CustomRoyaltyFee,
Hbar,
PrivateKey,
ScheduleCreateTransaction,
ScheduleInfoQuery,
ScheduleSignTransaction,
TokenAssociateTransaction,
TokenBurnTransaction,
TokenCreateTransaction,
TokenGrantKycTransaction,
TokenInfoQuery,
TokenMintTransaction,
TokenSupplyType,
TokenType,
TokenUpdateTransaction,
TransferTransaction,
} from "@hashgraph/sdk";
/**
* @typedef {import("@hashgraph/sdk").TokenInfo} TokenInfo
* @typedef {import("@hashgraph/sdk").TransactionReceipt} TransactionReceipt
*/
import dotenv from "dotenv";
dotenv.config();
// Configure accounts and client, and generate needed keys
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const treasuryId = AccountId.fromString(process.env.TREASURY_ID);
const treasuryKey = PrivateKey.fromString(process.env.TREASURY_PVKEY);
const aliceId = AccountId.fromString(process.env.ALICE_ID);
const aliceKey = PrivateKey.fromString(process.env.ALICE_PVKEY);
const bobId = AccountId.fromString(process.env.BOB_ID);
const bobKey = PrivateKey.fromString(process.env.BOB_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
const supplyKey = PrivateKey.generate();
const adminKey = PrivateKey.generate();
const kycKey = PrivateKey.generate();
const newKycKey = PrivateKey.generate();
const pauseKey = PrivateKey.generate();
const freezeKey = PrivateKey.generate();
const wipeKey = PrivateKey.generate();
async function main() {
// DEFINE CUSTOM FEE SCHEDULE
let nftCustomFee = new CustomRoyaltyFee()
.setNumerator(5)
.setDenominator(10)
.setFeeCollectorAccountId(treasuryId)
.setFallbackFee(new CustomFixedFee().setHbarAmount(new Hbar(200)));
// IPFS CONTENT IDENTIFIERS FOR WHICH WE WILL CREATE NFTs
const CID = [
"QmNPCiNA3Dsu3K5FxDPMG5Q3fZRwVTg14EXA92uqEeSRXn",
"QmZ4dgAgt8owvnULxnKxNe8YqpavtVCXmc1Lt2XajFpJs9",
"QmPzY5GxevjyfMUF5vEAjtyRoigzWp47MiKAtLBduLMC1T",
"Qmd3kGgSrAwwSrhesYcY7K54f3qD7MDo38r7Po2dChtQx5",
"QmWgkKz3ozgqtnvbCLeh7EaR1H8u5Sshx3ZJzxkcrT3jbw",
];
// CREATE NFT WITH CUSTOM FEE
let nftCreate = await new TokenCreateTransaction()
.setTokenName("Fall Collection")
.setTokenSymbol("LEAF")
.setTokenType(TokenType.NonFungibleUnique)
.setDecimals(0)
.setInitialSupply(0)
.setTreasuryAccountId(treasuryId)
.setSupplyType(TokenSupplyType.Finite)
.setMaxSupply(CID.length)
.setCustomFees([nftCustomFee])
.setAdminKey(adminKey)
.setSupplyKey(supplyKey)
.setKycKey(kycKey)
.setPauseKey(pauseKey)
.setFreezeKey(freezeKey)
.setWipeKey(wipeKey)
.freezeWith(client)
.sign(treasuryKey);
let nftCreateTxSign = await nftCreate.sign(adminKey);
let nftCreateSubmit = await nftCreateTxSign.execute(client);
let nftCreateRx = await nftCreateSubmit.getReceipt(client);
let tokenId = nftCreateRx.tokenId;
console.log(`Created NFT with Token ID: ${tokenId.toString()} \n`);
// TOKEN QUERY TO CHECK THAT THE CUSTOM FEE SCHEDULE IS ASSOCIATED WITH NFT
let tokenInfo = await tQueryFcn();
console.table(tokenInfo.customFees[0]);
// MINT NEW BATCH OF NFTs
const nftLeaf = [];
for (var i = 0; i < CID.length; i++) {
nftLeaf[i] = await tokenMinterFcn(CID[i]);
console.log(
`Created NFT ${tokenId.toString()} with serial: ${nftLeaf[
i
].serials[0].toString()}`
);
}
// BURN THE LAST NFT IN THE COLLECTION
let tokenBurnTx = await new TokenBurnTransaction()
.setTokenId(tokenId)
.setSerials([CID.length])
.freezeWith(client)
.sign(supplyKey);
let tokenBurnSubmit = await tokenBurnTx.execute(client);
let tokenBurnRx = await tokenBurnSubmit.getReceipt(client);
console.log(
`\nBurn NFT with serial ${CID.length}: ${tokenBurnRx.status.toString()}`
);
tokenInfo = await tQueryFcn();
console.log(`Current NFT supply: ${tokenInfo.totalSupply.toString()}`);
// MANUAL ASSOCIATION FOR ALICE'S ACCOUNT
let associateAliceTx = await new TokenAssociateTransaction()
.setAccountId(aliceId)
.setTokenIds([tokenId])
.freezeWith(client)
.sign(aliceKey);
let associateAliceTxSubmit = await associateAliceTx.execute(client);
let associateAliceRx = await associateAliceTxSubmit.getReceipt(client);
console.log(
`\n- Alice NFT manual association: ${associateAliceRx.status.toString()}`
);
// MANUAL ASSOCIATION FOR BOB'S ACCOUNT
let associateBobTx = await new TokenAssociateTransaction()
.setAccountId(bobId)
.setTokenIds([tokenId])
.freezeWith(client)
.sign(bobKey);
let associateBobTxSubmit = await associateBobTx.execute(client);
let associateBobRx = await associateBobTxSubmit.getReceipt(client);
console.log(
`- Bob NFT manual association: ${associateBobRx.status.toString()}`
);
// PART 2.1 STARTS ============================================================
console.log(
`\nPART 2.1 STARTS ============================================================\n`
);
// ENABLE TOKEN KYC FOR ALICE AND BOB
let aliceKyc = await kycEnableFcn(aliceId);
let bobKyc = await kycEnableFcn(bobId);
console.log(
`- Enabling token KYC for Alice's account: ${aliceKyc.status.toString()}`
);
console.log(
`- Enabling token KYC for Bob's account: ${bobKyc.status.toString()}\n`
);
// QUERY TO CHECK INTIAL KYC KEY
tokenInfo = await tQueryFcn();
console.log(
`- KYC key for the NFT is: \n${tokenInfo.kycKey.toString()} \n`
);
// UPDATE TOKEN PROPERTIES: NEW KYC KEY
let tokenUpdateTx = await new TokenUpdateTransaction()
.setTokenId(tokenId)
.setKycKey(newKycKey)
.freezeWith(client)
.sign(adminKey);
let tokenUpdateSubmitTx = await tokenUpdateTx.execute(client);
let tokenUpdateRx = await tokenUpdateSubmitTx.getReceipt(client);
console.log(
`- Token update transaction (new KYC key): ${tokenUpdateRx.status.toString()} \n`
);
// QUERY TO CHECK CHANGE IN KYC KEY
tokenInfo = await tQueryFcn();
console.log(`- KYC key for the NFT is: \n${tokenInfo.kycKey.toString()}`);
// PART 2.1 ENDS ============================================================
console.log(
`\nPART 2.1 ENDS ============================================================\n`
);
// BALANCE CHECK 1
let oB = await bCheckerFcn(treasuryId);
let aB = await bCheckerFcn(aliceId);
let bB = await bCheckerFcn(bobId);
console.log(
`- Treasury balance: ID:${tokenId.toString()} and ${oB.toString()}`
);
console.log(
`- Alice balance: ID:${tokenId.toString()} and ${aB.toString()}`
);
console.log(`- Bob balance: ID:${tokenId.toString()} and ${bB.toString()}`);
// 1st TRANSFER NFT TREASURY -> ALICE
let tokenTransferTx = await new TransferTransaction()
.addNftTransfer(tokenId, 2, treasuryId, aliceId)
.freezeWith(client)
.sign(treasuryKey);
let tokenTransferSubmit = await tokenTransferTx.execute(client);
let tokenTransferRx = await tokenTransferSubmit.getReceipt(client);
console.log(
`\n NFT transfer Treasury -> Alice status: ${tokenTransferRx.status.toString()} \n`
);
// BALANCE CHECK 2
oB = await bCheckerFcn(treasuryId);
aB = await bCheckerFcn(aliceId);
bB = await bCheckerFcn(bobId);
console.log(
`- Treasury balance: ID:${tokenId.toString()} and ${oB.toString()}`
);
console.log(
`- Alice balance: ID:${tokenId.toString()} and ${aB.toString()}`
);
console.log(`- Bob balance: ID:${tokenId.toString()} and ${bB.toString()}`);
// 2nd NFT TRANSFER NFT ALICE -> BOB
let tokenTransferTx2 = await new TransferTransaction()
.addNftTransfer(tokenId, 2, aliceId, bobId)
.addHbarTransfer(aliceId, 100)
.addHbarTransfer(bobId, -100)
.freezeWith(client)
.sign(aliceKey);
const tokenTransferTx2Sign = await tokenTransferTx2.sign(bobKey);
let tokenTransferSubmit2 = await tokenTransferTx2Sign.execute(client);
let tokenTransferRx2 = await tokenTransferSubmit2.getReceipt(client);
console.log(
`\n NFT transfer Alice -> Bob status: ${tokenTransferRx2.status.toString()} \n`
);
// BALANCE CHECK 3
oB = await bCheckerFcn(treasuryId);
aB = await bCheckerFcn(aliceId);
bB = await bCheckerFcn(bobId);
console.log(
`- Treasury balance: ID:${tokenId.toString()} and ${oB.toString()}`
);
console.log(
`- Alice balance: ID:${tokenId.toString()} and ${aB.toString()}`
);
console.log(`- Bob balance: ID:${tokenId.toString()} and ${bB.toString()}`);
// PART 2.2 STARTS ============================================================
console.log(
`\nPART 2.2 STARTS ============================================================\n`
);
// CREATE THE NFT TRANSFER FROM BOB->ALICE TO BE SCHEDULED
// REQUIRES ALICE'S AND BOB'S SIGNATURES
let txToSchedule = new TransferTransaction()
.addNftTransfer(tokenId, 2, bobId, aliceId)
.addHbarTransfer(aliceId, -200)
.addHbarTransfer(bobId, 200);
// SCHEDULE THE NFT TRANSFER TRANSACTION CREATED IN THE LAST STEP
let scheduleTx = await new ScheduleCreateTransaction()
.setScheduledTransaction(txToSchedule)
.execute(client);
let scheduleRx = await scheduleTx.getReceipt(client);
let scheduleId = scheduleRx.scheduleId;
let scheduledTxId = scheduleRx.scheduledTransactionId;
console.log(`- The schedule ID is: ${scheduleId.toString()}`);
console.log(
`- The scheduled transaction ID is: ${scheduledTxId.toString()} \n`
);
// SUBMIT ALICE'S SIGNATURE FOR THE TRANSFER TRANSACTION
let aliceSignTx = await new ScheduleSignTransaction()
.setScheduleId(scheduleId)
.freezeWith(client)
.sign(aliceKey);
let aliceSignSubmit = await aliceSignTx.execute(client);
let aliceSignRx = await aliceSignSubmit.getReceipt(client);
console.log(
`- Status of Alice's signature submission: ${aliceSignRx.status.toString()}`
);
// QUERY TO CONFIRM IF THE SCHEDULE WAS TRIGGERED (SIGNATURES HAVE BEEN ADDED)
let scheduleQuery = await new ScheduleInfoQuery()
.setScheduleId(scheduleId)
.execute(client);
console.log(
`- Schedule triggered (all required signatures received): ${(
scheduleQuery.executed !== null
).toString()}`
);
// SUBMIT BOB'S SIGNATURE FOR THE TRANSFER TRANSACTION
let bobSignTx = await new ScheduleSignTransaction()
.setScheduleId(scheduleId)
.freezeWith(client)
.sign(bobKey);
let bobSignSubmit = await bobSignTx.execute(client);
let bobSignRx = await bobSignSubmit.getReceipt(client);
console.log(
`- Status of Bob's signature submission: ${bobSignRx.status.toString()}`
);
// QUERY TO CONFIRM IF THE SCHEDULE WAS TRIGGERED (SIGNATURES HAVE BEEN ADDED)
scheduleQuery = await new ScheduleInfoQuery()
.setScheduleId(scheduleId)
.execute(client);
console.log(
`- Schedule triggered (all required signatures received): ${(
scheduleQuery.executed !== null
).toString()} \n`
);
// VERIFY THAT THE SCHEDULED TRANSACTION (TOKEN TRANSFER) EXECUTED
oB = await bCheckerFcn(treasuryId);
aB = await bCheckerFcn(aliceId);
bB = await bCheckerFcn(bobId);
console.log(
`- Treasury balance: ID:${tokenId.toString()} and ${oB.toString()}`
);
console.log(
`- Alice balance: ID:${tokenId.toString()} and ${aB.toString()}`
);
console.log(`- Bob balance: ID:${tokenId.toString()} and ${bB.toString()}`);
/**
* TOKEN MINTER FUNCTION
*
* @param {string} CID
* @returns {Promise<TransactionReceipt>}
*/
async function tokenMinterFcn(CID) {
const mintTx = new TokenMintTransaction()
.setTokenId(tokenId)
.setMetadata([Buffer.from(CID)])
.freezeWith(client);
let mintTxSign = await mintTx.sign(supplyKey);
let mintTxSubmit = await mintTxSign.execute(client);
return mintTxSubmit.getReceipt(client);
}
/**
* BALANCE CHECKER FUNCTION
*
* @param {string | AccountId} id
* @returns {Promise<Hbar>}
*/
async function bCheckerFcn(id) {
const balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(id)
.execute(client);
return balanceCheckTx.hbars;
}
/**
* KYC ENABLE FUNCTION
*
* @param {string | AccountId} id
* @returns {Promise<TransactionReceipt>}
*/
async function kycEnableFcn(id) {
let kycEnableTx = await new TokenGrantKycTransaction()
.setAccountId(id)
.setTokenId(tokenId)
.freezeWith(client)
.sign(kycKey);
let kycSubmitTx = await kycEnableTx.execute(client);
return kycSubmitTx.getReceipt(client);
}
/**
* TOKEN QUERY FUNCTION
*
* @returns {Promise<TokenInfo>}
*/
async function tQueryFcn() {
return new TokenInfoQuery().setTokenId(tokenId).execute(client);
}
}
void main();