2

I am trying to deploy the Lottery contract using solc compiler of verison 0.4.17 but it's showing me nothing. But the same contract is working good in the remix ide. I providing you the the Solidiy code and also the dependencies that I have used to deploy this to the rinkeby network using infura network.

Lottery.sol(This is the smart contract file)

pragma solidity ^0.4.17;

contract Lottery {
    address public manager;
    address[] public players;

    function Lottery() public {
        manager = msg.sender;
    }

    function enter() public payable {
        require(msg.value > .01 ether);

        players.push(msg.sender);
    }

    function random() private view returns (uint) {
        return uint(keccak256(block.difficulty, now, players));
    }

    function pickWinner() public restricted {
        uint index = random() % players.length;
        players[index].transfer(this.balance);
        players = new address[](0);
    }

    modifier restricted() {
        require(msg.sender == manager);
        _;
    }

    function getPlayers() public view returns (address[]) {
        return players;
    }
}

The following is the compile script

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

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

module.exports = solc.compile(source, 1).contracts[':Lottery'];
const path = require('path');
const fs = require('fs');
const solc = require('solc');

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

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

I want to deploy the above script from the local device(I am using mac) using solc compiler. I have to deploy the code using the following js script.

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const { interface, bytecode } = require('./compile');

const provider = new HDWalletProvider(
  'joy nuclear cause beauty strike quick priority upset hello strategy manual shadow',
  'rinkeby.infura.io/v3/979787f2e36741368aee9a3d1b73b438'
);
const web3 = new Web3(provider);

const deploy = async () => {
  const accounts = await web3.eth.getAccounts();

  console.log('Attempting to deploy from account', accounts[0]);

  const result = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({ data: bytecode })
    .send({ gas: '1000000', from: accounts[0] });

  console.log('Contract deployed to', result.options.address);
};
deploy();

In order to have you the general idea of the dependencies that I have used I am also providing you the package.json file here below

{
  "name": "set",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ganache-cli": "^6.4.3",
    "mocha": "^6.1.4",
    "solc": "^0.4.17",
    "truffle-hdwallet-provider": "^1.0.9",
    "web3": "^1.0.0-beta.37"
  }
}

Please let me know if you could be able to compile and deploy the script successfully. In my case when I try to deploy it this doesn't provide me any output. Waiting for your solutions

1 Answer 1

0

Its working for me in windows..Assuming compilation is successful and you are able to console log at all steps(contract deployed to)in deploy script, Try '0x' parameter in data: '0x' + bytecode.

3
  • Did not get you can you please be little bit clear May 19, 2019 at 17:50
  • Hi..try prefixing bytecode with 0x if your bytecode doesnt have start with 0x.. May 20, 2019 at 4:14
  • Yes, mate tried this too. I took the bytecode and added 0x to it but I am facing the same issue. May 20, 2019 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.