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

@slide-computer/tokens

v2.2.1

Published

JavaScript and TypeScript library to interact with tokens on the Internet Computer

Downloads

36

Readme

@slide-computer/tokens

JavaScript and TypeScript library to interact with tokens on the Internet Computer.


Installation

Using Tokens:

npm i --save @slide-computer/tokens

Install additional packages to add support for specific token standards:

@slide-computer/tokens-icrc1
@slide-computer/tokens-icrc2
@slide-computer/tokens-icrc4
@slide-computer/tokens-icrc7
@slide-computer/tokens-icrc10
@slide-computer/tokens-dip20
@slide-computer/tokens-ext
@slide-computer/tokens-dip721-v1
@slide-computer/tokens-dip721-v2
@slide-computer/tokens-dip721-v2-approval

In the browser:

import { createToken } from "@slide-computer/tokens";
import { Icrc1 } from "@slide-computer/tokens-icrc1";
import { Icrc2 } from "@slide-computer/tokens-icrc2";
import { Icrc4 } from "@slide-computer/tokens-icrc4";
import { Icrc7 } from "@slide-computer/tokens-icrc7";
import { Icrc10 } from "@slide-computer/tokens-icrc10";

To get started with a token, run

// ICRC-10 is listed first, because it should be the primary way of identifying
// supported standards before falling back to e.g. the ICRC-1 supported standards.
const token = await createToken([Icrc10, Icrc1, Icrc2, Icrc4, Icrc7], {
  canisterId: TOKEN_CANISTER_ID,
  // Required, must not be anonymous if tokens needs to be e.g. transferred
  agent: AGENT_INSTANCE,
  // Optional, recommended to use an anonymous agent for queries e.g. checking balance
  queryAgent: new HttpAgent(),
});

Check if a method is supported by the token before using it

if (token.decimals) {
  const decimals = await token.decimals();
}

For example check if a token is non-fungible or fungible by checking for support of ownerOf

const tokenIsNonFungible = !!token.ownerOf;

Multiple account string formats are supported (ICRC-1 is recommended)

// Create ICRC-1 format account string, if a token does not support this account format
// it will convert it internally into e.g. account hash or principal (default subaccount)
const account = encodeAccount({
  owner: ACCOUNT_PRINCIPAL,
  subaccount: SUBACCOUNT_BYTES // Optional, uses default subaccount if not set
});
const balance = await token.balanceOf(account);

// Create string from principal and get it's balance
const principal = ACCOUNT_PRINCIPAL.toText();
const balanceFromPrincipal = await token.balanceOf(principal);

// Create ICP ledger account hash and get balance
const accountHash = hashAccount({
  owner: ACCOUNT_PRINCIPAL,
  subaccount: SUBACCOUNT_BYTES // Optional, uses default subaccount if not set
});
// Check if account hash is supported to avoid an unsupported exception
if (token.usesAccountHash) {
  const balanceFromAccountHash = await token.balanceOf(accountHash);
}

Check balance of account from user input

const account = USER_INPUT;
if (!isAccount(account) || !isAccountHash(account)) {
  // Show user that account input is invalid
}
if (isAccountHash(account) && !token.usesAccountHash) {
  // Show user that this account format is not supported by token
}
const balance = await token.balanceOf(account);

Get NFT details: name, description, url, image and attributes

// Get NFT metadata
const metadata = await token.tokenMetadata(TOKEN_ID);

// Lookup details within metadata
const name = token.tokenMetadataToName?.(metadata);
const description = token.tokenMetadataToDescription?.(metadata);
const url = token.tokenMetadataToUrl?.(metadata);
const image = token.tokenMetadataToImage?.(metadata);
const attributes = token.tokenMetadataToAttributes?.(metadata);

Identify the token method and arguments behind a raw canister call

const decoded = token.decodeCall?.(method, args);
if (decoded.method === 'transfer') {
  // Show interface that dapp want to transfer `decoded.args[0].amount` fungible tokens
} else if (decoded.method === 'transferToken') {
  // Show interface that dapp want to transfer non-fungible token id `decoded.args[0].tokenId`
} else {
  // Show ICRC-21 consent message
}

Create token instance synchronously

// Get standards of asynchronously created instance and store these
const standards = token.implementedStandards;

// Create instance synchronously by passing in these stored standards
const token2 = createToken([Icrc10, Icrc1, Icrc2, Icrc4, Icrc7], {
  canisterId: TOKEN_CANISTER_ID,
  agent: AGENT_INSTANCE,
  queryAgent: new HttpAgent(),
  supportedStandards: standards
});

List of all available methods

See methods defined on the following exported types

Utility method types

Token method types

Token batch method types