AutoGen支付集成。
没有 AutoGen 包,您不需要一个。将真实的 blockchain0x 客户端包装在一个函数中,交给您的代理,它可以在 Base 上移动 USDC。
没有 AutoGen-specific package,而且你也不需要。AutoGen 已经会把普通的 typed function 转换为工具,因此你只需把真实的 Python client 封装成函数,通过 交给 ,代理就能在 Base 上发送 USDC、结算 invoice、读取钱包。设置 即可开始。
AutoGen已经将函数变成了工具。使用它。
AutoGen直接从Python函数的签名和文档字符串构建工具架构。一个专用的AutoGen适配器只会包装一个我们本来会写的函数,然后与AutoGen快速发展的API不同步。因此我们不提供一个。你写几行代码来调用真实的blockchain0x客户端,并完全控制参数、验证和错误处理。
这就是已发布的 LangChain 和 CrewAI 适配器在底层包装的相同客户端。在这里,您自己进行包装,这意味着没有适配器版本需要跟踪,并且在您的代理和 API 之间没有其他内容,只有您自己的函数。多代理团队以相同的方式工作:将钱包功能提供给一个代理,其他代理不使用它,支出权限保持在一个地方。
安装AutoGen和核心SDK。一个环境变量。
没有可以添加的专用 AutoGen 包。您安装 AutoGen(Python 3.10+)和真正的 blockchain0x 核心 SDK,然后编写下面的函数。这就是整个依赖列表。
pip install autogen-agentchat "autogen-ext[openai]" blockchain0x
export BLOCKCHAIN0X_API_KEY=sk_test_... # sk_test_ = Base Sepolia, sk_live_ = Base mainnet
BLOCKCHAIN0X_API_KEY 是你 dashboard 中的 sk_test_ testnet 或 sk_live_ mainnet key;client 会从环境变量读取它。如果你的 agent 也会收款,webhook handler 还需要 BLOCKCHAIN0X_WEBHOOK_SECRET,该值会在你创建或轮换 webhook 时由 dashboard 仅返回一次。
一个支付的函数,交给 AssistantAgent。
以下是整个集成。send_usdc调用真实的blockchain0x客户端;AssistantAgent(tools=[send_usdc])将其转变为模型可以调用的工具。AutoGen读取类型提示和文档字符串以构建架构,因此请保持它们的准确性。运行它,代理在Base上移动USDC。
from autogen_agentchat.agents import AssistantAgent from autogen_ext.models.openai import OpenAIChatCompletionClient from blockchain0x import Client blockchain0x = Client() # reads BLOCKCHAIN0X_API_KEY from the environment # AutoGen turns a plain typed function into a tool. No dedicated package needed. def send_usdc(agent_id: str, to: str, amount_wei: str) -> str: """Send a USDC payment from an agent wallet. amount_wei is USDC base units (6 decimals), so "10000" is 0.01 USDC. """ result = blockchain0x.payments.create( body={"agentId": agent_id, "to": to, "amountWei": amount_wei}, ) return str(result) treasurer = AssistantAgent( name="treasurer", model_client=OpenAIChatCompletionClient(model="gpt-4o"), tools=[send_usdc], system_message="You pay vendor invoices in USDC within owner-set limits.", ) result = await treasurer.run( task="Pay 0.01 USDC from agent agt_123 to 0xVendor for the dataset.", )
When the agent decides to pay, it calls send_usdc, the SDK submits the transfer, and you get a transaction hash back. amount_wei is base units, so 0.01 USDC is "10000". A sk_test_ key keeps it on Base Sepolia until you switch to sk_live_. Add more functions - read a wallet, settle an invoice - the same way; each becomes another entry in tools.
通过签名的 webhook 确认入站支付。
如果您的代理还接收USDC,请通过Webhook确认,而不是轮询。验证助手包含在Node SDK中;在Python服务中,您手动验证文档中的HMAC,这就是助手所做的一切。下面是FastAPI示例;相同的代码适用于任何异步Python框架。
import hmac, hashlib, os, time from fastapi import FastAPI, Request, HTTPException app = FastAPI() SECRET = os.environ["BLOCKCHAIN0X_WEBHOOK_SECRET"].encode() @app.post("/webhooks/payment") async def receive(request: Request): raw = await request.body() # RAW bytes - do not parse first sig = request.headers.get("X-Blockchain0x-Signature", "") ts = request.headers.get("X-Blockchain0x-Timestamp", "") parts = dict(p.split("=", 1) for p in sig.split(",") if "=" in p) t, v1 = parts.get("t", ts), parts.get("v1", sig) want = hmac.new(SECRET, t.encode() + b"." + raw, hashlib.sha256).hexdigest() if not hmac.compare_digest(want, v1) or abs(time.time() - int(t)) > 300: raise HTTPException(status_code=401) if request.headers.get("X-Blockchain0x-Event-Type") == "payment.received": await trigger_followup() # USDC landed - run the next step return {"ok": True}
该算法是针对字符串 t.rawBody 的 HMAC-SHA256,具有常量时间比较和 300 秒的重放窗口。通过 await request.body() 读取原始主体,绝不要 request.json() 重新序列化,因为这会更改签名覆盖的字节。已发布的事件是 payment.received、payment.sent、wallet.deployed 和 webhook.test。对于重的后续工作,排队一个作业(Celery、RQ、arq)并立即响应 200,而不是阻塞处理程序。
您正在包装的客户端是开放的。阅读它。
没有可克隆的 AutoGen starter package - 上面的 recipe 就是集成方式。blockchain0x SDK 在 GitHub 上开源;这个 recipe 封装了 Python SDK (blockchain0x-python),完整的方法表面已记录在 docs 中。可将其作为 function bodies 的参考。
github.com/tosh-labs/blockchain0x-python完整的 SDK method surface 和 scopes 已记录在 the docs 中。先使用 sk_test_ key 对接 Base Sepolia 进行测试,然后在 function 符合预期后切换到 sk_live_。
五个需要避免的群聊和异步陷阱。
AutoGen的多代理 + 异步设计解锁了强大的模式,但也引入了自己的陷阱。这些来自我们的支持收件箱。
没有 AutoGen 包 - 您包装 SDK
Blockchain0x 为 LangChain 和 CrewAI 以及 MCP 服务器提供适配器;没有专门的 AutoGen 包。上面的配方是支持的路径:一个普通类型化函数调用真实的 blockchain0x 客户端,传递给 AssistantAgent(tools=[...])。AutoGen 读取函数签名和文档字符串以构建工具架构,因此请保持文档字符串的准确性。
金额是USDC基本单位,以字符串形式表示
payments.create takes amountWei: a string of USDC base units, not a float and not a dollar figure. USDC has 6 decimals, so 0.01 USDC is "10000" and 5 USDC is "5000000". Type the tool argument as str and validate it before the call - a float that sneaks through either errors at the backend or moves a millionth of what you meant.
send_payment 可能会提前返回 503
payments.create 默认不重试,并且在链适配器连接到您的网络之前可能返回 503。不要让群聊在失败的支付上循环 - 在您的函数中捕获错误,返回模型可以采取的清晰消息,并依赖自动铸造的幂等性密钥,以便手动重试不会重复支付。
AutoGen 0.4+是异步的;SDK调用是同步的
AutoGen 0.4在异步循环中运行工具。blockchain0x客户端调用是同步的,这对于快速请求是可以的,但慢调用会阻塞循环。如果你在处理真实交易量,请用asyncio.to_thread包装SDK调用,以便事件循环保持响应。对于每次调用一个支付,直接调用是可以的。
根据原始主体验证传入的 webhook
如果您的代理还接收资金,请通过Webhook确认,而不是轮询。验证原始请求字节上的签名(await request.body()),绝不要请求.json()重新序列化,因为HMAC覆盖了确切的字节。它是HMAC-SHA256,具有300秒的重放窗口。下面的处理程序就是全部内容。