跳转到内容

您的第一笔交易

交易是在Aptos区块链上更改数据的基本方式。将它们视为发送包裹:您需要指定要发送的内容、发送给谁,然后跟踪直到确认交付。在区块链术语中,交易允许您转移币、调用智能合约函数和更新链上状态。

本教程将指导您在Aptos区块链上创建和提交您的第一笔交易。您将学习如何:

  1. 设置您的开发环境
  2. 创建测试账户并为其提供资金
  3. 构建转移币的交易
  4. 模拟交易以估算成本
  5. 签名并提交交易
  6. 验证交易是否成功执行

在我们可以创建交易之前,我们需要使用必要的工具和SDK设置我们的开发环境。

  1. 安装TypeScript SDK

    使用您首选的包管理器安装TypeScript SDK:

    Terminal window
    npm install @aptos-labs/ts-sdk
  2. 创建项目目录

    为您的项目创建一个新目录:

    Terminal window
    mkdir my-first-transaction
    cd my-first-transaction
  3. 创建新文件

    创建一个名为transaction.ts的新文件:

    Terminal window
    touch transaction.ts

让我们首先导入处理交易所需的模块。

import {
Account,
Aptos,
AptosConfig,
Network,
} from "@aptos-labs/ts-sdk";

创建一个连接到Aptos测试网的客户端。

const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);

让我们创建两个账户:Alice(发送者)和Bob(接收者)。

async function createAccounts() {
// 创建两个账户
const alice = Account.generate();
const bob = Account.generate();
console.log("Alice的地址:", alice.accountAddress.toString());
console.log("Bob的地址:", bob.accountAddress.toString());
return { alice, bob };
}

在发送交易之前,我们需要为Alice的账户提供一些测试网APT。

async function fundAccounts(alice: Account, bob: Account) {
// 为Alice提供资金
await aptos.fundAccount({
accountAddress: alice.accountAddress,
amount: 100_000_000, // 1 APT (APT有8位小数)
});
console.log("Alice已获得资金");
}

现在让我们创建一个从Alice向Bob转移APT的交易。

async function transferCoins(alice: Account, bob: Account) {
const transaction = await aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::coin::transfer",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [bob.accountAddress, 1_000_000], // 0.01 APT
},
});
// 模拟交易以检查gas费用
const simulation = await aptos.transaction.simulate.simple({
signerPublicKey: alice.publicKey,
transaction,
});
console.log("模拟结果:", simulation);
// 签名并提交交易
const committedTransaction = await aptos.signAndSubmitTransaction({
signer: alice,
transaction,
});
console.log("交易提交:", committedTransaction.hash);
// 等待交易完成
await aptos.waitForTransaction({ transactionHash: committedTransaction.hash });
console.log("交易完成!");
return committedTransaction.hash;
}

让我们检查账户余额以确认交易成功。

async function checkBalances(alice: Account, bob: Account) {
const aliceBalance = await aptos.getAccountAPTAmount({
accountAddress: alice.accountAddress,
});
const bobBalance = await aptos.getAccountAPTAmount({
accountAddress: bob.accountAddress,
});
console.log(`Alice的余额: ${aliceBalance} Octas`);
console.log(`Bob的余额: ${bobBalance} Octas`);
}
import {
Account,
Aptos,
AptosConfig,
Network,
} from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
async function main() {
console.log("开始第一笔交易演示...");
// 1. 创建账户
const alice = Account.generate();
const bob = Account.generate();
console.log("Alice的地址:", alice.accountAddress.toString());
console.log("Bob的地址:", bob.accountAddress.toString());
// 2. 为Alice提供资金
await aptos.fundAccount({
accountAddress: alice.accountAddress,
amount: 100_000_000, // 1 APT
});
console.log("Alice已获得资金");
// 3. 检查初始余额
console.log("\n--- 初始余额 ---");
await checkBalances(alice, bob);
// 4. 转移币
console.log("\n--- 转移币 ---");
const txnHash = await transferCoins(alice, bob);
// 5. 检查最终余额
console.log("\n--- 最终余额 ---");
await checkBalances(alice, bob);
console.log(`\n✅ 交易成功! 哈希: ${txnHash}`);
}
async function transferCoins(alice: Account, bob: Account) {
const transaction = await aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::coin::transfer",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [bob.accountAddress, 1_000_000], // 0.01 APT
},
});
const committedTransaction = await aptos.signAndSubmitTransaction({
signer: alice,
transaction,
});
await aptos.waitForTransaction({ transactionHash: committedTransaction.hash });
return committedTransaction.hash;
}
async function checkBalances(alice: Account, bob: Account) {
const aliceBalance = await aptos.getAccountAPTAmount({
accountAddress: alice.accountAddress,
});
const bobBalance = await aptos.getAccountAPTAmount({
accountAddress: bob.accountAddress,
});
console.log(`Alice的余额: ${aliceBalance} Octas`);
console.log(`Bob的余额: ${bobBalance} Octas`);
}
// 运行主函数
main().catch(console.error);
Terminal window
npx tsx transaction.ts

恭喜!您已经成功创建并提交了您在Aptos区块链上的第一笔交易。您学会了如何:

  • 设置开发环境
  • 创建和资助账户
  • 构建、签名和提交交易
  • 验证交易结果