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

Commit

Permalink
Add a util for explorer functionality + unit tests (#448)
Browse files Browse the repository at this point in the history
* Add a util for explorer functionality + unit tests
t

* Print the explorer url for sendMoney

* Nit: fix lint errors

* Fix dangling inspectResponse reference in call.js

* Undo a change to .eslintrc.yml

* Tweaks to shell output on near send
  • Loading branch information
janedegtiareva authored Jul 10, 2020
1 parent baa62ea commit 0913ec5
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 12 deletions.
5 changes: 5 additions & 0 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ yargs // eslint-disable-line
desc: 'Expected top-level account for a network',
type: 'string'
})
.option('explorerUrl', {
hidden: true,
desc: 'Base url for explorer',
type: 'string',
})
.option('verbose', {
desc: 'Prints out verbose output',
type: 'boolean',
Expand Down
2 changes: 1 addition & 1 deletion commands/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ async function scheduleFunctionCall(options) {
options.gas,
utils.format.parseNearAmount(options.amount));
const result = providers.getTransactionLastResult(functionCallResponse);
console.log(inspectResponse(result));
console.log(inspectResponse.prettyPrintResponse(result));
}
2 changes: 1 addition & 1 deletion commands/delete-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ async function deleteAccessKey(options) {
const near = await connect(options);
const account = await near.account(options.accountId);
const result = await account.deleteKey(options.accessKey);
console.log(inspectResponse(result));
console.log(inspectResponse.prettyPrintResponse(result));
}
2 changes: 1 addition & 1 deletion commands/tx-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {

const status = await near.connection.provider.txStatus(bs58.decode(hash), accountId);
console.log(`Transaction ${accountId}:${hash}`);
console.log(inspectResponse(status));
console.log(inspectResponse.prettyPrintResponse(status));

})
};
4 changes: 4 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function getConfig(env) {
walletUrl: 'https://wallet.near.org',
helperUrl: 'https://helper.mainnet.near.org',
helperAccount: 'near',
explorerUrl: 'https://explorer.mainnet.near.org',
};
case 'development':
case 'testnet':
Expand All @@ -22,6 +23,7 @@ function getConfig(env) {
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org',
helperAccount: 'testnet',
explorerUrl: 'https://explorer.testnet.near.org',
};
case 'devnet':
return {
Expand All @@ -31,6 +33,7 @@ function getConfig(env) {
walletUrl: 'https://wallet.devnet.near.org',
helperUrl: 'https://helper.devnet.near.org',
helperAccount: 'devnet',
explorerUrl: 'https://explorer.devnet.near.org/',
};
case 'betanet':
return {
Expand All @@ -40,6 +43,7 @@ function getConfig(env) {
walletUrl: 'https://wallet.betanet.near.org',
helperUrl: 'https://helper.betanet.near.org',
helperAccount: 'betanet',
explorerUrl: 'https://explorer.testnet.near.org',
};
case 'local':
return {
Expand Down
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const capture = require('./utils/capture-login-success');

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

// TODO: Fix promisified wrappers to handle error properly

Expand Down Expand Up @@ -43,7 +44,7 @@ exports.callViewFunction = async function (options) {
console.log(`View call: ${options.contractName}.${options.methodName}(${options.args || ''})`);
const near = await connect(options);
const account = await near.account(options.accountId || options.masterAccount || options.contractName);
console.log(inspectResponse(await account.viewFunction(options.contractName, options.methodName, JSON.parse(options.args || '{}'))));
console.log(inspectResponse.prettyPrintResponse(await account.viewFunction(options.contractName, options.methodName, JSON.parse(options.args || '{}'))));
};

// open a given URL in browser in a safe way.
Expand Down Expand Up @@ -158,7 +159,7 @@ exports.viewAccount = async function (options) {
state['formattedAmount'] = utils.format.formatNearAmount(state.amount);
}
console.log(`Account ${options.accountId}`);
console.log(inspectResponse(state));
console.log(inspectResponse.prettyPrintResponse(state));
};

exports.deleteAccount = async function (options) {
Expand All @@ -176,20 +177,26 @@ exports.keys = async function (options) {
let account = await near.account(options.accountId);
let accessKeys = await account.getAccessKeys();
console.log(`Keys for account ${options.accountId}`);
console.log(inspectResponse(accessKeys));
console.log(inspectResponse.prettyPrintResponse(accessKeys));
};

exports.sendMoney = async function (options) {
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))));
const result = await account.sendMoney(options.receiver, utils.format.parseNearAmount(options.amount));
if (options.verbose) {
console.log(inspectResponse.prettyPrintResponse(result));
}
const txnId = inspectResponse.getTxnId(result);
console.log(`Transaction Id ${txnId}`);
explorer.printTransactionUrl(txnId, options);
};

exports.stake = async function (options) {
console.log(`Staking ${options.amount} (${utils.format.parseNearAmount(options.amount)}) on ${options.accountId} with public key = ${qs.unescape(options.stakingKey)}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
const result = await account.stake(qs.unescape(options.stakingKey), utils.format.parseNearAmount(options.amount));
console.log(inspectResponse(result));
console.log(inspectResponse.prettyPrintResponse(result));
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"main": "index.js",
"scripts": {
"test": "./test/index.sh",
"test": "jest && ./test/index.sh",
"lint": "eslint .",
"fix": "eslint . --fix"
},
Expand All @@ -26,7 +26,7 @@
},
"devDependencies": {
"eslint": "^7.0.0",
"jest": "^26.0.1",
"jest": "^26.1.0",
"strip-ansi": "^6.0.0",
"strip-ansi-cli": "^2.0.0"
},
Expand Down
27 changes: 27 additions & 0 deletions test/unit/explorer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

const explorer = require('../../utils/explorer');

describe('generate explorer link', () => {
test('on environment with a known url', async () => {
const config = require('../../config')('development');
const url = explorer.generateTransactionUrl('61Uc5f7L42SDWFPYHx7goMc2xEN7YN4fgtw9baHA82hY', config);
expect(url).toEqual('https://explorer.testnet.near.org/transactions/61Uc5f7L42SDWFPYHx7goMc2xEN7YN4fgtw9baHA82hY');
});

test('on environment with an unknown url', async () => {
const config = require('../../config')('ci');
const url = explorer.generateTransactionUrl('61Uc5f7L42SDWFPYHx7goMc2xEN7YN4fgtw9baHA82hY', config);
expect(url).toEqual(null);
});

test('unknown txn id', async () => {
const config = require('../../config')('development');
const url = explorer.generateTransactionUrl(null, config);
expect(url).toEqual(null);
});

test('null options', async () => {
const url = explorer.generateTransactionUrl('61Uc5f7L42SDWFPYHx7goMc2xEN7YN4fgtw9baHA82hY', null);
expect(url).toEqual(null);
});
});
20 changes: 20 additions & 0 deletions test/unit/inspect-response.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const inspectResponse = require('../../utils/inspect-response');

describe('getTxnId', () => {
test('with expected data format', async () => {
const data = {
transaction: {
hash: 'BF1iyVWTkagisho3JKKUXiPQu2sMuuLsEbvQBDYHHoKE'
}
};
expect(inspectResponse.getTxnId(data)).toEqual('BF1iyVWTkagisho3JKKUXiPQu2sMuuLsEbvQBDYHHoKE');
});

test('with null response', async () => {
expect(inspectResponse.getTxnId(null)).toEqual(null);
});

test('with null transaction inside response', async () => {
expect(inspectResponse.getTxnId({})).toEqual(null);
});
});
22 changes: 22 additions & 0 deletions utils/explorer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Handle functionality related to explorer

const generateTransactionUrl = (txnId, options) => {
if (!txnId || !options) {
return null;
}
const explorerUrl = options.explorerUrl;
return explorerUrl ? `${explorerUrl}/transactions/${txnId}` : null;
};

const printTransactionUrl = (txnId, options) => {
const txnUrl = generateTransactionUrl(txnId, options);
if (txnUrl) {
console.log('To see the transaction in the transaction explorer, please open this url in your browser');
console.log(txnUrl);
}
};

module.exports = {
generateTransactionUrl,
printTransactionUrl,
};
21 changes: 20 additions & 1 deletion utils/inspect-response.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@

const util = require('util');
module.exports = (response) => {
const prettyPrintResponse = (response) => {
return util.inspect(response, { showHidden: true, depth: null, colors: true, maxArrayLength: null });
};

const getTxnId = (response) => {
// Currently supported response format:
//{
// ...
// transaction: {
// ...
// hash: 'BF1iyVWTkagisho3JKKUXiPQu2sMuuLsEbvQBDYHHoKE'
// },
if (!response || !response.transaction) {
return null;
}
return response.transaction.hash;
};

module.exports = {
prettyPrintResponse,
getTxnId,
};
2 changes: 1 addition & 1 deletion yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0913ec5

Please sign in to comment.