I am deploying my smart contracts using web3.js and compiling the code using solc. The contracts are deployed successfully with no issues however I am having a bit of a problem in verifying my contract code. Mostly the issue relies in the code flattening process as I am doing it manually and it seems to be generating a different byte code than the one I have compiled and deployed.

Basically here is how I deploy my multi file contract:

var inputs = {
        'Main.sol': fs.readFileSync('./Contracts/Main.sol').toString(),

    // Assumes imported files are in the same folder/local path
    function findImports(path) {
        return {
            'contents': fs.readFileSync('./Contracts/' + path).toString()

    var output = solc.compile({sources: inputs}, 0, findImports);
    const bytecode = output.contracts['Main.sol:Main'].bytecode;
    const abi = JSON.parse(output.contracts['Main.sol:Main'].interface);

    const contract = web3.eth.contract(abi);
    let gasLimit = web3.eth.getBlock("latest").gasLimit;

const contractInstance = contract.new({
        data: '0x' + bytecode,
        from: web3.eth.accounts[0],
        gas: gasLimit/2,       
        chainId: 3
    }, (err, res) => {
        if (err) {

        // Log the tx, you can explore status with eth.getTransaction()

        // If we have an address property, the contract was deployed
        if (res.address) {
            console.log('Contract address: ' + res.address);
            // Let's test the deployed contract

The code above works and the contract is deployed and is fully functional.

Is there a way to automatically flatten the code and create a single sol file using web3 or any other tool.

EDIT: I tried using this utility but still etherscan complains that the flattened code's bytcode does not match the contract

1 Answer 1


For creating a flat file I like truffle-flattener. Just npm install truffle-flattener

Run locally node node-modules/.bin/truffle-flattener ./contracts/your_contract.sol > contract_flat.sol

Your Answer

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

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