简体   繁体   English

松露部署的合同无法通过Etherscan进行验证

[英]Truffle deployed contracts can't be verified through Etherscan

I am faced with the problem the truffle generates a different bytecode than solcjs (local compiler) and also than Remix (online compiler). 我面临的问题是,松露生成的字节码不同于solcjs(本地编译器),也不同于Remix(在线编译器)。 And therefor I can't verify my contracts throght Etherscan (see https://etherscan.io/verifyContract ) 因此,我无法通过Etherscan验证我的合同(请参阅https://etherscan.io/verifyContract

My enviromnment: 我的环境:

OS: Ubuntu 15.10

$ uname -a
Linux sasha 4.2.0-42-generic #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

$ truffle version
Truffle v3.2.4

$ solcjs --version
0.4.11+commit.68ef5810.Emscripten.clang

What I tried to do: 我试图做的:

1) I created a simple smart contract Ownable: 1)我创建了一个简单的智能合约Ownable:

pragma solidity ^0.4.11;

contract Ownable {
    address public owner;

    function Ownable() {
        owner = msg.sender;
    }

    modifier onlyOwner {
        if (msg.sender != owner) throw;
        _;
    }

    function transferOwnership(address newOwner) onlyOwner {
        if (newOwner != address(0)) {
            owner = newOwner;
        }
    }
}

2) Init a new truffle project and copy Ownable.sol into "contract" folder. 2)启动一个新的松露项目并将Ownable.sol复制到“合同”文件夹中。

So the structure of project must be the following: 所以项目的结构必须如下:

test-comtract
-- contracts
   -- Ownable.sol
-- migrations
   -- 1_initial_migration.js
   -- 2_deploy_contracts.js
-- test
truffle.js

3) Compile the truffle project: 3)编制松露项目:

$ cd test-comtract 
$ truffle compile

After compilation "build" folder will be created. 编译完成后,将创建“build”文件夹。

4) Go to the "build" folder and search Ownable.json file there. 4)转到“build”文件夹并在那里搜索Ownable.json文件。 In this file search the generated bytecode in "unlinked_binary" attribute. 在此文件中,在“unlinked_binary”属性中搜索生成的字节码。 The value is shown below: 值如下所示:

5) Tried to compile through Remix online compiler. 5)试图通过Remix在线编译器编译。 https//ethereum.github.io/browser-solidity/#version=soljson-v0.4.11+commit.68ef5810.js&optimize=true HTTPS // ethereum.github.io /浏览器的坚固性/#版本= soljson-v0.4.11 + commit.68ef5810.js&优化=真

The result of the compilation is shown below: 编译结果如下:

6) Tried to compile by helps with solcjs: 6)尝试通过solcjs帮助编译:

$ cd test-comtract/contracts 
$ solcjs --optimize --bin Ownable.sol

In "test-comtract/contracts" folder will be created the file "Ownable.sol:Ownable.bin" 在“test-comtract / contracts”文件夹中将创建文件“Ownable.sol:Ownable.bin”

RESULT: The SolJS bytecode is identical to the Remix bytecode but different from the truffle bytecode. 结果:SolJS字节码与Remix字节码相同,但与松露字节码不同。

7) Also I deployed the Ownable.sol through truffle to the Ethereum (TEST_NET). 7)我还通过松露将Ownable.sol部署到以太坊(TEST_NET)。 You may see it by follow this URL: 您可以通过以下网址查看:

https://ropsten.etherscan.io/address/0x6c8e1b321ce94b12069222b543558d4159527ecd https://ropsten.etherscan.io/address/0x6c8e1b321ce94b12069222b543558d4159527ecd

Maybe somebody is faced with the such problem. 也许有人面临这样的问题。 I'll be very appreciate to hear any versions why it is happing. 我会非常感激地听到任何版本的原因。 Maybe this is a known bug of the Truffle framework. 也许这是Truffle框架的一个已知错误。 In google I found some more articles describes the similar problems, but nobody suggest a solution. 在谷歌我发现更多的文章描述了类似的问题,但没有人提出解决方案。 I very need help 我非常需要帮助

Thanks in advance! 提前致谢!

-- Alex - 亚历克斯

There's a compiler optimization in solc/truffle-compile that in this moment etherscan does not support when compiling contracts. 在solc / truffle-compile中有一个编译器优化,此时etherscan在编译契约时不支持。

https://github.com/trufflesuite/truffle-compile/blob/331809c73389f27f9dda40229061bb75b18f27ca/index.js#L70 https://github.com/trufflesuite/truffle-compile/blob/331809c73389f27f9dda40229061bb75b18f27ca/index.js#L70

Sent a comment to etherscan about it! 发送评论给etherscan关于它!

there is an verifier 2.0 that supports Truffle's compilation. 有一个验证程序2.0支持Truffle的编译。

https://etherscan.io/verifyContract2 https://etherscan.io/verifyContract2

You can do what I did and start guessing optimization levels and compilers. 你可以做我做的事情并开始猜测优化级别和编译器。 You might get it after 100 tries. 你可能会在100次尝试后得到它。 I did. 我做到了。 From now on I don't deploy anything on mainnet from truffle. 从现在开始,我不会在松露的主网上部署任何东西。 It's great for testing and development, but it doesn't agree with etherscan. 它非常适合测试和开发,但它与etherscan不一致。 For that you need to concatenate everything and use remix in the future. 为此,您需要连接所有内容并在将来使用remix。

I created truffle-plugin-verify to automate Truffle contract verification on Etherscan. 我创建了truffle-plugin-verify来自动化Etherscan上的Truffle合同验证。


  1. Install the plugin with npm 用npm安装插件
npm install truffle-plugin-verify
  1. Add the plugin to your truffle.js or truffle-config.js file 将插件添加到truffle.jstruffle.js truffle-config.js文件中
module.exports = {
  /* ... rest of truffle-config */

  plugins: [
    'truffle-plugin-verify'
  ]
}
  1. Generate an API Key on your Etherscan account (see the Etherscan website ) 在您的Etherscan帐户上生成API密钥(请参阅Etherscan网站
  2. Add your Etherscan API key to your truffle config 将您的Etherscan API密钥添加到您的松露配置中
module.exports = {
  /* ... rest of truffle-config */

  api_keys: {
    etherscan: 'MY_API_KEY'
  }
}

After migrating your contract to to a public network, you are able to verify it on Etherscan by running: 将合同迁移到公共网络后,您可以通过运行以下方式在Etherscan上进行验证:

truffle run verify ContractName [--network networkName]

More information can be found on the repository or in my article Automatically verify Truffle smart contracts on Etherscan . 更多信息可以在存储库或我的文章中自动验证Etherscan上的Truffle智能合约

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法卸载并重新安装 Truffle? - Can't uninstall and reinstall Truffle? 使用 Truffle 部署合约的 53 位问题 - Problems about 53 bits deploying contracts with Truffle Truffle deploy 编译合约但不迁移到网络 - Truffle deploy compiles contracts but doesnt migrate to network 是否可以在 Truffle 迁移中调用合约中的函数? - Is it possible to call functions from contracts within a migration on Truffle? 为已部署的智能合约创建全局通讯录 - Create a global Addressbook for deployed Smart Contracts 智能合约可以部署其他智能合约吗? - Can smart contracts deploy other smart contracts? Remix IDE 在已部署的合约选项下不显示已部署的功能 - Remix IDE does not show the deployed functions under the deployed contracts option 错误:Truffle 当前使用的是 solc 0.5.16,但您的一个或多个合约指定“pragma solidity ^0.8.0” - Error: Truffle is currently using solc 0.5.16, but one or more of your contracts specify “pragma solidity ^0.8.0” 松露验证 - 找不到模块'@chainlink/contracts/src/v0.6/VRFConsumerBase.sol' - Truffle verify - Cannot find module '@chainlink/contracts/src/v0.6/VRFConsumerBase.sol' 如果智能合约/DAO 无法轻松更新,它们如何在现实世界的混乱中幸存下来? - How can smart contracts / DAOs survive the chaos of the real world if they can't be easily updated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM