-
Notifications
You must be signed in to change notification settings - Fork 92
/
call.js
44 lines (42 loc) · 1.69 KB
/
call.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
const { providers, utils } = require('near-api-js');
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
module.exports = {
command: 'call <contractName> <methodName> [args]',
desc: 'schedule smart contract call which can modify state',
builder: (yargs) => yargs
.option('gas', {
desc: 'Max amount of gas this call can use',
type: 'string',
default: '100000000000000'
})
.option('amount', {
desc: 'Number of tokens to attach',
type: 'string',
default: '0'
})
.option('args', {
desc: 'Arguments to the contract call, in JSON format (e.g. \'{"param_a": "value"}\')',
type: 'string',
default: null
})
.option('accountId', {
required: true,
}),
handler: exitOnError(scheduleFunctionCall)
};
async function scheduleFunctionCall(options) {
console.log(`Scheduling a call: ${options.contractName}.${options.methodName}(${options.args || ''})` +
(options.amount && options.amount != '0' ? ` with attached ${options.amount} NEAR` : ''));
const near = await connect(options);
const account = await near.account(options.accountId);
const functionCallResponse = await account.functionCall(
options.contractName,
options.methodName,
JSON.parse(options.args || '{}'),
options.gas,
utils.format.parseNearAmount(options.amount));
const result = providers.getTransactionLastResult(functionCallResponse);
inspectResponse.prettyPrintResponse(result, options);
}