4
votes

I am faced with the problem the truffle generates a different bytecode than solcjs (local compiler) and also than Remix (online compiler). And therefor I can't verify my contracts throght Etherscan (see 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:

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.

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:

$ cd test-comtract 
$ truffle compile

After compilation "build" folder will be created.

4) Go to the "build" folder and search Ownable.json file there. In this file search the generated bytecode in "unlinked_binary" attribute. The value is shown below:

0x6060604052341561000c57fe5b5b60008054600160a060020a03191633600160a060020a03161790555b5b60f3806100386000396000f300606060405263ffffffff60e060020a6000350416638da5cb5b8114602a578063f2fde38b146053575bfe5b3415603157fe5b6037606e565b60408051600160a060020a039092168252519081900360200190f35b3415605a57fe5b606c600160a060020a0360043516607d565b005b600054600160a060020a031681565b60005433600160a060020a0390811691161460985760006000fd5b600160a060020a0381161560c25760008054600160a060020a031916600160a060020a0383161790555b5b5b505600a165627a7a72305820607fc60d96cffbd50e58fbc028c1e4b6f3dfdf356bd439390481a479ef8d25500029

5) Tried to compile through Remix online compiler. https//ethereum.github.io/browser-solidity/#version=soljson-v0.4.11+commit.68ef5810.js&optimize=true

The result of the compilation is shown below:

6060604052341561000c57fe5b5b60008054600160a060020a03191633600160a060020a03161790555b5b610119806100396000396000f300606060405263ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416638da5cb5b81146043578063f2fde38b14606c575bfe5b3415604a57fe5b60506087565b60408051600160a060020a039092168252519081900360200190f35b3415607357fe5b6085600160a060020a03600435166096565b005b600054600160a060020a031681565b60005433600160a060020a0390811691161460b15760006000fd5b600160a060020a0381161560e8576000805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0383161790555b5b5b505600a165627a7a723058201eb1de901e30ec9818544272a4c70946cd9cb7cd848237ba3dca118e44d771a60029

6) Tried to compile by helps with solcjs:

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

In "test-comtract/contracts" folder will be created the file "Ownable.sol:Ownable.bin"

6060604052341561000c57fe5b5b60008054600160a060020a03191633600160a060020a03161790555b5b610119806100396000396000f300606060405263ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416638da5cb5b81146043578063f2fde38b14606c575bfe5b3415604a57fe5b60506087565b60408051600160a060020a039092168252519081900360200190f35b3415607357fe5b6085600160a060020a03600435166096565b005b600054600160a060020a031681565b60005433600160a060020a0390811691161460b15760006000fd5b600160a060020a0381161560e8576000805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0383161790555b5b5b505600a165627a7a7230582083a10cb56ea55b0a181cbc132bdf1bd4e0e8a02d5664db73bbc62217f0b7b8d00029

RESULT: The SolJS bytecode is identical to the Remix bytecode but different from the truffle bytecode.

7) Also I deployed the Ownable.sol through truffle to the Ethereum (TEST_NET). You may see it by follow this URL:

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. In google I found some more articles describes the similar problems, but nobody suggest a solution. I very need help

Thanks in advance!

-- Alex

4

4 Answers

3
votes

There's a compiler optimization in solc/truffle-compile that in this moment etherscan does not support when compiling contracts.

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

Sent a comment to etherscan about it!

1
votes

there is an verifier 2.0 that supports Truffle's compilation.

https://etherscan.io/verifyContract2

1
votes

I created truffle-plugin-verify to automate Truffle contract verification on Etherscan.


  1. Install the plugin with npm
npm install truffle-plugin-verify
  1. Add the plugin to your truffle.js or truffle-config.js file
module.exports = {
  /* ... rest of truffle-config */

  plugins: [
    'truffle-plugin-verify'
  ]
}
  1. Generate an API Key on your Etherscan account (see the Etherscan website)
  2. Add your Etherscan API key to your truffle config
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:

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.

0
votes

You can do what I did and start guessing optimization levels and compilers. You might get it after 100 tries. 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. For that you need to concatenate everything and use remix in the future.