different bytecode for the same smart contract

453 Views Asked by At

I have a smart contract called SimpleContractB

 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract SimpleContractB {
  function example1() public returns(bool, bytes memory) {
    (bool isSuccess, bytes memory data) = address(msg.sender).call{ value: 1 ether }("");
    return (isSuccess, data);
  }

  function example2() pure public {
    revert("Demo internal transaction error!");
  }

  function example3() payable public {
    assert(msg.value > 1 ether);
  }

  function example4() payable public {
    require(
      msg.value > 1 ether,
      "Not enough Ether provided."
    );
  }
}

I'm using Truffle v5.6.8, and solc-js v0.8.11+commit.d7f03943.

The problem is that Truffle compiles to a different bytecode with solc.

bytecode from Truffle:

        the difference is here
...2122.09eaba953a1c59544a9531febfd70e08b13bb5df89f10bd47b60c91f15a6aa9c1.64736f6...

bytecode from solc

        the difference is here
...2122.0d049f2c7420c99ed39feca36ba29c813ce6344f3aea16f5116a9ea9ca15bbd4e.64736f6...

Why do they return different bytecode from each other and how to fix it.

1

There are 1 best solutions below

0
On BEST ANSWER

As far as I know that both Truffle and Hardhat also use solc to compile smart contract. However, the inputs passed into solc are not the same.

  • Truffle: It adds a prefix to the name of the sources which is project:/contracts/
var input = {
  language: 'Solidity',
  sources: {
    'project:/contracts/test.sol': {
      content: 'contract C { function f() public { } }'
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};
  • Hardhat: It also adds a prefix to the name of the sources which is contracts/
var input = {
  language: 'Solidity',
  sources: {
    'contracts/test.sol': {
      content: 'contract C { function f() public { } }'
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};

After trying to do that I've got the same bytecode with the compiled json file.

Hope that'll help you!