Skip to main content

Documentation Index

Fetch the complete documentation index at: https://seilabs-docs-evm-reference-and-sei-js-examples.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

ERC-20 Interaction

Standard ERC-20 contracts work on Sei without modification. This page covers the common read and write operations using viem and ethers.

Setup

import { createPublicClient, createWalletClient, http, parseAbi } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { sei } from '@sei-js/precompiles/viem';

const client = createPublicClient({ chain: sei, transport: http() });
const account = privateKeyToAccount('0xYourPrivateKey');
const walletClient = createWalletClient({ account, chain: sei, transport: http() });

const ERC20_ABI = parseAbi([
  'function name() view returns (string)',
  'function symbol() view returns (string)',
  'function decimals() view returns (uint8)',
  'function totalSupply() view returns (uint256)',
  'function balanceOf(address owner) view returns (uint256)',
  'function allowance(address owner, address spender) view returns (uint256)',
  'function transfer(address to, uint256 amount) returns (bool)',
  'function approve(address spender, uint256 amount) returns (bool)',
  'function transferFrom(address from, address to, uint256 amount) returns (bool)',
  'event Transfer(address indexed from, address indexed to, uint256 value)',
  'event Approval(address indexed owner, address indexed spender, uint256 value)',
]);

const TOKEN = '0xTokenAddress';

Reading Token Metadata

const [name, symbol, decimals, totalSupply] = await Promise.all([
  client.readContract({ address: TOKEN, abi: ERC20_ABI, functionName: 'name' }),
  client.readContract({ address: TOKEN, abi: ERC20_ABI, functionName: 'symbol' }),
  client.readContract({ address: TOKEN, abi: ERC20_ABI, functionName: 'decimals' }),
  client.readContract({ address: TOKEN, abi: ERC20_ABI, functionName: 'totalSupply' }),
]);

Reading Balances and Allowances

const balance = await client.readContract({
  address: TOKEN,
  abi: ERC20_ABI,
  functionName: 'balanceOf',
  args: ['0xOwnerAddress'],
});

const allowance = await client.readContract({
  address: TOKEN,
  abi: ERC20_ABI,
  functionName: 'allowance',
  args: ['0xOwnerAddress', '0xSpenderAddress'],
});

Transferring Tokens

import { parseUnits } from 'viem';

const hash = await walletClient.writeContract({
  address: TOKEN,
  abi: ERC20_ABI,
  functionName: 'transfer',
  args: ['0xRecipient', parseUnits('10', 18)],
});

const receipt = await client.waitForTransactionReceipt({ hash });

Approving a Spender

import { parseUnits, maxUint256 } from 'viem';

// Approve a specific amount
const hash = await walletClient.writeContract({
  address: TOKEN,
  abi: ERC20_ABI,
  functionName: 'approve',
  args: ['0xSpenderAddress', parseUnits('100', 18)],
});

// Or approve max (unlimited)
const hashMax = await walletClient.writeContract({
  address: TOKEN,
  abi: ERC20_ABI,
  functionName: 'approve',
  args: ['0xSpenderAddress', maxUint256],
});

Watching Transfer Events

const unwatch = client.watchContractEvent({
  address: TOKEN,
  abi: ERC20_ABI,
  eventName: 'Transfer',
  onLogs: (logs) => {
    logs.forEach(({ args }) => {
      console.log(`${args.from}${args.to}: ${args.value}`);
    });
  },
});

// Stop watching
unwatch();

Fetching Historical Transfers

const logs = await client.getContractEvents({
  address: TOKEN,
  abi: ERC20_ABI,
  eventName: 'Transfer',
  fromBlock: 0n,
  toBlock: 'latest',
});

CosmWasm Token Compatibility

CW20 tokens on Sei have ERC-20 pointer contracts that expose the standard ERC-20 interface. You can use all of the patterns above against a CW20 pointer address. See Pointer Contracts for how to look up the pointer address.