question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Override type checking in contract call

See original GitHub issue

One way that MetaMask can look up a user’s ETH or ERC20 balance is to call a balance checking contract: https://etherscan.io/address/0xb1f8e55c7f64d203c1400b9d8555d050f94adf39#code

Specifically, we call the function balances(address[] users, address[] tokens) external view returns (uint[]). We make the call directly on a Contract object instantiated from the ABI.

To check a user’s ETH balance, the contract makes a special case for the address 0x0 (note that this is not intended to be the null/0-address) when encountered in the tokens array. ethers, however, treats 0x0 as the invalid address it is. This is very reasonable, but can we bypass the type checking somehow?

(Alternatively, if anyone knows of another balance checking contract, that would also be helpful.)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rekmarkscommented, Nov 20, 2019

Hah! I investigated further, and we were not handling the ethers BigNumber type correctly. It works perfectly. Thank you for your help!

1reaction
ricmoocommented, Nov 20, 2019

If you use the AddressZero, it seems to work fine for me. 😃

const deployedContractAddress = "0xb1F8e55c7f64D203C1400B9D8555d050F94aDF39";
const SINGLE_CALL_BALANCES_ABI=[{"constant":true,"inputs":[{"name":"user","type":"address"},{"name":"token","type":"address"}],"name":"tokenBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"users","type":"address[]"},{"name":"tokens","type":"address[]"}],"name":"balances","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"}];
const provider = ethers.getDefaultProvider();
const c = new ethers.Contract(deployedContractAddress, SINGLE_CALL_BALANCES_ABI, provider);
c.balances([ "0x8ba1f109551bD432803012645Ac136ddd64DBA72" ], [ ethers.constants.AddressZero ]).then(console.log)
> [ BigNumber { _hex: '0x1cdd9cc432e6807e' } ]
provider.getBalance("0x8ba1f109551bD432803012645Ac136ddd64DBA72").then(console.log)
>  BigNumber { _hex: '0x1cdd9cc432e6807e' }
Read more comments on GitHub >

github_iconTop Results From Across the Web

solidity - Altering / overriding the method from main contract
Functions can be overridden by another function with the same name and the same number/types of inputs. If the overriding function has different ......
Read more >
Solidity override vs. virtual functions | by Kseniya Lifanova
A function that allows an inheriting contract to override its behavior will be marked at virtual . The function that overrides that base ......
Read more >
False positive requirement for override? · Issue #8141 - GitHub
TypeError: Derived contract must override function "foo". Two or more base classes define function with same name and parameter types.
Read more >
Method overriding | Solidity Programming Essentials
Method overriding refers to redefining a function available in the parent contract having the same name and signature in the derived contract.
Read more >
Contracts — Solidity 0.8.17 documentation
A contract can have multiple functions of the same name but with different parameter types. This process is called “overloading” and also applies...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found