UNPKG

@ar.io/sdk

Version:

[![codecov](https://codecov.io/gh/ar-io/ar-io-sdk/graph/badge.svg?token=7dXKcT7dJy)](https://codecov.io/gh/ar-io/ar-io-sdk)

173 lines (172 loc) 7.06 kB
import { mARIOToken } from '../../types/token.js'; import { addressFromOptions, epochInputFromOptions, formatARIOWithCommas, fundFromFromOptions, getTokenCostParamsFromOptions, paginationParamsFromOptions, readARIOFromOptions, requiredAddressFromOptions, requiredStringFromOptions, } from '../utils.js'; export async function getGateway(o) { const address = requiredAddressFromOptions(o); const gateway = await readARIOFromOptions(o).getGateway({ address, }); return gateway ?? { message: `No gateway found for address ${address}` }; } export async function listGateways(o) { const gateways = await readARIOFromOptions(o).getGateways(paginationParamsFromOptions(o)); return gateways.items.length ? gateways : { message: 'No gateways found' }; } export async function listAllDelegatesCLICommand(o) { const delegates = await readARIOFromOptions(o).getAllDelegates(paginationParamsFromOptions(o)); return delegates.items.length ? delegates : { message: 'No delegates found' }; } export async function getGatewayDelegates(o) { const address = requiredAddressFromOptions(o); const result = await readARIOFromOptions(o).getGatewayDelegates({ address, ...paginationParamsFromOptions(o), }); return result.items?.length ? result : { message: `No delegates found for gateway ${address}`, }; } export async function getDelegations(o) { const address = requiredAddressFromOptions(o); const result = await readARIOFromOptions(o).getDelegations({ address, ...paginationParamsFromOptions(o), }); return result.items?.length ? result : { message: `No delegations found for address ${address}`, }; } export async function getAllowedDelegates(o) { const address = requiredAddressFromOptions(o); const result = await readARIOFromOptions(o).getAllowedDelegates({ address, ...paginationParamsFromOptions(o), }); return result.items?.length ? result : { message: `No allow list found for gateway delegate ${address}`, }; } export async function getArNSRecord(o) { const name = requiredStringFromOptions(o, 'name'); return ((await readARIOFromOptions(o).getArNSRecord({ name, })) ?? { message: `No record found for name ${name}` }); } export async function listArNSRecords(o) { const records = await readARIOFromOptions(o).getArNSRecords(paginationParamsFromOptions(o)); return records.items.length ? records : { message: 'No records found' }; } export async function getArNSReservedName(o) { const name = requiredStringFromOptions(o, 'name'); return ((await readARIOFromOptions(o).getArNSReservedName({ name, })) ?? { message: `No reserved name found for name ${name}` }); } export async function listArNSReservedNames(o) { const reservedNames = await readARIOFromOptions(o).getArNSReservedNames(paginationParamsFromOptions(o)); return reservedNames.items.length ? reservedNames : { message: 'No reserved names found' }; } export async function getArNSReturnedName(o) { const name = requiredStringFromOptions(o, 'name'); const result = await readARIOFromOptions(o).getArNSReturnedName({ name }); return result ?? { message: `No returned name found for name ${name}` }; } export async function listArNSReturnedNames(o) { const returnedNames = await readARIOFromOptions(o).getArNSReturnedNames(paginationParamsFromOptions(o)); return returnedNames.items.length ? returnedNames : { message: 'No returned names found' }; } export async function getEpoch(o) { const epoch = await readARIOFromOptions(o).getEpoch(epochInputFromOptions(o)); return epoch ?? { message: `No epoch found for provided input` }; } export async function getPrescribedObservers(o) { const epoch = epochInputFromOptions(o); const result = await readARIOFromOptions(o).getPrescribedObservers(epoch); return (result ?? { message: `No prescribed observers found for epoch ${epoch}` }); } export async function getPrescribedNames(o) { const epoch = epochInputFromOptions(o); const result = await readARIOFromOptions(o).getPrescribedNames(epochInputFromOptions(o)); return result ?? { message: `No prescribed names found for epoch ${epoch}` }; } export async function getTokenCost(o) { const tokenCost = await readARIOFromOptions(o).getTokenCost(getTokenCostParamsFromOptions(o)); const output = { mARIOTokenCost: tokenCost, message: `The cost of the provided action is ${formatARIOWithCommas(new mARIOToken(tokenCost).toARIO())} ARIO`, }; return output; } export async function getCostDetails(o) { const costDetails = await readARIOFromOptions(o).getCostDetails({ ...getTokenCostParamsFromOptions(o), fundFrom: fundFromFromOptions(o), }); const output = { ...costDetails, message: `The cost of the provided action is ${formatARIOWithCommas(new mARIOToken(costDetails.tokenCost).toARIO())} ARIO${costDetails.fundingPlan && costDetails.fundingPlan.shortfall > 0 ? `. Insufficient funds for action. There is a shortfall of ${formatARIOWithCommas(new mARIOToken(costDetails.fundingPlan.shortfall).toARIO())} ARIO` : ''}`, }; return output; } export async function getPrimaryName(o) { const address = addressFromOptions(o); const name = o.name; const params = name !== undefined ? { name } : address !== undefined ? { address } : undefined; if (params === undefined) { throw new Error('Either --address or --name is required'); } const result = await readARIOFromOptions(o).getPrimaryName(params); return (result ?? { message: `No primary name found`, }); } export async function getGatewayVaults(o) { const address = requiredAddressFromOptions(o); const result = await readARIOFromOptions(o).getGatewayVaults({ address, ...paginationParamsFromOptions(o), }); return result.items?.length ? result : { message: `No vaults found for gateway ${address}`, }; } export async function getAllGatewayVaults(o) { const result = await readARIOFromOptions(o).getAllGatewayVaults(paginationParamsFromOptions(o)); return result.items?.length ? result : { message: `No vaults found`, }; } export async function getVault(o) { return readARIOFromOptions(o) .getVault({ address: requiredAddressFromOptions(o), vaultId: requiredStringFromOptions(o, 'vaultId'), }) .then((r) => r ?? { message: `No vault found for provided address and vault ID`, }); } export async function resolveArNSName(o) { const name = requiredStringFromOptions(o, 'name'); const result = await readARIOFromOptions(o).resolveArNSName({ name }); return result ?? { message: `No record found for name ${name}` }; }