0
import Web3 from 'web3';

window.ethereum.enable();
const web3 = new Web3(window.ethereum);

const address = "0x977b5A39B6E95A7688570BF1b77e8FAb150791af";
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: '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'
  },
  {
    inputs: [],
    name: 'test',
    outputs: [[Object]],
    stateMutability: 'pure',
    type: 'function',
    constant: true,
    payable: undefined,
    signature: '0xf8a8fd6d'
  }
];

const contract = new web3.eth.Contract(abi, address);

const testMyContract = async () => {
  const manager = await contract.methods.manager().call();
  console.log("manager", manager); // return empty []
  const helloWorld = await contract.methods.test().call();
  console.log("Hello world", helloWorld); // return empty []
}

testMyContract();

The address is of the contract deployed to Rinkeby. Whenever I call any method, it always returns an empty array. Even pure functions. I am using Metamask as a provider for web3.

3
  • 1
    Is your abi correct? There are a bunch of Object that shouldn't be in a real abi. – Ismael Mar 31 at 0:03
  • 1
    OMG, you are correct. Thank you... basically, I just console.log'd the abi. Using JSON.Stringify instead made the ABI output correctly. Thank you so much. – Barry Steyn Mar 31 at 0:58
  • Please add the solution as an answer so it helps other in similar situation. – Ismael Mar 31 at 1:02
0

The problem was the abi - it was incorrect, and web3 silently fails. I obtained my ABI by just doing a console.log(abi) - which is incorrect. If you are doing it this way, rather do console.log(JSON.stringify(abi)).

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.