Skip to content

Commit

Permalink
feat(aens): print missed name details, be human-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 10, 2024
1 parent 75217c3 commit 91c7731
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
44 changes: 41 additions & 3 deletions src/actions/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
//
// 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 {
print,
printBlock,
printBlockTransactions,
printName, printOracle, printQueries,
printOracle, printQueries,
printTransaction,
printUnderscored,
} from '../utils/print.js';
import {
checkPref, getBlock, getNameEntry, validateName,
checkPref, getBlock, getNameEntry, timeAgo, validateName,
} from '../utils/helpers.js';
import CliError from '../utils/CliError.js';

Expand Down Expand Up @@ -68,10 +69,47 @@ 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);
printName(await getNameEntry(name, sdk), json);
const nameEntry = await getNameEntry(name, sdk);

if (json) {
print(nameEntry);
return;
}

const height = await sdk.getHeight({ cached: true });
printUnderscored('Status', nameEntry.status);
printUnderscored('Name hash', nameEntry.id);
switch (nameEntry.status) {
case 'CLAIMED':
printUnderscored('Owner', nameEntry.owner);
if (nameEntry.pointers?.length) {
nameEntry.pointers.forEach(({ key, id }) => printUnderscored(`Pointer ${key}`, id));
} else printUnderscored('Pointers', 'N/A');
printUnderscored('TTL', formatTtl(nameEntry.ttl, height));
break;
case 'AUCTION':
printUnderscored('Highest bidder', nameEntry.highestBidder);
printUnderscored('Highest bid', formatCoins(nameEntry.highestBid));
printUnderscored('Ends at height', formatTtl(nameEntry.endsAt, height));
printUnderscored('Started at height', formatTtl(nameEntry.startedAt, height));
break;
case 'AVAILABLE':
break;
default:
throw new Error(`Unknown name status: ${nameEntry.status}`);
}
}

async function getContract(contractId, { json, ...options }) {
Expand Down
24 changes: 24 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,27 @@ export function decode(data, requiredPrefix) {
}

export const getFullPath = (path) => resolve(process.cwd(), path);

const units = [
['year', 365 * 24 * 60 * 60 * 1000],
['month', 30.5 * 24 * 60 * 60 * 1000],
['day', 24 * 60 * 60 * 1000],
['hour', 60 * 60 * 1000],
['minute', 60 * 1000],
['second', 1000],
];

export function timeAgo(date) {
const diff = Date.now() - date.getTime();
// TODO: revisit linter settings, the below rule is not relevant because babel is not used
// eslint-disable-next-line no-restricted-syntax
for (const [name, size] of units) {
const value = Math.floor(Math.abs(diff) / size);
if (value > 0) {
const plural = value > 1 ? 's' : '';
const description = `${value} ${name}${plural}`;
return diff > 0 ? `${description} ago` : `in ${description}`;
}
}
return 'about now';
}
13 changes: 0 additions & 13 deletions src/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,3 @@ export function printQueries(queries = [], json) {
print('------------------------------------------------------------------------------');
});
}

// Print `name`
export function printName(name, json) {
if (json) {
print(name);
return;
}
printUnderscored('Status', name.status ?? 'N/A');
printUnderscored('Name hash', name.id ?? 'N/A');
if (name.pointers?.length) name.pointers.forEach(({ key, id }) => printUnderscored(`Pointer ${key}`, id));
else printUnderscored('Pointers', 'N/A');
printUnderscored('TTL', name.ttl ?? 0);
}
14 changes: 8 additions & 6 deletions test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,6 @@ Ttl _____________________________________ ${resJson.ttl}
expect(res).to.equal(`
Status __________________________________ AVAILABLE
Name hash _______________________________ ${produceNameId(name)}
Pointers ________________________________ N/A
TTL _____________________________________ 0
`.trim());
});

Expand All @@ -274,19 +272,21 @@ TTL _____________________________________ 0
expect(res).to.equal(`
Status __________________________________ CLAIMED
Name hash _______________________________ ${resJson.id}
Owner ___________________________________ ${sdk.address}
Pointer myKey ___________________________ ${sdk.address}
Pointer account_pubkey __________________ ${sdk.address}
Pointer oracle_pubkey ___________________ ${sdk.address}
TTL _____________________________________ ${resJson.ttl}
TTL _____________________________________ ${resJson.ttl} (in 1 year)
`.trim());
}).timeout(6000);

it('Inspect Running Auction Name', async () => {
const auctionName = `a${Math.random().toString().slice(2, 9)}.chain`;
await (await sdk.aensPreclaim(auctionName)).claim();
const resJson = await executeInspect([auctionName, '--json']);
const endsAt = +resJson.startedAt + 14880;
expect(resJson).to.eql({
endsAt: String(+resJson.startedAt + 14880),
endsAt: String(endsAt),
highestBid: '19641800000000000000',
highestBidder: sdk.address,
id: resJson.id,
Expand All @@ -297,8 +297,10 @@ TTL _____________________________________ ${resJson.ttl}
expect(res).to.equal(`
Status __________________________________ AUCTION
Name hash _______________________________ ${resJson.id}
Pointers ________________________________ N/A
TTL _____________________________________ 0
Highest bidder __________________________ ${sdk.address}
Highest bid _____________________________ 19.6418ae
Ends at height __________________________ ${endsAt} (in 1 month)
Started at height _______________________ ${resJson.startedAt} (about now)
`.trim());
}).timeout(4000);
});

0 comments on commit 91c7731

Please sign in to comment.