0

this is my contract -

pragma solidity ^0.5.17;

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

constructor () 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(abi.encodePacked(block.difficulty, now, players)));

}

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

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

function getPLayers() public view returns(address payable[] memory) {
    return players;
}

}

compile.js -

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, 'UTF-8');

var input = {
  language: 'Solidity',
  sources: {
      'Lottery.sol' : {
     content:source
    }
  },
  settings: {
    optimizer: {
      enabled: true
    },
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};
var output = JSON.parse(solc.compile(JSON.stringify(input)));

var outputContracts = output.contracts['Lottery.sol']['Lottery'];

// console.log('Abi: ', outputContracts.abi);
// console.log('Bytecode: ', outputContracts.evm.bytecode.object);

module.exports.abi = outputContracts.abi;

module.exports.bytecode = outputContracts.evm.bytecode.object;

deploy.js -

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const { abi, bytecode } = require('./compile');
const mnemonic = "cluster couch nothing ....";
const provider = new HDWalletProvider(mnemonic,
"https://rinkeby.infura.io/v3/MY_API_KEY");

if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.Provider);
} else {
    // set the provider you want from Web3.providers
    console.log('No web3? You should consider trying MetaMask!');
   web3 = new Web3('ws://localhost:7545');
}

// 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(abi)
    .deploy({ data: '0x' + bytecode })
    .send({ from: accounts[0], gas: '1500000', gasPrice: '30000000000' });

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

deploy();

package.json -

{
  "name": "inbox",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@truffle/hdwallet-provider": "^1.4.1",
    "ganache-cli": "^6.12.2",
    "mocha": "^9.0.1",
    "solc": "^0.5.17",
    "truffle-hdwallet-provider": "^1.0.17",
    "web3": "^1.4.0"
  }
}

Outputs -

aiky@aiky-Inspiron-5570:~/Desktop/lottery$ node deploy.js
aiky@aiky-Inspiron-5570:~/Desktop/lottery$ node deploy.js
aiky@aiky-Inspiron-5570:~/Desktop/lottery$ node deploy.js
No web3? You should consider trying MetaMask!
Attempting to deploy from account 0x2EDA0C601CffEdd363bB96C54b1442560Fab871F
[
  {
    inputs: [],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'constructor',
    constant: undefined,
    signature: 'constructor'
  },
  {
    constant: false,
    inputs: [],
    name: 'enter',
    outputs: [],
    payable: true,
    stateMutability: 'payable',
    type: 'function',
    signature: '0xe97dcb62'
  },
  {
    constant: true,
    inputs: [],
    name: 'getPLayers',
    outputs: [ [Object] ],
    payable: false,
    stateMutability: 'view',
    type: 'function',
    signature: '0x1a9828b4'
  },
  {
    constant: true,
    inputs: [],
    name: 'manager',
    outputs: [ [Object] ],
    payable: false,
    stateMutability: 'view',
    type: 'function',
    signature: '0x481c6a75'
  },
  {
    constant: false,
    inputs: [],
    name: 'pickWinner',
    outputs: [],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'function',
    signature: '0x5d495aea'
  },
  {
   constant: true,
   inputs: [ [Object] ],
   name: 'players',
   outputs: [ [Object] ],
   payable: false,
   stateMutability: 'view',
   type: 'function',
   signature: '0xf71d96cb'
  }
]
Contract deployed to 0xDBfB50195391F5d644b19d1EC1DBBF2EE3030C2f

As you can see, If I deploy my contract without if statement in deploy.js using const web3 = new Web3(provider); I'm not getting any output but if I deploy in localhost with ganache, It gives me console logs of abi and deployed address. I cannot figure out what the problem is, I've rinkeby ether in metamak wallet, I've login into the metamask. Please give me some hints...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.