Skip to content

Commit

Permalink
Merge pull request #201 from nearprotocol/near-tokens-formatting
Browse files Browse the repository at this point in the history
Add pretty-printed amount of attonear to near state
  • Loading branch information
janedegtiareva committed Dec 6, 2019
2 parents 020eaa3 + d8d32f5 commit abf1fcf
Show file tree
Hide file tree
Showing 6 changed files with 1,427 additions and 38 deletions.
4 changes: 2 additions & 2 deletions bin/near-cli.js
Expand Up @@ -25,7 +25,7 @@ const createAccount = {
.option('initialBalance', {
desc: 'Number of tokens to transfer to newly created account',
type: 'string',
default: '1000000000000000000'
default: '10'
}),
handler: exitOnError(main.createAccount)
};
Expand Down Expand Up @@ -130,7 +130,7 @@ const scheduleFunctionCall = {
.option('amount', {
desc: 'Number of tokens to attach',
type: 'string',
default: '1000000000'
default: '0.0000000001'
}),
handler: exitOnError(main.scheduleFunctionCall)
};
Expand Down
23 changes: 16 additions & 7 deletions index.js
Expand Up @@ -7,14 +7,16 @@ const readline = require('readline');
const URL = require('url').URL;

const nearjs = require('nearlib');
const { KeyPair, keyStores } = require('nearlib');
const { KeyPair, keyStores, utils } = require('nearlib');
const UnencryptedFileSystemKeyStore = keyStores.UnencryptedFileSystemKeyStore;

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

// TODO: Fix promisified wrappers to handle error properly



// For smart contract:
exports.clean = async function() {
const rmDirFn = () => {
Expand All @@ -36,10 +38,14 @@ exports.deploy = async function(options) {

exports.scheduleFunctionCall = async function(options) {
console.log(`Scheduling a call: ${options.contractName}.${options.methodName}(${options.args || ''})` +
(options.amount ? ` with attached ${options.amount} NEAR` : ''));
(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 || '{}'), options.amount);
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));
};
Expand Down Expand Up @@ -112,6 +118,9 @@ exports.viewAccount = async function(options) {
let near = await connect(options);
let account = await near.account(options.accountId);
let state = await account.state();
if (state && state.amount) {
state['formattedAmount'] = utils.format.formatNearAmount(state.amount);
}
console.log(`Account ${options.accountId}`);
console.log(inspectResponse(state));
};
Expand All @@ -134,16 +143,16 @@ exports.keys = async function(options) {
};

exports.sendMoney = async function(options) {
console.log(`Sending ${options.amount} NEAR to ${options.receiver} from ${options.sender}`);
console.log(`Sending ${options.amount} (${utils.format.parseNearAmount(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, options.amount)));
console.log(inspectResponse(await account.sendMoney(options.receiver, utils.format.parseNearAmount(options.amount))));
};

exports.stake = async function(options) {
console.log(`Staking ${options.amount} on ${options.accountId} with public key = ${options.stakingKey}.`);
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, options.amount);
const result = await account.stake(options.stakingKey, utils.format.parseNearAmount(options.amount));
console.log(inspectResponse(result));
};
7 changes: 5 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "near-shell",
"version": "0.14.2",
"version": "0.15.0",
"description": "Command line utilities to interact with near blockchain",
"engines": {
"node": ">= 10"
Expand All @@ -27,16 +27,19 @@
},
"devDependencies": {
"eslint": "^6.4.0",
"jest": "^24.1.0",
"strip-ansi": "^6.0.0",
"strip-ansi-cli": "^2.0.0"
},
"dependencies": {
"bn": "^1.0.5",
"bs58": "^4.0.1",
"chalk": "^3.0.0",
"is-ci": "^2.0.0",
"jest-environment-node": "^24.5.0",
"ncp": "^2.0.0",
"near-assemblyscript": "^0.7.4",
"nearlib": "^0.17.1",
"nearlib": "^0.18.0",
"rimraf": "^3.0.0",
"update-notifier": "^3.0.1",
"yargs": "^15.0.1"
Expand Down
2 changes: 1 addition & 1 deletion test/test_account_operations.sh
Expand Up @@ -11,7 +11,7 @@ echo Create account
echo Get account state
RESULT=$(../bin/near state $testaccount | strip-ansi)
echo $RESULT
EXPECTED=".+Account $testaccount.+amount:.+'1000000000000000000'.+ "
EXPECTED=".+Account $testaccount.+amount:.+'10'.+ "
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
echo FAILURE Unexpected output from near view
exit 1
Expand Down
29 changes: 29 additions & 0 deletions utils/formatting-utils.js
@@ -0,0 +1,29 @@
const BN = require ('bn.js');

const exp = 24;
const unitsInOneNear = new BN('10', 10).pow(new BN(exp, 10));

function prettyPrintNearAmount(amt) {
let amtBN = new BN(amt, 10);
if (amtBN.lte(unitsInOneNear)) {
return `0.${amt.padStart(exp, '0')} NEAR`;
}
return `${amtBN.div(unitsInOneNear).toString(10, 0)}.${amtBN.mod(unitsInOneNear).toString(10, 0)} NEAR`;
}

function parseInputAmount(amt) {
if (!amt) { return amt; }
amt = amt.trim();
let split = amt.split('.');
if (split.length == 1) {
return `${amt.padEnd(exp + 1, '0')}`;
}
if (split.length > 2 || split[1].length > exp) {
throw 'Invalid input format';
}
let wholePart = new BN(split[0], 10).mul(unitsInOneNear);
let fractionPart = new BN(split[1].padEnd(exp, '0'), 10);
return `${wholePart.add(fractionPart).toString(10, 0)}`;
}

module.exports = { prettyPrintNearAmount, parseInputAmount };

0 comments on commit abf1fcf

Please sign in to comment.