From 746b3b9b6a5836370eefd55c11988e8aad275b4d Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Tue, 10 Dec 2024 11:51:51 -0500 Subject: [PATCH] get prices and cleanup --- sdk/src/suins-client.ts | 117 ++++++++++++++++++++++------------------ sdk/src/types.ts | 6 +-- 2 files changed, 66 insertions(+), 57 deletions(-) diff --git a/sdk/src/suins-client.ts b/sdk/src/suins-client.ts index cce512b3..d01d04d7 100644 --- a/sdk/src/suins-client.ts +++ b/sdk/src/suins-client.ts @@ -38,7 +38,7 @@ export class SuinsClient { } /** - * Returns the price list for SuiNS names. + * Returns the price list for SuiNS names in the base asset. */ // Format: @@ -47,7 +47,7 @@ export class SuinsClient { // [ 4, 4 ] => 100000000, // [ 5, 63 ] => 20000000 // } - async getPriceList(): Promise { + async getPriceList(): Promise { if (!this.constants.suinsObjectId) throw new Error('Suins object ID is not set'); if (!this.constants.suinsPackageId) throw new Error('Price list config not found'); @@ -92,7 +92,7 @@ export class SuinsClient { } /** - * Returns the renewal price list for SuiNS names. + * Returns the renewal price list for SuiNS names in the base asset. */ // Format: @@ -101,7 +101,7 @@ export class SuinsClient { // [ 4, 4 ] => 100000000, // [ 5, 63 ] => 20000000 // } - async getRenewalPriceList(): Promise { + async getRenewalPriceList(): Promise { if (!this.constants.suinsObjectId) throw new Error('Suins object ID is not set'); if (!this.constants.suinsPackageId) throw new Error('Price list config not found'); @@ -152,43 +152,44 @@ export class SuinsClient { return priceMap; } - async getNameRecord(name: string): Promise { - if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name'); - if (!this.constants.suinsPackageId) throw new Error('Suins package ID is not set'); - if (!this.constants.registryTableId) throw new Error('Registry table ID is not set'); - - const nameRecord = await this.#client.getDynamicFieldObject({ - parentId: this.constants.registryTableId, - name: { - type: getDomainType(this.constants.suinsPackageId.v1), - value: normalizeSuiNSName(name, 'dot').split('.').reverse(), - }, - }); - const fields = nameRecord.data?.content; - - // in case the name record is not found, return null - if (nameRecord.error?.code === 'dynamicFieldNotFound') return null; - - if (nameRecord.error || !fields || fields.dataType !== 'moveObject') - throw new Error('Name record not found. This domain is not registered.'); - const content = fields.fields as Record; - - const data: Record = {}; - content.value.fields.data.fields.contents.forEach((item: any) => { - // @ts-ignore-next-line - data[item.fields.key as string] = item.fields.value; - }); - - return { - name, - nftId: content.value.fields?.nft_id, - targetAddress: content.value.fields?.target_address!, - expirationTimestampMs: content.value.fields?.expiration_timestamp_ms, - data, - avatar: data.avatar, - contentHash: data.content_hash, - }; - } + // async getNameRecord(name: string): Promise { + // if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name'); + // if (!this.constants.suinsPackageId) throw new Error('Suins package ID is not set'); + + // const nameRecord = await this.#client.getDynamicFieldObject({ + // parentId: this.constants.suinsObjectId!, + // name: { + // type: getDomainType(this.constants.suinsPackageId.v1), + // value: normalizeSuiNSName(name, 'dot').split('.').reverse(), + // }, + // }); + + // return nameRecord; + // // const fields = nameRecord.data?.content; + + // // // in case the name record is not found, return null + // // if (nameRecord.error?.code === 'dynamicFieldNotFound') return null; + + // // if (nameRecord.error || !fields || fields.dataType !== 'moveObject') + // // throw new Error('Name record not found. This domain is not registered.'); + // // const content = fields.fields as Record; + + // // const data: Record = {}; + // // content.value.fields.data.fields.contents.forEach((item: any) => { + // // // @ts-ignore-next-line + // // data[item.fields.key as string] = item.fields.value; + // // }); + + // // return { + // // name, + // // nftId: content.value.fields?.nft_id, + // // targetAddress: content.value.fields?.target_address!, + // // expirationTimestampMs: content.value.fields?.expiration_timestamp_ms, + // // data, + // // avatar: data.avatar, + // // contentHash: data.content_hash, + // // }; + // } /** * Calculates the registration or renewal price for an SLD (Second Level Domain). @@ -209,14 +210,25 @@ export class SuinsClient { years: number; priceList: SuinsPriceList; }) { - if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name'); + if (!isValidSuiNSName(name)) { + throw new Error('Invalid SuiNS name'); + } validateYears(years); - if (isSubName(name)) throw new Error('Subdomains do not have a registration fee'); + + if (isSubName(name)) { + throw new Error('Subdomains do not have a registration fee'); + } const length = normalizeSuiNSName(name, 'dot').split('.')[0].length; - if (length === 3) return years * priceList.threeLetters; - if (length === 4) return years * priceList.fourLetters; - return years * priceList.fivePlusLetters; + + for (const [[minLength, maxLength], pricePerYear] of priceList.entries()) { + if (length >= minLength && length <= maxLength) { + return years * pricePerYear; + } + } + + // If no matching range is found, throw an error + throw new Error('No price available for the given name length'); } } @@ -235,10 +247,11 @@ export class SuinsClient { // }); // // Step 3: Fetch and log the renewal price list -// try { -// const renewalPriceList = await suinsClient.getRenewalPriceList(); -// console.log(renewalPriceList); -// } catch (error) { -// console.error('Error fetching renewal price list'); -// } +// const renewalPriceList = await suinsClient.getPriceList(); +// const price = suinsClient.calculatePrice({ +// name: 'example.sui', +// years: 2, +// priceList: renewalPriceList, +// }); +// console.log(price); // })(); diff --git a/sdk/src/types.ts b/sdk/src/types.ts index b71badcf..ba3cb6b6 100644 --- a/sdk/src/types.ts +++ b/sdk/src/types.ts @@ -41,11 +41,7 @@ export type SuinsClientConfig = { /** * The price list for SuiNS names. */ -export type SuinsPriceList = { - threeLetters: number; - fourLetters: number; - fivePlusLetters: number; -}; +export type SuinsPriceList = Map<[number, number], number>; /** * A NameRecord entry of SuiNS Names.