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

Commit

Permalink
Merge branch 'master' into hotfix/307-subdomains
Browse files Browse the repository at this point in the history
# Conflicts:
#	commands/create-account.js
  • Loading branch information
mikedotexe committed Apr 25, 2020
2 parents 8d39834 + 022f9c2 commit 9e217cc
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 79 deletions.
15 changes: 2 additions & 13 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,11 @@ const callViewFunction = {
handler: exitOnError(main.callViewFunction)
};

const { spawn } = require('child_process');

const build = {
command: 'build',
desc: 'build your smart contract',
handler: () => {
const gulp = spawn('gulp', [], {shell: process.platform == 'win32'});
gulp.stdout.on('data', function (data) {
console.log(data.toString());
});
gulp.stderr.on('data', function (data) {
console.log(data.toString());
});
gulp.on('exit', function (code) {
process.exit(code);
});
}
handler: exitOnError(main.build)
};

const clean = {
Expand Down
3 changes: 3 additions & 0 deletions commands/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { utils } = nearlib;
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const eventtracking = require('../utils/eventtracking');

module.exports = {
command: 'call <contractName> <methodName> [args]',
Expand All @@ -22,6 +23,7 @@ module.exports = {
};

async function scheduleFunctionCall(options) {
await eventtracking.track(eventtracking.EVENT_ID_SCHEDULE_FN_CALL_START, { node: options.nodeUrl });
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);
Expand All @@ -34,4 +36,5 @@ async function scheduleFunctionCall(options) {
utils.format.parseNearAmount(options.amount));
const result = nearlib.providers.getTransactionLastResult(functionCallResponse);
console.log(inspectResponse(result));
await eventtracking.track(eventtracking.EVENT_ID_SCHEDULE_FN_CALL_END, { node: options.nodeUrl, success: true });
}
3 changes: 3 additions & 0 deletions commands/create-account.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const { KeyPair } = require('near-api-js');
const eventtracking = require('../utils/eventtracking');
const NEAR_ENV_SUFFIXES = {
production: 'near',
default: 'test',
Expand Down Expand Up @@ -38,6 +39,7 @@ module.exports = {
};

async function createAccount(options) {
await eventtracking.track(eventtracking.EVENT_ID_CREATE_ACCOUNT_START, { nodeUrl: options.nodeUrl });
// NOTE: initialBalance is passed as part of config here, parsed in middleware/initial-balance
// periods are disallowed in top-level accounts and can only be used for subaccounts
const splitAccount = options.accountId.split('.');
Expand Down Expand Up @@ -86,4 +88,5 @@ async function createAccount(options) {
await near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair);
}
console.log(`Account ${options.accountId} for network "${options.networkId}" was created.`);
await eventtracking.track(eventtracking.EVENT_ID_CREATE_ACCOUNT_END, { node: options.nodeUrl, success: true });
}
5 changes: 4 additions & 1 deletion commands/dev-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ const { KeyPair } = require('near-api-js');
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const { readFile, writeFile } = require('fs').promises;
const eventtracking = require('../utils/eventtracking');

module.exports = {
command: 'dev-deploy [wasmFile]',
desc: 'deploy your smart contract using temporary account (TestNet only)',
builder: (yargs) => yargs
.option('wasmFile',{
.option('wasmFile', {
desc: 'Path to wasm file to deploy',
type: 'string',
default: './out/main.wasm'
Expand All @@ -29,6 +30,7 @@ module.exports = {
};

async function devDeploy(options) {
await eventtracking.track(eventtracking.EVENT_ID_DEV_DEPLOY_START, { node: options.nodeUrl });
const { nodeUrl, helperUrl, masterAccount, wasmFile } = options;

if (!helperUrl && !masterAccount) {
Expand All @@ -43,6 +45,7 @@ async function devDeploy(options) {
const account = await near.account(accountId);
await account.deployContract(contractData);
console.log(`Done deploying to ${accountId}`);
await eventtracking.track(eventtracking.EVENT_ID_DEV_DEPLOY_END, { node: options.nodeUrl, success: true });
}

async function createDevAccountIfNeeded({ near, keyStore, networkId, init, masterAccount }) {
Expand Down
5 changes: 4 additions & 1 deletion commands/generate-key.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const KeyPair = require('near-api-js').KeyPair;
const exitOnError = require('../utils/exit-on-error');
const eventtracking = require('../utils/eventtracking');

module.exports = {
command: 'generate-key <account-id>',
desc: 'generate key ',
builder: (yargs) => yargs,
handler: exitOnError(async (argv) => {
await eventtracking.track(eventtracking.EVENT_ID_GENERATE_KEY_START, { network: argv.networkId });
let near = await require('../utils/connect')(argv);
if (argv.accountId) {
const { deps: { keyStore }} = near.config;
const { deps: { keyStore } } = near.config;
const existingKey = await keyStore.getKey(argv.networkId, argv.accountId);
if (existingKey) {
console.log(`Account has existing key pair with ${existingKey.publicKey} public key`);
Expand All @@ -18,5 +20,6 @@ module.exports = {
console.log(`Generated key pair with ${keyPair.publicKey} public key`);
}
}
await eventtracking.track(eventtracking.EVENT_ID_GENERATE_KEY_END, { network: argv.networkId, success: true });
})
};
3 changes: 3 additions & 0 deletions commands/tx-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const bs58 = require('bs58');
const eventtracking = require('../utils/eventtracking');

module.exports = {
command: 'tx-status <hash>',
Expand All @@ -13,6 +14,7 @@ module.exports = {
required: true
}),
handler: exitOnError(async (argv) => {
await eventtracking.track(eventtracking.EVENT_ID_TX_STATUS_START, { node: argv.nodeUrl });
const near = await connect(argv);

const hashParts = argv.hash.split(':');
Expand All @@ -33,5 +35,6 @@ module.exports = {
const status = await near.connection.provider.txStatus(bs58.decode(hash), accountId);
console.log(`Transaction ${accountId}:${hash}`);
console.log(inspectResponse(status));
await eventtracking.track(eventtracking.EVENT_ID_TX_STATUS_END, { node: argv.nodeUrl, success: true });
})
};
67 changes: 50 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,54 @@ const readline = require('readline');
const URL = require('url').URL;
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 connect = require('./utils/connect');
const verify = require('./utils/verify-account');
const capture = require('./utils/capture-login-success');

const inspectResponse = require('./utils/inspect-response');
const eventtracking = require('./utils/eventtracking');

// TODO: Fix promisified wrappers to handle error properly

// For smart contract:
exports.clean = async function() {
exports.clean = async function () {
await eventtracking.track(eventtracking.EVENT_ID_CLEAN_START, {});
const rmDirFn = () => {
return new Promise(resolve => {
rimraf(yargs.argv.outDir, response => resolve(response));
});};
});
};
await rmDirFn();
console.log('Clean complete.');
await eventtracking.track(eventtracking.EVENT_ID_CLEAN_END, { success: true });
};

exports.deploy = async function(options) {
exports.deploy = async function (options) {
await eventtracking.track(eventtracking.EVENT_ID_DEPLOY_START, { node: options.nodeUrl });
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);
await eventtracking.track(eventtracking.EVENT_ID_DEPLOY_END, { node: options.nodeUrl, success: true });
};

exports.callViewFunction = async function(options) {
exports.callViewFunction = async function (options) {
await eventtracking.track(eventtracking.EVENT_ID_CALL_VIEW_FN_START, { node: options.nodeUrl });
console.log(`View call: ${options.contractName}.${options.methodName}(${options.args || ''})`);
const near = await connect(options);
// TODO: Figure out how to run readonly calls without account
const account = await near.account(options.accountId || options.masterAccount || 'register.near');
console.log(inspectResponse(await account.viewFunction(options.contractName, options.methodName, JSON.parse(options.args || '{}'))));
await eventtracking.track(eventtracking.EVENT_ID_CALL_VIEW_FN_END, { node: options.nodeUrl, success: true });
};

// For account:

exports.login = async function(options) {
exports.login = async function (options) {
await eventtracking.track(eventtracking.EVENT_ID_LOGIN_START, { node: options.nodeUrl });
if (!options.walletUrl) {
console.log('Log in is not needed on this environment. Please use appropriate master account for shell operations.');
} else {
Expand Down Expand Up @@ -100,21 +108,20 @@ exports.login = async function(options) {
input: process.stdin,
output: process.stdout
});

const getAccountFromConsole = async () => {
return await new Promise((resolve) => {
rl.question(
chalk`Please authorize at least one account at the URL above.\n\n` +
chalk`Which account did you authorize for use with NEAR Shell?\n` +
chalk`Which account did you authorize for use with NEAR Shell?\n` +
chalk`{bold Enter it here (if not redirected automatically):}\n`, async (accountId) => {
resolve(accountId);
resolve(accountId);
});
});
};

let accountId;
if (!tempUrl) {
accountId = await getAccountFromConsole();
accountId = await getAccountFromConsole();
} else {
accountId = await new Promise((resolve, reject) => {
let resolved = false;
Expand All @@ -128,17 +135,18 @@ exports.login = async function(options) {
}
rl.close();
capture.cancel();

// verify the accountId if we captured it or ...
try {
await verify(accountId, keyPair, options);
} catch (error) {
console.error('Failed to verify accountId.', error.message);
}
}
await eventtracking.track(eventtracking.EVENT_ID_LOGIN_END, { node: options.nodeUrl, success: true });
};

exports.viewAccount = async function(options) {
exports.viewAccount = async function (options) {
await eventtracking.track(eventtracking.EVENT_ID_ACCOUNT_STATE_START, { node: options.nodeUrl });
let near = await connect(options);
let account = await near.account(options.accountId);
let state = await account.state();
Expand All @@ -147,36 +155,61 @@ exports.viewAccount = async function(options) {
}
console.log(`Account ${options.accountId}`);
console.log(inspectResponse(state));
await eventtracking.track(eventtracking.EVENT_ID_ACCOUNT_STATE_END, { node: options.nodeUrl, success: true });
};

exports.deleteAccount = async function(options) {
exports.deleteAccount = async function (options) {
await eventtracking.track(eventtracking.EVENT_ID_DELETE_ACCOUNT_START, { node: options.nodeUrl });

console.log(
`Deleting account. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, beneficiary: ${options.beneficiaryId}`);
const near = await connect(options);
const account = await near.account(options.accountId);
await account.deleteAccount(options.beneficiaryId);
console.log(`Account ${options.accountId} for network "${options.networkId}" was deleted.`);
await eventtracking.track(eventtracking.EVENT_ID_DELETE_ACCOUNT_END, { node: options.nodeUrl, success: true });
};

exports.keys = async function(options) {
exports.keys = async function (options) {
await eventtracking.track(eventtracking.EVENT_ID_ACCOUNT_KEYS_START, { node: options.nodeUrl });
let near = await connect(options);
let account = await near.account(options.accountId);
let accessKeys = await account.getAccessKeys();
console.log(`Keys for account ${options.accountId}`);
console.log(inspectResponse(accessKeys));
await eventtracking.track(eventtracking.EVENT_ID_ACCOUNT_KEYS_END, { node: options.nodeUrl, success: true });
};

exports.sendMoney = async function(options) {
exports.sendMoney = async function (options) {
await eventtracking.track(eventtracking.EVENT_ID_SEND_TOKENS_START, { node: options.nodeUrl, amount: options.amount });
console.log(`Sending ${options.amount} NEAR to ${options.receiver} from ${options.sender}`);
const near = await connect(options);
const account = await near.account(options.sender);
console.log(inspectResponse(await account.sendMoney(options.receiver, utils.format.parseNearAmount(options.amount))));
await eventtracking.track(eventtracking.EVENT_ID_SEND_TOKENS_END, { node: options.nodeUrl, success: true });
};

exports.stake = async function(options) {
exports.stake = async function (options) {
await eventtracking.track(eventtracking.EVENT_ID_STAKE_START, { node: options.nodeUrl, amount: options.amount });
console.log(`Staking ${options.amount} (${utils.format.parseNearAmount(options.amount)}) on ${options.accountId} with public key = ${options.stakingKey}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
const result = await account.stake(options.stakingKey, utils.format.parseNearAmount(options.amount));
console.log(inspectResponse(result));
await eventtracking.track(eventtracking.EVENT_ID_STAKE_END, { node: options.nodeUrl, success: true });
};

exports.build = async function () {
await eventtracking.track(eventtracking.EVENT_ID_BUILD_START, {});
const gulp = spawn('gulp', [], { shell: process.platform == 'win32' });
gulp.stdout.on('data', function (data) {
console.log(data.toString());
});
gulp.stderr.on('data', function (data) {
console.log(data.toString());
});
gulp.on('exit', function (code) {
process.exit(code);
});
await eventtracking.track(eventtracking.EVENT_ID_BUILD_END, { success: true });
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "near-shell",
"version": "0.20.8",
"version": "0.20.9",
"description": "General purpose command line tools for interacting with NEAR Protocol",
"engines": {
"node": ">= 10"
Expand Down Expand Up @@ -37,13 +37,15 @@
"flagged-respawn": "^1.0.1",
"is-ci": "^2.0.0",
"jest-environment-node": "^25.1.0",
"mixpanel": "^0.11.0",
"ncp": "^2.0.0",
"near-api-js": "^0.23.1",
"open": "^7.0.1",
"rimraf": "^3.0.0",
"stoppable": "^1.1.0",
"tcp-port-used": "^1.0.1",
"update-notifier": "^4.0.0",
"uuid": "^7.0.3",
"v8flags": "^3.1.3",
"yargs": "^15.0.1"
},
Expand Down
5 changes: 4 additions & 1 deletion test/index.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/bin/bash
export NODE_ENV=${NODE_ENV:-test}
OVERALL_RESULT=0
mkdir ~/.near-config
echo '{"trackingEnabled":false}' > ~/.near-config/settings.json
for test in ./test/test_*; do
echo ""
echo "Running $test"
"$test"
if [ $? -ne 0 ]; then
if [ $? -ne 0 ]; then
echo "$test FAIL"
OVERALL_RESULT=1
else
Expand Down
2 changes: 1 addition & 1 deletion test_environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const NodeEnvironment = require('jest-environment-node');
const nearlib = require('near-api-js');
const fs = require('fs');

const INITIAL_BALANCE = '100000000000';
const INITIAL_BALANCE = '500000000000000000000000000';
const testAccountName = 'test.near';

class LocalTestEnvironment extends NodeEnvironment {
Expand Down
Loading

0 comments on commit 9e217cc

Please sign in to comment.