如何使用standard-input-json验证Solidity源码?❤️

  • Meta
  • 更新于 1天前
  • 阅读 3748

使用standard-input-json验证Solidity源码

使用 Standard-Input-Json(以BSC测试链为例)

使用编译工具获取 Input-Json 的三种方法。

1. Solidity 源码

Storage.sol

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/access/Ownable.sol";

contract Storage is Ownable {
    uint256 number;

    function setNumber(uint256 num) public onlyOwner {
        number = num;
    }

    function getNumber() public view returns (uint256) {
        return number;
    }
}

2. JSON格式的输入

Storage.json

{
    "language": "Solidity",
    "sources": {
        "contracts/Storage.sol": {
            "content": "// SPDX-License-Identifier: MIT......"
        },
        "@openzeppelin/contracts/access/Ownable.sol": {
            "content": "// SPDX-License-Identifier: MIT......"
        },
        "@openzeppelin/contracts/utils/Context.sol": {
            "content": "// SPDX-License-Identifier: MIT......"
        }
    },
    "settings": {
        "optimizer": {
            "enabled": true,
            "runs": 200
        },
        "outputSelection": {
            "*": {
                "*": [
                    "abi",
                    "evm.bytecode",
                    "evm.deployedBytecode",
                    "evm.methodIdentifiers",
                    "metadata"
                ],
                "": [
                    "ast"
                ]
            }
        }
    }
}

content内容:将solidity源码转换为JSON字符串。

3.获取 Json

方法1. Hardhat项目下

编译合约:

npx hardhat compile

打开artifacts\build-info路径下的json文件 input字段内容

image.png

方法2. Remix

编译合约后,打开contracts/artifacts/build-info路径下的json文件 input字段内容

image.png

部署合约

remix环境下

image.png 与Storage.json配置文件相同

方法3. Foundry项目下

Storage.sol 在 Foundry项目src文件夹下。

执行forge命令: forge verify-contract --show-standard-json-input 0x0000000000000000000000000000000000000000 src/Storage.sol:Storage

image.png

4.BSC浏览器验证合约

https://testnet.bscscan.com/address/0x1b3104004ebda264b88d04afb6ea66d70a2d51ac#code 1.

image.png 2.

image.png 3.

image.png 4.

image.png

喜欢就点个赞👍哦😄

点赞 4
收藏 4
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
Meta
Meta
0x0148...de2f
Solidity