Konnte ABI nicht von der solc.compile-Antwort web3.js finden

Ich bin sehr neu bei Ethereum und habe versucht, den Bytecode und ABI von solc.compile mit web3js zu erhalten, konnte aber den ABI nicht finden. Ich habe den Bytecode. Ich habe die JSON-Antwort in Dateien gespeichert und konnte nur diese Ausgaben erhalten.

  1. Assembly.json
  2. bytecode.json
  3. DeployedBytecode.json
  4. gasEstimates.json
  5. LegacyAssembly.json
  6. methodIdentifiers.json

Hier ist mein Code:

const inboxPath = path.resolve(__dirname,'contracts','inbox.sol');
const source = fs.readFileSync(inboxPath,'utf8');

// build path
const buildPath = path.resolve(__dirname, 'compiled');


/**
 * the new version only supports standard JSON in and out
 * @type {{settings: {outputSelection: {"*": {"*": string[]}}}, sources: {"inbox.sol": {content: string}}, language: string}}
 */
const input = {
    language: 'Solidity',
    sources: {
        'inbox.sol':{
            content: source,
        },
    },
    settings:{
        outputSelection: {
            '*': {
                '*': ['evm', 'bytecode'],
            },
        },
    },
};

async function compile() {
    const response = await solc.compile(JSON.stringify(input));

    const output = await JSON.parse(solc.compile(JSON.stringify(input)));

    const contracts = output.contracts['inbox.sol']["Inbox"].evm;

    const compiledResults = JSON.parse(response)["contracts"]["inbox.sol"]["Inbox"]["evm"];

    for (const contractName in contracts) {
        fs.writeFileSync(`${buildPath}/${contractName}.json`, JSON.stringify(contracts[contractName]))
    }
}

compile().then();

Hier sind die Versionen von web3js, solidity und anderen notwendigen Paketen, die ich verwende:

  "dependencies": {
    "ganache-cli": "^6.12.2",
    "mocha": "^9.2.0",
    "solc": "^0.8.11",
    "web3": "^1.0.0-beta.26"
  }

Antworten (1)

Habe die Lösung. Ich habe vergessen, abi bei der Ausgabeauswahl hinzuzufügen. Hier ist die korrigierte Eingangsvariable.

const input = {
    language: 'Solidity',
    sources: {
        'inbox.sol':{
            content: source,
        },
    },
    settings:{
        outputSelection: {
            '*': {
                '*': ['evm', 'bytecode', 'abi'],
                // use * to get all the fields
                // '*': ["*"]
            },
        },
    },
};