2

Currently, I'm trying to get the bytecode from this contract code for example:

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

contract GetSet {
  uint256 private total;
  mapping(address => uint256) private numbers;
  mapping(uint256 => uint256) private heavyArr;

  function getTotal() external view returns (uint256) {
    return total;
  }

  function setNumber(uint256 _num) external returns (bool) {
    total = total - numbers[msg.sender] + _num;
    numbers[msg.sender] = _num;
    return true;
  }

  function setHeavy() external returns (bool) {
    for(uint i = 0; i < 100; i++) {
      heavyArr[i] = heavyArr[i] + 1;
    }
    return true;
  }

  function getNumber(address _user) external view returns (uint256) {
    return numbers[_user];
  }
}

both Complitation have optimization enabled with counts of 200 and using version v0.8.7+commit.e28d00a7

Here is how I used solc-js for compile:

  const input = {
    language: 'Solidity',
    sources: {
      'GetSet.sol': {
        content: contractCode,
      },
    },
    settings: {
      outputSelection: {
        '*': {
          '*': ['*'],
        },
      },
      optimizer: {
        // disabled by default
        enabled: true,
        // Optimize for how many times you intend to run the code.
        // Lower values will optimize more for initial deployment cost, higher values will optimize more for high-frequency usage.
        runs: 200,
      },
    },
  };
  solc.loadRemoteVersion('v0.8.7+commit.e28d00a7', function (err, solcSnapshot) {
    // NOTE: Use `solcSnapshot` here with the same interface `solc` has
    const output = JSON.parse(solcSnapshot.compile(JSON.stringify(input)));

    // `output` here contains the JSON output as specified in the documentation
    for (const contractName in output.contracts['GetSet.sol']) {
      console.log(
        output.contracts['GetSet.sol'][contractName].evm.bytecode.object
      );
    }
  });

and this is on remix IDE:

enter image description here

as you can see the settings are the same, I got the result from both as following:

SOLC-JS:

608060405234801561001057600080fd5b5061022e806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80633fb5c1cb14610051578063775a25e314610079578063942ea4661461008b578063f47aa95d146100b4575b600080fd5b61006461005f36600461017f565b6100bc565b60405190151581526020015b60405180910390f35b6000545b604051908152602001610070565b61007d61009936600461014f565b6001600160a01b031660009081526001602052604090205490565b6100646100ff565b33600090815260016020526040812054815483916100d9916101b0565b6100e39190610198565b6000908155338152600160208190526040909120929092555090565b6000805b606481101561014757600081815260026020526040902054610126906001610198565b6000828152600260205260409020558061013f816101c7565b915050610103565b506001905090565b60006020828403121561016157600080fd5b81356001600160a01b038116811461017857600080fd5b9392505050565b60006020828403121561019157600080fd5b5035919050565b600082198211156101ab576101ab6101e2565b500190565b6000828210156101c2576101c26101e2565b500390565b60006000198214156101db576101db6101e2565b5060010190565b634e487b7160e01b600052601160045260246000fdfea2646970667358221220ff0bb87edaf438ba1bb207088847e9b6deb81cc9d80570f2b69f2bcac80ddb9c64736f6c63430008070033

REMIX IDE:

608060405234801561001057600080fd5b5061022e806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80633fb5c1cb14610051578063775a25e314610079578063942ea4661461008b578063f47aa95d146100b4575b600080fd5b61006461005f36600461017f565b6100bc565b60405190151581526020015b60405180910390f35b6000545b604051908152602001610070565b61007d61009936600461014f565b6001600160a01b031660009081526001602052604090205490565b6100646100ff565b33600090815260016020526040812054815483916100d9916101b0565b6100e39190610198565b6000908155338152600160208190526040909120929092555090565b6000805b606481101561014757600081815260026020526040902054610126906001610198565b6000828152600260205260409020558061013f816101c7565b915050610103565b506001905090565b60006020828403121561016157600080fd5b81356001600160a01b038116811461017857600080fd5b9392505050565b60006020828403121561019157600080fd5b5035919050565b600082198211156101ab576101ab6101e2565b500190565b6000828210156101c2576101c26101e2565b500390565b60006000198214156101db576101db6101e2565b5060010190565b634e487b7160e01b600052601160045260246000fdfea2646970667358221220647d8ef7760cb26e1d7a68edeccb79b5ca1a63efc856378d920201d40085f93764736f6c63430008070033

the 2 bytecode are different at the end

enter image description here

What am I missing, I double check all the settings even metadatas are the same but I don't get why they are different

{
  "compiler": {
    "version": "0.8.7+commit.e28d00a7"
  },
  "language": "Solidity",
  "output": {
    "abi": [
      {
        "inputs": [
          {
            "internalType": "address",
            "name": "_user",
            "type": "address"
          }
        ],
        "name": "getNumber",
        "outputs": [
          {
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
          }
        ],
        "stateMutability": "view",
        "type": "function"
      },
      {
        "inputs": [],
        "name": "getTotal",
        "outputs": [
          {
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
          }
        ],
        "stateMutability": "view",
        "type": "function"
      },
      {
        "inputs": [],
        "name": "setHeavy",
        "outputs": [
          {
            "internalType": "bool",
            "name": "",
            "type": "bool"
          }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "inputs": [
          {
            "internalType": "uint256",
            "name": "_num",
            "type": "uint256"
          }
        ],
        "name": "setNumber",
        "outputs": [
          {
            "internalType": "bool",
            "name": "",
            "type": "bool"
          }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
      }
    ],
    "devdoc": {
      "kind": "dev",
      "methods": {},
      "version": 1
    },
    "userdoc": {
      "kind": "user",
      "methods": {},
      "version": 1
    }
  },
  "settings": {
    "compilationTarget": {
      "GetSet.sol": "GetSet"
    },
    "evmVersion": "london",
    "libraries": {},
    "metadata": {
      "bytecodeHash": "ipfs"
    },
    "optimizer": {
      "enabled": true,
      "runs": 200
    },
    "remappings": []
  },
  "sources": {
    "GetSet.sol": {
      "keccak256": "0xbe599f5bcbd0c9cd18e7aec3a5bdf55709bfe578823a7943d3f8a084959967f6",
      "license": "MIT",
      "urls": [
        "bzz-raw://d6f14d23add195e111ce54578e8332bf9d49654cc5a1092bd12a9f1f7664aed5",
        "dweb:/ipfs/QmSrXqqsPzgGxYNSnBz2f5xfKpXi7sXiJr2Fva9L8gsuKY"
      ]
    }
  },
  "version": 1
}

1 Answer 1

2

Could be a spacing issue or comments...don't know if remix adds anything, but looks like it's the metadata that's different and not the actual logic: Why does changing comments in the smart contract change its bytecode?

2
  • is there any way how to differ metadata from logic in bytecode?
    – l2ysho
    Mar 12 at 18:36
  • 1
    metadata is last 32 bytes (hash of contract)
    – thefett
    Mar 24 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.