Skip to content

Commit

Permalink
feat(ar.io cli): init pagination from CLI layer PE-5854
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Nov 25, 2024
1 parent 2e9272d commit f52ce1f
Showing 5 changed files with 117 additions and 34 deletions.
63 changes: 39 additions & 24 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -28,17 +28,22 @@ import {
globalOptions,
joinNetworkOptions,
nameOptions,
paginationAddressOptions,
paginationOptions,
transferOptions,
} from './options.js';
import {
AddressOptions,
GetVaultOptions,
GlobalOptions,
NameOptions,
PaginationAddressOptions,
PaginationOptions,
} from './types.js';
import {
formatIOWithCommas,
makeCommand,
paginationParamsFromOptions,
readIOFromOptions,
requiredAddressFromOptions,
requiredNameFromOptions,
@@ -113,26 +118,31 @@ makeCommand({
makeCommand({
name: 'get-gateways',
description: 'Get the gateways of the network',
options: [],
options: paginationOptions,
}).action(async (_, command) => {
await runCommand<GlobalOptions>(command, async (options) => {
// TODO: Pagination from CLI
return (await readIOFromOptions(options).getGateways()).items;
await runCommand<PaginationOptions>(command, async (options) => {
const result = await readIOFromOptions(options).getGateways({
...paginationParamsFromOptions(options),
});

return result.items.length
? result.items
: { message: 'No gateways found' };
});
});

makeCommand({
name: 'get-gateway-delegates',
description: 'Get the delegates of a gateway',
options: addressOptions,
options: paginationAddressOptions,
}).action(async (_, command) => {
await runCommand<AddressOptions>(command, async (options) => {
await runCommand<PaginationAddressOptions>(command, async (options) => {
const address = requiredAddressFromOptions(options);
const result = await readIOFromOptions(options).getGatewayDelegates({
address,
...paginationParamsFromOptions(options),
});

// TODO: Paginate from CLI
return result.items?.length
? result.items
: {
@@ -146,17 +156,22 @@ makeCommand({
description: 'Get all stake delegated to gateways from this address',
options: addressOptions,
}).action(async (_, command) => {
await runCommand<AddressOptions>(command, async (options) => {
const address = requiredAddressFromOptions(options);
const result = await readIOFromOptions(options).getDelegations({ address });

// TODO: Paginate from CLI
return result.items?.length
? result.items
: {
message: `No delegations found for address ${address}`,
};
});
await runCommand<AddressOptions & PaginationOptions>(
command,
async (options) => {
const address = requiredAddressFromOptions(options);
const result = await readIOFromOptions(options).getDelegations({
address,
...paginationParamsFromOptions(options),
});

return result.items?.length
? result.items
: {
message: `No delegations found for address ${address}`,
};
},
);
});

makeCommand({
@@ -174,13 +189,13 @@ makeCommand({

makeCommand({
name: 'list-arns-records',
description: '',
options: [],
description: 'List all ArNS records',
options: paginationOptions,
}).action(async (_, command) => {
await runCommand<GlobalOptions>(command, async (options) => {
const result = await readIOFromOptions(options).getArNSRecords({});
console.log('result', result);
// TODO: Paginate params from CLI -- cursor is `name`
await runCommand<PaginationOptions>(command, async (options) => {
const result = await readIOFromOptions(options).getArNSRecords({
...paginationParamsFromOptions(options),
});
return result.items;
});
});
33 changes: 31 additions & 2 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
@@ -122,6 +122,22 @@ export const optionMap = {
alias: '--name <name>',
description: 'The ArNS name to interact with',
},
cursor: {
alias: '--cursor <cursor>',
description: 'The cursor for pagination',
},
limit: {
alias: '--limit <limit>',
description: 'The limit for pagination',
},
sortBy: {
alias: '--sort-by <sortBy>',
description: 'The field to sort by',
},
sortOrder: {
alias: '--sort-order <sortOrder>',
description: 'The order to sort by, either "asc" or "desc"',
},
};

export const globalOptions = [
@@ -138,8 +154,21 @@ export const walletOptions = [
optionMap.privateKey,
];

export const paginationOptions = [
...walletOptions,
optionMap.cursor,
optionMap.limit,
optionMap.sortBy,
optionMap.sortOrder,
];

export const addressOptions = [...walletOptions, optionMap.address];

export const paginationAddressOptions = [
...addressOptions,
...paginationOptions,
];

export const nameOptions = [optionMap.name];

export const getVaultOptions = [...addressOptions, optionMap.vaultId];
@@ -152,15 +181,15 @@ export const transferOptions = [

export const joinNetworkOptions = [
...walletOptions,
// optionMap.quantity,
optionMap.operatorStake,
optionMap.disableAutoStake,
optionMap.disableDelegatedStaking,
optionMap.minDelegatedStake,
optionMap.delegateRewardShareRatio,
optionMap.label,
optionMap.note,
optionMap.properties,
// optionMap.observer,
optionMap.observerAddress,
optionMap.fqdn,
optionMap.port,
optionMap.protocol,
8 changes: 7 additions & 1 deletion src/cli/types.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ import {
AoAddressParams,
AoGetArNSNameParams,
AoJoinNetworkParams,
PaginationParams,
} from '../types/io.js';

export type GlobalOptions = {
@@ -46,10 +47,15 @@ export type CLIOptionsFromAoParams<T> = Partial<{
[K in keyof T]: T[K] extends number ? string : T[K];
}>;

export type PaginationOptions = GlobalOptions &
CLIOptionsFromAoParams<PaginationParams>;

export type AddressOptions = WalletOptions &
CLIOptionsFromAoParams<AoAddressParams>;

export type NameOptions = WalletOptions &
export type PaginationAddressOptions = AddressOptions & PaginationOptions;

export type NameOptions = GlobalOptions &
CLIOptionsFromAoParams<AoGetArNSNameParams>;

export type GetVaultOptions = AddressOptions & {
39 changes: 33 additions & 6 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ import {
IO_DEVNET_PROCESS_ID,
IO_TESTNET_PROCESS_ID,
Logger,
PaginationParams,
fromB64Url,
sha256B64Url,
} from '../node/index.js';
@@ -35,6 +36,7 @@ import {
GlobalOptions,
JsonSerializable,
NameOptions,
PaginationOptions,
WalletOptions,
} from './types.js';

@@ -159,6 +161,15 @@ export function writeIOFromOptions(
});
}

export function formatIOWithCommas(value: IOToken): string {
const [integerPart, decimalPart] = value.toString().split('.');
const integerWithCommas = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
if (decimalPart === undefined) {
return integerWithCommas;
}
return integerWithCommas + '.' + decimalPart;
}

export function requiredAddressFromOptions(options: AddressOptions): string {
if (options.address !== undefined) {
return options.address;
@@ -180,11 +191,27 @@ export function requiredNameFromOptions(options: NameOptions): string {
throw new Error('No name provided. Use `--name "my-name"`');
}

export function formatIOWithCommas(value: IOToken): string {
const [integerPart, decimalPart] = value.toString().split('.');
const integerWithCommas = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
if (decimalPart === undefined) {
return integerWithCommas;
const defaultCliPaginationLimit = 10; // more friendly UX than 100
export function paginationParamsFromOptions(
options: PaginationOptions,
// TODO: Use a type for sortBy and we could assert against arrays of the fields we want sort by
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): PaginationParams & { sortBy: any } {
const { cursor, limit, sortBy, sortOrder } = options;
if (sortOrder !== undefined && !['asc', 'desc'].includes(sortOrder)) {
throw new Error(
`Invalid sort order: ${sortOrder}, must be "asc" or "desc"`,
);
}
return integerWithCommas + '.' + decimalPart;

const numberLimit = limit !== undefined ? +limit : defaultCliPaginationLimit;
if (isNaN(numberLimit) || numberLimit <= 0) {
throw new Error(`Invalid limit: ${numberLimit}, must be a positive number`);
}
return {
cursor,
limit: numberLimit,
sortBy,
sortOrder,
};
}
8 changes: 7 additions & 1 deletion src/types/io.ts
Original file line number Diff line number Diff line change
@@ -328,6 +328,12 @@ export type AoAddressParams = {
address: WalletAddress;
};

export type AoBalanceParams = {
address: WalletAddress;
};

export type AoGetArNSRecordsParams = PaginationParams<AoArNSNameDataWithName>;

// Interfaces

export interface AoIORead {
@@ -376,7 +382,7 @@ export interface AoIORead {
name: string;
}): Promise<AoArNSNameData | undefined>;
getArNSRecords(
params?: PaginationParams<AoArNSNameDataWithName>,
params?: AoGetArNSRecordsParams,
): Promise<PaginationResult<AoArNSNameDataWithName>>;
getArNSReservedNames(
params?: PaginationParams<AoArNSReservedNameDataWithName>,

0 comments on commit f52ce1f

Please sign in to comment.