TypeScript SDK
TypeScript SDK 可让您连接、探索并与 Aptos 区块链交互。您可以使用它请求数据、发送交易、设置测试环境等。
npm i @aptos-labs/ts-sdk 快速入门 查看快速入门,在 5 分钟内获得可运行的演示
账户 生成、恢复并为账户注资
获取数据 读取账本、账户和 View 函数数据
提交交易 构建、模拟、签名并提交交易
示例 SDK 仓库中的可运行示例目录
测试 展示 SDK 各项功能用法的端到端测试
来自已测试示例套件的 APT 转账
Section titled “来自已测试示例套件的 APT 转账”以下片段取自 SDK 仓库中的
simple_transfer.ts。
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();