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

@ngeth/ethers-core

v0.0.26

Published

Provide common pattern around ethers.js

Downloads

11

Readme

ethers-core

Provide common pattern around ethers.js

EthersContract

Super strongly typed contract built around the ethers.js Contract.

It's mainly used by @ngeth/hardhat output:

interface ERC20Events {
  events: {
    Approval: (owner: string, spender: string, value: BigNumber) => void;
    Transfer: (from: string, to: string, value: BigNumber) => void;
  };
  filters: {
    Approval: (owner?: FilterParam<string>, spender?: FilterParam<string>) => TypedFilter<"Approval">;
    Transfer: (from?: FilterParam<string>, to?: FilterParam<string>) => TypedFilter<"Transfer">;
  };
  queries: {
    Approval: { owner: string; spender: string; value: BigNumber };
    Transfer: { from: string; to: string; value: BigNumber };
  };
}

class ERC20 extends EthersContract<ERC20Events> {}

EthersContract keeps track of the filters :

const erc20 = new ERC20(address, abi);
const mint = erc20.filters.Transfer(constant.AddressZero);
const events = await erc20.queryFilter(mint);
// Typescript knows that initialOwners is string[]
const initialOwners = events.map(event => event.args.to);

Tokens

Common operation on tokens:

erc20Balance(received: Event[], sent: Event[]): BigNumber

Calculate balance of user based on Transfer events. Useful to get the balance at a specific block.

const receivedFilter = erc20.filters.Transfer(undefined, address);
const sentFilter = erc20.filters.Transfer(address);
const [received, sent] = await Promise.all([
  erc20.queryFilter(receivedFilter, fromBlock, toBlock),
  erc20.queryFilter(sentFilter, fromBlock, toBlock),
]);
const balance = erc20Balance(received, sent);

erc721Tokens(received: Event[], sent: Event[]): string[]

Returns the list of tokenIds owned by an address

const receivedFilter = erc721.filters.Transfer(undefined, address);
const sentFilter = erc721.filters.Transfer(address);
const [received, sent] = await Promise.all([
  erc721.queryFilter(receivedFilter),
  erc721.queryFilter(sentFilter),
]);
const tokens = erc721Tokens(received, sent);

erc1155Tokens(received: Event[], batchReceived: Event[], sent: Event[], batchSent: Event[]): Record<string, BigNumber>

Returns the amounts of tokenIds owned by an address

const [receivedSingle, receivedBatch, sentSingle, sentBatch] = await Promise.all([
  erc1155.queryFilter(erc1155.filters.TransferSingle(undefined, undefined, address)),
  erc1155.queryFilter(erc1155.filters.TransferBatch(undefined, undefined, address)),
  erc1155.queryFilter(erc1155.filters.TransferSingle(undefined, address)),
  erc1155.queryFilter(erc1155.filters.TransferBatch(undefined, address)),
]);
const tokensBalance = erc1155Tokens(receivedSingle, receivedBatch, sentSingle, sentBatch);

Chain

Toolkit to manage Chains metadata based on https://github.com/ethereum-lists/chains repository.

getChain(chainId: string|number): Promise<Chain>

Return the metadata associated with the chainId in https://github.com/ethereum-lists/chains

getChainIcons(name: string, format?: 'png' | 'jpg' | 'svg'): Promise<ChainIcon>

Return the chain icon metadata based on the name provided by the chain metadata (see getChain above).

defaultCustomChains

Static values to add http://localhost:8545 & Hardhat local network as a supported Chain

ERC1193

This is an abstract class to create custom ERC1193 classes.

InjectedProviders

This is an implementation of the ERC1193 classes for web3 injected providers.

const erc1193 = new InjectedProviders();
// Ask the user to connect a wallet (coinbase or Metamask)
const [address] = await erc1193.enable();

// Ask the user to switch to Mumbai, and set the chain if it doesn't exist yet
const mumbai = await getChain(80001);
await erc1193.addChain(mumbai);
await erc1193.switchChain(mumbai);