0

Hi I am trying to deploy a contract but keep getting this error. I am new to solidity.

I am running the cmd node deploy.js

error message

const interface = JSON.parse(compile).contracts["lottery.sol"].Lottery.abi; ^ SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () at Object. (C:\Users\LG\OneDrive\Desktop\Solidity\lottery\truffle-smart-contract\deploy.js:5:24) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47

pragma solidity ^0.5.16;

contract Lottery {
    address public manager;
    address payable [] public players;
    
    function Start_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(abi.encodePacked(block.difficulty, now, players)));
    }
    
    function pickWinner() public restricted {   
        address payable winner = players[random() % players.length];

        winner.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;
    }
    
}

Deploy.js

const HDWalletProvider = require('@truffle/hdwallet-provider');
const Web3 = require('web3');
const compile = require('./compile');
const interface = JSON.parse(compile).contracts["lottery.sol"].Lottery.abi;
const bytecode = JSON.parse(compile).contracts["Inbox.sol"].Lottery.evm.bytecode.object;


const provider = new HDWalletProvider(
    '<mnemonic phrases>',
    'https://rinkeby.infura.io/v3/<xxxxxxxxx>'
);

const web3 = new Web3(provider);

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

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

    const result = await new web3.eth.Contract(interface)
        .deploy({data: '0x' + bytecode})
        .send({from: accounts[0]});
    
    console.log(interface);
    console.log('Contract deployed to', results.options.address);
};

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: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
}; 

module.exports = JSON.parse(solc.compile(JSON.stringify(input)));
0

This part is suspicious. ["Inbox.sol"].Lottery

You read Inbox.sol, why do I access "Lottery" instead of "Inbox"?

1
  • yeah. @Ming you can confirm the interface and bytecode using console.log Apr 11 at 14:13

Your Answer

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

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