-
Notifications
You must be signed in to change notification settings - Fork 148
/
token-reject.js
240 lines (217 loc) · 7.91 KB
/
token-reject.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
import {
AccountCreateTransaction,
PrivateKey,
TokenCreateTransaction,
TransferTransaction,
AccountId,
Client,
TokenType,
TokenMintTransaction,
TokenRejectTransaction,
TokenRejectFlow,
NftId,
AccountBalanceQuery,
TokenSupplyType,
} from "@hashgraph/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 CID = [
"QmNPCiNA3Dsu3K5FxDPMG5Q3fZRwVTg14EXA92uqEeSRXn",
"QmZ4dgAgt8owvnULxnKxNe8YqpavtVCXmc1Lt2XajFpJs9",
"QmPzY5GxevjyfMUF5vEAjtyRoigzWp47MiKAtLBduLMC1T",
];
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringED25519(process.env.OPERATOR_KEY);
const network = process.env.HEDERA_NETWORK;
const client = Client.forName(network).setOperator(operatorId, operatorKey);
// create a treasury account
const treasuryPrivateKey = PrivateKey.generateED25519();
const treasuryAccountId = (
await (
await new AccountCreateTransaction()
.setKey(treasuryPrivateKey)
.setMaxAutomaticTokenAssociations(100)
.execute(client)
).getReceipt(client)
).accountId;
// create a receiver account with unlimited max auto associations
const receiverPrivateKey = PrivateKey.generateED25519();
const receiverAccountId = (
await (
await new AccountCreateTransaction()
.setKey(receiverPrivateKey)
.setMaxAutomaticTokenAssociations(-1)
.execute(client)
).getReceipt(client)
).accountId;
// create a nft collection
const nftCreationTx = await (
await new TokenCreateTransaction()
.setTokenType(TokenType.NonFungibleUnique)
.setTokenName("Example Fungible Token")
.setTokenSymbol("EFT")
.setMaxSupply(CID.length)
.setSupplyType(TokenSupplyType.Finite)
.setSupplyKey(operatorKey)
.setAdminKey(operatorKey)
.setTreasuryAccountId(treasuryAccountId)
.freezeWith(client)
.sign(treasuryPrivateKey)
).execute(client);
const nftId = (await nftCreationTx.getReceipt(client)).tokenId;
console.log("NFT ID: ", nftId.toString());
// create a fungible token
const ftCreationTx = await (
await new TokenCreateTransaction()
.setTokenName("Example Fungible Token")
.setTokenSymbol("EFT")
.setInitialSupply(100000000)
.setSupplyKey(operatorKey)
.setAdminKey(operatorKey)
.setTreasuryAccountId(treasuryAccountId)
.freezeWith(client)
.sign(treasuryPrivateKey)
).execute(client);
const ftId = (await ftCreationTx.getReceipt(client)).tokenId;
console.log("FT ID: ", ftId.toString());
// mint 3 NFTs to treasury
const nftSerialIds = [];
for (let i = 0; i < CID.length; i++) {
const { serials } = await (
await new TokenMintTransaction()
.setTokenId(nftId)
.addMetadata(Buffer.from(CID[i]))
.execute(client)
).getReceipt(client);
const [serial] = serials;
nftSerialIds.push(new NftId(nftId, serial));
}
// transfer nfts to receiver
await (
await (
await new TransferTransaction()
.addNftTransfer(
nftSerialIds[0],
treasuryAccountId,
receiverAccountId,
)
.addNftTransfer(
nftSerialIds[1],
treasuryAccountId,
receiverAccountId,
)
.addNftTransfer(
nftSerialIds[2],
treasuryAccountId,
receiverAccountId,
)
.freezeWith(client)
.sign(treasuryPrivateKey)
).execute(client)
).getReceipt(client);
// transfer fungible tokens to receiver
await (
await (
await new TransferTransaction()
.addTokenTransfer(ftId, treasuryAccountId, -1)
.addTokenTransfer(ftId, receiverAccountId, 1)
.freezeWith(client)
.sign(treasuryPrivateKey)
).execute(client)
).getReceipt(client);
console.log("=======================");
console.log("Before Token Reject");
console.log("=======================");
const receiverFTBalanceBefore = (
await new AccountBalanceQuery()
.setAccountId(receiverAccountId)
.execute(client)
).tokens.get(ftId);
const treasuryFTBalanceBefore = (
await new AccountBalanceQuery()
.setAccountId(treasuryAccountId)
.execute(client)
).tokens.get(ftId);
const receiverNFTBalanceBefore = (
await new AccountBalanceQuery()
.setAccountId(receiverAccountId)
.execute(client)
).tokens.get(nftId);
const treasuryNFTBalanceBefore = (
await new AccountBalanceQuery()
.setAccountId(treasuryAccountId)
.execute(client)
).tokens.get(nftId);
console.log("Receiver FT balance: ", receiverFTBalanceBefore.toInt());
console.log("Treasury FT balance: ", treasuryFTBalanceBefore.toInt());
console.log(
"Receiver NFT balance: ",
receiverNFTBalanceBefore ? receiverNFTBalanceBefore.toInt() : 0,
);
console.log("Treasury NFT balance: ", treasuryNFTBalanceBefore.toInt());
// reject fungible tokens back to treasury
const tokenRejectResponse = await (
await (
await new TokenRejectTransaction()
.setOwnerId(receiverAccountId)
.addTokenId(ftId)
.freezeWith(client)
.sign(receiverPrivateKey)
).execute(client)
).getReceipt(client);
// reject NFTs back to treasury
const rejectFlowResponse = await (
await new TokenRejectFlow()
.setOwnerId(receiverAccountId)
.setNftIds(nftSerialIds)
.freezeWith(client)
.sign(receiverPrivateKey)
.execute(client)
).getReceipt(client);
const tokenRejectStatus = tokenRejectResponse.status.toString();
const tokenRejectFlowStatus = rejectFlowResponse.status.toString();
console.log("=======================");
console.log("After Token Reject Transaction and flow");
console.log("=======================");
const receiverFTBalanceAfter = (
await new AccountBalanceQuery()
.setAccountId(receiverAccountId)
.execute(client)
).tokens.get(ftId);
const treasuryFTBalanceAfter = (
await new AccountBalanceQuery()
.setAccountId(treasuryAccountId)
.execute(client)
).tokens.get(ftId);
const receiverNFTBalanceAfter = (
await new AccountBalanceQuery()
.setAccountId(receiverAccountId)
.execute(client)
).tokens.get(nftId);
const treasuryNFTBalanceAfter = (
await new AccountBalanceQuery()
.setAccountId(treasuryAccountId)
.execute(client)
).tokens.get(nftId);
console.log("TokenReject response:", tokenRejectStatus);
console.log("TokenRejectFlow response:", tokenRejectFlowStatus);
console.log("Receiver FT balance: ", receiverFTBalanceAfter.toInt());
console.log("Treasury FT balance: ", treasuryFTBalanceAfter.toInt());
console.log(
"Receiver NFT balance: ",
receiverNFTBalanceAfter ? receiverNFTBalanceAfter.toInt() : 0,
);
console.log("Treasury NFT balance: ", treasuryNFTBalanceAfter.toInt());
client.close();
}
void main();