Skip to content

Commit

Permalink
feat: show extra info in tx details, consistent fields naming
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 15, 2024
1 parent e7f2e58 commit 5a9f4cc
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 267 deletions.
40 changes: 8 additions & 32 deletions src/actions/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ export async function preClaim(walletPath, name, options) {
ttl, fee, nonce, waitMined,
});
if (waitMined) {
printTransaction(
preClaimTx,
json,
);
await printTransaction(preClaimTx, json, sdk);
} else {
print(`Transaction send to the chain. Tx hash: ${preClaimTx.hash}`);
}
Expand All @@ -60,10 +57,7 @@ export async function claim(walletPath, name, salt, options) {
nonce, ttl, fee, waitMined, nameFee,
});
if (waitMined) {
printTransaction(
claimTx,
json,
);
await printTransaction(claimTx, json, sdk);
} else {
print(`Transaction send to the chain. Tx hash: ${claimTx.hash}`);
}
Expand Down Expand Up @@ -94,10 +88,7 @@ export async function updateName(walletPath, name, addresses, options) {
},
);
if (waitMined) {
printTransaction(
updateTx,
json,
);
await printTransaction(updateTx, json, sdk);
} else {
print(`Transaction send to the chain. Tx hash: ${updateTx.hash}`);
}
Expand All @@ -121,10 +112,7 @@ export async function extendName(walletPath, name, nameTtl, options) {
ttl, fee, nonce, waitMined, nameTtl, extendPointers: true,
});
if (waitMined) {
printTransaction(
updateTx,
json,
);
await printTransaction(updateTx, json, sdk);
} else {
print(`Transaction send to the chain. Tx hash: ${updateTx.hash}`);
}
Expand All @@ -150,10 +138,7 @@ export async function transferName(walletPath, name, address, options) {
ttl, fee, nonce, waitMined,
});
if (waitMined) {
printTransaction(
transferTX,
json,
);
await printTransaction(transferTX, json, sdk);
} else {
print(`Transaction send to the chain. Tx hash: ${transferTX.hash}`);
}
Expand All @@ -177,10 +162,7 @@ export async function revokeName(walletPath, name, options) {
ttl, fee, nonce, waitMined,
});
if (waitMined) {
printTransaction(
revokeTx,
json,
);
await printTransaction(revokeTx, json, sdk);
} else {
print(`Transaction send to the chain. Tx hash: ${revokeTx.hash}`);
}
Expand All @@ -203,10 +185,7 @@ export async function nameBid(walletPath, name, nameFee, options) {
nonce, ttl, fee, waitMined,
});
if (waitMined) {
printTransaction(
nameBidTx,
json,
);
await printTransaction(nameBidTx, json, sdk);
} else {
print(`Transaction send to the chain. Tx hash: ${nameBidTx.hash}`);
}
Expand Down Expand Up @@ -239,8 +218,5 @@ export async function fullClaim(walletPath, name, options) {
},
);

printTransaction(
updateTx,
json,
);
await printTransaction(updateTx, json, sdk);
}
2 changes: 1 addition & 1 deletion src/actions/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ export async function broadcast(signedTx, options) {
const { txHash } = await sdk.api.postTransaction({ tx: signedTx });
const tx = await (waitMined ? sdk.poll(txHash) : sdk.api.getTransactionByHash(txHash));

printTransaction(tx, json);
await printTransaction(tx, json, sdk);
if (!waitMined && !json) print('Transaction send to the chain.');
}
4 changes: 3 additions & 1 deletion src/actions/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ export async function call(fn, args, walletPath, options) {
});
if (json) print(callResult);
else {
if (callResult.hash) printTransaction(await sdk.api.getTransactionByHash(callResult.hash), json);
if (callResult.hash) {
await printTransaction(await sdk.api.getTransactionByHash(callResult.hash), json, sdk);
}
print('----------------------Call info-----------------------');
printUnderscored('Contract address', contract.$options.address);
printUnderscored('Gas price', callResult.result?.gasPrice);
Expand Down
15 changes: 3 additions & 12 deletions src/actions/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// This script initialize all `inspect` function

import BigNumber from 'bignumber.js';
import { Encoding, unpackTx as _unpackTx, Tag } from '@aeternity/aepp-sdk';
import { initSdk } from '../utils/cli.js';
import {
Expand All @@ -14,7 +13,7 @@ import {
printUnderscored,
} from '../utils/print.js';
import {
checkPref, getBlock, getNameEntry, timeAgo, validateName,
checkPref, getBlock, getNameEntry, formatCoins, formatTtl, validateName,
} from '../utils/helpers.js';
import CliError from '../utils/CliError.js';

Expand All @@ -32,13 +31,14 @@ async function getBlockByHash(hash, { json, ...options }) {
async function getTransactionByHash(hash, { json, ...options }) {
checkPref(hash, Encoding.TxHash);
const sdk = initSdk(options);
printTransaction(await sdk.api.getTransactionByHash(hash), json);
await printTransaction(await sdk.api.getTransactionByHash(hash), json, sdk);
}

async function unpackTx(encodedTx, { json }) {
checkPref(encodedTx, Encoding.Transaction);
const txUnpacked = _unpackTx(encodedTx);
if (json) print(txUnpacked);
// TODO: use printTransaction instead
else printEntries({ 'Tx Type': Tag[txUnpacked.tag], ...txUnpacked });
}

Expand Down Expand Up @@ -69,15 +69,6 @@ async function getBlockByHeight(height, { json, ...options }) {
printBlock(await sdk.api.getKeyBlockByHeight(+height), json);
}

const formatCoins = (coins) => `${new BigNumber(coins).shiftedBy(-18).toFixed()}ae`;

const formatTtl = (ttl, height) => {
const date = new Date();
const diff = Math.abs(ttl - height) < 3 ? 0 : ttl - height;
date.setMinutes(date.getMinutes() + diff * 3);
return `${ttl} (${timeAgo(date)})`;
};

async function getName(name, { json, ...options }) {
validateName(name);
const sdk = initSdk(options);
Expand Down
8 changes: 4 additions & 4 deletions src/actions/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function createOracle(walletPath, queryFormat, responseFormat, opti
queryFee,
});
if (waitMined) {
printTransaction(oracle, json);
await printTransaction(oracle, json, sdk);
} else {
print('Transaction send to the chain. Tx hash: ', oracle);
}
Expand All @@ -62,7 +62,7 @@ export async function extendOracle(walletPath, oracleId, oracleTtl, options) {
},
});
if (waitMined) {
printTransaction(extended, json);
await printTransaction(extended, json, sdk);
} else {
print('Transaction send to the chain. Tx hash: ', extended);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export async function createOracleQuery(walletPath, oracleId, query, options) {
queryFee,
});
if (waitMined) {
printTransaction(oracleQuery, json);
await printTransaction(oracleQuery, json, sdk);
} else {
print('Transaction send to the chain. Tx hash: ', oracleQuery);
}
Expand Down Expand Up @@ -131,7 +131,7 @@ export async function respondToQuery(
},
});
if (waitMined) {
printTransaction(queryResponse, json);
await printTransaction(queryResponse, json, sdk);
} else {
print('Transaction send to the chain. Tx hash: ', queryResponse);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/spend.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ Example call:
},
);
if (!json) print('Transaction mined');
printTransaction(tx, json);
await printTransaction(tx, json, sdk);
});
10 changes: 10 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// # Utils `helpers` Module
// That script contains base helper function

import BigNumber from 'bignumber.js';
import { resolve } from 'path';
import { Encoding, decode as _decode, produceNameId } from '@aeternity/aepp-sdk';
import CliError from './CliError.js';
Expand Down Expand Up @@ -106,3 +107,12 @@ export function timeAgo(date) {
}
return 'about now';
}

export const formatCoins = (coins) => `${new BigNumber(coins).shiftedBy(-18).toFixed()}ae`;

export const formatTtl = (ttl, height) => {
const date = new Date();
const diff = Math.abs(ttl - height) < 2 ? 0 : ttl - height;
date.setMinutes(date.getMinutes() + diff * 3);
return `${ttl} (${timeAgo(date)})`;
};
85 changes: 54 additions & 31 deletions src/utils/print.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Encoding, unpackTx } from '@aeternity/aepp-sdk';
import { decode } from './helpers.js';
import {
Encoding, unpackTx, AbiVersion, VmVersion,
} from '@aeternity/aepp-sdk';
import {
decode, formatCoins, formatTtl as formatTtlUnbound, timeAgo,
} from './helpers.js';

const ROW_WIDTH = 40;

Expand Down Expand Up @@ -49,66 +53,85 @@ function printTxField(tx, verboseName, field, handleValue = (a) => a) {
printUnderscored(verboseName, tx[field] == null ? 'N/A' : handleValue(tx[field]));
}

export function printTransaction(_tx, json) {
function printTransactionSync(_tx, json, currentHeight) {
if (json) {
print(_tx);
return;
}
const tx = { ..._tx, ..._tx.tx };
const formatTtl = (ttl) => (currentHeight ? formatTtlUnbound(ttl, currentHeight) : ttl);
const formatTtlObject = ({ type, value }) => {
switch (type) {
case 'delta': return formatTtl(+value + +tx.blockHeight);
case 'block': return formatTtl(value);
default: throw new Error(`Unknown ttl type: ${type}`);
}
};
const formatTtlSeconds = (seconds) => {
const date = new Date();
date.setSeconds(date.getSeconds() + seconds);
return `${seconds} (${timeAgo(date).replace('in ', '')})`;
};

printUnderscored('Tx hash', tx.hash);
printUnderscored('Transaction hash', tx.hash);
printUnderscored('Block hash', tx.blockHash);
printUnderscored('Block height', tx.blockHeight);
printTxField(tx, 'Block height', 'blockHeight', formatTtl);
printUnderscored('Signatures', tx.signatures);
printUnderscored('Tx Type', tx.type);
printUnderscored('Transaction type', tx.type);

printTxField(tx, 'Account', 'accountId');
printTxField(tx, 'Client TTL', 'clientTtl');
printTxField(tx, 'Sender account', 'senderId');
printTxField(tx, 'Recipient account', 'recipientId');
printTxField(tx, 'Account address', 'accountId');
printTxField(tx, 'Client TTL', 'clientTtl', formatTtlSeconds);
printTxField(tx, 'Sender address', 'senderId');
printTxField(tx, 'Recipient address', 'recipientId');
printTxField(tx, 'Name ID', 'nameId');
printTxField(tx, 'Name TTL', 'nameTtl');
printTxField(tx, 'Name TTL', 'nameTtl', formatTtl);
printTxField(tx, 'Name', 'name');
printTxField(tx, 'Name Fee', 'nameFee');
printTxField(tx, 'Name Salt', 'nameSalt');
printTxField(tx, 'Owner', 'ownerId');
printTxField(tx, 'Caller Account', 'callerId');
printTxField(tx, 'Contract Address', 'contractId');
printTxField(tx, 'Oracle ID', 'oracleId', (id) => id.replace(/^\w{2}_/, 'ok_'));
printTxField(tx, 'Name fee', 'nameFee', formatCoins);
printTxField(tx, 'Name salt', 'nameSalt');
printTxField(tx, 'Owner address', 'ownerId');
printTxField(tx, 'Caller address', 'callerId');
printTxField(tx, 'Contract address', 'contractId');
printTxField(tx, 'Oracle ID', 'oracleId');
printTxField(tx, 'Query', 'query');
if ('pointers' in tx) {
if (tx.pointers.length === 0) printUnderscored('Pointers', 'N/A');
else tx.pointers.forEach(({ key, id }) => printUnderscored(`Pointer ${key}`, id));
}
printTxField(tx, 'Amount', 'amount');
printTxField(tx, 'Amount', 'amount', formatCoins);
printTxField(tx, 'Payload', 'payload');
printTxField(tx, 'Deposit', 'deposit');
printTxField(tx, 'Deposit', 'deposit', formatCoins);
printTxField(tx, 'Gas', 'gas');
printTxField(tx, 'Gas Price', 'gasPrice');
printTxField(tx, 'Gas price', 'gasPrice', formatCoins);
printTxField(tx, 'Bytecode', 'code');
printTxField(tx, 'Call data', 'callData');
printTxField(tx, 'Commitment', 'commitmentId');
printTxField(tx, 'Salt', 'salt');
printTxField(tx, 'Query', 'queryId');
printTxField(tx, 'Fee', 'fee');
printTxField(tx, 'Fee', 'fee', formatCoins);
printTxField(tx, 'Response', 'response');
printTxField(tx, 'Query Fee', 'queryFee');
printTxField(tx, 'Oracle Ttl', 'oracleTtl');
printTxField(tx, 'Query Ttl', 'queryTtl');
printTxField(tx, 'Response Ttl', 'responseTtl');
printTxField(tx, 'Query Format', 'queryFormat');
printTxField(tx, 'Response Format', 'responseFormat');
printTxField(tx, 'Query fee', 'queryFee', formatCoins);
printTxField(tx, 'Oracle TTL', 'oracleTtl', formatTtlObject);
printTxField(tx, 'Query TTL', 'queryTtl', formatTtlObject);
printTxField(tx, 'Response TTL', 'responseTtl', formatTtlObject);
printTxField(tx, 'Query format', 'queryFormat');
printTxField(tx, 'Response format', 'responseFormat');
printTxField(tx, 'Nonce', 'nonce');
printTxField(tx, 'TTL', 'ttl');
printTxField(tx, 'TTL', 'ttl', formatTtl);
printTxField(tx, 'Version', 'version');
printTxField(tx, 'VM Version', 'vmVersion');
printTxField(tx, 'ABI Version', 'abiVersion');
printTxField(tx, 'VM version', 'vmVersion', (v) => `${v} (${VmVersion[v]})`);
printTxField(tx, 'ABI version', 'abiVersion', (v) => `${v} (${AbiVersion[v]})`);
}

export async function printTransaction(tx, json, sdk) {
const height = await sdk.getHeight({ cache: true });
printTransactionSync(tx, json, height);
}

export function printBlockTransactions(ts) {
ts.forEach((tx) => {
print('<<--------------- Transaction --------------->>');
printTransaction(tx, false);
// TODO: consider using async version
printTransactionSync(tx, false);
});
}

Expand Down
20 changes: 19 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chai from 'chai';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { before, after } from 'mocha';
import mockFs from 'mock-fs';
Expand Down Expand Up @@ -136,3 +136,21 @@ export function randomName(length = 18) {
.map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
return `${random}.chain`;
}

export function expectToMatchLines(value, testLines) {
try {
const valueLines = value.split('\n');
testLines.forEach((test) => {
if (typeof test === 'string') return expect(valueLines.shift()).to.be.equal(test);
if (test instanceof RegExp) return expect(valueLines.shift()).to.be.match(test);
throw new Error(`Unexpected test line: ${test}`);
});
expect(valueLines.join('\n')).to.be.equal('');
} catch (error) {
const stackItems = error.stack.split('\n');
stackItems.splice(1, 3);
error.stack = stackItems.join('\n');
error.message += `\nWhole value:\n${value}`;
throw error;
}
}

0 comments on commit 5a9f4cc

Please sign in to comment.