-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathtoken-airdrop-example.js
More file actions
419 lines (373 loc) · 12.3 KB
/
token-airdrop-example.js
File metadata and controls
419 lines (373 loc) · 12.3 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import {
Client,
PrivateKey,
AccountId,
AccountCreateTransaction,
TokenAirdropTransaction,
Hbar,
TokenCreateTransaction,
TokenType,
TokenMintTransaction,
AccountBalanceQuery,
TokenClaimAirdropTransaction,
TokenCancelAirdropTransaction,
TokenRejectTransaction,
NftId,
} from "@hiero-ledger/sdk";
import dotenv from "dotenv";
dotenv.config();
async function main() {
if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
process.env.HEDERA_NETWORK == null
) {
throw new Error(
"Environment variables OPERATOR_ID, HEDERA_NETWORK, and OPERATOR_KEY are required.",
);
}
const client = Client.forName(process.env.HEDERA_NETWORK).setOperator(
AccountId.fromString(process.env.OPERATOR_ID),
PrivateKey.fromStringECDSA(process.env.OPERATOR_KEY),
);
const CID = [
"QmNPCiNA3Dsu3K5FxDPMG5Q3fZRwVTg14EXA92uqEeSRXn",
"QmZ4dgAgt8owvnULxnKxNe8YqpavtVCXmc1Lt2XajFpJs9",
"QmPzY5GxevjyfMUF5vEAjtyRoigzWp47MiKAtLBduLMC1T",
"Qmd3kGgSrAwwSrhesYcY7K54f3qD7MDo38r7Po2dChtQx5",
"QmWgkKz3ozgqtnvbCLeh7EaR1H8u5Sshx3ZJzxkcrT3jbw",
];
/**
* STEP 1:
* Create 4 accounts
*/
const privateKey = PrivateKey.generateECDSA();
const { accountId: accountId1 } = await (
await new AccountCreateTransaction()
.setECDSAKeyWithAlias(privateKey)
.setInitialBalance(new Hbar(10))
.setMaxAutomaticTokenAssociations(-1)
.execute(client)
).getReceipt(client);
const privateKey2 = PrivateKey.generateECDSA();
const { accountId: accountId2 } = await (
await new AccountCreateTransaction()
.setECDSAKeyWithAlias(privateKey2)
.setInitialBalance(new Hbar(10))
.setMaxAutomaticTokenAssociations(1)
.execute(client)
).getReceipt(client);
const privateKey3 = PrivateKey.generateECDSA();
const { accountId: accountId3 } = await (
await new AccountCreateTransaction()
.setKeyWithoutAlias(privateKey3)
.setInitialBalance(new Hbar(10))
.setMaxAutomaticTokenAssociations(0)
.execute(client)
).getReceipt(client);
const treasuryKey = PrivateKey.generateECDSA();
const { accountId: treasuryAccount } = await (
await new AccountCreateTransaction()
.setECDSAKeyWithAlias(treasuryKey)
.setInitialBalance(new Hbar(10))
.setMaxAutomaticTokenAssociations(-1)
.execute(client)
).getReceipt(client);
/**
* STEP 2:
* Create FT and NFT mint
*/
const INITIAL_SUPPLY = 300;
const tokenCreateTx = await new TokenCreateTransaction()
.setTokenName("Fungible Token")
.setTokenSymbol("TFT")
.setTokenMemo("Example memo")
.setDecimals(3)
.setInitialSupply(INITIAL_SUPPLY)
.setTreasuryAccountId(treasuryAccount)
.setAdminKey(client.operatorPublicKey)
.setFreezeKey(client.operatorPublicKey)
.setSupplyKey(client.operatorPublicKey)
.setMetadataKey(client.operatorPublicKey)
.setPauseKey(client.operatorPublicKey)
.freezeWith(client)
.sign(treasuryKey);
const { tokenId } = await (
await tokenCreateTx.execute(client)
).getReceipt(client);
const { tokenId: nftId } = await (
await (
await new TokenCreateTransaction()
.setTokenName("Test NFT")
.setTokenSymbol("TNFT")
.setTokenType(TokenType.NonFungibleUnique)
.setTreasuryAccountId(treasuryAccount)
.setAdminKey(client.operatorPublicKey)
.setFreezeKey(client.operatorPublicKey)
.setSupplyKey(client.operatorPublicKey)
.setMetadataKey(client.operatorPublicKey)
.setPauseKey(client.operatorPublicKey)
.freezeWith(client)
.sign(treasuryKey)
).execute(client)
).getReceipt(client);
let serialsNfts = [];
for (let i = 0; i < CID.length; i++) {
const { serials } = await (
await new TokenMintTransaction()
.setTokenId(nftId)
.addMetadata(Buffer.from("-"))
.execute(client)
).getReceipt(client);
serialsNfts.push(serials[0]);
}
/**
* STEP 3:
* Airdrop fungible tokens to 3 accounts
*/
const AIRDROP_SUPPLY_PER_ACCOUNT = INITIAL_SUPPLY / 3;
const airdropRecord = await (
await (
await new TokenAirdropTransaction()
.addTokenTransfer(
tokenId,
treasuryAccount,
-AIRDROP_SUPPLY_PER_ACCOUNT,
)
.addTokenTransfer(
tokenId,
accountId1,
AIRDROP_SUPPLY_PER_ACCOUNT,
)
.addTokenTransfer(
tokenId,
treasuryAccount,
-AIRDROP_SUPPLY_PER_ACCOUNT,
)
.addTokenTransfer(
tokenId,
accountId2,
AIRDROP_SUPPLY_PER_ACCOUNT,
)
.addTokenTransfer(
tokenId,
treasuryAccount,
-AIRDROP_SUPPLY_PER_ACCOUNT,
)
.addTokenTransfer(
tokenId,
accountId3,
AIRDROP_SUPPLY_PER_ACCOUNT,
)
.freezeWith(client)
.sign(treasuryKey)
).execute(client)
).getRecord(client);
/**
* STEP 4: Get the transaction record and see the pending airdrops
*/
const { newPendingAirdrops } = airdropRecord;
console.log("Pending airdrops length", newPendingAirdrops.length);
console.log("Pending airdrop", newPendingAirdrops[0]);
/**
* STEP 5:
* Query to verify account 1 and Account 2 have received the airdrops and Account 3 has not
*/
let account1Balance = await new AccountBalanceQuery()
.setAccountId(accountId1)
.execute(client);
let account2Balance = await new AccountBalanceQuery()
.setAccountId(accountId2)
.execute(client);
let account3Balance = await new AccountBalanceQuery()
.setAccountId(accountId3)
.execute(client);
console.log(
"Account1 balance after airdrop: ",
account1Balance.tokens.get(tokenId).toInt(),
);
console.log(
"Account2 balance after airdrop: ",
account2Balance.tokens.get(tokenId).toInt(),
);
console.log(
"Account3 balance after airdrop: ",
account3Balance.tokens.get(tokenId),
);
/**
* Step 6: Claim the airdrop for Account 3
*/
await (
await (
await new TokenClaimAirdropTransaction()
.addPendingAirdropId(newPendingAirdrops[0].airdropId)
.freezeWith(client)
.sign(privateKey3)
).execute(client)
).getReceipt(client);
const account3BalanceAfterClaim = await new AccountBalanceQuery()
.setAccountId(accountId3)
.execute(client);
console.log(
"Account3 balance after airdrop claim",
account3BalanceAfterClaim.tokens.get(tokenId).toInt(),
);
/**
* Step 7:
* Airdrop the NFTs to the 3 accounts
*/
const { newPendingAirdrops: newPendingAirdropsNfts } = await (
await (
await new TokenAirdropTransaction()
.addNftTransfer(
nftId,
serialsNfts[0],
treasuryAccount,
accountId1,
)
.addNftTransfer(
nftId,
serialsNfts[1],
treasuryAccount,
accountId2,
)
.addNftTransfer(
nftId,
serialsNfts[2],
treasuryAccount,
accountId3,
)
.freezeWith(client)
.sign(treasuryKey)
).execute(client)
).getRecord(client);
/**
* Step 8:
* Get the transaction record and verify two pending airdrops (for Account 2 & 3)
*/
console.log("Pending airdrops length", newPendingAirdropsNfts.length);
console.log("Pending airdrop for Account 0:", newPendingAirdropsNfts[0]);
console.log("Pending airdrop for Account 1:", newPendingAirdropsNfts[1]);
/**
* Step 9:
* Query to verify Account 1 received the airdrop and Account 2 and Account 3 did not
*/
account1Balance = await new AccountBalanceQuery()
.setAccountId(accountId1)
.execute(client);
account2Balance = await new AccountBalanceQuery()
.setAccountId(accountId2)
.execute(client);
console.log(
"Account 1 NFT Balance after airdrop",
account1Balance.tokens.get(nftId).toInt(),
);
console.log(
"Account 2 NFT Balance after airdrop",
account2Balance.tokens.get(nftId),
);
console.log(
"Account 3 NFT Balance after airdrop",
account3Balance.tokens.get(nftId),
);
/**
* Step 10:
* Claim the airdrop for Account 2
*/
await (
await (
await new TokenClaimAirdropTransaction()
.addPendingAirdropId(newPendingAirdropsNfts[0].airdropId)
.freezeWith(client)
.sign(privateKey2)
).execute(client)
).getReceipt(client);
account2Balance = await new AccountBalanceQuery()
.setAccountId(accountId2)
.execute(client);
console.log(
"Account 2 nft balance after claim: ",
account2Balance.tokens.get(nftId).toInt(),
);
/**
* Step 11:
* Cancel the airdrop for Account 3
*/
console.log("Cancelling airdrop for account 3");
await new TokenCancelAirdropTransaction()
.addPendingAirdropId(newPendingAirdropsNfts[1].airdropId)
.execute(client);
account3Balance = await new AccountBalanceQuery()
.setAccountId(accountId3)
.execute(client);
console.log(
"Account 3 nft balance after cancel: ",
account3Balance.tokens.get(nftId),
);
/**
* Step 12:
* Reject the NFT for Account 2
*/
console.log("Rejecting NFT for account 2");
await (
await (
await new TokenRejectTransaction()
.setOwnerId(accountId2)
.addNftId(new NftId(nftId, serialsNfts[1]))
.freezeWith(client)
.sign(privateKey2)
).execute(client)
).getReceipt(client);
/**
* Step 13:
* Query to verify Account 2 no longer has the NFT
*/
account2Balance = await new AccountBalanceQuery()
.setAccountId(accountId2)
.execute(client);
console.log(
"Account 2 nft balance after reject: ",
account2Balance.tokens.get(nftId).toInt(),
);
/**
* Step 14:
* Query to verify treasury has received the NFT back
*/
let treasuryBalance = await new AccountBalanceQuery()
.setAccountId(treasuryAccount)
.execute(client);
console.log(
"Treasury nft balance after reject: ",
treasuryBalance.tokens.get(nftId).toInt(),
);
/**
* Step 15:
* Reject the fungible tokens for Account 3
*/
console.log("Rejecting fungible tokens for account 3: ");
await (
await (
await new TokenRejectTransaction()
.setOwnerId(accountId3)
.addTokenId(tokenId)
.freezeWith(client)
.sign(privateKey3)
).execute(client)
).getReceipt(client);
account3Balance = await new AccountBalanceQuery()
.setAccountId(accountId3)
.execute(client);
console.log(
"Account 3 balance after reject: ",
account3Balance.tokens.get(tokenId).toInt(),
);
treasuryBalance = await new AccountBalanceQuery()
.setAccountId(treasuryAccount)
.execute(client);
console.log(
"Treasury balance after reject: ",
treasuryBalance.tokens.get(tokenId).toInt(),
);
client.close();
}
void main();