[Ethereum] Importing in SOLC to deploy smart contract

contract-deploymentopenzeppelin-contractssolc

I don't run an Ethereum framework like Truffle and I want to deploy a smart contract using infura node.

The smart contract is very simple:

pragma solidity ^0.5.2;  

import "http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";

  /**  
  * @title ACoin is basic ERC20 Token  
  */  
contract ACoin is ERC20, ERC20Detailed, Ownable{  

  uint256 public initialSupply = 100000000000; 
  /**  
  * @dev assign totalSupply to account creating this contract 
  */  
  constructor() public ERC20Detailed("ACoin", "ACO", 7) {  

   _mint(msg.sender, initialSupply);

 }


}

My compile script should be working fine:

const path = require('path');
const fs = require('fs');
const solc = require('solc');

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

console.log(solc.compile(source, 1).errors);

module.exports = solc.compile(source, 1).contracts[':ACoin'];

The issue is that solc " doesn't know what import means" and I'm getting this error:

[ ':3:1: ParserError: Source
"http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol"
not found: File not supplied initially.\nimport
"http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";\r\n^——————————————————————————————–^\n', ':4:1: ParserError: Source
"http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol"
not found: File not supplied initially.\nimport
"http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol";\r\n^——————————————————————————————–^\n', ':5:1: ParserError: Source
"http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol"
not found: File not supplied initially.\nimport
"http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";\r\n^—————————————————————————————————-^\n' ]

I looked into OpenZeppelin repo where I'm told the solution to my problem. An npm install openzeppelin-solidity & import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol" later, the problem still persists:

[ ':3:1: ParserError: Source
"openzeppelin-solidity/contracts/token/ERC20/ERC20.sol" not found:
File not supplied initially.\nimport
"openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";\r\n^————————————————————-^\n',

Is there anything I can do without using Truffle?

Important to mention: the smart contract provided will work in Remix

Best Answer

solc can't get things over a network - it can only import local files, which was your initial error.

I'm assuming your npm install step is still leading to failure because the compiler is still looking in the wrong place. (I'd assume the OpenZeppelin stuff has gone into the node_modules folder.)

Take a look at how to manually map in localised - i.e. downloaded/cloned - files by passing the relevant options to solc.

An example from the documentation:

> solc github.com/ethereum/dapp-bin/=/usr/local/dapp-bin/ source.sol

(Remix, being an IDE, has the in-built intelligence to know how to pull files from GitHub prior to the compilation step.)

Related Topic