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

@token-kit/onchain

v0.0.2

Published

token-kit onchain tools

Downloads

47

Readme

@token-kit/onchain

This module provides some onchain tools.

tokenType

This function is to detect the type and subTypes of a given token contract address on the Ethereum blockchain.

Usage

import { tokenType } from "@token-kit/onchain";

const client = YOUR_VIEM_CLIENT;

const result = await tokenType("TOKEN_ADDRESS", client);

The tokenType function returns an object with the following structure:

{
   type: string;
   subTypes?: string[];
   scriptURI?: string[];
}

For type, this function supports the following types:

  • ERC20
  • ERC721
  • ERC1155
  • Unknown: If the contract doesn't match any of the above standards

For subTypes, it supports the following types:

  • ERC5169
  • IERC721TokenReceiver
  • IERC721Metadata
  • IERC721Enumerable
  • IERC1155TokenReceiver
  • IERC1155Metadata_URI

For ERC5169 tokens, the function will return the scriptURI if available.

Error Handling

If an error occurs during the detection process, the function will return { type: "Unknown" }.

Dependencies

This module requires viem library to interact with the Ethereum blockchain.

tokenData

This function is to get on-chain data for a given token (ERC20/ERC721/ERC1155), it will optionally fetch token metadata or Opensea contract metadata if exists.

Usage

import { tokenData } from "@token-kit/onchain";

const client = YOUR_VIEM_CLIENT;
const options: TokenDataOptions = {
   ...
}

const result = await tokenData(
  client,
  "TOKEN_ADDRESS",
  "TOKEN_ID", // only required for ERC721 and ERC1155 token
  options, // optional
);

The tokenData function returns an object depends on the token type:

ERC20TokenData {
  type: TokenType;
  name: string;
  symbol: string;
  decimals: number;
  totalSupply: number;
}

ERC721TokenData {
  type: TokenType;
  owner: `0x${string}`;
  tokenURI: string;
  tokenMetadata?: unknown; // parsed json result
  contractMetadata?: unknown; // parsed json result
}

ERC1155TokenData {
  type: TokenType;
  uri: string;
  tokenMetadata?: unknown; // parsed json result
  contractMetadata?: unknown; // parsed json result
}

For options

| option | description | | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | includeTokenMetadata: boolean | optional, control whether token metadata will be fetched, metadata source is decided by token type, it will use tokenURI value for ERC721, and uri value for ERC1155 | | includeContractMetadata: boolean | optional, control whether try to load Opensea contract-level metadata. Note. this is not part of the token metadata, if the provided token contract doesn't implement the function, it will be null | | ipfsGatewayDomain: string | optional, customize ipfs gateway for metadata fetching, default is gateway.pinata.cloud | | fetchHandler: (uri: string) => Promise<unknown> | optional, if you need to load metadata with custom network protocol, or process the raw metadata, this option will allow you to override the default fetch handler, provided function needs to take the metadata uri (for both token or contract-level), and return the result as a JSON object. Note: default metadata fetch handler only handles https / ipfs based metadata uri. |

Error Handling

If the token type of the provided address cannot be identified, it throws return error

Dependencies

This module requires viem library to interact with the Ethereum blockchain.

myNfts

This function is to get all the ERC721 enumerable NFTs by providing a known token

Usage

import { myNfts } from "@token-kit/onchain";

const client = YOUR_VIEM_CLIENT;
const options: MyNftsOptions = {
   ...
}

const result = await myNfts(
  client,
  "TOKEN_ADDRESS",
  "TOKEN_ID",
  options, // optional
);

The myNfts function returns:

MyNfts {
  owner: `0x${string}`;
  tokens: {
    tokenId: bigint;
    tokenURI: string;
    tokenMetadata?: unknown;
  }[];
}

For options

| option | description | | ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | includeTokenMetadata: boolean | optional, control whether token metadata will be fetched | | ipfsGatewayDomain: string | optional, customize ipfs gateway for metadata fetching, default is gateway.pinata.cloud | | fetchHandler: (uri: string) => Promise<unknown> | optional, if you need to load metadata with custom network protocol, or process the raw metadata, this option will allow you to override the default fetch handler, provided function needs to take the metadata uri, and return the result as a JSON object. Note: default metadata fetch handler only handles https / ipfs based metadata uri. |

Error Handling

If the token is not ERC721 Enumerable, it will throw an error

Dependencies

This module requires viem library to interact with the Ethereum blockchain.