Skip to content

Commit

Permalink
feat!: accept amount suffixed with "ae" instead of "denomination" option
Browse files Browse the repository at this point in the history
BREAKING CHANGE: "account spend" doesn't accept "denomination" parameter
  • Loading branch information
davidyuk committed Aug 13, 2022
1 parent a2de285 commit 70ceb67
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 45 deletions.
13 changes: 7 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"dependencies": {
"@aeternity/aepp-sdk": "^12.1.2",
"bignumber.js": "^9.1.0",
"commander": "^9.3.0",
"fs-extra": "^10.1.0",
"prompts": "^2.4.2"
Expand Down
6 changes: 3 additions & 3 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

import fs from 'fs-extra';
import { generateKeyPair, AE_AMOUNT_FORMATS } from '@aeternity/aepp-sdk';
import { generateKeyPair } from '@aeternity/aepp-sdk';
import CliError from '../utils/CliError';
import { writeWallet } from '../utils/account';
import { initSdkByWalletFile, getAccountByWalletFile } from '../utils/cli';
Expand Down Expand Up @@ -96,12 +96,12 @@ export async function sign(walletPath, tx, options) {
// this function allow you to `send` token's to another `account`
export async function spend(walletPath, receiverNameOrAddress, amount, options) {
const {
ttl, json, nonce, fee, payload = '', denomination = AE_AMOUNT_FORMATS.AETTOS,
ttl, json, nonce, fee, payload = '',
} = options;
const sdk = await initSdkByWalletFile(walletPath, options);

let tx = await sdk.spend(amount, receiverNameOrAddress, {
ttl, nonce, payload, fee, denomination,
ttl, nonce, payload, fee,
});
// if waitMined false
if (typeof tx !== 'object') {
Expand Down
8 changes: 8 additions & 0 deletions src/arguments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Argument, Option } from 'commander';
import BigNumber from 'bignumber.js';
import { NODE_URL } from './utils/constant';

export const coinAmountParser = (amount) => (
new BigNumber(amount.replace(/ae$/, '')).shiftedBy(amount.endsWith('ae') ? 18 : 0)
);

export const feeOption = new Option('-F, --fee [fee]', 'Override the transaction fee')
.argParser(coinAmountParser);

export const nonceArgument = new Argument('<nonce>', 'Unique number that is required to sign transaction securely')
.argParser((nonce) => +nonce);

Expand Down
14 changes: 8 additions & 6 deletions src/commands/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
*/
// We'll use `commander` for parsing options
import { Command } from 'commander';
import { AE_AMOUNT_FORMATS, TX_TTL } from '@aeternity/aepp-sdk';
import { TX_TTL } from '@aeternity/aepp-sdk';
import { getCmdFromArguments } from '../utils/cli';
import * as Account from '../actions/account';
import { nodeOption, jsonOption } from '../arguments';
import {
nodeOption, jsonOption, coinAmountParser, feeOption,
} from '../arguments';

const program = new Command().name('aecli account');

Expand All @@ -45,13 +47,13 @@ program
//
// Example: `aecli account spend ./myWalletKeyFile ak_1241rioefwj23f2wfdsfsdsdfsasdf 100 --password testpassword --ttl 20` --> this tx will leave for 20 blocks
program
.command('spend <wallet_path> <receiverIdOrName> <amount>')
.command('spend <wallet_path> <receiverIdOrName>')
.argument('<amount>', 'Amount of coins to send in aettos or in ae (example: 1.2ae)', coinAmountParser)
.option('--networkId [networkId]', 'Network id (default: ae_mainnet)')
.option('--payload [payload]', 'Transaction payload.', '')
.option('-F, --fee [fee]', 'Spend transaction fee.')
.addOption(feeOption)
.option('-T, --ttl [ttl]', 'Validity of the spend transaction in number of blocks (default forever)', TX_TTL)
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.option('-D, --denomination [denomination]', 'Denomination of amount', AE_AMOUNT_FORMATS.AETTOS)
.action((walletPath, receiverIdOrName, amount, ...args) => Account.spend(walletPath, receiverIdOrName, amount, getCmdFromArguments(args)));

// ## Initialize `transfer` command
Expand All @@ -69,7 +71,7 @@ program
.argument('<fraction>', 'Fraction of balance to spend (between 0 and 1)', (v) => +v)
.option('--networkId [networkId]', 'Network id (default: ae_mainnet)')
.option('--payload [payload]', 'Transaction payload.', '')
.option('-F, --fee [fee]', 'Spend transaction fee.')
.addOption(feeOption)
.option('-T, --ttl [ttl]', 'Validity of the spend transaction in number of blocks (default forever)', TX_TTL)
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.action((walletPath, receiver, percentage, ...args) => Account.transferFunds(walletPath, receiver, percentage, getCmdFromArguments(args)));
Expand Down
8 changes: 5 additions & 3 deletions src/commands/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import { COMPILER_URL } from '../utils/constant';
import { getCmdFromArguments } from '../utils/cli';
import CliError from '../utils/CliError';
import * as Contract from '../actions/contract';
import { nodeOption, jsonOption, gasOption } from '../arguments';
import {
nodeOption, jsonOption, gasOption, feeOption,
} from '../arguments';

const callArgs = new Argument('[args]', 'JSON-encoded arguments array of contract call')
.argParser((argsText) => {
Expand Down Expand Up @@ -117,7 +119,7 @@ program
.addOption(gasOption)
.option('-s --callStatic', 'Call static')
.option('-t --topHash', 'Hash of block to make call')
.option('-F, --fee [fee]', 'Spend transaction fee.')
.addOption(feeOption)
.option('-T, --ttl [ttl]', 'Validity of the spend transaction in number of blocks (default forever)', TX_TTL)
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.description('Execute a function of the contract')
Expand Down Expand Up @@ -145,7 +147,7 @@ program
.option('-P, --password [password]', 'Wallet Password')
.addOption(gasOption)
.option('-G --gasPrice [gas]', 'Amount of gas to deploy the contract', MIN_GAS_PRICE)
.option('-F, --fee [fee]', 'Spend transaction fee.')
.addOption(feeOption)
.option('-T, --ttl [ttl]', 'Validity of the spend transaction in number of blocks (default forever)', TX_TTL)
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.description('Deploy a contract on the chain')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import { Command } from 'commander';
import { TX_TTL, NAME_TTL, CLIENT_TTL } from '@aeternity/aepp-sdk';
import { getCmdFromArguments } from '../utils/cli';
import * as AENS from '../actions/aens';
import { nodeOption, jsonOption } from '../arguments';
import { nodeOption, jsonOption, feeOption } from '../arguments';

const program = new Command().name('aecli name');

// ## Initialize `options`
program
.addOption(nodeOption)
.option('--ttl [ttl]', 'Override the ttl that the transaction is going to be sent with', TX_TTL)
.option('--fee [fee]', 'Override the fee that the transaction is going to be sent with')
.addOption(feeOption)
.option('--nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.option('-P, --password [password]', 'Wallet Password')
.option('--networkId [networkId]', 'Network id (default: ae_mainnet)')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import {
import { RESPONSE_TTL } from '../utils/constant';
import { getCmdFromArguments } from '../utils/cli';
import * as Oracle from '../actions/oracle';
import { nodeOption, jsonOption } from '../arguments';
import { nodeOption, jsonOption, feeOption } from '../arguments';

const program = new Command().name('aecli oracle');

// ## Initialize `options`
program
.addOption(nodeOption)
.option('--ttl [ttl]', 'Override the ttl that the transaction is going to be sent with', TX_TTL)
.option('--fee [fee]', 'Override the fee that the transaction is going to be sent with')
.addOption(feeOption)
.option('--nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.option('-P, --password [password]', 'Wallet Password')
.option('--networkId [networkId]', 'Network id (default: ae_mainnet)')
Expand Down
24 changes: 12 additions & 12 deletions src/commands/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { RESPONSE_TTL } from '../utils/constant';
import { getCmdFromArguments } from '../utils/cli';
import * as Transaction from '../actions/transaction';
import {
nodeOption, jsonOption, gasOption, nonceArgument,
nodeOption, jsonOption, gasOption, nonceArgument, feeOption,
} from '../arguments';

const program = new Command().name('aecli tx');
Expand All @@ -37,7 +37,7 @@ const program = new Command().name('aecli tx');
program
.addOption(nodeOption)
// .option('--nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.option('--fee [fee]', 'Override the fee that the transaction is going to be sent with')
.addOption(feeOption)
.option('--ttl [fee]', 'Override the ttl that the transaction is going to be sent with', TX_TTL)
.option('-f --force', 'Ignore node version compatibility check')
.addOption(jsonOption);
Expand Down Expand Up @@ -75,7 +75,7 @@ program
.addArgument(nonceArgument)
.argument('[pointers...]')
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.option('--nameTtl [nameTtl]', 'Validity of name.', NAME_TTL)
.option('--clientTtl [clientTtl]', 'Client ttl.', CLIENT_TTL)
.description('Build name update transaction.')
Expand All @@ -90,7 +90,7 @@ program
.command('name-claim <accountId> <salt> <domain>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.option('--nameFee [nameFee]', 'Name fee.')
.description('Build name claim transaction.')
.action((accountId, salt, domain, nonce, ...args) => Transaction.nameClaim(accountId, salt, domain, nonce, getCmdFromArguments(args)));
Expand All @@ -104,7 +104,7 @@ program
.command('name-transfer <accountId> <recipientId> <domain>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.description('Build name tansfer transaction.')
.action((accountId, transferId, domain, nonce, ...args) => Transaction.nameTransfer(accountId, transferId, domain, nonce, getCmdFromArguments(args)));

Expand All @@ -117,7 +117,7 @@ program
.command('name-revoke <accountId> <domain>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.description('Build name revoke transaction.')
.action((accountId, domain, nonce, ...args) => Transaction.nameRevoke(accountId, domain, nonce, getCmdFromArguments(args)));

Expand All @@ -130,7 +130,7 @@ program
.command('contract-deploy <ownerId> <contractBytecode> <initCallData>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.addOption(gasOption)
.option('-G --gasPrice [gas]', 'Amount of gas to deploy the contract', MIN_GAS_PRICE)
.option('--amount [amount]', 'Amount', 0)
Expand All @@ -146,7 +146,7 @@ program
.command('contract-call <callerId> <contractId> <callData>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.addOption(gasOption)
.option('-G --gasPrice [gas]', 'Amount of gas to deploy the contract', MIN_GAS_PRICE)
.option('--amount [amount]', 'Amount', 0)
Expand All @@ -162,7 +162,7 @@ program
.command('oracle-register <accountId> <queryFormat> <responseFormat>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.option('--queryFee [queryFee]', 'Oracle Query fee.', QUERY_FEE)
.option('--oracleTtl [oracleTtl]', 'Oracle Ttl.', ORACLE_TTL.value)
.description('Build oracle register transaction.')
Expand All @@ -177,7 +177,7 @@ program
.command('oracle-post-query <accountId> <oracleId> <query>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.option('--queryFee [queryFee]', 'Oracle Query fee.', QUERY_FEE)
.option('--queryTtl [oracleTtl]', 'Oracle Ttl.', QUERY_TTL.value)
.option('--responseTtl [oracleTtl]', 'Oracle Ttl.', RESPONSE_TTL)
Expand All @@ -193,7 +193,7 @@ program
.command('oracle-extend <callerId> <oracleId> <oracleTtl>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.description('Build oracle extend transaction.')
.action((callerId, oracleId, oracleTtl, nonce, ...args) => Transaction.oracleExtend(callerId, oracleId, oracleTtl, nonce, getCmdFromArguments(args)));

Expand All @@ -206,7 +206,7 @@ program
.command('oracle-respond <callerId> <oracleId> <queryId> <response>')
.addArgument(nonceArgument)
.option('-T, --ttl [ttl]', 'Validity of the transaction in number of blocks (default forever)', TX_TTL)
.option('-F, --fee [fee]', 'Transaction fee.')
.addOption(feeOption)
.option('--responseTtl [oracleTtl]', 'Oracle Ttl.', RESPONSE_TTL)
.description('Build oracle extend transaction.')
.action((callerId, oracleId, queryId, response, nonce, ...args) => Transaction.oracleRespond(callerId, oracleId, queryId, response, nonce, getCmdFromArguments(args)));
Expand Down
19 changes: 9 additions & 10 deletions test/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import fs from 'fs-extra';
import { before, describe, it } from 'mocha';
import { expect } from 'chai';
import { generateKeyPair, AE_AMOUNT_FORMATS, formatAmount } from '@aeternity/aepp-sdk';
import { generateKeyPair } from '@aeternity/aepp-sdk';
import { getSdk, executeProgram, WALLET_NAME } from './index';
import accountProgram from '../src/commands/account';

Expand Down Expand Up @@ -76,19 +76,18 @@ describe('Account Module', () => {
(+receiverBalance).should.equal(amount);
});

it('Spend coins to another wallet using denomination', async () => {
const amount = 1; // 1 AE
const denomination = AE_AMOUNT_FORMATS.AE;
it('Spend coins to another wallet in ae', async () => {
const receiverKeys = generateKeyPair();
await executeAccount(['spend', WALLET_NAME, '--password', 'test', '-D', denomination, receiverKeys.publicKey, amount]);
const receiverBalance = await sdk.getBalance(receiverKeys.publicKey);
receiverBalance.should.equal(
formatAmount(amount, { denomination: AE_AMOUNT_FORMATS.AE }),
);
const { tx: { tx: { fee } } } = await executeAccount([
'spend', WALLET_NAME, '--password', 'test', '--json',
receiverKeys.publicKey, '1ae', '--fee', '0.02ae',
]);
expect(await sdk.getBalance(receiverKeys.publicKey)).to.be.equal('1000000000000000000');
expect(fee).to.be.equal('20000000000000000');
});

it('Spend fraction of coins to account by name', async () => {
const fraction = 0.0001;
const fraction = 0.000001;
const { publicKey } = generateKeyPair();
const balanceBefore = await sdk.getBalance(await sdk.address());
await executeAccount(['transfer', WALLET_NAME, '--password', 'test', publicKey, fraction]);
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function executeProgram(program, args) {
const options = getProgramOptions(program);
try {
const allArgs = [
...args,
...args.map((arg) => arg.toString()),
...['config', 'decode', 'sign', 'unpack'].includes(args[0]) ? [] : ['--url', url],
...args[0] === 'contract' ? ['--compilerUrl', compilerUrl] : [],
];
Expand Down

0 comments on commit 70ceb67

Please sign in to comment.