From de1dbb7dee769b7a8b30a2361a25534b14338cb9 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Sun, 29 Dec 2019 22:05:49 -0800 Subject: [PATCH 1/3] Move `near call` command into separate file --- bin/near-cli.js | 14 +------------- commands/call.js | 31 +++++++++++++++++++++++++++++++ index.js | 15 --------------- 3 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 commands/call.js diff --git a/bin/near-cli.js b/bin/near-cli.js index a074d83c..244cce11 100644 --- a/bin/near-cli.js +++ b/bin/near-cli.js @@ -101,18 +101,6 @@ const deploy = { handler: exitOnError(main.deploy) }; -const scheduleFunctionCall = { - command: 'call [args]', - desc: 'schedule smart contract call which can modify state', - builder: (yargs) => yargs - .option('amount', { - desc: 'Number of tokens to attach', - type: 'string', - default: '0.0000000001' - }), - handler: exitOnError(main.scheduleFunctionCall) -}; - const callViewFunction = { command: 'view [args]', desc: 'make smart contract call which can view state', @@ -188,7 +176,7 @@ yargs // eslint-disable-line .command(require('../commands/tx-status')) .command(build) .command(deploy) - .command(scheduleFunctionCall) + .command(require('../commands/call')) .command(callViewFunction) .command(sendMoney) .command(clean) diff --git a/commands/call.js b/commands/call.js new file mode 100644 index 00000000..d8f161eb --- /dev/null +++ b/commands/call.js @@ -0,0 +1,31 @@ +const nearlib = require('nearlib'); +const { utils } = nearlib; +const exitOnError = require('../utils/exit-on-error'); +const connect = require('../utils/connect'); +const inspectResponse = require('../utils/inspect-response'); + +module.exports = { + command: 'call [args]', + desc: 'schedule smart contract call which can modify state', + builder: (yargs) => yargs + .option('amount', { + desc: 'Number of tokens to attach', + type: 'string', + default: '0.0000000001' + }), + handler: exitOnError(scheduleFunctionCall) +}; + +async function scheduleFunctionCall(options) { + console.log(`Scheduling a call: ${options.contractName}.${options.methodName}(${options.args || ''})` + + (options.amount ? ` with attached ${utils.format.parseNearAmount(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 || '{}'), + utils.format.parseNearAmount(options.amount)); + const result = nearlib.providers.getTransactionLastResult(functionCallResponse); + console.log(inspectResponse(result)); +}; diff --git a/index.js b/index.js index abaef73a..5fd5caf0 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,6 @@ const rimraf = require('rimraf'); const readline = require('readline'); const URL = require('url').URL; -const nearjs = require('nearlib'); const { KeyPair, keyStores, utils } = require('nearlib'); const UnencryptedFileSystemKeyStore = keyStores.UnencryptedFileSystemKeyStore; @@ -36,20 +35,6 @@ exports.deploy = async function(options) { await account.deployContract(contractData); }; -exports.scheduleFunctionCall = async function(options) { - console.log(`Scheduling a call: ${options.contractName}.${options.methodName}(${options.args || ''})` + - (options.amount ? ` with attached ${utils.format.parseNearAmount(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 || '{}'), - utils.format.parseNearAmount(options.amount)); - const result = nearjs.providers.getTransactionLastResult(functionCallResponse); - console.log(inspectResponse(result)); -}; - exports.callViewFunction = async function(options) { console.log(`View call: ${options.contractName}.${options.methodName}(${options.args || ''})`); const near = await connect(options); From 602e5538d361dbcfaa6e8583c42e16d5c817a54e Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Sun, 29 Dec 2019 22:17:46 -0800 Subject: [PATCH 2/3] Fix bug with `near call` passing --amount as gas --- commands/call.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/commands/call.js b/commands/call.js index d8f161eb..c33696eb 100644 --- a/commands/call.js +++ b/commands/call.js @@ -8,23 +8,29 @@ module.exports = { command: 'call [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.0000000001' + default: '0' }), handler: exitOnError(scheduleFunctionCall) }; async function scheduleFunctionCall(options) { console.log(`Scheduling a call: ${options.contractName}.${options.methodName}(${options.args || ''})` + - (options.amount ? ` with attached ${utils.format.parseNearAmount(options.amount)} NEAR` : '')); + (options.amount && options.amount != '0' ? ` with attached ${utils.format.parseNearAmount(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 = nearlib.providers.getTransactionLastResult(functionCallResponse); console.log(inspectResponse(result)); From 6d4707c3c6aeae04e611b3e7081e92441a76a941 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Sun, 29 Dec 2019 22:19:11 -0800 Subject: [PATCH 3/3] yarn fix --- commands/call.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/call.js b/commands/call.js index c33696eb..63af7b8e 100644 --- a/commands/call.js +++ b/commands/call.js @@ -34,4 +34,4 @@ async function scheduleFunctionCall(options) { utils.format.parseNearAmount(options.amount)); const result = nearlib.providers.getTransactionLastResult(functionCallResponse); console.log(inspectResponse(result)); -}; +}