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

@lunarislab/web3tokens

v1.0.24

Published

A library for interacting with EVM tokens such as ERC20, ERC721 and ERC1155.

Downloads

37

Readme

Vision and Goals

The primary goal of Web3Tokens is to simplify the use of smart contracts, specifically focusing on token interactions. By providing a user-friendly interface and abstracting the complexities of smart contracts, ABIs, and signatures, Web3Tokens empowers developers—especially those who are new to blockchain development—to jump right in and start building.

Prerequisites

Before you start using Web3Tokens, you should have a basic understanding of Viem. This knowledge is essential for correctly instantiating the client and making the most of the library's features.

Installation

To install the package, run the following command:

npm i @lunarislab/web3tokens

Example Setup

import { Web3Tokens } from '@lunarislab/web3tokens';
import { createPublicClient, createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia } from "viem/chains";

// Create an account using a private key
const account = privateKeyToAccount(process.env.PRIVATE_KEY);

// Configuration for initializing clients
const config = {
    chain: sepolia,
    transport: http(),
    account
};

// Initialize the library
const tokens = new Web3Tokens({
    public: createPublicClient(config),
    wallet: createWalletClient(config)
});

// Ready to use!

ERC20

ERC20 is the most commonly used token standard. Below are examples of how to interact with ERC20 tokens.

Getting a Token Contract Instance

// Retrieve the token contract instance
const token = tokens.erc20().get('0x...');

// Add extensions if needed
const token = tokens.erc20()
    .setMintable()
    .setOwnable()
    .get('0x...');

// fetch contract metadata (name, symbol and decimals)
await token.fetch();

console.log(token)

Calling Contract Methods

Reading from the Contract

const balance = await token.balanceOf(account.address);

Writing to the Contract

// Simulate a transaction
const transaction = await token.transfer({
    to: "0x...",
    value: 50000
}).simulate();

// Execute a transaction
const transaction = await token.transfer({
    to: "0x...",
    value: 50000
}).execute();

// Retrieve the transaction hash
console.log(transaction.hash);

// Wait for the transaction receipt (4 confirmations by default)
const receipt = await transaction.waitForReceipt(4);

Batch Transactions

If using a smart client with a similar interface to Viem, you can batch functions. Here's an example using zerodev batch transactions:

const transaction1 = await token.transfer({
    to: "0x...",
    value: 50000
}).getTxData();

const transaction2 = await token.transfer({
    to: "0x...",
    value: 50000
}).getTxData();

// Send batch transactions using a smart account client
const txHash = await kernelClient.sendTransactions({
  transactions: [
    transaction1,
    transaction2
  ],
});

Listening to Contract Events

token.on('Transfer', (data) => {
    console.log(data);
    // Additional logic here
});

ERC721 and ERC1155

ERC721 and ERC1155 follow a similar approach to ERC20.

Example Setup

const erc721 = tokens.erc721().get('0x...');
const erc1155 = tokens.erc1155().get('0x...');

await erc721.fetch(); // fetch name and symbol
console.log(erc721)

you can also add extensions to the tokens:

// add ERC721 extensions if needed
const erc721Burnable = tokens.erc721().setBurnable().get('0x...');

// add ERC1155 extensions if needed
const erc1155Access = tokens.erc1155().setAccessControl().get('0x...');

Contribution and Community

Web3Tokens is an open-source project, and all contributions are welcome. Whether you’re a seasoned developer or just starting, your ideas and code improvements are valuable. Feel free to open issues or pull requests on our GitHub repository.

Documentation

For more detailed information, please refer to the Web3Tokens Documentation.