Back

blockchain - truffle 对nft erc-721 contract 进行verify validation 源代码校验

发布时间: 2022-06-18 23:58:00

refer to:

https://github.com/rkalis/truffle-plugin-verify

https://medium.com/quick-programming/verify-a-smart-contract-on-etherscan-using-truffle-cb2656fd9c41

解决方案

1. 安装这个plugin: npm i -D truffle-plugin-verify

2. 修改truffle-config.js 增加如下内容:

plugins: ['truffle-plugin-verify']

3. 在ethersan.io上申请api key

4. 修改truffle-config.js 增加:

  api_keys: {
    etherscan: '4ANBPF8KQQHJ6J9BBU7WU53BNPDMXH????'
  }

5. 开始verify: truffle run verify MyTestNft --network goerli

这里要求我们的机器可以直接访问到  etherscan.io  不出意外的话是无法访问的,所以需要梯子 

(该过程略,对于linux 来说,就是 trojan command line + pproxy  + HTTP_PROXY, HTTPS_PROXY变量   )

否则会看到这个

Verifying TestMyNft
Could not find TestMyNft artifact at /mnt/d/workspace/test_erc_721/build/contracts/TestMyNft.json
Failed to verify 1 contract(s): TestMyNft

6. 设置好梯子之后,我们继续验证。为了方便调试,我增加了 --debug 标签。另外指定了contract的地址

truffle run verify MyTestNft@0x81ec27587694f9996a69dc26230643dd619cfaba --network goerli --debug

DEBUG logging is turned ON
Running truffle-plugin-verify v0.5.26
Retrieving network's network ID & chain ID
Verifying MyTestNft@0x81ec27587694f9996a69dc26230643dd619cfaba
Reading artifact file at /mnt/d/workspace/test_erc_721/build/contracts/MyTestNft.json
Custom address 0x81ec27587694f9996a69dc26230643dd619cfaba specified
Retrieving constructor parameters from https://api-goerli.etherscan.io/api?apiKey=4ANBPF8KQQHJ6J9BBU7WU53BNPDMXHYJMW&module=account&action=txlist&address=0x81ec27587694f9996a69dc26230643dd619cfaba&page=1&sort=asc&offset=1
Constructor parameters retrieved: 0x
Sending verify request with POST arguments:
{
  "apikey": "4ANBPF8KQQHJ6J9BBU7WU53BNPDMXHYJMW",
  "module": "contract",
  "action": "verifysourcecode",
  "contractaddress": "0x81ec27587694f9996a69dc26230643dd619cfaba",
  "sourceCode": "{\"language\":\"Solidity\",\"sources\"

  "constructorArguements": ""   
}
Checking status of verification request 2adu3ip28csr2mydpeq52jyteke7wswknetjjqcgi5fgeqcdlk
Pass - Verified: https://goerli.etherscan.io/address/0x81ec27587694f9996a69dc26230643dd619cfaba#code
Successfully verified 1 contract(s).

大概总耗时2分钟吧。 然后就可以看到成功校验了源代码

下面是踩坑过程:普通方式无法对truffle进行contract verify (智能合约源代码校验)

正常的渠道,对 erc-721验证,有这样三种方式: (不考虑 Vyper 的话)

1. 单sol文件

2. 多个sol文件

3. 单个json 文件

我们使用truffle 发布nft的时候,往往会有 import, 这样就会产生多个sol文件, 而你引入的 sol文件中又会引入其他的sol文件,

所以如果仅仅导入明面上看到的sol文件的话,你会进入到 import hell中:

在进行 contract verify(合约的源代码验证)的时候,会发现远远不够, 各种import根本找不到

使用单个json文件的话, 校验又会无法通过:

我怀疑是truffle 在build文件夹中生成的json文件不符合etherscan的格式要求,缺少了abi 这个项目

会提示abi找不到:

Back