跳转到内容

Python SDK 快速入门

  1. 安装 Python SDK

    该 SDK 需要 Python 3.12+

    Terminal window
    pip install aptos-sdk
  2. 创建项目文件

    Terminal window
    mkdir aptos-python-quickstart
    cd aptos-python-quickstart
    touch quickstart.py
  3. 设置 Aptos 客户端

    使用 v2 API(aptos_sdk.v2)。创建客户端后即会建立连接。

    import asyncio
    from aptos_sdk.v2 import Account, Aptos, AptosConfig, Network
    async def main():
    async with Aptos(AptosConfig(network=Network.DEVNET)) as aptos:
    print("Connected to Aptos devnet")
    asyncio.run(main())
  4. 创建并为账户注资

    先生成凭据,再为账户注资,网络才会识别其存在。在 localnet 和 devnet 上可使用水龙头。

    alice = Account.generate()
    bob = Account.generate()
    await aptos.faucet.fund_account(alice.address, 100_000_000)
    await aptos.faucet.fund_account(bob.address, 10_000_000)

    在 testnet 上,请从水龙头页面铸造。

  5. 转移 APT

    aptos.coin.transfer 会构建、签名并提交 APT 转账。随后等待交易上链。

    txn_hash = await aptos.coin.transfer(alice, bob.address, 1_000)
    result = await aptos.transaction.wait_for_transaction(txn_hash)
    print(f"Success: {result['success']}")

    有关更底层的构建 → 模拟 → 签名 → 提交 → 等待流程,请参阅 构建交易

"""Create two accounts, fund them, and transfer APT on devnet."""
import asyncio
from aptos_sdk.v2 import Account, Aptos, AptosConfig, Network
ALICE_INITIAL_BALANCE = 100_000_000
BOB_INITIAL_BALANCE = 10_000_000
TRANSFER_AMOUNT = 1_000
async def main():
async with Aptos(AptosConfig(network=Network.DEVNET)) as aptos:
alice = Account.generate()
bob = Account.generate()
print("=== Addresses ===")
print(f"Alice: {alice.address}")
print(f"Bob: {bob.address}")
await asyncio.gather(
aptos.faucet.fund_account(alice.address, ALICE_INITIAL_BALANCE),
aptos.faucet.fund_account(bob.address, BOB_INITIAL_BALANCE),
)
print("=== Initial Balances ===")
print(f"Alice: {await aptos.coin.balance(alice.address)}")
print(f"Bob: {await aptos.coin.balance(bob.address)}")
txn_hash = await aptos.coin.transfer(alice, bob.address, TRANSFER_AMOUNT)
result = await aptos.transaction.wait_for_transaction(txn_hash)
print(f"Transaction: {txn_hash}")
print(f"Success: {result['success']}")
print("=== Final Balances ===")
print(f"Alice: {await aptos.coin.balance(alice.address)}")
print(f"Bob: {await aptos.coin.balance(bob.address)}")
asyncio.run(main())

运行:

Terminal window
python quickstart.py

你刚刚学习了如何通过以下步骤转移 APT:

  1. 使用 Aptos 客户端连接网络。
  2. 创建并为账户注资。
  3. 提交转账并等待执行。