@iota/iota-names-sdk
Version:
IOTA-Names SDK
446 lines (445 loc) • 18 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var iota_names_transaction_exports = {};
__export(iota_names_transaction_exports, {
IotaNamesTransaction: () => IotaNamesTransaction
});
module.exports = __toCommonJS(iota_names_transaction_exports);
var import_bcs = require("@iota/iota-sdk/bcs");
var import_utils = require("@iota/iota-sdk/utils");
var import_constants = require("./constants.js");
var import_helpers = require("./helpers.js");
var import_utils2 = require("./utils.js");
class IotaNamesTransaction {
constructor(client, transaction) {
this.iotaNamesClient = client;
this.transaction = transaction;
}
/**
* Registers a name.
*/
async register(params) {
const paymentIntent = this.initRegistration(params.name);
const couponCodes = params.couponCodes;
let discountedPrice = null;
if (couponCodes && couponCodes.length > 0) {
discountedPrice = await this.iotaNamesClient.calculateDiscountedPrice({
coupons: couponCodes,
name: params.name,
years: 1,
isRegistration: true,
address: params.address
});
for (const couponCode of couponCodes) {
this.applyCoupon(couponCode, paymentIntent);
}
}
const amounts = [discountedPrice ?? this.getBasePrice(paymentIntent)];
const payment = this.transaction.splitCoins(this.transaction.object(params.coin), amounts);
const receipt = this.generateReceipt({
paymentIntent,
payment,
coinConfig: params.coinConfig || { type: import_utils.IOTA_TYPE_ARG }
});
return this.finalizeRegister(receipt);
}
/**
* Registers a name for a given amount of years.
*/
async registerWithYears(params) {
const paymentIntent = this.initRegistrationWithYears(params.name, params.years);
const couponCodes = params.couponCodes;
let discountedPrice = null;
if (couponCodes && couponCodes.length > 0) {
discountedPrice = await this.iotaNamesClient.calculateDiscountedPrice({
coupons: couponCodes,
name: params.name,
years: params.years,
isRegistration: true,
address: params.address
});
for (const couponCode of couponCodes) {
this.applyCoupon(couponCode, paymentIntent);
}
}
const amounts = [discountedPrice ?? this.getBasePrice(paymentIntent)];
const payment = this.transaction.splitCoins(this.transaction.object(params.coin), amounts);
const receipt = this.generateReceipt({
paymentIntent,
payment,
coinConfig: params.coinConfig || { type: import_utils.IOTA_TYPE_ARG }
});
return this.finalizeRegister(receipt);
}
/**
* Renews an NFT for a number of years.
*/
async renew(params) {
const paymentIntent = this.initRenewal(params.nft, params.years);
const couponCodes = params.couponCodes;
let discountedPrice = null;
if (couponCodes && couponCodes.length > 0) {
discountedPrice = await this.iotaNamesClient.calculateDiscountedPrice({
coupons: couponCodes,
name: params.name,
years: params.years,
isRegistration: false,
address: params.address
});
for (const couponCode of couponCodes) {
this.applyCoupon(couponCode, paymentIntent);
}
}
const amounts = [discountedPrice ?? this.getBasePrice(paymentIntent)];
const payment = this.transaction.splitCoins(this.transaction.object(params.coin), amounts);
const receipt = this.generateReceipt({
paymentIntent,
payment,
coinConfig: params.coinConfig || { type: import_utils.IOTA_TYPE_ARG }
});
this.finalizeRenew(receipt, params.nft);
}
initRegistration(name) {
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const packageId = this.iotaNamesClient.getPackage("packageId");
return this.transaction.moveCall({
target: `${packageId}::payment::init_registration`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.pure.string(name)
]
});
}
initRegistrationWithYears(name, years) {
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const packageId = this.iotaNamesClient.getPackage("packageId");
return this.transaction.moveCall({
target: `${packageId}::payment::init_registration_with_years`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.pure.string(name),
this.transaction.pure.u8(years)
]
});
}
initRenewal(nft, years) {
const packageId = this.iotaNamesClient.getPackage("packageId");
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
return this.transaction.moveCall({
target: `${packageId}::payment::init_renewal`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(nft),
this.transaction.pure.u8(years)
]
});
}
handleBasePayment(paymentIntent, payment, paymentType) {
const paymentsPackageId = this.iotaNamesClient.getPackage("paymentsPackageId");
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
return this.transaction.moveCall({
target: `${paymentsPackageId}::payments::handle_base_payment`,
arguments: [this.transaction.object(iotaNamesObjectId), paymentIntent, payment],
typeArguments: [paymentType]
});
}
finalizeRegister(receipt) {
const packageId = this.iotaNamesClient.getPackage("packageId");
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
return this.transaction.moveCall({
target: `${packageId}::payment::register`,
arguments: [
receipt,
this.transaction.object(iotaNamesObjectId),
this.transaction.object.clock()
]
});
}
finalizeRenew(receipt, nft) {
const packageId = this.iotaNamesClient.getPackage("packageId");
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
return this.transaction.moveCall({
target: `${packageId}::payment::renew`,
arguments: [
receipt,
this.transaction.object(iotaNamesObjectId),
this.transaction.object(nft),
this.transaction.object.clock()
]
});
}
getBasePrice(paymentIntent) {
const packageId = this.iotaNamesClient.getPackage("packageId");
return this.transaction.moveCall({
target: `${packageId}::payment::request_base_amount`,
arguments: [paymentIntent]
});
}
applyCoupon(couponCode, paymentIntent) {
const couponsPackageId = this.iotaNamesClient.getPackage("couponsPackageId");
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
return this.transaction.moveCall({
target: `${couponsPackageId}::coupon_house::apply_coupon`,
arguments: [
paymentIntent,
this.transaction.object(iotaNamesObjectId),
this.transaction.pure.string(couponCode),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID)
]
});
}
generateReceipt(params) {
const receipt = this.handleBasePayment(
params.paymentIntent,
params.payment,
params.coinConfig.type
);
return receipt;
}
/**
* Creates a subname.
*/
createSubname({
parentNft,
name,
expirationTimestampMs,
allowChildCreation,
allowTimeExtension
}) {
if (!(0, import_utils2.isValidIotaName)(name)) throw new Error("Invalid IOTA names");
const isParentSubname = (0, import_helpers.isNestedSubname)(name);
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const subnamesPackageId = !isParentSubname ? this.iotaNamesClient.getPackage("subnamesPackageId") : null;
const tempSubnameProxyPackageId = isParentSubname ? this.iotaNamesClient.getPackage("tempSubnameProxyPackageId") : null;
const subNft = this.transaction.moveCall({
target: isParentSubname ? `${tempSubnameProxyPackageId}::subname_proxy::new` : `${subnamesPackageId}::subnames::new`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(parentNft),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID),
this.transaction.pure.string((0, import_utils2.normalizeIotaName)(name, "dot")),
this.transaction.pure.u64(expirationTimestampMs),
this.transaction.pure.bool(!!allowChildCreation),
this.transaction.pure.bool(!!allowTimeExtension)
]
});
return subNft;
}
/**
* Builds the PTB to create a leaf subname.
* Parent can be a `NameRegistration` or a `SubnameRegistration` object.
* Can be passed in as an ID or a TransactionArgument.
*/
createLeafSubname({
parentNft,
name,
targetAddress
}) {
if (!(0, import_utils2.isValidIotaName)(name)) throw new Error("Invalid IOTA names");
const isParentSubname = (0, import_helpers.isNestedSubname)(name);
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const subnamesPackageId = !isParentSubname ? this.iotaNamesClient.getPackage("subnamesPackageId") : null;
const tempSubnameProxyPackageId = isParentSubname ? this.iotaNamesClient.getPackage("tempSubnameProxyPackageId") : null;
this.transaction.moveCall({
target: isParentSubname ? `${tempSubnameProxyPackageId}::subname_proxy::new_leaf` : `${subnamesPackageId}::subnames::new_leaf`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(parentNft),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID),
this.transaction.pure.string((0, import_utils2.normalizeIotaName)(name, "dot")),
this.transaction.pure.address(targetAddress)
]
});
}
/**
* Removes a leaf subname.
*/
removeLeafSubname({ parentNft, name }) {
if (!(0, import_utils2.isValidIotaName)(name)) throw new Error("Invalid IOTA names");
const isParentSubname = (0, import_helpers.isNestedSubname)(name);
if (!(0, import_helpers.isSubname)(name)) throw new Error("This can only be invoked for subnames");
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const subnamesPackageId = !isParentSubname ? this.iotaNamesClient.getPackage("subnamesPackageId") : null;
const tempSubnameProxyPackageId = isParentSubname ? this.iotaNamesClient.getPackage("tempSubnameProxyPackageId") : null;
this.transaction.moveCall({
target: isParentSubname ? `${tempSubnameProxyPackageId}::subname_proxy::remove_leaf` : `${subnamesPackageId}::subnames::remove_leaf`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(parentNft),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID),
this.transaction.pure.string((0, import_utils2.normalizeIotaName)(name, "dot"))
]
});
}
/**
* Sets the target address of an NFT.
*/
setTargetAddress({
nft,
// Can be string or argument
address,
isSubname: isSubname2
}) {
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const packageId = !isSubname2 ? this.iotaNamesClient.getPackage("packageId") : null;
const tempSubnameProxyPackageId = isSubname2 ? this.iotaNamesClient.getPackage("tempSubnameProxyPackageId") : null;
this.transaction.moveCall({
target: isSubname2 ? `${tempSubnameProxyPackageId}::subname_proxy::set_target_address` : `${packageId}::controller::set_target_address`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(nft),
this.transaction.pure(import_bcs.bcs.option(import_bcs.bcs.Address).serialize(address).toBytes()),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID)
]
});
}
/**
* Sets a public name for the user.
*/
setPublic(name) {
if (!(0, import_utils2.isValidIotaName)(name)) throw new Error("Invalid IOTA names");
const packageId = this.iotaNamesClient.getPackage("packageId");
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
this.transaction.moveCall({
target: `${packageId}::controller::set_reverse_lookup`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.pure.string((0, import_utils2.normalizeIotaName)(name, "dot"))
]
});
}
/**
* Unsets a Public name for the user.
*/
unsetPublic() {
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const packageId = this.iotaNamesClient.getPackage("packageId");
this.transaction.moveCall({
target: `${packageId}::controller::unset_reverse_lookup`,
arguments: [this.transaction.object(iotaNamesObjectId)]
});
}
/**
* Edits the setup of a subname.
*/
editSetup({
parentNft,
name,
allowChildCreation,
allowTimeExtension
}) {
if (!(0, import_utils2.isValidIotaName)(name)) throw new Error("Invalid IOTA names");
const isParentSubname = (0, import_helpers.isNestedSubname)(name);
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const subnamesPackageId = !isParentSubname ? this.iotaNamesClient.getPackage("subnamesPackageId") : null;
const tempSubnameProxyPackageId = isParentSubname ? this.iotaNamesClient.getPackage("tempSubnameProxyPackageId") : null;
this.transaction.moveCall({
target: isParentSubname ? `${tempSubnameProxyPackageId}::subname_proxy::edit_setup` : `${subnamesPackageId}::subnames::edit_setup`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(parentNft),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID),
this.transaction.pure.string((0, import_utils2.normalizeIotaName)(name, "dot")),
this.transaction.pure.bool(!!allowChildCreation),
this.transaction.pure.bool(!!allowTimeExtension)
]
});
}
/**
* Extends the expiration of a subname.
*/
extendExpiration({
nft,
expirationTimestampMs
}) {
const subnamesPackageId = this.iotaNamesClient.getPackage("subnamesPackageId");
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
this.transaction.moveCall({
target: `${subnamesPackageId}::subnames::extend_expiration`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(nft),
this.transaction.pure.u64(expirationTimestampMs)
]
});
}
/**
* Sets the user data of an NFT.
*/
setUserData({
nft,
value,
key,
isSubname: isSubname2
}) {
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const packageId = !isSubname2 ? this.iotaNamesClient.getPackage("packageId") : null;
const tempSubnameProxyPackageId = isSubname2 ? this.iotaNamesClient.getPackage("tempSubnameProxyPackageId") : null;
if (!Object.values(import_constants.ALLOWED_METADATA).some((x) => x === key)) throw new Error("Invalid key");
this.transaction.moveCall({
target: isSubname2 ? `${tempSubnameProxyPackageId}::subname_proxy::set_user_data` : `${packageId}::controller::set_user_data`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(nft),
this.transaction.pure.string(key),
this.transaction.pure.string(value),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID)
]
});
}
/**
* Unsets the user data of an NFT.
*/
unsetUserData({
nft,
key,
isSubname: isSubname2
}) {
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const packageId = !isSubname2 ? this.iotaNamesClient.getPackage("packageId") : null;
const tempSubnameProxyPackageId = isSubname2 ? this.iotaNamesClient.getPackage("tempSubnameProxyPackageId") : null;
if (!Object.values(import_constants.ALLOWED_METADATA).some((x) => x === key)) throw new Error("Invalid key");
this.transaction.moveCall({
target: isSubname2 ? `${tempSubnameProxyPackageId}::subname_proxy::unset_user_data` : `${packageId}::controller::unset_user_data`,
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(nft),
this.transaction.pure.string(key),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID)
]
});
}
/**
* Burns an expired NFT to collect storage rebates.
*/
burnExpired({ nft, isSubname: isSubname2 }) {
const iotaNamesObjectId = this.iotaNamesClient.getPackage("iotaNamesObjectId");
const packageId = this.iotaNamesClient.getPackage("packageId");
this.transaction.moveCall({
target: `${packageId}::controller::${isSubname2 ? "burn_expired_subname" : "burn_expired"}`,
// Update this
arguments: [
this.transaction.object(iotaNamesObjectId),
this.transaction.object(nft),
this.transaction.object(import_utils.IOTA_CLOCK_OBJECT_ID)
]
});
}
}
//# sourceMappingURL=iota-names-transaction.js.map