Skip to content

Commit

Permalink
Removes use of is_active for ANS queries (#418)
Browse files Browse the repository at this point in the history
Removes use of is_active for ANS queries
  • Loading branch information
kaw2k authored Jul 8, 2024
1 parent 2880363 commit 7ce0f98
Show file tree
Hide file tree
Showing 7 changed files with 542 additions and 852 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T

- [`Fix`] Support migrated coins in coin balance lookup indexer queries
- Add support for BlockEpilogueTransaction
- [`Fix`] Fixes a bug with ANS not returning subdomains with an expiration policy of 1 when the subdomain is expired but the parent domain is not.

# 1.22.2 (2024-06-26)

Expand Down
40 changes: 36 additions & 4 deletions src/internal/ans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@ export function isValidANSName(name: string): { domainName: string; subdomainNam
};
}

export enum SubdomainExpirationPolicy {
Independent = 0,
FollowsDomain = 1,
}

/**
* A helper function to determine if a given ANS name is considered active or
* not. Domains are considered active if their expiration date is in the
* future. Subdomains have two policies which modify their behavior. They can
* follow their parent's expiration (1) in which they ignore their own
* expiration timestamp or they can expire independently (0) in which they can
* expire before their parent but not afterwards.
*
* @param name - An ANS name returned from one of the functions of the SDK
* @returns A boolean representing if the contract considers the name active or not
*/
export function isActiveANSName(name: GetANSNameResponse[0]): boolean {
if (!name) return false;

const isTLDExpired = new Date(name.domain_expiration_timestamp).getTime() < Date.now();
const isExpired = new Date(name.expiration_timestamp).getTime() < Date.now();

// If we are a subdomain, if our parent is expired we are always expired
if (name.subdomain && isTLDExpired) return false;

// If we are a subdomain and our expiration policy is to follow the domain, we
// are active (since we know our parent is not expired by this point)
if (name.subdomain && name.subdomain_expiration_policy === SubdomainExpirationPolicy.FollowsDomain) return true;

// At this point, we are either a TLD or a subdomain with an independent
// expiration policy, we are active as long as we the expiration timestamp
return !isExpired;
}

export const LOCAL_ANS_ACCOUNT_PK =
process.env.ANS_TEST_ACCOUNT_PRIVATE_KEY ?? "0x37368b46ce665362562c6d1d4ec01a08c8644c488690df5a17e13ba163e20221";
export const LOCAL_ANS_ACCOUNT_ADDRESS =
Expand Down Expand Up @@ -344,7 +378,6 @@ export async function getName(args: {
const where: CurrentAptosNamesBoolExp = {
domain: { _eq: domainName },
subdomain: { _eq: subdomainName },
is_active: { _eq: true },
};

const data = await queryIndexer<GetNamesQuery>({
Expand All @@ -365,7 +398,7 @@ export async function getName(args: {
res = sanitizeANSName(res);
}

return res;
return isActiveANSName(res) ? res : undefined;
}

interface QueryNamesOptions {
Expand Down Expand Up @@ -492,13 +525,12 @@ export async function getDomainSubdomains(
...(args.options?.where ?? {}),
domain: { _eq: domain },
subdomain: { _neq: "" },
is_active: { _eq: true },
},
},
},
});

return data.current_aptos_names.map(sanitizeANSName);
return data.current_aptos_names.map(sanitizeANSName).filter(isActiveANSName);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/internal/queries/ansTokenFragment.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ fragment AnsTokenFragment on current_aptos_names {
token_standard
is_primary
owner_address
subdomain_expiration_policy
domain_expiration_timestamp
}
28 changes: 16 additions & 12 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type AnsTokenFragmentFragment = {
token_standard?: string | null;
is_primary?: boolean | null;
owner_address?: string | null;
subdomain_expiration_policy?: any | null;
domain_expiration_timestamp?: any | null;
};

export type CurrentTokenOwnershipFieldsFragment = {
Expand Down Expand Up @@ -94,15 +96,15 @@ export type GetAccountCoinsDataQueryVariables = Types.Exact<{

export type GetAccountCoinsDataQuery = {
current_fungible_asset_balances: Array<{
amount: any;
asset_type: string;
amount?: any | null;
asset_type?: string | null;
is_frozen: boolean;
is_primary: boolean;
last_transaction_timestamp: any;
last_transaction_version: any;
is_primary?: boolean | null;
last_transaction_timestamp?: any | null;
last_transaction_version?: any | null;
owner_address: string;
storage_id: string;
token_standard: string;
token_standard?: string | null;
metadata?: {
token_standard: string;
symbol: string;
Expand Down Expand Up @@ -392,15 +394,15 @@ export type GetCurrentFungibleAssetBalancesQueryVariables = Types.Exact<{

export type GetCurrentFungibleAssetBalancesQuery = {
current_fungible_asset_balances: Array<{
amount: any;
asset_type: string;
amount?: any | null;
asset_type?: string | null;
is_frozen: boolean;
is_primary: boolean;
last_transaction_timestamp: any;
last_transaction_version: any;
is_primary?: boolean | null;
last_transaction_timestamp?: any | null;
last_transaction_version?: any | null;
owner_address: string;
storage_id: string;
token_standard: string;
token_standard?: string | null;
}>;
};

Expand Down Expand Up @@ -509,6 +511,8 @@ export type GetNamesQuery = {
token_standard?: string | null;
is_primary?: boolean | null;
owner_address?: string | null;
subdomain_expiration_policy?: any | null;
domain_expiration_timestamp?: any | null;
}>;
};

Expand Down
2 changes: 2 additions & 0 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const AnsTokenFragmentFragmentDoc = `
token_standard
is_primary
owner_address
subdomain_expiration_policy
domain_expiration_timestamp
}
`;
export const CurrentTokenOwnershipFieldsFragmentDoc = `
Expand Down
Loading

0 comments on commit 7ce0f98

Please sign in to comment.