forked from Burstall/hedera-nft-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenGraveyard.js
276 lines (238 loc) · 8.81 KB
/
tokenGraveyard.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
const {
Hbar,
Client,
AccountId,
PrivateKey,
ContractFunctionParameters,
ContractExecuteTransaction,
TokenId,
ContractId,
HbarUnit,
ContractCallQuery,
// eslint-disable-next-line no-unused-vars
TransactionReceipt,
} = require('@hashgraph/sdk');
const readlineSync = require('readline-sync');
require('dotenv').config();
const fs = require('fs');
const Web3 = require('web3');
const web3 = new Web3();
const CONTRACT_ID_TESTNET = ContractId.fromString('0.0.3969576');
const CONTRACT_ID_MAINNET = ContractId.fromString('0.0.1279390');
const DEFAULT_COST = new Hbar(5);
// Get operator from .env file
const operatorKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const operatorId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const env = process.env.ENVIRONMENT ?? null;
let client;
let contractId;
let payment;
let abi;
async function main() {
if (getArgFlag('h')) {
console.log('Usage: node tokenGraveyard.js [-associate 0.0.XXX | -cost] [-pmt Z]');
console.log(' -associate 0.0.XXX specify the token to associate for the graveyard');
console.log(' -cost query the minimum cost of the service');
console.log(' -pay Z override the default payment for usage');
process.exit(0);
}
if (getArgFlag('pay')) {
payment = new Hbar(getArg('pay'), HbarUnit.Hbar);
}
else {
payment = DEFAULT_COST;
}
if (!operatorKey || !operatorId) {
console.log('ERROR: Must specify ACCOUNT_ID & PRIVATE_KEY in .env file');
process.exit(1);
}
if (env.toUpperCase() == 'TEST') {
client = Client.forTestnet();
contractId = CONTRACT_ID_TESTNET;
}
else if (env.toUpperCase() == 'MAIN') {
client = Client.forMainnet();
contractId = CONTRACT_ID_MAINNET;
}
else {
console.log('ERROR: Must specify either MAIN or TEST as environment in .env file');
process.exit(1);
}
client.setOperator(operatorId, operatorKey);
const json = JSON.parse(fs.readFileSync('./abi/TokenGraveyard.json', 'utf8'));
abi = json.abi;
console.log('\n-Loading ABI...\n');
console.log('\n-Using ENIVRONMENT:', env);
console.log('\n-Using Operator:', operatorId.toString());
console.log('\n-Using Contract: ',
contractId.toString(),
' / ', contractId.toSolidityAddress());
// either -cost or -token needs specification
if (getArgFlag('cost')) {
try {
console.log('\n-getCost Query');
// generate function call with function name and parameters
const functionCallAsUint8Array = encodeFunctionCall('getCost', []);
// query the contract
const contractCall = await new ContractCallQuery()
.setContractId(contractId)
.setFunctionParameters(functionCallAsUint8Array)
.setMaxQueryPayment(new Hbar(2))
.setGas(100000)
.execute(client);
const results = decodeFunctionResult('getCost', contractCall.bytes);
console.log('Cost to associate a new token for burial:', new Hbar(results.amt, HbarUnit.Tinybar).toString());
}
catch (err) {
console.log(JSON.stringify(err, null, 4));
}
}
else if (getArgFlag('associate')) {
const tokenId = TokenId.fromString(getArg('associate'));
console.log('\n-Associate token:', tokenId.toString());
console.log('\n-Contract:', contractId.toString());
console.log('\n **ONLY WORKS IF NO FALL BACK ROYALTY FEE**');
if (!readlineSync.keyInYNStrict('\nReady to prepare the burial site? Once associated send the tokens to the contract using any method you prefer' + contractId.toString())) {
console.log('**User Aborted - exiting**');
process.exit(0);
}
try {
console.log('\n-Attempting to associate token');
const gasLim = 800000;
const params = new ContractFunctionParameters()
.addAddress(tokenId.toSolidityAddress());
const [associateRx] = await contractExecuteFcn(contractId, gasLim, 'tokenAssociate', params, payment);
// console.log('Function results', JSON.stringify(contractOutput, 3));
const associateStatus = associateRx.status;
console.log('\n-Association: ' + associateStatus.toString());
if (associateStatus.toString() == 'SUCCESS') {
console.log('\n-Association successful. Send the tokens to', contractId.toString(), 'to bury them.');
}
}
catch (err) {
console.log(err);
}
}
else {
console.log('Must specify either -cost (check cost) or -associate 0.0.XXX to use the script.');
process.exit(1);
}
}
/**
* Helper function to encapsulate the calling of a SC method
* @param {ContractId} cId the SC to call
* @param {number} gasLim the upper limit for gas to use
* @param {string} fcnName the name of the function to call
* @param {ContractFunctionParameters} params parameters for the function
* @param {Hbar} amountHbar amount of hbar to pay for the service
* @returns {[TransactionReceipt, any]} the transaction reciept and the decoded function result
*/
async function contractExecuteFcn(cId, gasLim, fcnName, params, amountHbar) {
const contractExecuteTx = await new ContractExecuteTransaction()
.setContractId(cId)
.setGas(gasLim)
.setFunction(fcnName, params)
.setPayableAmount(amountHbar)
.execute(client);
// get the results of the function call;
const record = await contractExecuteTx.getRecord(client);
// console.log('record bytes:', JSON.stringify(record.contractFunctionResult.bytes, 4));
// console.log('Execution return', fcnName, JSON.stringify(contractExecuteTx, 3));
record.contractFunctionResult.logs.forEach((log) => {
if (log.data == '0x') return;
// convert the log.data (uint8Array) to a string
const logStringHex = '0x'.concat(Buffer.from(log.data).toString('hex'));
// get topics from log
const logTopics = [];
log.topics.forEach((topic) => {
logTopics.push('0x'.concat(Buffer.from(topic).toString('hex')));
});
// decode the event data
const event = decodeEvent('GraveyardEvent', logStringHex, logTopics.slice(1));
if (event) {
// output the from address stored in the event
let outputStr = '\n';
for (let f = 0; f < event.__length__; f++) {
const field = event[f];
let output = field.startsWith('0x') ? AccountId.fromSolidityAddress(field).toString() : field;
output = f == 0 ? output : ' : ' + output;
outputStr += output;
}
console.log(outputStr);
}
else {
console.log('ERROR decoding (part of) log message');
}
});
const contractResults = decodeFunctionResult(fcnName, record.contractFunctionResult.bytes);
const contractExecuteRx = await contractExecuteTx.getReceipt(client);
return [contractExecuteRx, contractResults];
}
/**
* Helper method to decode emitted events taken from transaction record
* @param {string} eventName name of the event expected to be emitted
* @param {string} log log as a Hex string
* @param {string[]} topics topic data as array
* @returns {any} the decoded event object
*/
function decodeEvent(eventName, log, topics) {
const eventAbi = abi.find((event) => event.name === eventName && event.type === 'event');
try {
const decodedLog = web3.eth.abi.decodeLog(eventAbi.inputs, log, topics);
return decodedLog;
}
catch (err) {
console.log('ERROR decoding event', eventName, log, topics, JSON.stringify(err, null, 4));
}
}
/**
* Decodes the result of a contract's function execution
* @param functionName the name of the function within the ABI
* @param resultAsBytes a byte array containing the execution result
*/
function decodeFunctionResult(functionName, resultAsBytes) {
const functionAbi = abi.find(func => func.name === functionName);
const functionParameters = functionAbi.outputs;
const resultHex = '0x'.concat(Buffer.from(resultAsBytes).toString('hex'));
const result = web3.eth.abi.decodeParameters(functionParameters, resultHex);
return result;
}
/**
* Helper function to encode a function call
* @param {string} functionName name of function to call
* @param {string[]} parameters array of parameters to the function call - typically empty
* @returns {Buffer} function call encoded as a Unit8 Array ready to send
*/
function encodeFunctionCall(functionName, parameters) {
const functionAbi = abi.find((func) => func.name === functionName && func.type === 'function');
const encodedParametersHex = web3.eth.abi.encodeFunctionCall(functionAbi, parameters).slice(2);
return Buffer.from(encodedParametersHex, 'hex');
}
/**
* Helper method to search command line arguments for a given flag eg -t 0.0.XXX
* to get token 0.0.XXX as a string
* @param {string} arg the argument to look for
* @returns {string} argument following the flag
*/
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;
}
/**
* Helper method to search command line arguments for a given flag eg -h as a help switch
* @param {string} arg the argument to look for
* @returns {boolean} flag found
*/
function getArgFlag(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
if (customIndex > -1) {
return true;
}
return false;
}
main();