Skip to content

Commit

Permalink
Merge pull request #41 from nearprotocol/method-calls
Browse files Browse the repository at this point in the history
Support method calls in CLI
  • Loading branch information
vgrichina authored May 22, 2019
2 parents fe01c97 + 7d68d6e commit d63d0dc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
28 changes: 26 additions & 2 deletions bin/near
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,27 @@ const deploy = {
desc: 'Path to wasm file to deploy',
type: 'string',
default: './out/main.wasm'
})
.alias({
'accountId': ['account_id', 'contractName', 'contract_name'],
}),
handler: (argv) => exitOnError(main.deploy(argv))
};

const scheduleFunctionCall = {
command: 'call <contractName> <methodName> [args]',
desc: 'schedule smart contract call which can modify state',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.scheduleFunctionCall(argv))
};

const callViewFunction = {
command: 'view <contractName> <methodName> [args]',
desc: 'make smart contract call which can view state',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.callViewFunction(argv))
};

const { spawn } = require('child_process');
const build = {
command: 'build',
Expand Down Expand Up @@ -85,6 +102,11 @@ yargs // eslint-disable-line
type: 'string',
default: 'http://localhost:3030'
})
.option('networkId', {
desc: 'NEAR network ID, allows using different keys based on network',
type: 'string',
default: 'default'
})
.option('helperUrl', {
desc: 'NEAR contract helper URL',
type: 'string',
Expand All @@ -100,18 +122,20 @@ yargs // eslint-disable-line
.command(createAccount)
.command(build)
.command(deploy)
.command(scheduleFunctionCall)
.command(callViewFunction)
.command(clean)
.command(newProject)
.config(config)
.alias({
'accountId': ['account_id', 'contractName', 'contract_name'],
'accountId': ['account_id'],
'nodeUrl': 'node_url',
'networkId': ['network_id'],
'wasmFile': 'wasm_file',
'projectDir': 'project_dir',
'outDir': 'out_dir'
})
.showHelpOnFail(true)
.demandCommand(1, 'Please enter a command')
.strict()
.argv;

37 changes: 25 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,13 @@ exports.createDevAccount = async function(options) {

await neardev.connect(options);
await options.deps.createAccount(options.accountId, keyPair.getPublicKey());
const keyStore = new UnencryptedFileSystemKeyStore();
const keyStore = new UnencryptedFileSystemKeyStore('./', options.networkId);
keyStore.setKey(options.accountId, keyPair);
console.log("Create account complete.");
};

async function deployContractAndWaitForTransaction(accountId, data, near) {
const deployContractResult = await near.deployContract(accountId, data);
const waitResult = await near.waitForTransactionResult(deployContractResult);
return waitResult;
}

exports.deploy = async function(options) {
const keyStore = new UnencryptedFileSystemKeyStore();
async function connect(options) {
const keyStore = new UnencryptedFileSystemKeyStore('./', options.networkId);
if (!options.accountId) {
// see if we only have one account in keystore and just use that.
const accountIds = await keyStore.getAccountIds();
Expand All @@ -72,16 +66,35 @@ exports.deploy = async function(options) {
storage: {},
};

const near = await neardev.connect(options);
const contractData = [...fs.readFileSync(options.wasmFile)];
return await neardev.connect(options);
}

exports.deploy = async function(options) {
console.log(
`Starting deployment. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, file: ${options.wasmFile}`);
const res = await deployContractAndWaitForTransaction(options.accountId, contractData, near);
const near = await connect(options);
const contractData = [...fs.readFileSync(options.wasmFile)];
const res = await near.waitForTransactionResult(
await near.deployContract(options.accountId, contractData));
if (res.status == "Completed") {
console.log("Deployment succeeded.");
} else {
console.log("Deployment transaction did not succeed: ", res);
process.exit(1);
}
};

exports.scheduleFunctionCall = async function(options) {
console.log(`Scheduling a call: ${options.contractName}.${options.methodName}(${options.args || ''})` +
(options.amount ? ` with attached ${options.amount} NEAR` : ''));
const near = await connect(options);
console.log('Result:', await near.waitForTransactionResult(
await near.scheduleFunctionCall(options.amount, options.accountId,
options.contractName, options.methodName, JSON.parse(options.args || '{}'))));
};

exports.callViewFunction = async function(options) {
console.log(`View call: ${options.contractName}.${options.methodName}(${options.args || ''})`);
const near = await connect(options);
console.log('Result:', await near.callViewFunction(options.contractName, options.methodName, JSON.parse(options.args || '{}')));
};

0 comments on commit d63d0dc

Please sign in to comment.