@ar.io/sdk
Version:
[](https://codecov.io/gh/ar-io/ar-io-sdk)
213 lines (212 loc) • 8.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buyRecordCLICommand = buyRecordCLICommand;
exports.upgradeRecordCLICommand = upgradeRecordCLICommand;
exports.extendLeaseCLICommand = extendLeaseCLICommand;
exports.increaseUndernameLimitCLICommand = increaseUndernameLimitCLICommand;
exports.requestPrimaryNameCLICommand = requestPrimaryNameCLICommand;
exports.setPrimaryNameCLICommand = setPrimaryNameCLICommand;
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const ant_js_1 = require("../../common/ant.js");
const utils_js_1 = require("../utils.js");
async function buyRecordCLICommand(o) {
const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
const type = (0, utils_js_1.recordTypeFromOptions)(o);
const years = (0, utils_js_1.positiveIntegerFromOptions)(o, 'years');
const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
const referrer = (0, utils_js_1.referrerFromOptions)(o);
const processId = o.processId;
if (!o.skipConfirmation) {
const existingRecord = await ario.getArNSRecord({
name,
});
if (existingRecord !== undefined) {
throw new Error(`ArNS Record ${name} is already owned`);
}
await (0, utils_js_1.assertEnoughBalanceForArNSPurchase)({
ario,
address: signerAddress,
costDetailsParams: {
intent: 'Buy-Name',
type,
name,
years,
fundFrom,
fromAddress: signerAddress,
},
});
// assert spawn new ant with module id
let antSpawnConfirmation = '';
if (processId === undefined) {
const { moduleId, version } = await ant_js_1.ANT.versions.getLatestANTVersion();
antSpawnConfirmation = `Note: A new ANT process will be spawned with module ${moduleId} (v${version}) and assigned to this name.`;
}
await (0, utils_js_1.assertConfirmationPrompt)(`Are you sure you want to ${type} the record ${name}? ${antSpawnConfirmation}`, o);
}
return ario.buyRecord({
name: (0, utils_js_1.requiredStringFromOptions)(o, 'name'),
processId,
type,
years,
fundFrom: (0, utils_js_1.fundFromFromOptions)(o),
paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
referrer,
}, (0, utils_js_1.customTagsFromOptions)(o));
}
async function upgradeRecordCLICommand(o) {
const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
const referrer = (0, utils_js_1.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 (0, utils_js_1.assertEnoughBalanceForArNSPurchase)({
ario,
address: signerAddress,
costDetailsParams: {
intent: 'Upgrade-Name',
name,
fundFrom,
fromAddress: signerAddress,
},
});
await (0, utils_js_1.assertConfirmationPrompt)(`Are you sure you want to upgrade the lease of ${name} to a permabuy?`, o);
}
return ario.upgradeRecord({
name,
fundFrom,
paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
referrer,
});
}
async function extendLeaseCLICommand(o) {
const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
const years = (0, utils_js_1.requiredPositiveIntegerFromOptions)(o, 'years');
const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
const referrer = (0, utils_js_1.referrerFromOptions)(o);
const { ario, signerAddress } = (0, utils_js_1.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 (0, utils_js_1.assertEnoughBalanceForArNSPurchase)({
ario: ario,
address: signerAddress,
costDetailsParams: {
intent: 'Extend-Lease',
name,
years,
fundFrom,
fromAddress: signerAddress,
},
});
await (0, utils_js_1.assertConfirmationPrompt)(`Are you sure you want to extend the lease of ${name} by ${years}?`, o);
}
return ario.extendLease({
name,
years,
paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
referrer,
}, (0, utils_js_1.customTagsFromOptions)(o));
}
async function increaseUndernameLimitCLICommand(o) {
const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
const increaseCount = (0, utils_js_1.requiredPositiveIntegerFromOptions)(o, 'increaseCount');
const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
const referrer = (0, utils_js_1.referrerFromOptions)(o);
const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
if (!o.skipConfirmation) {
const existingRecord = await ario.getArNSRecord({
name,
});
if (existingRecord === undefined) {
throw new Error(`ArNS Record ${name} does not exist`);
}
await (0, utils_js_1.assertEnoughBalanceForArNSPurchase)({
ario,
address: signerAddress,
costDetailsParams: {
intent: 'Increase-Undername-Limit',
name,
quantity: increaseCount,
fundFrom,
fromAddress: signerAddress,
},
});
await (0, utils_js_1.assertConfirmationPrompt)(`Are you sure you want to increase the undername limit of ${name} by ${increaseCount}?`, o);
}
return ario.increaseUndernameLimit({
name,
increaseCount,
paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
referrer,
}, (0, utils_js_1.customTagsFromOptions)(o));
}
async function requestPrimaryNameCLICommand(o) {
const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
const referrer = (0, utils_js_1.referrerFromOptions)(o);
const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
if (!o.skipConfirmation) {
await (0, utils_js_1.assertEnoughBalanceForArNSPurchase)({
ario,
address: signerAddress,
costDetailsParams: {
intent: 'Primary-Name-Request',
name,
fromAddress: signerAddress,
fundFrom,
},
});
await (0, utils_js_1.assertConfirmationPrompt)(`Are you sure you want to request the primary name ${name}?`, o);
}
const { result } = await ario.requestPrimaryName({
name,
fundFrom,
paidBy: (0, utils_js_1.stringArrayFromOptions)(o, 'paidBy'),
referrer,
}, (0, utils_js_1.customTagsFromOptions)(o));
if (result?.request === undefined) {
throw new Error('Failed to request primary name for name ' + name);
}
return result.request;
}
async function setPrimaryNameCLICommand(o) {
const { ario, signerAddress } = (0, utils_js_1.writeARIOFromOptions)(o);
const name = (0, utils_js_1.requiredStringFromOptions)(o, 'name');
const fundFrom = (0, utils_js_1.fundFromFromOptions)(o);
const referrer = (0, utils_js_1.referrerFromOptions)(o);
if (!o.skipConfirmation) {
await (0, utils_js_1.assertConfirmationPrompt)(`Are you sure you want to set the primary name ${name} for address ${signerAddress}?`, o);
}
return ario.setPrimaryName({ name, fundFrom, referrer });
}