1

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.

3 Answers 3

3

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.

2
  • old post but the solution works.Any idea why this issue occurs? Oct 5, 2021 at 0:42
  • kudos! you saved my day Nov 2, 2021 at 6:49
0

I have new solution for this problem. If you use defaut values for compiler, you problably got this error.

So do this change and you're gona be able to compile on your application.

before:

  const input = {
  language: 'Solidity',
  sources: {
    [CONTRACT_FILE]: {
      content: content
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
}

after:

  const input = {
  language: "Solidity",
  sources: {
    [CONTRACT_FILE]: {
      content: source,
    },
  },
  settings: {
    optimizer: {
      enabled: true,
      runs: 200,
    },
    outputSelection: {
      "*": {
        "": ["ast"],
        "*": [
          "abi",
          "metadata",
          "devdoc",
          "userdoc",
          "storageLayout",
          "evm.legacyAssembly",
          "evm.bytecode",
          "evm.deployedBytecode",
          "evm.methodIdentifiers",
          "evm.gasEstimates",
          "evm.assembly",
        ],
      },
    },
    evmVersion: "byzantium",
  },
};

That way your compiler will behave like remix.

0

I faced similar issue while I was working with Chainlink.

I didn't have enough LINK tokens in Operator account of Chainlink Node. I passed the LINK tokens from Metamask to the address in the Key Management section of the Node Operator.

Also, check if the deployed Operator account is able to accept request by pasting the oracle contract address and checking the logs on https://etherscan.io/.

Hope this helps someone.

1
  • It is unrelated since the contract doesn't use Chainlink.
    – Ismael
    Mar 19 at 0:21

Your Answer

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

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