0

Ihave to compile some old contracts (having solidity pragma is <0.5. For this purpose, I have just copy and paste this contract from Etehrscan and save with its name in contracts folder. Install old version of solc npm install solc@0.4.25. Then i read that filev(through my following code) and gives it to solc.compile(), which gives following error. What is going wrong with my code. NOTE: I am read all question on captioned subject, but no one is applied on my question. Here is my code:

const path = require('path');
const fs = require('fs-extra');
const solc = require('solc');
const sourceFolderPath = path.resolve(__dirname, 'contracts');
const buildFolderPath = path.resolve(__dirname, 'build');


const getContractSource = contractFileName => {
    const contractPath = path.resolve(__dirname, 'contracts', contractFileName);
    return fs.readFileSync(contractPath, 'utf8');
};

let sources = {};

var walk = function (dir) {
    var results = [];
    var list = fs.readdirSync(dir);
    list.forEach(function (file) {
        file = dir + '/' + file;
        var stat = fs.statSync(file);
        if (stat && stat.isDirectory()) {
            results = results.concat(walk(file));
        } else {
            if (file.substr(file.length - 4, file.length) === ".sol") {
                sources = {
                    ...sources,
                    [file]: {
                        content: getContractSource(file)

                    }
                };
            }

            results.push(file);

        }
    });

    return results;
};

walk(sourceFolderPath);
    // console.log("soruces: ", sources)
    // console.log("results: ", results)
const input = {
    language: 'Solidity',
    sources,
    settings: {
        outputSelection: {
            '*': {
                '*': ['*']
            }
        }
    }
}
// console.log("input: ", input)
console.log('\nCompiling contracts...');
// console.log(JSON.stringify(input))
const output = solc.compile(JSON.stringify(input));
console.log(output)

Further i will do following things... just for your information,

// const output = JSON.parse(solc.compile(JSON.stringify(input))); ... here i will use solc.compile output

// let shouldBuild = true;

// if (output.errors) {
//     // console.error(output.errors);
//     // throw '\nError in compilation please check the contract\n';
//     for (error of output.errors) {
//         if (error.severity === 'error') {
//             shouldBuild = false;
//             throw 'Error found';
//             break;
//         }
//     }
// }

// if (shouldBuild) {
//     console.log('\nBuilding please wait...');

//     fs.removeSync(buildFolderPath);
//     fs.ensureDirSync(buildFolderPath);

//     for (let contractFile in output.contracts) {
//         for (let key in output.contracts[contractFile]) {
//             var c = contractFile.substr(40,contractFile.length-4)
//             if(c.search(key)==0) {

//             fs.outputJsonSync(
//                 path.resolve(buildFolderPath, `${key}.json`),
//                 {
//                     abi: output.contracts[contractFile][key]["abi"],
//                     bytecode: output.contracts[contractFile][key]["evm"]["bytecode"]["object"]
//                 },
//                 {
//                     spaces: 2,
//                     EOL: "\n"
//                 }
//             );
//         }
//         }
//     }
//     console.log('Build finished successfully!\n');
// } else {
//     console.log('\nBuild failed\n');
// }

Here is my output;

    Compiling contracts...
    {
      contracts: {},
      errors: [
        ':1:1: ParserError: Expected pragma, import directive or contract/interface/library definition.\n' +
          ' ... { ... \n' +
          '     ^\n'
      ],
      sourceList: [ '' ],
      sources: {}
}

NOTE 1: previously i had install solc versin ^0.5.12 (which i think was global)... but now i have installed version 0.4.25 local into my project folder, may be due to this this error occured.. but i am not sure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.