Initializes a new instance of the Aptos class with the provided configuration. This allows you to interact with the Aptos blockchain using the specified network settings.
The configuration settings for the Aptos client.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
async function runExample() {
// Create a configuration for connecting to the Aptos testnet
const config = new AptosConfig({ network: Network.TESTNET });
// Initialize the Aptos client with the configuration
const aptos = new Aptos(config);
console.log("Aptos client initialized:", aptos);
}
runExample().catch(console.error);
Fetches all top-level domain names for a specified account.
The arguments for retrieving account domains.
A promise of an array of ANSName.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Fetching all top-level domain names for a specific account
const domains = await aptos.getAccountDomains({
accountAddress: "0x1", // replace with a real account address
options: {
limit: 10, // specify the number of names to fetch
offset: 0, // specify the offset for pagination
orderBy: "created_at", // specify the order by which to sort the names
where: {
// additional filters can be specified here
},
},
});
console.log(domains);
}
runExample().catch(console.error);
Fetches all names for an account, including both top-level domains and subdomains.
The arguments for fetching account names.
A promise of an array of ANSName.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Fetch account names for a specific address
const accountNames = await aptos.getAccountNames({
accountAddress: "0x1", // replace with a real account address
options: {
limit: 10, // specify how many names to fetch
orderBy: "name", // specify the order by which to sort the names
},
});
console.log(accountNames);
}
runExample().catch(console.error);
Fetches all subdomain names for a specified account.
The arguments for retrieving subdomains.
A promise of an array of ANSName.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Fetching subdomain names for a specific account
const subdomains = await aptos.getAccountSubdomains({
accountAddress: "0x1", // replace with a real account address
options: {
limit: 10, // specify the number of subdomains to fetch
offset: 0, // specify the offset for pagination
orderBy: "name", // specify the order by which to sort the names
},
});
console.log(subdomains);
}
runExample().catch(console.error);
Fetches all subdomain names for a given domain, excluding the domain itself.
The arguments for fetching subdomains.
A promise that resolves to an array of ANSName.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Fetching subdomains for a specific domain
const subdomains = await aptos.getDomainSubdomains({
domain: "test", // replace with your domain
options: {
limit: 10, // specify the number of subdomains to fetch
offset: 0, // specify the starting point for fetching
orderBy: "name", // specify the order by which to sort the results
},
});
console.log(subdomains);
}
runExample().catch(console.error);
Retrieve the expiration time of a domain name or subdomain name from the contract.
The arguments for retrieving the expiration.
A string of the name to retrieve.
number as a unix timestamp in milliseconds.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Get the expiration time for the domain "test.aptos"
const exp = await aptos.getExpiration({ name: "test.aptos" });
// Log the expiration date
console.log(new Date(exp)); // Outputs the expiration date
}
runExample().catch(console.error);
Fetches a single name from the indexer based on the provided name argument.
The arguments for retrieving the name.
A string of the name to retrieve, e.g. "test.aptos.apt" or "test.apt" or "test". Can be inclusive or exclusive of the .apt suffix and can be a subdomain.
A promise of an ANSName or undefined if the name is not active.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Fetching a name from the indexer
const name = await aptos.getName({ name: "test.aptos" }); // replace with a real name
console.log(name);
}
runExample().catch(console.error);
Retrieve the owner address of a specified domain name or subdomain name from the contract.
The arguments for retrieving the owner address.
A string representing the name of the domain or subdomain to retrieve the owner address for.
AccountAddress if the name is owned, undefined otherwise.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Retrieve the owner address of "test.aptos"
const owner = await aptos.getOwnerAddress({ name: "test.aptos" });
console.log(owner); // Logs the owner address or undefined if not owned
}
runExample().catch(console.error);
Retrieve the primary name for an account. An account can have multiple names, but only one primary name, which may not exist.
The arguments for retrieving the primary name.
An AccountAddressInput (address) of the account.
A string if the account has a primary name, undefined otherwise.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Retrieve the primary name for the specified account address
const name = await aptos.getPrimaryName({ address: "0x1" }); // replace with a real account address
console.log(name);
}
runExample().catch(console.error);
Retrieve the target address of a domain or subdomain name, which indicates the address the name points to for use on-chain. Note that the target address can point to addresses that do not own the name.
The arguments for retrieving the target address.
A string representing the name, which can be a primary name, a subdomain, or a combination (e.g., "primary", "primary.apt", "secondary.primary", "secondary.primary.apt").
AccountAddress if the name has a target, undefined otherwise.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Retrieve the target address for the specified domain name
const targetAddr = await aptos.getTargetAddress({ name: "test.aptos" });
console.log(targetAddr); // Logs the target address, e.g., 0x123...
}
runExample().catch(console.error);
Registers a new name.
This function allows you to register a domain or subdomain name with specific expiration policies and options.
SimpleTransaction
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Registering a subdomain name assuming def.apt is already registered and belongs to the sender alice.
const txn = await aptos.registerName({
sender: "0x1", // replace with a real sender account
name: "test.aptos.apt",
expiration: {
policy: "subdomain:independent",
expirationDate: Date.now() + 30 * 24 * 60 * 60 * 1000, // expires in 30 days
},
});
console.log("Transaction:", txn);
}
runExample().catch(console.error);
Renews a domain name for one year. If a domain name was minted with V1 of the contract, it will automatically be upgraded to V2 via this transaction.
The arguments for renewing the domain.
A string representing the domain to renew. Subdomains cannot be renewed.
Optional
options?: InputGenerateTransactionOptionsOptional transaction options.
The sender account, which must be the domain owner.
Optional
years?: 1The number of years to renew the name. Currently, only one year is permitted.
SimpleTransaction
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Renew the domain "test" for one year
const transaction = await aptos.renewDomain({
sender: Account.generate(), // replace with a real account
name: "test"
});
console.log(transaction);
}
runExample().catch(console.error);
Sets the primary name for the sender account, allowing them to designate a single primary name among potentially multiple names. An account may not have a primary name.
The arguments for setting the primary name.
Optional
name?: stringA string representing the name to set as primary (e.g., "test.aptos").
Optional
options?: InputGenerateTransactionOptionsOptional transaction options.
The sender account.
SimpleTransaction
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Set the primary name for the sender account
const sender = Account.generate(); // replace with a real account
await aptos.setPrimaryName({ sender, name: "test.aptos" });
const primaryName = await aptos.getPrimaryName({ address: sender.accountAddress });
console.log("Primary Name:", primaryName); // Should log: "Primary Name: test.aptos"
}
runExample().catch(console.error);
Sets the target address of a domain or subdomain name, pointing it to a specified address for use on-chain. The target address can be different from the owner of the name.
The arguments for setting the target address.
The AccountAddressInput of the address to set the domain or subdomain to.
A string representing the domain or subdomain name (e.g., "test.aptos").
Optional
options?: InputGenerateTransactionOptionsOptional settings for generating the transaction.
The account initiating the transaction.
SimpleTransaction
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function runExample() {
// Setting the target address for a domain name
const sender = Account.generate(); // replace with a real account
const address = "0x1"; // replace with a real account address
await aptos.setTargetAddress({
sender: sender,
name: "test.aptos",
address: address,
});
const targetAddress = await aptos.getTargetAddress({ name: "test.aptos" });
console.log(targetAddress); // Should log the address set for "test.aptos"
}
runExample().catch(console.error);
A class to handle all
ANS
operations.