forked from near/near-api-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculate-gas.js
65 lines (57 loc) · 2.23 KB
/
calculate-gas.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
const { connect, keyStores, utils } = require("near-api-js");
const path = require("path");
const homedir = require("os").homedir();
const chalk = require("chalk");
const CREDENTIALS_DIR = ".near-credentials";
const ACCOUNT_ID = "near-example.testnet";
const CONTRACT_ID = "guest-book.testnet";
const METHOD_NAME = "addMessage";
const MAX_GAS = "300000000000000";
const ATTACHED_DEPOSIT = "0";
const args = {
text: "Howdy!",
};
const credentialsPath = path.join(homedir, CREDENTIALS_DIR);
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);
const config = {
keyStore,
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
};
calculateGas(CONTRACT_ID, METHOD_NAME, args, ATTACHED_DEPOSIT);
async function calculateGas(contractId, methodName, args, depositAmount) {
const near = await connect(config);
const account = await near.account(ACCOUNT_ID);
const result = await account.functionCall({
contractId,
methodName,
args,
gas: MAX_GAS,
attachedDeposit: utils.format.parseNearAmount(depositAmount),
});
const { totalGasBurned, totalTokensBurned } = result.receipts_outcome.reduce(
(acc, receipt) => {
acc.totalGasBurned += receipt.outcome.gas_burnt;
acc.totalTokensBurned += utils.format.formatNearAmount(
receipt.outcome.tokens_burnt
);
return acc;
},
{
totalGasBurned: result.transaction_outcome.outcome.gas_burnt,
totalTokensBurned: utils.format.formatNearAmount(
result.transaction_outcome.outcome.tokens_burnt
),
}
);
console.log(chalk`{white ------------------------------------------------------------------------ }`)
console.log(chalk`{bold.green RESULTS} {white for: [ {bold.blue ${METHOD_NAME}} ] called on contract: [ {bold.blue ${CONTRACT_ID}} ]}` )
console.log(chalk`{white ------------------------------------------------------------------------ }`)
console.log(chalk`{bold.white Gas Burnt} {white |} {bold.yellow ${totalGasBurned}}`);
console.log(chalk`{bold.white Tokens Burnt} {white |} {bold.yellow ${totalTokensBurned}}`);
console.log(chalk`{white ------------------------------------------------------------------------ }`)
return {
totalTokensBurned,
totalGasBurned,
};
}