Skip to content

Commit

Permalink
Allow easily formatting input for attonear with symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
janedegtiareva committed Nov 23, 2019
1 parent 4642659 commit 158e853
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
9 changes: 9 additions & 0 deletions __tests__/unittest.js
Expand Up @@ -16,3 +16,12 @@ test('formatting attonear amounts', async() => {
expect(format.prettyPrintNearAmount('99999')).toEqual('99999 attonear'); // do not format smaller values
expect(format.prettyPrintNearAmount('8999999999837087887000')).toEqual('~9000E attonear');
});

test('parseInputAmount', async() => {
expect(format.parseInputAmount("5K ")).toEqual(5000);
expect(format.parseInputAmount("6 M ")).toEqual(6000000);
expect(format.parseInputAmount("1.5 K ")).toEqual(1500);
expect(format.parseInputAmount('9000E')).toEqual(9000000000000000000000);
expect(format.parseInputAmount('8999999999837087887000')).toEqual('8999999999837087887000');
expect(format.parseInputAmount(null)).toEqual(null);
});
14 changes: 9 additions & 5 deletions index.js
Expand Up @@ -40,7 +40,11 @@ exports.scheduleFunctionCall = async function(options) {
(options.amount ? ` with attached ${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 || '{}'),
format.parseInputAmount(options.amount));
const result = nearjs.providers.getTransactionLastResult(functionCallResponse);
console.log(inspectResponse(result));
};
Expand Down Expand Up @@ -138,16 +142,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} (${format.parseInputAmount(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, format.parseInputAmount(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} (${format.parseInputAmount(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, format.parseInputAmount(options.amount));
console.log(inspectResponse(result));
};
22 changes: 21 additions & 1 deletion utils/formatting-utils.js
Expand Up @@ -8,6 +8,15 @@
*/
const symbols = ['', 'K', 'M', 'G', 'T', 'P', 'E'];

const symbolsToExponent = {
K: 1000,
M: 1000 * 1000,
G: 1000 * 1000 * 1000,
T: 1000 * 1000 * 1000 * 1000,
P: 1000 * 1000 * 1000 * 1000 * 1000,
E: 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
}

function prettyPrintNearAmount(amt) {
if (amt <= 99999) {
return `${amt} attonear`;
Expand All @@ -21,4 +30,15 @@ function prettyPrintNearAmount(amt) {
return `~${Math.round(amt)}${symbols[i]} attonear`;
}

module.exports = { prettyPrintNearAmount };
function parseInputAmount(amt) {
if (!amt) { return amt; }
amt = amt.trim();
const symbol = amt[amt.length - 1];
if (symbolsToExponent[symbol]) {
amt = amt.substring(0, amt.length - 1).trim();
amt = amt * symbolsToExponent[symbol];
}
return amt;
}

module.exports = { prettyPrintNearAmount, parseInputAmount };

0 comments on commit 158e853

Please sign in to comment.