Introduction
Welcome to the Web3Tokens library documentation! Web3Tokens is designed to simplify the interaction with Ethereum Virtual Machine (EVM) tokens, including ERC20, ERC721, and ERC1155 standards. This library aims to make it easier for new developers to engage with smart contracts, offering an accessible entry point into blockchain development.
Vision and Goals
Web3Tokens aims to:
- Simplify smart contract tokens interactions.
- Provide an accessible entry point for new blockchain developers.
- Offer a modular and flexible library for fine-grained control over token instances.
Target Audience
Web3Tokens is designed for:
- New Blockchain Developers: Developers new to blockchain who need an easy way to interact with tokens.
- Experienced Developers: Developers looking for a modular and time-saving library.
Core Features
Modularity
Web3Tokens allows you to build token instances with various extensions, providing more fine-grained control compared to many other libraries.
// Example of adding extensions
const token = tokens.erc20()
.setMintable()
.setOwnable()
.get('0x...');
Managed ABIs
The library manages ABIs internally, so you don't have to.
// Example of interacting with a token without worrying about the ABI
const balance = await token.balanceOf(account.address);
Prerequisites
Before using Web3Tokens, you should have a basic understanding of Viem to correctly instantiate the client.
Installation
Install the package via npm:
npm i @lunarislab/web3tokens
Getting Started
Here’s a quick example to get you started:
Initialize Viem and Web3Tokens:
import { Web3Tokens } from '@lunarislab/web3tokens';
import { createPublicClient, createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia } from "viem/chains";
const account = privateKeyToAccount(process.env.PRIVATE_KEY);
const config = { chain: sepolia, transport: http(), account };
const tokens = new Web3Tokens({
public: createPublicClient(config),
wallet: createWalletClient(config)
});
Interact with an ERC20 Token:
const token = tokens.erc20().get('0x...');
const balance = await token.balanceOf(account.address);
// Simulate a transfer
const transaction = await token.transfer({ to: "0x...", value: 50000 }).simulate();
// Execute a transfer
const executedTx = await token.transfer({ to: "0x...", value: 50000 }).execute();
console.log(executedTx.hash);
const receipt = await executedTx.waitForReceipt(4);
Listen to Events:
token.on('Transfer', (data) => {
console.log(data);
// Additional logic
});
Batch Transactions
Using a smart client, you can batch multiple transactions:
const tx1 = await token.transfer({ to: "0x...", value: 50000 }).getTxData();
const tx2 = await token.transfer({ to: "0x...", value: 50000 }).getTxData();
const txHash = await kernelClient.sendTransactions({
transactions: [tx1, tx2],
});
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.
License
Web3Tokens is fully open-source, allowing you to use, modify, and distribute the library freely.