智能合约与以太坊 / 系统课时

根据 ABI 识别函数编码

根据 ABI 识别函数编码

DApp 调用合约时,发送的不是 Solidity 函数名,而是一串按 ABI 编码的字节。本课用一个代币转账接口,拆出函数选择器、输入参数、返回值与事件日志,并用 ethers v6 在本地编码、反解验证,无需连接测试网。

学习目标

  • 从 ABI 的函数和事件条目还原规范签名,识别四字节函数选择器。
  • 区分调用数据、函数返回数据与交易日志的编码位置。
  • 使用 ethers v6 验证一次转账调用的字节长度和解码结果。

从 ABI 还原函数签名

ABI(Application Binary Interface)是描述合约对外接口的结构化约定,不包含函数内部实现。标准 JSON ABI 中,type: "function" 表示函数,name 给出名称,inputs 与 outputs 分别声明入参与返回值的类型,stateMutability 标记 view、pure、payable 或 nonpayable。事件条目使用 type: "event" 和参数的 indexed 标志。

例如 transfer(address to, uint256 amount) returns (bool) 的规范签名是 transfer(address,uint256):只保留函数名和规范化输入类型,不含参数名、空格及返回类型。keccak256 计算签名的哈希,取前 4 字节得到选择器 0xa9059cbb。这里用的是 Ethereum 的 Keccak-256,不要误用标准 SHA3-256。uint 在规范签名中必须写为 uint256。

把调用数据与返回值分开看

对 transfer(0x0000000000000000000000000000000000000001, 25),calldata 前 4 字节是选择器,后面依次编码 address 和 uint256,每个静态参数占 32 字节,所以总长 4 + 32 + 32 = 68 字节。地址占参数字的低 20 字节,前面补零;数值 25 按大端整数编码,前面补零。这里的 25 是合约接收的整数,不会由 ABI 自动按代币小数位换算。

函数成功返回 bool true 时,返回数据是单个 32 字节编码字,没有函数选择器;它通常来自 eth_call 的结果或合约调用的返回数据,不是交易回执中的事件日志。

DApp 调用数据、EVM 返回数据与事件日志的编码关系

用 ethers v6 编码并反解

安装 Node.js 20 或更新的 LTS 版本,在空目录执行以下命令:

bash
npm init -y
npm install ethers@6

预期结果:命令成功退出,目录中有 package.json、package-lock.json 与 node_modules;依赖版本及 npm 安装日志随环境变化。

保存以下完整示例为 abi.mjs,使用标准 JSON ABI 对象而不是凭函数名称猜参数顺序:

js
import { Interface } from "ethers";

const abi = [
  {
    type: "function", name: "transfer", stateMutability: "nonpayable",
    inputs: [{ name: "to", type: "address" }, { name: "amount", type: "uint256" }],
    outputs: [{ name: "", type: "bool" }]
  },
  {
    type: "event", name: "Transfer", anonymous: false,
    inputs: [
      { name: "from", type: "address", indexed: true },
      { name: "to", type: "address", indexed: true },
      { name: "amount", type: "uint256", indexed: false }
    ]
  }
];
const iface = new Interface(abi);
const from = "0x0000000000000000000000000000000000000002";
const to = "0x0000000000000000000000000000000000000001";

// 函数调用编码:4 字节选择器加两个 32 字节参数
const calldata = iface.encodeFunctionData("transfer", [to, 25n]);
console.log("selector:", calldata.slice(0, 10));
console.log("calldata bytes:", (calldata.length - 2) / 2);
console.log("decoded amount:", iface.decodeFunctionData("transfer", calldata)[1].toString());

// 本地模拟成功返回值,并非真实链上调用
const result = iface.encodeFunctionResult("transfer", [true]);
console.log("return bytes:", (result.length - 2) / 2);
console.log("decoded return:", iface.decodeFunctionResult("transfer", result)[0]);

// 本地构造事件日志,检查 indexed 参数的位置
const log = iface.encodeEventLog("Transfer", [from, to, 25n]);
console.log("topics:", log.topics.length);
console.log("topic0 bytes:", (log.topics[0].length - 2) / 2);
console.log("event amount:", iface.decodeEventLog("Transfer", log.data, log.topics)[2].toString());

运行:

bash
node abi.mjs

预期输出:

text
selector: 0xa9059cbb
calldata bytes: 68
decoded amount: 25
return bytes: 32
decoded return: true
topics: 3
topic0 bytes: 32
event amount: 25

从事件日志识别参数

Transfer(address,address,uint256) 的事件签名同样按名称和输入类型组成,但非匿名事件的 topic0 使用完整 32 字节 Keccak-256 哈希,不是函数的 4 字节选择器。两个 indexed address 分别位于 topic1 和 topic2,未索引的 amount 位于日志 data,所以本例有 3 个 topics。

查看真实交易时,先从回执的 logs 找到合约地址,再按 ABI 解码对应事件;不能拿 eth_call 的返回数据当日志。indexed string、动态数组等复杂参数在 topics 中保存特殊编码的哈希,通常不能从 topic 还原原始内容。匿名事件不使用签名 topic0,也不能直接套用上述位置规则。

避免三个常见误判

  • 同名函数不等于同一选择器。 foo(uint256) 与 foo(address) 有不同签名;遇到重载时,用完整签名选择 ABI 条目。4 字节选择器理论上也可能发生碰撞,不能仅凭它断言合约的源码或意图。
  • 返回值类型不参与选择器。 修改 returns (bool) 不会改变 transfer(address,uint256) 的选择器,却可能使链下解码失败;读取时要使用与实际部署版本匹配的 ABI。
  • ABI 能编码,不代表链上调用成功。 上面的脚本只做离线编码。真正写入交易仍需合约地址、签名账户、RPC、Gas 和链上执行;应检查交易回执的 status 与日志,再判断状态是否生效。

需要核对字段定义时,可查阅 Solidity ABI 规范 与 ethers v6 Interface 文档;排查异常输入时优先核对规范签名、参数类型及真实部署合约的 ABI。

按自己的节奏,确认真正理解。

目录与阅读设置

阅读设置

17

记录一个想法

根据 ABI 识别函数编码

0 / 10000 · 笔记保存到账号。未提交的修改会在关闭、离开或刷新后丢失。

登录哪都通