-
Notifications
You must be signed in to change notification settings - Fork 244
/
calculate-gas.ts
64 lines (58 loc) · 2.25 KB
/
calculate-gas.ts
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
import {
formatNearAmount,
functionCall,
getSignerFromKeystore,
getTestnetRpcProvider,
MAX_GAS,
} from '@near-js/client';
import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node';
import chalk from 'chalk';
import { join } from 'node:path';
import { homedir } from 'node:os';
const CONTRACT_ID = "guest-book.testnet";
const METHOD_NAME = "addMessage";
export default async function calculateGas(accountId: string, method = METHOD_NAME, contract = CONTRACT_ID) {
if (!accountId) {
console.log(chalk`{red pnpm calculateGas -- ACCOUNT_ID}`);
return;
}
// initialize testnet RPC provider
const rpcProvider = getTestnetRpcProvider();
// initialize the transaction signer using a pre-existing key for `accountId`
const signer = getSignerFromKeystore(accountId, 'testnet', new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials')));
const {
outcome: {
receipts_outcome: receiptsOutcome,
transaction_outcome: { outcome: transactionOutcome },
},
} = await functionCall({
sender: accountId,
receiver: contract,
method,
args: {
text: 'Howdy!',
},
gas: MAX_GAS,
deps: {
rpcProvider,
signer,
},
});
const { totalGasBurned, totalTokensBurned } = receiptsOutcome.reduce(
(acc, receipt) => {
acc.totalGasBurned += receipt.outcome.gas_burnt;
acc.totalTokensBurned += BigInt(receipt.outcome.tokens_burnt);
return acc;
},
{
totalGasBurned: transactionOutcome.gas_burnt,
totalTokensBurned: BigInt(transactionOutcome.tokens_burnt),
}
);
console.log(chalk`{white ------------------------------------------------------------------------ }`);
console.log(chalk`{bold.green RESULTS} {white for: [ {bold.blue ${method}} ] called on contract: [ {bold.blue ${contract}} ]}` );
console.log(chalk`{white ------------------------------------------------------------------------ }`);
console.log(chalk`{bold.white Gas Burnt} {white |} {bold.yellow ${formatNearAmount(totalGasBurned.toString())}}`);
console.log(chalk`{bold.white Tokens Burnt} {white |} {bold.yellow ${formatNearAmount(totalTokensBurned.toString())}}`);
console.log(chalk`{white ------------------------------------------------------------------------ }`);
}