0

I am new to solidity and I am facing a strange issue. I have created the following Contract:

pragma solidity >=0.7.0 <=0.8.0;
//SPDX-License-Identifier: UNLICENSED

contract Lottery{

    address public manager;
    address payable [] public players;

    constructor() {
        manager = msg.sender;
    }

    function getManager() public pure returns (string memory)
    {
        return '123';
    }

    function enter() public payable {

        require(msg.value> .001 ether);

        players.push(payable(msg.sender));
    }

    function pickWinner() public restricted {

        uint index = random() % players.length;
        address payable winner = players[index];
        winner.transfer(address(this).balance);

        players = new address payable [](0);
    }


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


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


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


}

I have deployed it to rinkeby network, and I am able to call the method getManager from Remix successfully. When I try to call the method from my javascript code, it returns an empty string.

ABI and contract address (my lottery.js file):

import myWeb3 from './web3';

const address = '0x96F422C353D6983A0fb2d4a8EC8fe08E188ABaa3';

const abi = [
  {
    inputs: [],
    stateMutability: 'nonpayable',
    type: 'constructor',
    constant: undefined,
    payable: undefined,
    signature: 'constructor'
  },
  {
    inputs: [],
    name: 'enter',
    outputs: [],
    stateMutability: 'payable',
    type: 'function',
    constant: undefined,
    payable: true,
    signature: '0xe97dcb62'
  },
  {
    inputs: [],
    name: 'getManager',
    outputs: [ [Object] ],
    stateMutability: 'pure',
    type: 'function',
    constant: true,
    payable: undefined,
    signature: '0xd5009584'
  },
  {
    inputs: [],
    name: 'getPlayers',
    outputs: [ [Object] ],
    stateMutability: 'view',
    type: 'function',
    constant: true,
    payable: undefined,
    signature: '0x8b5b9ccc'
  },
  {
    inputs: [],
    name: 'manager',
    outputs: [ [Object] ],
    stateMutability: 'view',
    type: 'function',
    constant: true,
    payable: undefined,
    signature: '0x481c6a75'
  },
  {
    inputs: [],
    name: 'pickWinner',
    outputs: [],
    stateMutability: 'nonpayable',
    type: 'function',
    constant: undefined,
    payable: undefined,
    signature: '0x5d495aea'
  },
  {
    inputs: [ [Object] ],
    name: 'players',
    outputs: [ [Object] ],
    stateMutability: 'view',
    type: 'function',
    constant: true,
    payable: undefined,
    signature: '0xf71d96cb'
  }
];

export default new myWeb3.eth.Contract(abi,address);

Then I call lottery.methods.getManager().call() and returns empty string [] at my chrome console.

Any advice? I have read similar cases but I could not find a solution for my case.

*Just an update on the issue, the rest of the function i.e. enter or pickWinner work just fine. It seems that I cannot read data from the contract.

0

Finally I found the solution.. The issue was caused by the ABI. I was using the ABI as generated by the compiler (the one I mention to my initial post). I switched to the ABI as generated by remix and it worked. There were some slight changes to the output types.

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.