@ar.io/sdk
Version:
[](https://codecov.io/gh/ar-io/ar-io-sdk)
175 lines (174 loc) • 6.62 kB
JavaScript
import { assertConfirmationPrompt, assertEnoughBalanceForArNSPurchase, customTagsFromOptions, fundFromFromOptions, positiveIntegerFromOptions, recordTypeFromOptions, referrerFromOptions, requiredPositiveIntegerFromOptions, requiredStringFromOptions, stringArrayFromOptions, writeARIOFromOptions, } from '../utils.js';
export async function buyRecordCLICommand(o) {
const { ario, signerAddress } = writeARIOFromOptions(o);
const name = requiredStringFromOptions(o, 'name');
const type = recordTypeFromOptions(o);
const years = positiveIntegerFromOptions(o, 'years');
const fundFrom = fundFromFromOptions(o);
const referrer = referrerFromOptions(o);
const processId = o.processId;
if (processId === undefined) {
// TODO: Spawn ANT process, register it to ANT registry, get process ID
throw new Error('Process ID must be provided for buy-record');
}
if (!o.skipConfirmation) {
const existingRecord = await ario.getArNSRecord({
name,
});
if (existingRecord !== undefined) {
throw new Error(`ArNS Record ${name} is already owned`);
}
await assertEnoughBalanceForArNSPurchase({
ario,
address: signerAddress,
costDetailsParams: {
intent: 'Buy-Name',
type,
name,
years,
fundFrom,
fromAddress: signerAddress,
},
});
await assertConfirmationPrompt(`Are you sure you want to ${type} the record ${name}?`, o);
}
return ario.buyRecord({
name: requiredStringFromOptions(o, 'name'),
processId,
type,
years,
fundFrom: fundFromFromOptions(o),
paidBy: stringArrayFromOptions(o, 'paidBy'),
referrer,
}, customTagsFromOptions(o));
}
export async function upgradeRecordCLICommand(o) {
const name = requiredStringFromOptions(o, 'name');
const { ario, signerAddress } = writeARIOFromOptions(o);
const fundFrom = fundFromFromOptions(o);
const referrer = referrerFromOptions(o);
if (!o.skipConfirmation) {
const existingRecord = await ario.getArNSRecord({
name,
});
if (existingRecord === undefined) {
throw new Error(`ArNS Record ${name} does not exist`);
}
if (existingRecord.type === 'permabuy') {
throw new Error(`ArNS Record ${name} is already a permabuy`);
}
await assertEnoughBalanceForArNSPurchase({
ario,
address: signerAddress,
costDetailsParams: {
intent: 'Upgrade-Name',
name,
fundFrom,
fromAddress: signerAddress,
},
});
await assertConfirmationPrompt(`Are you sure you want to upgrade the lease of ${name} to a permabuy?`, o);
}
return ario.upgradeRecord({
name,
fundFrom,
paidBy: stringArrayFromOptions(o, 'paidBy'),
referrer,
});
}
export async function extendLeaseCLICommand(o) {
const name = requiredStringFromOptions(o, 'name');
const years = requiredPositiveIntegerFromOptions(o, 'years');
const fundFrom = fundFromFromOptions(o);
const referrer = referrerFromOptions(o);
const { ario, signerAddress } = writeARIOFromOptions(o);
if (!o.skipConfirmation) {
const existingRecord = await ario.getArNSRecord({
name,
});
if (existingRecord === undefined) {
throw new Error(`ArNS Record ${name} does not exist`);
}
if (existingRecord.type === 'permabuy') {
throw new Error(`ArNS Record ${name} is a permabuy and cannot be extended`);
}
await assertEnoughBalanceForArNSPurchase({
ario: ario,
address: signerAddress,
costDetailsParams: {
intent: 'Extend-Lease',
name,
years,
fundFrom,
fromAddress: signerAddress,
},
});
await assertConfirmationPrompt(`Are you sure you want to extend the lease of ${name} by ${years}?`, o);
}
return ario.extendLease({
name,
years,
paidBy: stringArrayFromOptions(o, 'paidBy'),
referrer,
}, customTagsFromOptions(o));
}
export async function increaseUndernameLimitCLICommand(o) {
const name = requiredStringFromOptions(o, 'name');
const increaseCount = requiredPositiveIntegerFromOptions(o, 'increaseCount');
const fundFrom = fundFromFromOptions(o);
const referrer = referrerFromOptions(o);
const { ario, signerAddress } = writeARIOFromOptions(o);
if (!o.skipConfirmation) {
const existingRecord = await ario.getArNSRecord({
name,
});
if (existingRecord === undefined) {
throw new Error(`ArNS Record ${name} does not exist`);
}
await assertEnoughBalanceForArNSPurchase({
ario,
address: signerAddress,
costDetailsParams: {
intent: 'Increase-Undername-Limit',
name,
quantity: increaseCount,
fundFrom,
fromAddress: signerAddress,
},
});
await assertConfirmationPrompt(`Are you sure you want to increase the undername limit of ${name} by ${increaseCount}?`, o);
}
return ario.increaseUndernameLimit({
name,
increaseCount,
paidBy: stringArrayFromOptions(o, 'paidBy'),
referrer,
}, customTagsFromOptions(o));
}
export async function requestPrimaryNameCLICommand(o) {
const { ario, signerAddress } = writeARIOFromOptions(o);
const fundFrom = fundFromFromOptions(o);
const referrer = referrerFromOptions(o);
const name = requiredStringFromOptions(o, 'name');
if (!o.skipConfirmation) {
// TODO: Assert name requested is not already owned?
// TODO: More assertions?
await assertEnoughBalanceForArNSPurchase({
ario,
address: signerAddress,
costDetailsParams: {
intent: 'Primary-Name-Request',
name,
fromAddress: signerAddress,
fundFrom,
},
});
await assertConfirmationPrompt(`Are you sure you want to request the primary name ${name}?`, o);
}
return ario.requestPrimaryName({
name,
fundFrom,
paidBy: stringArrayFromOptions(o, 'paidBy'),
referrer,
}, customTagsFromOptions(o));
}