是否可以在没有源代码的情况下获取合约的 ABI?

Is it possible to get the ABI of a contract without the source code?

是否可以在没有源代码的情况下获取已知合约地址的ABI? 我发现的唯一方法是使用 etherscan 的 API,但它仅适用于经过验证的合约。 感谢任何帮助,谢谢!

ABI JSON 是从源代码生成的。因此,除非您知道源代码,否则无法从中生成 ABI JSON。

简单回答:否


长答案:也许吧。 ABI是从源代码生成的,但是如果你知道这些功能是什么,你可以自己“创建”ABI。

合约的ABI代表应用程序二进制接口,它只是定义了如何与智能合约进行交互。

比如,你可能不知道某个合约的源代码是什么,但你知道它有一个transfer功能。您可以将 ABI“制作”为:

[
  {
  "constant":false,
  "inputs":[
    {"name":"_to","type":"address"},
    {"name":"_value","type":"uint256"}
  ],
  "name":"transfer",
  "outputs":[
    {"name":"success",
    "type":"bool"}
  ],
  "payable":false,
  "stateMutability":"nonpayable",
  "type":"function"
  }
]

或者编译接口,因为编译接口会输出ABI。

pragma solidity ^0.8.8;

interface ContractInterface {
  function transfer(address to, uint256 value) external returns (bool success);
}

因为 ABI 和接口不必包含智能合约能够实现的每一个功能。

此外,还有Decompilers这样的东西试图反编译字节码来弄清楚合约是什么,这样你就可以获得ABI。

我猜你想让 ABI 调用合约上的函数,

如果您知道要调用的函数的函数签名,您仍然可以在没有 ABI 的情况下与合约交互:

let test = await provider.call({
    // contract we want to talk to
    to: address,

    // `function name() view returns (string)`
    data: "0x313ce567",
});
console.log(`Decimals: ${test}`)

小数:0x0000000000000000000000000000000000000000000000000000000000000009