@vgabriel45/erc20-utils
v1.0.1
Published
A TypeScript utility library for ERC20 token operations including transfer encoding and function selectors
Maintainers
Readme
ERC20 Utils
A TypeScript utility library for ERC20 token operations.
Installation
npm install @vgabriel45/erc20-utils
# or
yarn add @vgabriel45/erc20-utils
# or
pnpm add @vgabriel45/erc20-utilsUsage
Basic Import
import ERC20Utils from '@vgabriel45/erc20-utils';
// or
import { ERC20Utils } from '@vgabriel45/erc20-utils';Initialize
const erc20Utils = new ERC20Utils();Get Transfer Function Selector
const transferSelector = await erc20Utils.transferSelector();
console.log(transferSelector);
// Returns the ABI function selector for ERC20 transferEncode Transfer Function Call
import { Address } from 'viem';
const recipient: Address = '0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6';
const amount = BigInt(1000000000000000000); // 1 ETH in wei
const encodedTransfer = await erc20Utils.transferData(recipient, amount);
console.log(encodedTransfer);
// Returns: 0xa9059cbb000000000000000000000000742d35cc6634c0532925a3b8d4c9db96c4b4d8b6000000000000000000000000000000000000000000000000000de0b6b3a7640000Complete Example
import ERC20Utils from '@vgabriel45/erc20-utils';
import { Address } from 'viem';
async function transferERC20() {
const erc20Utils = new ERC20Utils();
// Get the transfer function selector
const selector = await erc20Utils.transferSelector();
// Encode a transfer call
const recipient: Address = '0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6';
const amount = BigInt(1000000000000000000); // 1 ETH in wei
const encodedData = await erc20Utils.transferData(recipient, amount);
console.log('Transfer selector:', selector);
console.log('Encoded transfer data:', encodedData);
return encodedData;
}
transferERC20();API Reference
ERC20Utils
The main class for ERC20 utility operations.
Constructor
new ERC20Utils()Creates a new instance of ERC20Utils.
Methods
transferSelector(): Promise<AbiFunction>
Returns the ABI function selector for the ERC20 transfer function.
Returns: Promise - The transfer function selector
transferData(to: Address, value: bigint): Promise<0x${string}>
Encodes an ERC20 transfer function call.
Parameters:
to(Address): The recipient addressvalue(bigint): The amount to transfer in wei
Returns: Promise<0x${string}> - The encoded transfer data
Dependencies
This library uses:
Development
Prerequisites
- Node.js 18+
- pnpm (recommended) or npm
Setup
# Clone the repository
git clone <repository-url>
cd erc20-utils
# Install dependencies
pnpm install
# Build the library
pnpm build
# Run tests
pnpm test
# Run tests in watch mode
pnpm test:uiScripts
pnpm build- Build the librarypnpm dev- Build in watch modepnpm test- Run testspnpm test:ui- Run tests with UIpnpm test:run- Run tests oncepnpm test:coverage- Run tests with coveragepnpm clean- Clean build artifacts
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
