weixin_39717865
weixin_39717865
2020-12-26 03:57

Compile Error: TypeError: Cannot read property 'TestToken.sol' of undefined

When I compile contract with three .sol files(if I use remix online compiler, they can be successfully compiled), the console prints error message: TypeError: Cannot read property 'TestToken.sol' of undefined when I print the compiled output variable, it says:


> console.log(output)
{ errors:
   [ { component: 'general',
       formattedMessage:
        'TestToken.sol:2:1: ParserError: Source "/root/TestToken/StandardToken.sol" not found: File import callback not supported\nimport "/root/TestToken/StandardToken.sol";\r\n^-----------------------------------------^\n',
       message:
        'Source "/root/TestToken/StandardToken.sol" not found: File import callback not supported',
       severity: 'error',
       sourceLocation: [Object],
       type: 'ParserError' } ],
  sources: {} }

I use absolute path to include each .sol, why is it still unable to find source file?

compiling js code


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': ''
};
for (var contractName in output.contracts['TestToken.sol']) {
    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);
})

the three .sol files, which are in same folder:/root/TestToken/ TestToken.sol


pragma solidity ^0.5.9;
import "/root/TestToken/StandardToken.sol";
contract TestToken is StandardToken {...}

StandardToken.sol


pragma solidity ^0.5.9;
import "/root/TestToken/Token.sol";
contract StandardToken is Token {...}

Token.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);
}

该提问来源于开源项目:ethereum/solc-js

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39776298 weixin_39776298 4月前

    You use different names for your source file. In the input json, you call it TestToken.sol, while in the import statement, you try to access it as /root/TestToken/Token.sol. If you change them to match, it should work.

    点赞 评论 复制链接分享
  • weixin_39717865 weixin_39717865 4月前

    I modified the code in input json, its still can not work.

    
    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: {
            '/root/TestToken/TestToken.sol': {
                content: contractSource
            },
        },
        settings: {
            outputSelection: {
                '*': {
                    '*': [ '*' ]
                }
            }
        }
    });
    
    点赞 评论 复制链接分享
  • weixin_39776298 weixin_39776298 4月前

    You have to either provide all the files in the input json or use a callback to retrieve them. The compiler does not automatically access your file system.

    点赞 评论 复制链接分享
  • weixin_39717865 weixin_39717865 4月前

    I list all the files needed as code below, it works well. Meanwhile I found the import path should be relative path of main .sol file(for this case, it's the TestToken.sol). Another option is to use flatter file which combile all seperate files into one. Thanks for your help.

    
    sources: {
            'TestToken.sol': {
                content: contractSource1
            },
            'StandardToken.sol': {
                content: contractSource2
            },
            'Token.sol': {
                content: contractSource3
            }
        }
    
    点赞 评论 复制链接分享

相关推荐