Go-Ethereum

Solc 編譯器錯誤“TypeError:無法讀取未定義的屬性 ‘TestToken.sol’”

  • January 24, 2022

我建立了一個包含三個文件的契約,這些文件引用了Token Factory。我在remix線上工具上做了一個測試,可以成功編譯。

當我試圖通過 .js 文件中的 solc 模組編譯它們時,出現錯誤提示“TypeError:無法讀取未定義的屬性’TestToken.sol’”

> for (var contractName in output.contracts['TestToken.sol']) {
...     TokenJson.abi = output.contracts['TestToken.sol']["TestToken"].abi;
...     TokenJson.bytecode = output.contracts['TestToken.sol']["TestToken"].bytecode;
... }
TypeError: Cannot read property 'TestToken.sol' of undefined

請幫助如何解決問題,非常感謝。

solc 編譯器 .js 文件如下:

var fs = require('fs');
var solc =  require('solc');
let contractSource = fs.readFileSync('/root/TestToken/TestToken.sol', 'utf-8');
let jsonContractSource = JSON.stringify({
   language: 'Solidity',
   sources: {
       'TestToken.sol': {
           content: contractSource
       }
   },
   settings: {
       outputSelection: {
           '*': {
               '*': [ '*' ]
           }
       }
   }
});
let output = JSON.parse(solc.compile(jsonContractSource));
TokenJson = {
   'abi': {},
   'bytecode': ''
};
   TokenJson.abi = output.contracts['TestToken.sol']["TestToken"].abi;
   TokenJson.bytecode = output.contracts['TestToken.sol']["TestToken"].evm.bytecode.object;

fs.writeFile('/root/TestToken/TestToken.json', JSON.stringify(TokenJson), function(err){
   if(err)
       console.error(err);
})

契約文件:

測試令牌.sol

pragma solidity ^0.5.9;
import "/root/TestToken/StandardToken.sol";
contract TestToken is StandardToken {
/* Public variables */
string public name;
uint8 public decimals;
string public symbol;
string public version = '0.1';

constructor(
   uint256 _initialAmount,
   string memory _tokenName,
   uint8 _decimalUnits,
   string memory _tokenSymbol
   ) public {
   balances[msg.sender] = _initialAmount;
   totalTokenSupply = _initialAmount;
   name = _tokenName;
   decimals = _decimalUnits;
   symbol = _tokenSymbol;
}

function approveAndCall(address _spender, uint256 _value) public returns (bool success) {
   allowed[msg.sender][_spender] = _value;
   emit Approval(msg.sender, _spender, _value);
   return true;
} }

標準令牌.sol

pragma solidity ^0.5.9;
import "/root/TestToken/Token.sol";
contract StandardToken is Token {
   function transfer(address _to, uint256 _value) public returns (bool success) {
       //if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
       if (balances[msg.sender] >= _value && _value > 0) {
           balances[msg.sender] -= _value;
           balances[_to] += _value;
           emit Transfer(msg.sender, _to, _value);
           return true;
       } else { return false; }
   }
   function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
       //if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
       if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) {
           balances[_to] += _value;
           balances[_from] -= _value;
           allowed[_from][msg.sender] -= _value;
           emit Transfer(_from, _to, _value);
           return true;
       } else { return false; }
   }
   function balanceOf(address _owner) public view returns (uint256 balance) {
       return balances[_owner];
   }
   function approve(address _spender, uint256 _value) public returns (bool success) {
       allowed[msg.sender][_spender] = _value;
       emit Approval(msg.sender, _spender, _value);
       return true;
   }
   function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
     return allowed[_owner][_spender];
   }
   mapping (address => uint256) balances;
   mapping (address => mapping (address => uint256)) allowed;
   uint256 totalTokenSupply;
}

令牌.sol

pragma solidity ^0.5.9;
contract Token {
   function totalSupply() public view returns (uint256 supply) {}
   function balanceOf(address _owner) public view returns (uint256 balance) {}
   function transfer(address _to, uint256 _value) public returns (bool success) {}
   function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {}
   function approve(address _spender, uint256 _value) public returns (bool success) {}
   function allowance(address _owner, address _spender) public view returns (uint256 remaining) {}
   event Transfer(address indexed _from, address indexed _to, uint256 _value);
   event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

檢查 SOLC 的版本和你的 sol 文件的solidity 版本。‘源文件需要不同的編譯器版本’

您的 sol 文件版本是 pragma solidity ^0.5.9; 你的 SOLC 版本是什麼…?

let contractSource = fs.readFileSync('/root/TestToken/TestToken.sol', 'utf-8')

我相信你在這裡得到了錯誤。此目錄在 posix 系統中有效。如果您使用的是非 posix 作業系統,這將不起作用。更好地使用內置在“路徑”模組中的 node.js。

const path=require("path")
const TestTokenPath = path.resolve(__dirname, "TestToken", "TestToken.sol");
const source = fs.readFileSync(TestTokenPath, "utf8");

此外,您還定義了這個 JSON 對象:

TokenJson = {
   'abi': {},
   'bytecode': ''
};

但你不能像普通對像一樣訪問它的屬性:

   TokenJson.abi // this is invalid

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