【问题标题】:Is it possible to get the ABI of a contract without the source code?是否可以在没有源代码的情况下获得合约的 ABI?
【发布时间】:2021-12-17 23:50:40
【问题描述】:

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

【问题讨论】:

    标签: blockchain ethereum solidity web3 web3js


    【解决方案1】:

    我猜你希望 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

    【讨论】:

      【解决方案2】:

      简单的答案:没有


      长答案:也许吧。 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 和接口不必包含智能合约能够执行的每一项功能。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-10
          • 1970-01-01
          • 1970-01-01
          • 2020-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多