Skip to content

Commit

Permalink
feat(aens): show auction details
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 10, 2024
1 parent 797186c commit 75217c3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 69 deletions.
49 changes: 16 additions & 33 deletions src/actions/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
import { isAddressValid, getDefaultPointerKey } from '@aeternity/aepp-sdk';
import { initSdkByWalletFile } from '../utils/cli.js';
import { print, printTransaction } from '../utils/print.js';
import { isAvailable, updateNameStatus, validateName } from '../utils/helpers.js';
import { getNameEntry, validateName } from '../utils/helpers.js';
import CliError from '../utils/CliError.js';

async function ensureNameStatus(name, sdk, status, operation) {
const nameEntry = await getNameEntry(name, sdk);
if (nameEntry.status !== status) {
throw new CliError(`AENS name is ${nameEntry.status} and cannot be ${operation}`);
}
}

// ## Claim `name` function
export async function preClaim(walletPath, name, options) {
const {
Expand All @@ -20,10 +27,7 @@ export async function preClaim(walletPath, name, options) {
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
const nameEntry = await updateNameStatus(name, sdk);
if (!isAvailable(nameEntry)) {
throw new CliError('AENS name not available');
}
await ensureNameStatus(name, sdk, 'AVAILABLE', 'preclaimed');
// Create `pre-claim` transaction
const preClaimTx = await sdk.aensPreclaim(name, {
ttl, fee, nonce, waitMined,
Expand All @@ -49,10 +53,7 @@ export async function claim(walletPath, name, salt, options) {
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
const nameEntry = await updateNameStatus(name, sdk);
if (!isAvailable(nameEntry)) {
throw new CliError('AENS name not available');
}
await ensureNameStatus(name, sdk, 'AVAILABLE', 'claimed');

// Wait for next block and create `claimName` transaction
const claimTx = await sdk.aensClaim(name, salt, {
Expand Down Expand Up @@ -82,10 +83,7 @@ export async function updateName(walletPath, name, addresses, options) {
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name` is unavailable and we can update it
const nameEntry = await updateNameStatus(name, sdk);
if (isAvailable(nameEntry)) {
throw new CliError(`AENS name is ${nameEntry.status} and cannot be updated`);
}
await ensureNameStatus(name, sdk, 'CLAIMED', 'updated');

// Create `updateName` transaction
const updateTx = await sdk.aensUpdate(
Expand Down Expand Up @@ -116,10 +114,7 @@ export async function extendName(walletPath, name, nameTtl, options) {
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name` is unavailable and we can update it
const nameEntry = await updateNameStatus(name, sdk);
if (isAvailable(nameEntry)) {
throw new CliError(`AENS name is ${nameEntry.status} and cannot be extended`);
}
await ensureNameStatus(name, sdk, 'CLAIMED', 'extended');

// Create `updateName` transaction
const updateTx = await sdk.aensUpdate(name, {}, {
Expand Down Expand Up @@ -148,10 +143,7 @@ export async function transferName(walletPath, name, address, options) {
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name` is unavailable and we can transfer it
const nameEntry = await updateNameStatus(name, sdk);
if (isAvailable(nameEntry)) {
throw new CliError('AENS name is available, nothing to transfer');
}
await ensureNameStatus(name, sdk, 'CLAIMED', 'transferred');

// Create `transferName` transaction
const transferTX = await sdk.aensTransfer(name, address, {
Expand All @@ -178,10 +170,7 @@ export async function revokeName(walletPath, name, options) {
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if `name` is unavailable and we can revoke it
const nameEntry = await updateNameStatus(name, sdk);
if (isAvailable(nameEntry)) {
throw new CliError('AENS name is available, nothing to revoke');
}
await ensureNameStatus(name, sdk, 'CLAIMED', 'revoked');

// Create `revokeName` transaction
const revokeTx = await sdk.aensRevoke(name, {
Expand All @@ -207,10 +196,7 @@ export async function nameBid(walletPath, name, nameFee, options) {
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
const nameEntry = await updateNameStatus(name, sdk);
if (!isAvailable(nameEntry)) {
throw new CliError('Auction do not start or already end');
}
await ensureNameStatus(name, sdk, 'AUCTION', 'bidded');

// Wait for next block and create `claimName` transaction
const nameBidTx = await sdk.aensBid(name, nameFee, {
Expand All @@ -236,10 +222,7 @@ export async function fullClaim(walletPath, name, options) {
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
const nameEntry = await updateNameStatus(name, sdk);
if (!isAvailable(nameEntry)) {
throw new CliError('AENS name not available');
}
await ensureNameStatus(name, sdk, 'AVAILABLE', 'claimed');

// Wait for next block and create `claimName` transaction
nonce = nonce && +nonce;
Expand Down
4 changes: 2 additions & 2 deletions src/actions/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
printUnderscored,
} from '../utils/print.js';
import {
checkPref, getBlock, updateNameStatus, validateName,
checkPref, getBlock, getNameEntry, validateName,
} from '../utils/helpers.js';
import CliError from '../utils/CliError.js';

Expand Down Expand Up @@ -71,7 +71,7 @@ async function getBlockByHeight(height, { json, ...options }) {
async function getName(name, { json, ...options }) {
validateName(name);
const sdk = initSdk(options);
printName(await updateNameStatus(name, sdk), json);
printName(await getNameEntry(name, sdk), json);
}

async function getContract(contractId, { json, ...options }) {
Expand Down
30 changes: 16 additions & 14 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// That script contains base helper function

import { resolve } from 'path';
import { Encoding, decode as _decode } from '@aeternity/aepp-sdk';
import { Encoding, decode as _decode, produceNameId } from '@aeternity/aepp-sdk';
import CliError from './CliError.js';

// ## Method which retrieve block info by hash
Expand Down Expand Up @@ -49,21 +49,23 @@ export function checkPref(hash, hashType) {

// ## AENS helpers methods

// Get `name` status
export async function updateNameStatus(name, sdk) {
try {
return { ...await sdk.getName(name), status: 'CLAIMED' };
} catch (e) {
if (e.response && e.response.status === 404) {
return { name, status: 'AVAILABLE' };
}
throw e;
}
// Get `name` entry
export async function getNameEntry(nameAsString, sdk) {
const handle404 = (error) => {
if (error.response?.status === 404) return undefined;
throw error;
};
const [name, auction] = await Promise.all([
sdk.api.getNameEntryByName(nameAsString).catch(handle404),
sdk.api.getAuctionEntryByName(nameAsString).catch(handle404),
]);
return {
id: produceNameId(nameAsString),
...name ?? auction,
status: (name && 'CLAIMED') || (auction && 'AUCTION') || 'AVAILABLE',
};
}

// Check if `name` is `AVAILABLE`
export function isAvailable(name) { return name.status === 'AVAILABLE'; }

// Validate `name`
export function validateName(name) {
if (typeof name !== 'string') throw new CliError('Name must be a string');
Expand Down
18 changes: 11 additions & 7 deletions test/inspect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { before, describe, it } from 'mocha';
import { expect } from 'chai';
import {
AbiVersion, generateKeyPair, Tag, VmVersion,
AbiVersion, generateKeyPair, produceNameId, Tag, VmVersion,
} from '@aeternity/aepp-sdk';
import { executeProgram, getSdk } from './index.js';
import inspectProgram from '../src/commands/inspect.js';
Expand Down Expand Up @@ -240,13 +240,13 @@ Ttl _____________________________________ ${resJson.ttl}
it('Inspect Unclaimed Name', async () => {
const resJson = await executeInspect([name, '--json']);
expect(resJson).to.eql({
name,
id: produceNameId(name),
status: 'AVAILABLE',
});
const res = await executeInspect([name]);
expect(res).to.equal(`
Status __________________________________ AVAILABLE
Name hash _______________________________ N/A
Name hash _______________________________ ${produceNameId(name)}
Pointers ________________________________ N/A
TTL _____________________________________ 0
`.trim());
Expand Down Expand Up @@ -286,13 +286,17 @@ TTL _____________________________________ ${resJson.ttl}
await (await sdk.aensPreclaim(auctionName)).claim();
const resJson = await executeInspect([auctionName, '--json']);
expect(resJson).to.eql({
name: auctionName,
status: 'AVAILABLE',
endsAt: String(+resJson.startedAt + 14880),
highestBid: '19641800000000000000',
highestBidder: sdk.address,
id: resJson.id,
startedAt: resJson.startedAt,
status: 'AUCTION',
});
const res = await executeInspect([auctionName]);
expect(res).to.equal(`
Status __________________________________ AVAILABLE
Name hash _______________________________ N/A
Status __________________________________ AUCTION
Name hash _______________________________ ${resJson.id}
Pointers ________________________________ N/A
TTL _____________________________________ 0
`.trim());
Expand Down
17 changes: 4 additions & 13 deletions test/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('AENS Module', () => {
preClaim.blockHeight.should.be.gt(0);
preClaim.salt.should.be.a('number');
preClaim.commitmentId.should.contain('cm');
nameResult.name.should.be.equal(name2);
nameResult.id.should.satisfy((id) => id.startsWith('nm_'));
nameResult.status.should.equal('AVAILABLE');
}).timeout(4000);

Expand Down Expand Up @@ -231,23 +231,14 @@ describe('AENS Module', () => {
});

it('Fail on open again', async () => {
const preClaim = await executeName([
'pre-claim',
WALLET_NAME,
'--password',
'test',
name,
'--json',
]);
await executeName([
'claim',
'pre-claim',
WALLET_NAME,
'--password',
'test',
name,
preClaim.salt,
'--json',
]).should.be.rejectedWith('error: Transaction not found');
}).timeout(15000);
]).should.be.rejectedWith('AENS name is AUCTION and cannot be preclaimed');
});
});
});

0 comments on commit 75217c3

Please sign in to comment.