名称: Bankr开发 - 任意交易
描述: 该技能应用于构建通过Bankr提交原始EVM交易、自定义合约调用或执行预构建calldata的应用。涵盖JSON格式、验证和集成模式。
版本: 1.0.0
任意交易能力
通过Bankr API提交具有显式calldata的原始EVM交易。
功能概述
| 操作 |
描述 |
| 提交calldata |
在任何支持的链上执行预构建的calldata |
| 自定义合约调用 |
使用原始函数调用与任何合约交互 |
| 带数据的价值转移 |
发送ETH/MATIC同时执行calldata |
JSON模式
{
"to": "0x...",
"data": "0x...",
"value": "0",
"chainId": 8453
}
| 字段 |
类型 |
必需 |
描述 |
to |
字符串 |
是 |
合约地址(0x + 40个十六进制字符) |
data |
字符串 |
是 |
Calldata(0x + 十六进制,或“0x”表示空) |
value |
字符串 |
是 |
以字符串表示的Wei金额 |
chainId |
数字 |
是 |
目标链(1, 137, 或 8453) |
支持的链
| 链 |
链ID |
| 以太坊 |
1 |
| Polygon |
137 |
| Base |
8453 |
| Unichain |
130 |
使用方式
import { execute } from "./bankr-client";
// 提交任意交易
const txJson = {
to: "0x1234567890abcdef1234567890abcdef12345678",
data: "0xa9059cbb...",
value: "0",
chainId: 8453
};
await execute(`提交此交易: ${JSON.stringify(txJson)}`);
集成模式
import { execute } from "./bankr-client";
interface ArbitraryTx {
to: string;
data: string;
value: string;
chainId: number;
}
async function submitArbitraryTx(tx: ArbitraryTx): Promise<void> {
// 提交前验证
if (!tx.to.match(/^0x[a-fA-F0-9]{40}$/)) {
throw new Error("地址格式无效");
}
if (!tx.data.startsWith("0x")) {
throw new Error("Calldata必须以0x开头");
}
if (![1, 137, 8453, 130].includes(tx.chainId)) {
throw new Error("不支持的链");
}
const prompt = `提交此交易: ${JSON.stringify(tx)}`;
await execute(prompt);
}
// 示例:ERC-20转账
await submitArbitraryTx({
to: "0xTokenContractAddress...",
data: "0xa9059cbb000000000000000000000000...", // transfer(address,uint256)
value: "0",
chainId: 8453
});
错误处理
try {
await submitArbitraryTx(tx);
} catch (error) {
if (error.message.includes("reverted")) {
// 交易回滚 - 检查calldata编码
console.error("交易回滚:", error);
} else if (error.message.includes("insufficient")) {
// 资金不足用于gas和价值
console.error("资金不足:", error);
} else if (error.message.includes("unsupported")) {
// 链不支持
console.error("不支持的链:", error);
}
}
相关技能
bankr-client-patterns - 客户端设置和执行函数
bankr-api-basics - API基础
bankr-token-trading - 高级交易操作