10分钟以内
第一笔 Agent 支付
在 Ergo testnet 上。
无需理论。无需繁琐设置。一个 npm 包、30行代码、一条命令——即可在 Ergo testnet explorer 上看到真实交易。
bash
npm install @fleet-sdk/core
前提条件
- 已安装 Node.js 18+
- 一个 testnet 地址(在 Nautilus wallet → Settings → Testnet mode 中创建)
- 从 testnet.ergofaucet.org 获取 testnet ERG
- 5分钟不受干扰的时间
01
安装 Fleet SDK
~30秒bash
mkdir my-agent && cd my-agent npm init -y npm install @fleet-sdk/core node-fetch
Fleet SDK 是 Ergo 官方的 TypeScript/JS SDK。支持 Node.js 和浏览器。
02
创建 agent-pay.js
~2分钟javascript
// agent-pay.js
import { TransactionBuilder, OutputBuilder, SAFE_MIN_BOX_VALUE } from "@fleet-sdk/core";
// ── Config ───────────────────────────────────────────────────────────────────
const TESTNET_API = "https://api-testnet.ergoplatform.com";
const YOUR_ADDRESS = "YOUR_TESTNET_ADDRESS"; // paste your testnet address
const RECEIVER_ADDRESS = "3WwbzW6u8hKWBcL1W7kNVMr25s2UHfSBnYtwSHvrRQt7DdPuoXrt"; // testnet receiver
// ── 1. Fetch unspent boxes ────────────────────────────────────────────────────
const res = await fetch(
`${TESTNET_API}/api/v1/boxes/unspent/byAddress/${YOUR_ADDRESS}`
);
const { items: inputs } = await res.json();
// ── 2. Build transaction ──────────────────────────────────────────────────────
const unsignedTx = new TransactionBuilder(await getCurrentHeight())
.from(inputs)
.to(
new OutputBuilder("1000000", RECEIVER_ADDRESS) // 0.001 ERG
)
.sendChangeTo(YOUR_ADDRESS)
.payMinFee()
.build()
.toEIP12Object();
console.log("Unsigned TX:", JSON.stringify(unsignedTx, null, 2));
// → Sign with Nautilus wallet or server-side key, then submit
async function getCurrentHeight() {
const r = await fetch(`${TESTNET_API}/api/v1/info`);
const info = await r.json();
return info.fullHeight;
}此脚本获取你的 testnet UTxO,构建交易,并输出未签名的 TX 对象。
03
运行脚本
~10秒bash
node agent-pay.js
你将看到未签名交易的 JSON。使用 Nautilus(浏览器)或服务端密钥签名,然后 POST 到 /api/v1/transactions。
04
添加 Note(Agent 支付)
~5分钟javascript
// Note payment — agent pays for an API call
import { TransactionBuilder, OutputBuilder, SByte, SColl } from "@fleet-sdk/core";
const TASK_HASH = "a1b2c3d4..."; // blake2b256 of task output
const noteOutput = new OutputBuilder("5000000", RECEIVER_ADDRESS) // 0.005 ERG
.setAdditionalRegisters({
R4: SColl(SByte, Buffer.from(TASK_HASH, "hex")), // task hash
R5: SByte(await getCurrentHeight() + 100), // expiry: +100 blocks
});
const tx = new TransactionBuilder(await getCurrentHeight())
.from(inputs)
.to(noteOutput)
.sendChangeTo(YOUR_ADDRESS)
.payMinFee()
.build();这扩展了基本 TX 以创建 Note —— 一种带有 task hash 寄存器的持有人票据。接收方通过 Reserve 兑换。
在 Explorer 上查看你的 TX
提交后,你的交易将出现在 Ergo testnet explorer 上。在以下地址粘贴你的 TX ID:
https://testnet.ergoplatform.com/transactions/{TX_ID}