Coder Social home page Coder Social logo

eth-balance-checker's Introduction

Ethereum Balance Checker

A smart contract and library pair that allows you to check for multiple ERC20 and Ether balances across multiple addresses in a single RPC call.

Demo

You can find a demo over here: https://wbobeirne.github.io/eth-balance-checker-demo

The source for that demo is available here: https://github.com/wbobeirne/eth-balance-checker-demo

NPM Package

npm install --save eth-balance-checker
# OR
yarn add eth-balance-checker

Contract

The main contract is in contracts/BalanceChecker.sol. Its associated ABI is in abis/BalanceChecker.abi.json. Both of these are included in the NPM package if you want to compile them yourself, or extend them with another contract.

Deployed Addresses

  • mainnet: 0xb1f8e55c7f64d203c1400b9d8555d050f94adf39
  • ropsten: 0x8D9708f3F514206486D7E988533f770a16d074a7
  • rinkeby: 0x3183B673f4816C94BeF53958BaF93C671B7F8Cf2
  • kovan: 0x55ABBa8d669D60A10c104CC493ec5ef389EC92bb
  • goerli: 0x9788C4E93f9002a7ad8e72633b11E8d1ecd51f9b
  • binance smart chain mainnet: 0x2352c63A83f9Fd126af8676146721Fa00924d7e4
  • binance smart chain testnet: 0x2352c63A83f9Fd126af8676146721Fa00924d7e4
  • polygon: 0x2352c63A83f9Fd126af8676146721Fa00924d7e4
  • mumbai: 0x2352c63A83f9Fd126af8676146721Fa00924d7e4
  • Optimism: 0xB1c568e9C3E6bdaf755A60c7418C269eb11524FC
  • Optimism Kovan: 0xB1c568e9C3E6bdaf755A60c7418C269eb11524FC
  • arbitrum: 0x151E24A486D7258dd7C33Fb67E4bB01919B7B32c
  • avalanche: 0xD023D153a0DFa485130ECFdE2FAA7e612EF94818 and 0xE0baF851F839874141bB73327f9C606147a52358
  • fantom: 0x07f697424ABe762bB808c109860c04eA488ff92B and 0xfC701A6b65e1BcF59fb3BDbbe5cb41f35FC7E009
  • ether-classic: 0xfC701A6b65e1BcF59fb3BDbbe5cb41f35FC7E009
  • Tron: TN8RtFXeQZyFHGmH1iiSRm5r4CRz1yWkCf --> Yes, it also works in Tron! Use hex addresses format to interact with it (41...).

Library

There are separate libraries for web3.js and ethers.js, both with identical APIs. Just import the functions from either eth-balance-checker/lib/web3 or eth-balance-checker/lib/ethers. For all functions, pass "0x0" as the "token" address to get the ether balance of an address.

All functions also take in an optional 4th options parameter, those options are as follows:

interface Options {
  // Choose a custom contract address. Must be provided to run the
  // code on non-mainnet network.
  contractAddress?: string;
}

getAddressBalances

Parameters
  • provider: Web3 | Ethers.Provider - The provider to use for the contract call.
  • address: string - The address to lookup balances for
  • tokens: string[] - Array of token contract addresses. Only supports ERC20 tokens.
  • options?: Options - Options for the contract, see above for options.
Returns
Promise<{
  // Ether balance
  "0x0": "100",
  // Token balances
  "0x123...": "500",
  "0x456...": "100000",
  ...
}>
Example
import Web3 from 'web3';
import { getAddressBalances } from 'eth-balance-checker/lib/web3';

const web3 = new Web3(...);
const address = '0x123...';
const tokens = ['0x0', '0x456...'];
getAddressBalances(web3, address, tokens).then(balances => {
  console.log(balances); // { "0x0": "100", "0x456...": "200" }
});

getAddressesBalances

Parameters
  • provider: Web3 | Ethers.Provider - The provider to use for the contract call.
  • addresses: string[] - Array of addresses to lookup balances for.
  • tokens: string[] - Array of token contract addresses. Only supports ERC20 tokens.
  • options?: Options - Options for the contract, see above for options.
Returns
Promise<{
  // Address as the key
  "0x123...": {
    // Ether balance
    "0x0": "100",
    // Token balances
    "0x456...": "500",
    "0x789...": "10000",
    ...
  },
  ...
}>
Example
import * as Ethers from 'ethers';
import { getAddressesBalances } from 'eth-balance-checker/lib/ethers';

const ethers = Ethers.getDefaultProvider();
const addresses = ['0x123...', '0x456...'];
const tokens = ['0x0', '0x789...'];
getAddressesBalances(ethers, addresses, tokens).then(balances => {
  console.log(balances); // { "0x123...": { "0x0": "100", ... }, ... }
});

Development

Setup

Requires node 8+. Just install packages, then use commands as needed:

npm install
# OR
yarn

Commands

  • build:contract - Runs truffle compile on the contract, and extracts ABI.
  • build:lib - Runs tsc and outputs js and typedefs to lib/
  • build - Runs build:contract then build:lib
  • test:contract - Runs truffle test
  • test:lib - No tests implemented yet
  • test - Runs tst:contract and test:lib

Credits

eth-balance-checker's People

Contributors

eulerbeat5 avatar eviljordan avatar jhonnyjason avatar lovely-necromancer avatar polsaker avatar varanauskas avatar wbobeirne avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

eth-balance-checker's Issues

Ethers TypeScript types incompatible

I am trying to use the library with the most recent Ethers library (5.4.1) in TypeScript.

Yet, because Ethers updated their types, the types are incompatible.

I am getting this error:

  Type 'import("/node_modules/@ethersproject/abstract-provider/lib/index").Provider' is not assignable to type 'import("/node_modules/eth-balance-checker/node_modules/ethers/providers/abstract-provider").Provider'.
    The types returned by 'getGasPrice()' are incompatible between these types.
      Type 'Promise<import("/node_modules/@ethersproject/bignumber/lib/bignumber").BigNumber>' is not assignable to type 'Promise<import("/node_modules/eth-balance-checker/node_modules/ethers/utils/bignumber").BigNumber>'.
        Property 'maskn' is missing in type 'import("/node_modules/@ethersproject/bignumber/lib/bignumber").BigNumber' but required in type 'import("/node_modules/eth-balance-checker/node_modules/ethers/utils/bignumber").BigNumber'.```

Move truffle to devDependencies

As I understand truffle is only required for development/testing of the solidity contract.

As users querying the balance on one of the supported networks query an already deployed contract directly, they do not require truffle.

Thus, truffle could be migrated to devDependencies

Publish package updates to NPM

Package has received multiple updates to the master branch, however, the latest version on NPM is published more than 3 years ago.

Please publish the changes to NPM manually, or work on a solution to automatically publish them on GitHub release (and start making releases)

address for kovan

0x55ABBa8d669D60A10c104CC493ec5ef389EC92bb

I depoly it on to kovan chain for use

Bug: invalid argument when fetching native currency balance "0x0"

Error:

invalid address (argument=\"address\", value=\"0x0\", code=INVALID_ARGUMENT, version=address/5.4.0) (argument=null, value=\"0x0\", code=INVALID_ARGUMENT, version=abi/5.0.7)

And if i add '0x0'.padEnd(42) this error is thrown Provided address 0x0 is invalid, the capitalization checksum test failed, or it's an indirect IBAN address which can't be converted.

Tron address

Hi I'm getting this error when trying with TRON
Provided address 41856001c68f53200cfa0bec38fc08ba8fd94bafc7 is invalid, the capitalization checksum test failed, or it's an indirect IBAN address which can't be converted.

If someone could provide and example of how to use it with Web3 on tron I'll appreciate it.

testnet contracts

Hi,
Very awesome Contract!

Is it also deployed on any testnet?

Cheers!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.