Skip to content

Commit

Permalink
refactor(account)!: combine spend and transfer commands
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `account transfer` command removed
Use `account spend` instead.
```diff
- aecli account transfer 0.42 <recipient>
+ aecli account spend 42% <recipient>
```
  • Loading branch information
davidyuk committed Apr 20, 2023
1 parent db5ae9d commit 2a2a94b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 60 deletions.
48 changes: 17 additions & 31 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,43 +85,29 @@ export async function sign(walletPath, tx, { networkId: networkIdOpt, json, ...o
}

// ## `Spend` function
// 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,
} = options;
// this function allow you to `send` coins to another `account`
export async function spend(
walletPath,
receiverNameOrAddress,
{ amount, fraction },
{
ttl, json, nonce, fee, payload, ...options
},
) {
const sdk = await initSdkByWalletFile(walletPath, options);

let tx = await sdk.spend(amount, receiverNameOrAddress, {
ttl, nonce, payload: encode(Buffer.from(payload), Encoding.Bytearray), fee,
});
if (json) print({ tx });
else {
print('Transaction mined');
printTransaction(tx, json);
}
}

// ## `Transfer` function
// this function allow you to `send` % of balance to another `account`
export async function transferFunds(walletPath, receiver, fraction, options) {
const {
ttl, json, nonce, fee, payload,
} = options;
const sdk = await initSdkByWalletFile(walletPath, options);
const tx = await sdk[amount != null ? 'spend' : 'transferFunds'](
amount ?? fraction / 100,
receiverNameOrAddress,
{
ttl, nonce, payload: encode(Buffer.from(payload), Encoding.Bytearray), fee,
},
);

let tx = await sdk.transferFunds(fraction, receiver, {
ttl, nonce, payload: encode(Buffer.from(payload), Encoding.Bytearray), fee,
});
// if waitMined false
if (typeof tx !== 'object') {
tx = await sdk.api.getTransactionByHash(tx);
} else if (!json) {
print('Transaction mined');
}
if (json) {
print({ tx });
} else {
print('Transaction mined');
printTransaction(tx, json);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Argument, Option } from 'commander';
import BigNumber from 'bignumber.js';
import { NODE_URL, COMPILER_URL } from './utils/constant';

export const coinAmountParser = (amount) => (
new BigNumber(amount.replace(/ae$/, '')).shiftedBy(amount.endsWith('ae') ? 18 : 0)
);
export const coinAmountParser = (amount) => {
if (amount.endsWith('ae')) return new BigNumber(amount.slice(0, -2)).shiftedBy(18);
return new BigNumber(amount);
};

export const feeOption = new Option('-F, --fee [fee]', 'Override the transaction fee')
.argParser(coinAmountParser);
Expand Down
32 changes: 10 additions & 22 deletions src/commands/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,22 @@ const addCommonOptions = (p) => p
//
// Example: `aecli account spend ./myWalletKeyFile ak_1241rioefwj23f2wfdsfsdsdfsasdf 100 --password testpassword --ttl 20` --> this tx will leave for 20 blocks
addCommonOptions(program
.command('spend <wallet_path> <receiverIdOrName>')
.argument('<amount>', 'Amount of coins to send in aettos or in ae (example: 1.2ae)', coinAmountParser)
.addOption(networkIdOption)
.option('--payload [payload]', 'Transaction payload.', '')
.addOption(feeOption)
.addOption(ttlOption)
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.action(Account.spend));

// ## Initialize `transfer` command
//
// You can use this command to send % of balance to another account
//
// Example: `aecli account transfer ./myWalletKeyFile ak_1241rioefwj23f2wfdsfsdsdfsasdf 0.5 --password testpassword`
//
// You can set transaction `ttl(Time to leave)`. If not set use default.
//
// Example: `aecli account transfer ./myWalletKeyFile ak_1241rioefwj23f2wfdsfsdsdfsasdf 0.5 --password testpassword --ttl 20` --> this tx will leave for 20 blocks
addCommonOptions(program
.command('transfer <wallet_path>')
.command('spend <wallet_path>')
.argument('<receiver>', 'Address or name of recipient account')
.argument('<fraction>', 'Fraction of balance to spend (between 0 and 1)', (v) => +v)
.argument(
'<amountOrPercent>',
'Amount of coins to send in aettos/ae (example 1.2ae), or percent of sender balance (example 42%)',
(amount) => {
if (amount.endsWith('%')) return { fraction: +amount.slice(0, -1) };
return { amount: coinAmountParser(amount) };
},
)
.addOption(networkIdOption)
.option('--payload [payload]', 'Transaction payload.', '')
.addOption(feeOption)
.addOption(ttlOption)
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.action(Account.transferFunds));
.action(Account.spend));

// ## Initialize `sign` command
//
Expand Down
7 changes: 3 additions & 4 deletions test/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ describe('Account Module', () => {
expect(fee).to.be.equal('20000000000000000');
});

it('Spend fraction of coins to account by name', async () => {
const fraction = 0.000001;
it('Spend percent of coins to account', async () => {
const { publicKey } = generateKeyPair();
const balanceBefore = await sdk.getBalance(sdk.address);
await executeAccount(['transfer', WALLET_NAME, '--password', 'test', publicKey, fraction]);
expect(+await sdk.getBalance(publicKey)).to.be.equal(balanceBefore * fraction);
await executeAccount(['spend', WALLET_NAME, '--password', 'test', publicKey, '42%']);
expect(+await sdk.getBalance(publicKey)).to.be.equal(balanceBefore * 0.42);
});

it('Get account nonce', async () => {
Expand Down

0 comments on commit 2a2a94b

Please sign in to comment.