用于在移动端(iOS/Android)和 Web 环境中操作 N42 链上 ERC-20 代币的 TypeScript SDK。
- ✅ 完整的 ERC-20 操作(查询、转账、授权)
- ✅ EIP-1559 手续费支持
- ✅ WebSocket 事件订阅
- ✅ 指数退避重试策略
- ✅ 异步安全(async/await)
- ✅ TypeScript 类型支持
- ✅ 错误处理与重试机制
npm install @n42/erc20-sdk或使用 yarn:
yarn add @n42/erc20-sdkimport { N42SDK, N42Config } from '@n42/erc20-sdk';
const config: N42Config = {
rpcUrl: 'https://rpc.n42.world',
wsUrl: 'wss://ws.n42.world',
chainId: 2042,
timeoutMs: 12000,
retry: {
type: 'exponential',
baseMs: 300,
maxMs: 3000,
},
};
const sdk = await N42SDK.initialize(config);import { Wallet, MnemonicStrength } from '@n42/erc20-sdk';
// 创建新钱包
const wallet = Wallet.create(MnemonicStrength.WORDS_12);
// 或导入现有钱包
const wallet = Wallet.import('your mnemonic phrase here');
// 获取地址
console.log(wallet.address);import { TokenAmountUtil } from '@n42/erc20-sdk';
const erc20 = sdk.erc20('0xYourTokenAddress');
// 查询基础信息
const symbol = await erc20.symbol();
const decimals = await erc20.decimals();
const name = await erc20.name();
const totalSupply = await erc20.totalSupply();
// 查询余额
const balance = await erc20.balanceOf(wallet.address);
// 转换为可读格式
const readable = TokenAmountUtil.toDecimalString(
TokenAmountUtil.fromMinUnits(balance, decimals)
);
console.log(`${readable} ${symbol}`);import { ERC20Transfer, TxOptions, Gwei } from '@n42/erc20-sdk';
// 准备转账
const amount = TokenAmountUtil.fromDecimal('12.5', decimals);
const transfer: ERC20Transfer = {
to: '0xReceiverAddress',
amount: amount.minUnits,
};
// 可选:配置手续费
const options: TxOptions = {
maxFeePerGas: Gwei.toWei(2.0),
maxPriorityFeePerGas: Gwei.toWei(1.0),
nonce: 'auto', // 或手动指定数字
gasLimit: 'auto', // 或手动指定 bigint
};
// 发送转账
const txHash = await erc20.transfer(transfer, wallet, options);
// 等待确认
const receipt = await sdk.waitForReceipt(txHash, 120);
console.log('Transaction confirmed:', receipt);import { ERC20Approve } from '@n42/erc20-sdk';
const approve: ERC20Approve = {
spender: '0xSpenderAddress',
amount: TokenAmountUtil.fromDecimal('1000', decimals).minUnits,
};
const txHash = await erc20.approve(approve, wallet);
const receipt = await sdk.waitForReceipt(txHash);
// 查询授权额度
const allowance = await erc20.allowance(wallet.address, '0xSpenderAddress');// 订阅 Transfer 事件
const subscription = erc20.events.transfer(
null, // from (null = 所有地址)
wallet.address, // to (只监听发送到此地址的转账)
(event) => {
console.log(`Received ${event.value} from ${event.from}`);
}
);
// 取消订阅
subscription.cancel();
// 订阅 Approval 事件
const approvalSub = erc20.events.approval(
wallet.address,
null,
(event) => {
console.log(`Approval: ${event.value} to ${event.spender}`);
}
);初始化 SDK 实例。
获取 ERC-20 代币实例。
等待交易确认。
name(): Promise<string>symbol(): Promise<string>decimals(): Promise<number>totalSupply(): Promise<bigint>balanceOf(address: string): Promise<bigint>allowance(owner: string, spender: string): Promise<bigint>
transfer(transfer: ERC20Transfer, from: Wallet, options?: TxOptions): Promise<string>approve(approve: ERC20Approve, from: Wallet, options?: TxOptions): Promise<string>transferFrom(from: string, to: string, amount: bigint, wallet: Wallet, options?: TxOptions): Promise<string>
events.transfer(from: string | null, to: string | null, callback: EventCallback<TransferEvent>): EventSubscriptionevents.approval(owner: string | null, spender: string | null, callback: EventCallback<ApprovalEvent>): EventSubscription
创建新钱包。
从助记词导入钱包。
从私钥导入钱包。
从最小单位创建 TokenAmount。
从十进制字符串创建 TokenAmount。
转换为可读的十进制字符串。
SDK 提供了详细的错误类型:
import {
NetworkError,
InsufficientFundsError,
NonceError,
TransactionRevertedError,
} from '@n42/erc20-sdk';
try {
await erc20.transfer(transfer, wallet);
} catch (error) {
if (error instanceof InsufficientFundsError) {
console.error('余额不足');
} else if (error instanceof NetworkError) {
console.error('网络错误:', error.message);
} else if (error instanceof TransactionRevertedError) {
console.error('交易失败:', error.reason);
}
}const mainnetConfig: N42Config = {
rpcUrl: 'https://rpc.n42.world',
wsUrl: 'wss://ws.n42.world',
chainId: 2042,
};const testnetConfig: N42Config = {
rpcUrl: 'https://rpc.testnet.n42.world',
wsUrl: 'wss://ws.testnet.n42.world',
chainId: 204201,
};- 私钥管理:在生产环境中,私钥应存储在安全容器中(iOS Keychain / Android Keystore)
- 环境变量:使用环境变量管理 RPC URL 和链 ID
- 错误处理:始终处理可能的错误,特别是网络错误和交易失败
- 手续费控制:设置合理的 maxFeePerGas 上限以防止异常高额手续费
SDK 提供完整的 TypeScript 类型定义,支持智能提示和类型检查。
MIT
欢迎提交 Issue 和 Pull Request。