Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
deploy: add initFunction, initArgs, initGas, initDeposit flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedotexe committed Jul 10, 2020
1 parent e22f761 commit 022879b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
21 changes: 20 additions & 1 deletion bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,33 @@ const stake = {

// For contract:
const deploy = {
command: 'deploy',
command: 'deploy [accountId] [wasmFile] [initFunction] [initArgs] [initGas] [initDeposit]',
// command: 'deploy',
desc: 'deploy your smart contract',
builder: (yargs) => yargs
.option('wasmFile', {
desc: 'Path to wasm file to deploy',
type: 'string',
default: './out/main.wasm'
})
.option('initFunction', {
desc: 'Initialization method',
type: 'string',
default: 'new'
})
.option('initArgs', {
desc: 'Initialization arguments',
})
.option('initGas', {
desc: 'Gas for initialization call',
type: 'number',
default: 10000000000000
})
.option('initDeposit', {
desc: 'Deposit in Ⓝ to send for initialization call',
type: 'string',
default: '0'
})
.alias({
'accountId': ['account_id', 'contractName', 'contract_name'],
}),
Expand Down
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const qs = require('querystring');
const chalk = require('chalk'); // colorize output
const open = require('open'); // open URL in default browser
const { spawn } = require('child_process');
const { KeyPair, utils } = require('near-api-js');
const { KeyPair, utils, transactions } = require('near-api-js');

const connect = require('./utils/connect');
const verify = require('./utils/verify-account');
Expand All @@ -34,10 +34,22 @@ exports.clean = async function () {
exports.deploy = async function (options) {
console.log(
`Starting deployment. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, file: ${options.wasmFile}`);

const near = await connect(options);
const contractData = [...fs.readFileSync(options.wasmFile)];
const account = await near.account(options.accountId);
await account.deployContract(contractData);
if (options.initFunction) {
// Deploy with init function and args
await account.signAndSendTransaction(options.accountId,
[
transactions.deployContract(fs.readFileSync(options.wasmFile)),
transactions.functionCall(options.initFunction, Buffer.from(options.initArgs), options.initGas, options.initDeposit),
]
);
} else {
// Normal deploy without initialization
await account.deployContract(contractData);
}
};

exports.callViewFunction = async function (options) {
Expand Down

0 comments on commit 022879b

Please sign in to comment.