Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BCC] Add list transactions #3406

Merged
merged 9 commits into from
Oct 5, 2024
21 changes: 11 additions & 10 deletions packages/bitcore-client/bin/wallet
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ const program = require('commander');
program
.version(require('../package.json').version)
.description('CLI wallet utility for Bitcore-node')
.command('create', 'create a wallet').alias('c')
.command('list', 'list wallets').alias('l')
.command('import', 'import addresses/keys').alias('i')
.command('register', 'register a wallet with bitcore-node').alias('r')
.command('balance', 'check wallet balance').alias('b')
.command('utxos', 'get wallet utxos').alias('u')
.command('broadcast', 'broadcast transaction')
.command('tx', 'create transaction').alias('t')
.command('bump-tx', 'increase transaction fee')
.command('derive', 'derive an address').alias('d')
.command('check', 'check if wallet is registered and print last address').alias('h')
.command('create', 'create a wallet').alias('c')
.command('derive', 'derive an address').alias('d')
.command('flags', 'check or set wallet flags (XRP only)')
.command('import', 'import addresses/keys').alias('i')
.command('list', 'list wallets').alias('l')
.command('paypro', 'pay using payment protocol').alias('p')
.command('register', 'register a wallet with bitcore-node').alias('r')
.command('send', 'simple send from wallet to an address').alias('s')
.command('sign', 'sign a transaction')
.command('sign-message', 'sign a message with an address')
.command('storage', 'storage util for wallets')
.command('token', 'add an ERC20 token to an EVM wallet')
.command('token-rm', 'remove an ERC20 token from an EVM wallet')
.command('flags', 'check or set wallet flags (XRP only)')
.command('storage', 'storage util for wallets')
.command('sign-message', 'sign a message with an address')
.command('tx', 'create transaction').alias('t')
.command('tx-list', 'list wallet transactions').alias('txns')
kajoseph marked this conversation as resolved.
Show resolved Hide resolved
.command('utxos', 'get wallet utxos').alias('u')
.parse(process.argv);


Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-client/bin/wallet-balance-all
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const getBalance = new Transform({
transform (wallet, encoding, callback) {
const { name, chain, network, storageType } = wallet;
Wallet.loadWallet({ name, path, storageType })
.then(wallet => wallet.getBalance(time))
.then(wallet => wallet.getBalance({ time }))
.then(balance => {
const { confirmed } = balance;
callback(null, `${name}: ${confirmed / 1e8} ${chain} (${network})\n`);
Expand Down
33 changes: 0 additions & 33 deletions packages/bitcore-client/bin/wallet-transaction-list

This file was deleted.

84 changes: 84 additions & 0 deletions packages/bitcore-client/bin/wallet-tx-list
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env node

const program = require('commander');
const { Writable } = require('stream');
const { Wallet } = require('../ts_build/src/wallet');
const { getCurrencyObj } = require('../ts_build/src/utils');

program
.version(require('../package.json').version)
.requiredOption('--name <name>', 'REQUIRED - Wallet name')
.option('--startDate <startDate>', 'Start date for the query')
.option('--endDate <endDate>', 'End date for the query')
.option('--startBlock <startBlock>', 'Start block for the query')
.option('--endBlock <endBlock>', 'End block for the query')
.option('--includeMempool', 'Include unconfirmed transactions')
.option('--token <tokenName>', 'Get token transactions by the token name')
.option('--raw', 'Output raw transaction data')
.option('--storageType <storageType>', 'Name of the database to use (default Level)')
.option('--path <path>', 'Custom wallet storage path')
.parse(process.argv);

let wallet;

const main = async () => {
const { name, path, startBlock, startDate, endBlock, endDate, includeMempool, storageType, token: tokenName, raw } = program.opts();
try {
wallet = await Wallet.loadWallet({ name, path, storageType });
const list = wallet.listTransactions({ startBlock, startDate, endBlock, endDate, includeMempool, tokenName });
if (raw) {
list.pipe(process.stdout);
} else {
const tokenObj = wallet.getTokenObj({ tokenName }); // null if no tokenName provided
const currencyObj = await getCurrencyObj(wallet.chain, tokenObj?.address);
let txs = '[';
list.pipe(new Writable({
objectMode: true,
write: function(tx, _, next) {
txs += tx.toString();
next();
},
final: function(done) {
txs = JSON.parse(txs.trim().replaceAll('\n', ',') + ']');
for (const tx of txs) {
if (tx.category === 'fee') continue;
tx.chain = wallet.chain; // tx.chain is not always populated
console.log(`[${new Date(tx.blockTime).toLocaleString()}] ${tx.txid} ${catSymbol(tx)} ${displayAmt(tx, tokenObj || currencyObj)}`);
}
done();
}
}))
}
} catch (e) {
console.error(e);
}
};

function catSymbol(tx) {
switch (tx.category) {
case 'send':
return '=>';
case 'receive':
return '<=';
case 'move':
return '<>';
default:
return '=?';
}
}

function displayAmt(tx, currObj) {
const baseToCommon = sats => Math.abs(sats) / Math.pow(10, currObj.decimals);
kajoseph marked this conversation as resolved.
Show resolved Hide resolved
const dc = currObj.displayCode || currObj.symbol;
const gwei = tx.gasPrice / 1e9; // NaN if not EVM
const drops = tx.chain === 'XRP' ? tx.fee : NaN;
kajoseph marked this conversation as resolved.
Show resolved Hide resolved
const feeRate = tx.fee / (tx.vsize ?? tx.size);
const feeRateStr = !isNaN(gwei) ? ` - ${gwei.toFixed(1)} gwei` :
!isNaN(drops) ? '' :
` - ${feeRate.toFixed(2)} sat/vB`;
return `${baseToCommon(tx.satoshis)} ${dc}${!currObj.native ? /* hide fee for tokens */ '' : ` (${baseToCommon(tx.fee)} ${dc}${feeRateStr})`}`;
}

main()
.catch(console.error)
.finally(() => wallet?.storage?.close());
2 changes: 2 additions & 0 deletions packages/bitcore-client/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ export class Wallet {
gasLimit?: number;
gasPrice?: number;
contractAddress?: string;
chainId?: number;
replaceByFee?: boolean;
lockUntilBlock?: number;
lockUntilDate?: Date;
Expand Down Expand Up @@ -513,6 +514,7 @@ export class Wallet {
data: params.data,
tokenAddress: tokenContractAddress,
contractAddress: params.contractAddress,
chainId: params.chainId,
replaceByFee: params.replaceByFee,
lockUntilBlock: params.lockUntilBlock,
lockUntilDate: params.lockUntilDate,
Expand Down