首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以太扫描验证问题

以太扫描验证问题
EN

Ethereum用户
提问于 2019-05-22 08:40:17
回答 3查看 669关注 0票数 2

为了某种目的。我使用web3.js0.20来部署合同。按按钮自动部署合同,而不是通过混合IDE)。当我部署合同时,以太扫描需要验证可靠性。是否有通过Web3自动验证合同的方法?非常感谢。

EN

回答 3

Ethereum用户

发布于 2019-05-22 08:45:19

Etherscan (一个专有的第三方应用程序)并没有内置到web3.js中,但Etherscan确实有一个用于验证合同源代码的API:https://etherscan.io/apis#contracts

票数 1
EN

Ethereum用户

发布于 2019-05-22 10:06:39

这里有(依赖于web3 v1.0.0-beta.34和块菌-平坦v1.2.10).

创建一个名为verify.js的文件,其内容如下,并将其放在package.json旁边:

代码语言:javascript
复制
const WORK_DIR = "./PROJ_DIR"; // your contracts folder should be under this path
const NODE_DIR = "../node_modules";
const INPUT_FILE = process.argv[2];

const fs        = require("fs");
const path      = require("path");
const request   = require("request");
const spawnSync = require("child_process").spawnSync;

const input = JSON.parse(fs.readFileSync(INPUT_FILE, {encoding: "utf8"}));
//  input example:
//  {
//      "network"        : "api", // use "api" for mainnet or "api-<testnet>" for testnet
//      "apiKey"         : "",    // generate this value at https://etherscan.io/myapikey
//      "compilerVersion": "v0.4.24+commit.e67f0147",
//      "optimization"   : {"used": 1, "runs": 6000},
//      "contracts"      : {
//          "Contract1": {"addr": "0x0000000000000000000000000000000000000001", "args": "<abi-encoded constructor arguments>"},
//          "Contract2": {"addr": "0x0000000000000000000000000000000000000002", "args": "<abi-encoded constructor arguments>"},
//          "Contract3": {"addr": "0x0000000000000000000000000000000000000003", "args": "<abi-encoded constructor arguments>"}
//      }
//  }

function run() {
    for (const pathName of getPathNames("contracts")) {
        const contractName = path.basename(pathName, ".sol");
        if (input.contracts.hasOwnProperty(contractName))
            post(contractName, getSourceCode(pathName));
    }
}

function getPathNames(dirName) {
    let pathNames = [];
    for (const fileName of fs.readdirSync(WORK_DIR + "/" + dirName)) {
        if (fs.statSync(WORK_DIR + "/" + dirName + "/" + fileName).isDirectory())
            pathNames = pathNames.concat(getPathNames(dirName + "/" + fileName));
        else if (fileName.endsWith(".sol"))
            pathNames.push(dirName + "/" + fileName);
    }
    return pathNames;
}

function getSourceCode(pathName) {
    const result = spawnSync("node", [NODE_DIR + "/truffle-flattener/index.js", pathName], {cwd: WORK_DIR});
    return result.output.toString().slice(1, -1);
}

function post(contractName, sourceCode) {
    console.log(contractName + ": sending verification request...");
    request.post({
            url: "https://" + input.network + ".etherscan.io/api",
            form: {
                module               : "contract",
                action               : "verifysourcecode",
                sourceCode           : sourceCode,
                contractname         : contractName,
                apikey               : input.apiKey,
                compilerversion      : input.compilerVersion,
                optimizationUsed     : input.optimization.used,
                runs                 : input.optimization.runs,
                contractaddress      : input.contracts[contractName].addr,
                constructorArguements: input.contracts[contractName].args,
            }
        },
        function(error, response, body) {
            if (error) {
                console.log(contractName + ": " + error);
            }
            else {
                body = JSON.parse(body);
                if (body.status == "1")
                    get(contractName, body.result);
                else
                    console.log(contractName + ": " + body.result);
            }
        }
    );
}

function get(contractName, guid) {
    console.log(contractName + ": checking verification status...");
    request.get(
        "https://" + input.network + ".etherscan.io/api?module=contract&action=checkverifystatus&guid=" + guid,
        function(error, response, body) {
            if (error) {
                console.log(contractName + ": " + error);
            }
            else {
                body = JSON.parse(body);
                if (body.result == "Pending in queue")
                    get(contractName, guid);
                else
                    console.log(contractName + ": " + body.result);
            }
        }
    );
}

run();

package.json中,添加:

代码语言:javascript
复制
  "scripts": {
    "verify": "node verify.js"
  },
  "devDependencies": {
    "truffle-flattener": "1.2.10",
    "web3": "1.0.0-beta.34"
  }

在运行npm install之后,可以通过从命令行运行npm verify input.txt来验证您的契约集。您需要根据input.txt顶部的输入示例创建verify.js

票数 0
EN

Ethereum用户

发布于 2019-06-02 04:18:05

我创建了truffle-plugin-verify来自动验证Etherscan上的信托合同。

  1. 用npm安装插件
代码语言:javascript
复制
npm install truffle-plugin-verify
  1. 将插件添加到truffle.jstruffle-config.js文件中
代码语言:javascript
复制
module.exports = {
  /* ... rest of truffle-config */

  plugins: [
    'truffle-plugin-verify'
  ]
}
  1. 在您的以太扫描帐户上生成一个API密钥(请参阅以太扫描网站)
  2. 将Etherscan键添加到您的松露配置中
代码语言:javascript
复制
module.exports = {
  /* ... rest of truffle-config */

  api_keys: {
    etherscan: 'MY_API_KEY'
  }
}

将合同迁移到公共网络后,您可以通过运行以下命令在以太扫描上验证该协议:

代码语言:javascript
复制
truffle run verify ContractName [--network networkName]

更多信息可以在存储库或我的文章在以太扫描上自动验证特鲁弗智能合同中找到。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/70995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档