Solidity

無法從 solc.compile 響應 web3.js 中找到 ABI

  • February 5, 2022

我對乙太坊很陌生,試圖使用 web3js 從 solc.compile 獲取字節碼和 ABI,但找不到 ABI。我得到了字節碼。我保存了對文件的 json 響應,並且只能得到這些輸出。

  1. 程序集.json
  2. 字節碼.json
  3. 部署的Bytecode.json
  4. gasEstimates.json
  5. legacyAssembly.json
  6. 方法標識符.json

這是我的程式碼:

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();

這是我正在使用的 web3js、solidity 和其他必要軟體包的版本:

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

得到了解決方案。我忘了在輸出選擇上添加 abi。這是校正後的輸入變數。

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

引用自:https://ethereum.stackexchange.com/questions/120876