名称: 使用-algokit-utils 描述: AlgoKit Utils库,用于从TypeScript或Python应用程序与Algorand区块链交互。适用于连接Algorand网络(LocalNet、TestNet、MainNet)、发送支付或转移资产、创建和管理账户、从客户端代码部署或交互智能合约,或组合交易组。不适用于编写智能合约代码(请使用构建智能合约技能)。强触发词包括“如何连接到Algorand?”、“发送支付交易”、“创建账户”、“部署我的合约”、“获取AlgorandClient”、“AlgorandClient.fromEnvironment”。
AlgoKit Utils
使用AlgoKit Utils从TypeScript或Python应用程序与Algorand区块链交互。
概述 / 核心工作流
- 创建一个
AlgorandClient实例 - 获取或创建用于签名的账户
- 使用
algorand.send.*方法发送交易 - 或使用
algorand.newGroup()组合交易组
如何操作
-
初始化 AlgorandClient:
TypeScript:
import { AlgorandClient } from '@algorandfoundation/algokit-utils' const algorand = AlgorandClient.fromEnvironment() // 或者: AlgorandClient.defaultLocalNet() // 或者: AlgorandClient.testNet() // 或者: AlgorandClient.mainNet()Python:
from algokit_utils import AlgorandClient algorand = AlgorandClient.from_environment() # 或者: AlgorandClient.default_localnet() # 或者: AlgorandClient.testnet() # 或者: AlgorandClient.mainnet() -
获取账户:
TypeScript:
const account = await algorand.account.fromEnvironment('DEPLOYER')Python:
account = algorand.account.from_environment("DEPLOYER") -
发送交易:
TypeScript:
await algorand.send.payment({ sender: account.addr, receiver: 'RECEIVERADDRESS', amount: algo(1), })Python:
algorand.send.payment(PaymentParams( sender=account.address, receiver="RECEIVERADDRESS", amount=AlgoAmount(algo=1), ))
重要规则 / 指南
- 使用 AlgorandClient — 这是主要入口点;避免使用已弃用的基于函数的API
- 默认使用 fromEnvironment() — 通过环境变量在本地和生产环境中均可工作
- 注册签名者 — 使用
algorand.account获取账户;签名者会自动注册 - 使用 algo() 辅助函数 — 对于TypeScript,使用
algo(1)而不是原始的微Algo
常见变体 / 边缘情况
| 场景 | 方法 |
|---|---|
| LocalNet 开发 | AlgorandClient.defaultLocalNet() |
| TestNet/MainNet | AlgorandClient.testNet() 或 .mainNet() |
| 自定义节点 | AlgorandClient.fromConfig({ algodConfig: {...} }) |
| 部署合约 | 使用类型化的应用客户端工厂(参见应用客户端文档) |
| 交易组 | algorand.newGroup().addPayment(...).addAssetOptIn(...).send() |
参考 / 延伸阅读
语言特定的参考文档组织在子文件夹中:
- TypeScript (
references/typescript/): AlgorandClient - Python (
references/python/): AlgorandClient
外部文档: