forked from Burstall/hedera-nft-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburnFT.js
153 lines (121 loc) · 3.78 KB
/
burnFT.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
require('dotenv').config();
const {
AccountId,
PrivateKey,
Client,
TokenBurnTransaction,
} = require('@hashgraph/sdk');
const fetch = require('cross-fetch');
// Configure accounts and client, and generate needed keys
const operatorId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const operatorKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const supplyKey = PrivateKey.fromString(process.env.SUPPLY_KEY);
const env = process.env.ENVIRONMENT ?? null;
const baseUrlForMainnet = 'https://mainnet-public.mirrornode.hedera.com';
const baseUrlForTestnet = 'http://testnet.mirrornode.hedera.com';
const maxRetries = 6;
async function getTokenType(tokenId) {
const baseUrl = env == 'MAIN' ? baseUrlForMainnet : baseUrlForTestnet;
const routeUrl = `/api/v1/tokens/${tokenId}`;
const tokenDetailJSON = await fetchJson(baseUrl + routeUrl);
return [tokenDetailJSON.type, tokenDetailJSON.decimals];
}
async function fetchJson(url, depth = 0) {
if (depth >= maxRetries) return null;
if (depth > (maxRetries / 2)) console.log('Attempt: ', depth, url);
depth++;
try {
const res = await fetchWithTimeout(url);
if (res.status != 200) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
return res.json();
}
catch (err) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fetchWithTimeout(resource, options = {}) {
const { timeout = 30000 } = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal,
});
clearTimeout(id);
return response;
}
function getArg(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
let customValue;
if (customIndex > -1) {
// Retrieve the value after --custom
customValue = process.argv[customIndex + 1];
}
return customValue;
}
function getArgFlag(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
if (customIndex > -1) {
return true;
}
return false;
}
async function main() {
const help = getArgFlag('h');
if (help) {
console.log('Usage: node burnFT.js -amt XXX');
console.log(' -amt XXXX to burn');
process.exit(0);
}
// Get the token ID
const tokenId = process.env.TOKEN_ID;
// Log the token ID
console.log(`- Burning supply from FT with Token ID: ${tokenId} \n`);
console.log(`- Using account: ${operatorId} to pay`);
const [type, decimal] = await getTokenType(tokenId);
if (type != 'FUNGIBLE_COMMON') {
console.log(`Token is of type ${type} and not suitable for this command`);
process.exit(1);
}
const amt = Number(getArg('amt'));
if (!amt) {
console.log('Must specify how many to burn');
}
let client;
if (env == 'TEST') {
client = Client.forTestnet();
console.log('- Burning tokens in *TESTNET*');
}
else if (env == 'MAIN') {
client = Client.forMainnet();
console.log('- Burning tokens in *MAINNET*');
}
else {
console.log('ERROR: Must specify either MAIN or TEST as environment in .env file');
return;
}
console.log(`Burning ${amt} tokens`);
client.setOperator(operatorId, operatorKey);
const transaction = new TokenBurnTransaction()
.setTokenId(tokenId)
.setAmount(amt * (10 ** decimal))
.freezeWith(client);
// Sign with the supply private key of the token
const signTx = await transaction.sign(supplyKey);
// Submit the transaction to a Hedera network
const txResponse = await signTx.execute(client);
// Request the receipt of the transaction
const receipt = await txResponse.getReceipt(client);
// Get the transaction consensus status
const transactionStatus = receipt.status;
console.log('The transaction consensus status ' + transactionStatus.toString());
console.log('Burn complete');
}
main();