You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用ethers.utils.FormatTypes.full返回对象而非字符串问题求助

问题根因

你遇到的两个核心认知误区:

  • new ethers.utils.Interface是同步构造函数,不需要加await,属于冗余写法,不影响运行结果但无实际意义。
  • 官方文档标注的FormatTypes.full返回字符串,是单个ABI片段调用format方法的返回值;对整个Interface实例调用format时,返回的是所有ABI片段格式化后组成的字符串数组,和你预期的输出结构完全一致。你看到的"对象"本质是数组:JavaScript中数组属于引用类型,typeof []的返回值就是object,属于正常现象。

解决方法

直接使用返回的数组即可,示例代码如下:

const iface = new ethers.utils.Interface(Bank.abi);
const humanReadableABI = iface.format(ethers.utils.FormatTypes.full);

// 验证输出:会打印你预期的第一个方法字符串
console.log(humanReadableABI[0]) // 输出 'function deposit() public payable'

如果需要将整份ABI转为单个字符串做正则匹配,直接对数组做拼接处理即可:

// 按换行拼接为单个完整字符串
const abiFullStr = humanReadableABI.join('\n')

内容的提问来源于stack exchange,提问作者loekTheDreamer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 12:06:05