Giter Club home page Giter Club logo

bsc-scan's Introduction

bsc-scan

THIS IS A FORK OF https://github.com/MyCryptoHQ/eth-scan FOR THE BINANCE SMART CHAIN

bsc-scan is a library written in TypeScript, to help you fetch BNB or (BEP-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 0x7E9f1Ee1905B5Bde9522c20bbb78bAbe3c3FB4ca on the Binance Smart Chain mainnet.

It can use Web3.js, Ethers.js, JSON-RPC (HTTP), or an EIP-1193-compatible 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 @iam4x/bsc-scan

or

npm install @iam4x/bsc-scan

Example

import { getEtherBalances } from '@iam4x/bsc-scan';

getEtherBalances('http://127.0.0.1:8545', [
  '0x9a0decaffb07fb500ff7e5d253b16892dbec006a',
  '0xeb65f72a2f5464157288ac15f1bb56c56e6be375',
  '0x1b96c634f9e9fcfb76932e165984901701352ffd',
  '0x740539b55ee5dc58efffb88fea44a9008f8daa6f',
  '0x95d9e32dc03770699a6a5e5858165b174d500015'
]).then(console.log);

Results in:

{
  '0x9a0decaffb07fb500ff7e5d253b16892dbec006a': BigInt(1000000000000000000),
  '0xeb65f72a2f5464157288ac15f1bb56c56e6be375': BigInt(1000000000000000000),
  '0x1b96c634f9e9fcfb76932e165984901701352ffd': BigInt(1000000000000000000),
  '0x740539b55ee5dc58efffb88fea44a9008f8daa6f': BigInt(1000000000000000000),
  '0x95d9e32dc03770699a6a5e5858165b174d500015': BigInt(1000000000000000000)
}

API

getEtherBalances(provider, addresses, options)

Get Ether balances for addresses.

  • provider <Provider> - A Web3 instance, Ethers.js provider, JSONRPC endpoint, or EIP-1193 compatible provider

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

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

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

getTokenBalances(provider, addresses, token, options)

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.

  • provider <Provider> - A Web3 instance, Ethers.js provider, JSONRPC endpoint, or EIP-1193 compatible provider

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

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

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

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

getTokensBalance(provider, address, tokens, options)

Get ERC-20 token balances from tokens for address. If one of the token addresses specified is not a token, a balance of 0 will be used.

  • provider <Provider> - A Web3 instance, Ethers.js provider, JSONRPC endpoint, or EIP-1193 compatible provider

  • address <string> - The address to get token balances for.

  • tokens <string[]> - An array of ERC-20 token addresses.

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

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

getTokensBalances(provider, addresses, tokens, options)

  • provider <Provider> - A Web3 instance, Ethers.js provider, JSONRPC endpoint, or EIP-1193 compatible provider.

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

  • tokens <string[]> - An array of ERC-20 token addresses.

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

  • Returns: <Promise<BalanceMap<BalanceMap>>> - 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 0x86f25b64e1fe4c5162cdeed5245575d32ec549db.

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

BalanceMap

A BalanceMap is an object with an address as key and a bigint as value.

Providers

Currently, bsc-scan has support for four different providers:

  • Ethers.js, by using an existing Ethers.js provider
  • Web3, by using an instance of the Web3 class
  • HTTP, by using a URL of a JSONRPC endpoint as string
  • EIP-1193 compatible provider, like window.ethereum

Compatiblity

bsc-scan uses ES6+ functionality, which may not be supported on all platforms. If you need compatibility with older browsers or Node.js versions, you can use something like Babel.

There is an ES compatible version available, which should work with module bundlers like Webpack and Rollup.

bsc-scan's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar iam4x avatar mrluit avatar mrtenz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.