npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eth-scan

v0.1.2

Published

An efficient Ether and token balance scanner

Downloads

30

Readme

eth-scan

eth-scan is a library written in TypeScript, to help you fetch Ether or (ERC-20) token balances for multiple addresses in an efficient way. The library uses a smart contract to fetch the balances in a single call to a node. The contract is currently deployed at 0x82Ea2E7834Bb0D6224dd6fd7125d44b83d6D6809 on the Ethereum mainnet.

It can use Web3.js, Ethers.js or regular HTTP as provider to get the balances. See Getting Started for more info.

Note: Even though eth_call doesn't use any gas, the block gas limit still applies, and the maximum number of addresses you can fetch in a single call is limited. By default this library batches calls per 1000 addresses.

Getting started

The library is published on npm. To install it, use npm or yarn:

yarn add eth-scan

or

npm install eth-scan

Example

import EthScan, { HttpProvider } from 'eth-scan';

const scanner = new EthScan(new HttpProvider('http://127.0.0.1:8545'));

scanner.getEtherBalances([
  '0x9a0decaffb07fb500ff7e5d253b16892dbec006a',
  '0xeb65f72a2f5464157288ac15f1bb56c56e6be375',
  '0x1b96c634f9e9fcfb76932e165984901701352ffd',
  '0x740539b55ee5dc58efffb88fea44a9008f8daa6f',
  '0x95d9e32dc03770699a6a5e5858165b174d500015'
]).then(console.log);

Results in:

{
  '0x9a0decaffb07fb500ff7e5d253b16892dbec006a': 1000000000000000000n,
  '0xeb65f72a2f5464157288ac15f1bb56c56e6be375': 1000000000000000000n,
  '0x1b96c634f9e9fcfb76932e165984901701352ffd': 1000000000000000000n,
  '0x740539b55ee5dc58efffb88fea44a9008f8daa6f': 1000000000000000000n,
  '0x95d9e32dc03770699a6a5e5858165b174d500015': 1000000000000000000n
}

Note that this library uses the native JavaScript BigInt type, which currently does not work in all browsers. Use a polyfill if your application requires support for other browsers.

API

EthScan

new EthScan(provider, options?)

The main class used to get Ether or token balances.

  • provider <Provider> - An instance of the Web3.js, Ethers.js or HTTP provider class.

  • options <EthScanOptions> (optional) - The options to use.

getEtherBalances(addresses)

Get Ether balances for addresses.

  • addresses <string[]> - An array of addresses as hexadecimal string.

  • Returns: <Promise> - A promise with an object with the addresses and the balances.

getTokenBalances(token, addresses)

Get ERC-20 token balances from token for addresses. This does not check if the address specified is a token and will throw an error if it isn't.

  • token <string> - The address of the ERC-20 token.

  • addresses <string[]> - An array of addresses as hexadecimal string.

  • Returns: <Promise> - A promise with an object with the addresses and the balances.

EthScanOptions

  • contractAddress <string> (optional) - The address of the smart contract to use. Defaults to 0x82Ea2E7834Bb0D6224dd6fd7125d44b83d6D6809.

  • batchSize <number> (optional) - The size of the call batches. Defaults to 1000.

Providers

There are currently three available providers.

new EthersProvider(provider)

Create a provider from an existing Ethers.js provider.

  • provider <Provider> - An instance of an Ethers.js provider.

new HttpProvider(url)

Create a provider that uses a simple HTTP request.

  • url <string> - The URL of the node to connect to.

new Web3Provider(web3)

Create a provider from an existing Web3.js instance.

  • web3 <Web3> - An instance of the Web3 class.