TypeScript SDK
The TypeScript SDK allows you to connect, explore, and interact on the Aptos blockchain. You can use it to request data, send transactions, set up test environments, and more!
npm i @aptos-labs/ts-sdkExamples
Section titled “Examples” Quickstart See the quickstart to get a working demo in < 5 minutes
Accounts Generate, restore, and fund accounts
Fetching Data Read ledger, account, and view-function data
Submitting Transactions Build, simulate, sign, and submit transactions
Examples Catalog of runnable examples in the SDK repository
Tests End-to-end tests that demonstrate each SDK feature
Transfer APT from the tested example suite
Section titled “Transfer APT from the tested example suite”The snippet below is pulled from
simple_transfer.ts
in the SDK repository.
67 collapsed lines
/* eslint-disable no-console */
/** * This example shows how to use the Aptos client to create accounts, fund them, and transfer between them. */
import { Account, AccountAddress, Aptos, AptosConfig, Network, NetworkToNetworkName } from "@aptos-labs/ts-sdk";
const ALICE_INITIAL_BALANCE = 1_000_000_000;const BOB_INITIAL_BALANCE = 100;const TRANSFER_AMOUNT = 100;
// Default to devnet, but allow for overridingconst APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK ?? Network.DEVNET];
const balance = async ( aptos: Aptos, name: string, address: AccountAddress, versionToWaitFor?: bigint,): Promise<number> => { const amount = await aptos.getAccountAPTAmount({ accountAddress: address, minimumLedgerVersion: versionToWaitFor, }); console.log(`${name}'s balance is: ${amount}`); return amount;};
const example = async () => { console.log("This example will create two accounts (Alice and Bob), fund them, and transfer between them.");
// Set up the client const config = new AptosConfig({ network: APTOS_NETWORK }); const aptos = new Aptos(config);
// Create two accounts const alice = Account.generate(); const bob = Account.generate();
console.log("=== Addresses ===\n"); console.log(`Alice's address is: ${alice.accountAddress}`); console.log(`Bob's address is: ${bob.accountAddress}`);
// Fund the accounts console.log("\n=== Funding accounts ===\n");
const aliceFundTxn = await aptos.fundAccount({ accountAddress: alice.accountAddress, amount: ALICE_INITIAL_BALANCE, }); console.log("Alice's fund transaction: ", aliceFundTxn);
const bobFundTxn = await aptos.fundAccount({ accountAddress: bob.accountAddress, amount: BOB_INITIAL_BALANCE, }); console.log("Bob's fund transaction: ", bobFundTxn);
// Show the balances console.log("\n=== Balances ===\n"); const aliceBalance = await balance(aptos, "Alice", alice.accountAddress); const bobBalance = await balance(aptos, "Bob", bob.accountAddress);
if (aliceBalance !== ALICE_INITIAL_BALANCE) throw new Error("Alice's balance is incorrect"); if (bobBalance !== BOB_INITIAL_BALANCE) throw new Error("Bob's balance is incorrect");
// Transfer between users const txn = await aptos.transaction.build.simple({ sender: alice.accountAddress, data: { function: "0x1::aptos_account::transfer", functionArguments: [bob.accountAddress, TRANSFER_AMOUNT], }, });
console.log("\n=== Transfer transaction ===\n"); const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn });
await aptos.waitForTransaction({ transactionHash: committedTxn.hash }); console.log(`Committed transaction: ${committedTxn.hash}`);15 collapsed lines
console.log("\n=== Balances after transfer ===\n"); const newAliceBalance = await balance(aptos, "Alice", alice.accountAddress); const newBobBalance = await balance(aptos, "Bob", bob.accountAddress);
// Bob should have the transfer amount if (newBobBalance !== TRANSFER_AMOUNT + BOB_INITIAL_BALANCE) throw new Error("Bob's balance after transfer is incorrect");
// Alice should have the remainder minus gas if (newAliceBalance >= ALICE_INITIAL_BALANCE - TRANSFER_AMOUNT) throw new Error("Alice's balance after transfer is incorrect");};
example();