SuperAPI AI 工具集

Bankr Dev - Arbitrary Transactions

编程与开发

查看项目

更新于 10-15 16:56

技能介绍

用途

Bankr Dev - Arbitrary Transactions 用于在构建应用时,通过 Bankr API 提交原始 EVM 交易、自定义合约调用,或执行预先构建好的 calldata。它面向需要直接与链上合约交互、而不依赖更高层封装接口的开发场景。

主要能力

该技能支持以下操作:

  • 提交 calldata:在任意支持的链上执行预构建的 calldata。
  • 自定义合约调用:使用原始函数调用与任意合约交互。
  • 带数据的价值转移:在执行 calldata 的同时发送 ETH/MATIC。

JSON Schema

提交交易时使用如下 JSON 结构:

{
  "to": "0x...",
  "data": "0x...",
  "value": "0",
  "chainId": 8453
}

字段说明:

字段 类型 必填 说明
to string 是 合约地址(0x + 40 个十六进制字符)
data string 是 Calldata(0x + 十六进制,或 "0x" 表示空)
value string 是 以字符串表示的 wei 数量
chainId number 是 目标链(1、137 或 8453)

支持的链

链 Chain ID
Ethereum 1
Polygon 137
Base 8453
Unichain 130

使用方式

在代码中引入 bankr-client 的 execute 函数,将交易对象序列化后作为提示提交:

import { execute } from "./bankr-client";

// Submit arbitrary transaction
const txJson = {
  to: "0x1234567890abcdef1234567890abcdef12345678",
  data: "0xa9059cbb...",
  value: "0",
  chainId: 8453
};

await execute(`Submit this transaction: ${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> {
  // Validate before submission
  if (!tx.to.match(/^0x[a-fA-F0-9]{40}$/)) {
    throw new Error("Invalid address format");
  }
  if (!tx.data.startsWith("0x")) {
    throw new Error("Calldata must start with 0x");
  }
  if (![1, 137, 8453, 130].includes(tx.chainId)) {
    throw new Error("Unsupported chain");
  }

  const prompt = `Submit this transaction: ${JSON.stringify(tx)}`;
  await execute(prompt);
}

// Example: ERC-20 transfer
await submitArbitraryTx({
  to: "0xTokenContractAddress...",
  data: "0xa9059cbb000000000000000000000000...", // transfer(address,uint256)
  value: "0",
  chainId: 8453
});

错误处理

针对常见错误可做如下处理:

try {
  await submitArbitraryTx(tx);
} catch (error) {
  if (error.message.includes("reverted")) {
    // Transaction reverted - check calldata encoding
    console.error("Transaction reverted:", error);
  } else if (error.message.includes("insufficient")) {
    // Not enough funds for gas + value
    console.error("Insufficient funds:", error);
  } else if (error.message.includes("unsupported")) {
    // Chain not supported
    console.error("Unsupported chain:", error);
  }
}

适用场景

  • 需要提交原始 EVM 交易的应用。
  • 需要执行自定义合约调用的应用。
  • 需要执行预构建 calldata 的应用。
  • 需要在执行 calldata 的同时发送原生代币的场景。

相关技能

  • bankr-client-patterns:客户端设置与 execute 函数。
  • bankr-api-basics:API 基础知识。
  • bankr-token-trading:更高层的交易操作。

限制

资料中未提供关于收费、价格、平台限制或性能效果的更多信息。支持的链以资料列出的 Ethereum、Polygon、Base、Unichain 为准。