b402-sdk
v1.0.1
Published
Accept crypto payments with zero gas fees on BSC - USD1, USDT, USDC
Maintainers
Readme
b402-sdk
Accept crypto payments on Binance Smart Chain with zero gas fees for users.
Installation
npm install b402-sdk ethers@6Quick Start
import { B402 } from 'b402-sdk';
import { Wallet } from 'ethers';
// User's wallet
const wallet = new Wallet(process.env.PRIVATE_KEY!);
// Create B402 client
const b402 = new B402('mainnet');
// Send payment (user pays ZERO gas!)
const result = await b402.pay(wallet, {
amount: '0.01', // Amount in human-readable format
token: 'USD1', // USD1, USDT, or USDC
recipient: '0x...' // Seller's wallet address
});
if (result.success) {
console.log('Payment sent!', result.txHash);
// View on BSCScan: https://bscscan.com/tx/${result.txHash}
}How It Works
- User signs payment authorization with MetaMask (EIP-712) - FREE
- B402 facilitator validates signature
- Facilitator submits transaction on-chain and pays gas (~$0.003)
- Payment settles in <3 seconds
User pays ZERO gas fees.
Supported Tokens
Mainnet (BSC)
USD1- World Liberty Financial stablecoinUSDT- Tether USDUSDC- USD Coin
Testnet (BSC Testnet)
USDT- Testnet USDT
Before First Payment
Users need to approve the B402 relayer contract once:
import { Contract } from 'ethers';
const usd1 = new Contract(
'0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d', // USD1 on BSC
['function approve(address spender, uint256 amount)'],
wallet
);
await usd1.approve(
'0xE1C2830d5DDd6B49E9c46EbE03a98Cb44CD8eA5a', // B402 Relayer
'115792089237316195423570985008687907853269984665640564039457584007913129639935' // Max approval
);After this one-time approval, all future payments are instant and gasless!
API Reference
new B402(network?, facilitatorUrl?)
Create B402 client.
network:'mainnet'or'testnet'(default:'mainnet')facilitatorUrl: Custom facilitator URL (optional)
b402.pay(wallet, options)
Send payment.
Parameters:
wallet: ethers.js Wallet objectoptions.amount: Amount as string (e.g.,'0.01')options.token:'USD1'|'USDT'|'USDC'options.recipient: Seller's wallet addressoptions.timeoutSeconds: Payment validity period (default: 3600)
Returns:
{
success: boolean;
txHash?: string;
error?: string;
payer: string;
recipient: string;
amount: string;
token: string;
}Example: Complete Payment Flow
import { B402 } from 'b402-sdk';
import { Wallet, Contract, JsonRpcProvider } from 'ethers';
async function sendPayment() {
// Setup
const provider = new JsonRpcProvider('https://bsc-dataseed1.binance.org');
const wallet = new Wallet(process.env.PRIVATE_KEY!, provider);
// Check USD1 balance
const usd1 = new Contract(
'0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d',
['function balanceOf(address) view returns (uint256)',
'function allowance(address,address) view returns (uint256)',
'function approve(address,uint256) returns (bool)'],
wallet
);
const balance = await usd1.balanceOf(wallet.address);
console.log('Balance:', balance.toString());
// Check/approve relayer
const allowance = await usd1.allowance(
wallet.address,
'0xE1C2830d5DDd6B49E9c46EbE03a98Cb44CD8eA5a'
);
if (allowance === 0n) {
console.log('Approving relayer...');
const tx = await usd1.approve(
'0xE1C2830d5DDd6B49E9c46EbE03a98Cb44CD8eA5a',
'115792089237316195423570985008687907853269984665640564039457584007913129639935'
);
await tx.wait();
console.log('Approved!');
}
// Send payment
const b402 = new B402('mainnet');
const result = await b402.pay(wallet, {
amount: '0.01',
token: 'USD1',
recipient: '0x96625c0e209b4e0d4741d8a3ffb6b91c0da6fa5f'
});
if (result.success) {
console.log('Payment successful!');
console.log('TX:', result.txHash);
console.log('View on BSCScan: https://bscscan.com/tx/' + result.txHash);
} else {
console.error('Payment failed:', result.error);
}
}
sendPayment();Live Example
Check out this real transaction on BSC mainnet: https://bscscan.com/tx/0x665845a92fd09e4673dac65ae23725cd58d2350d0347d2fca921bbe4a96eab30
- User paid: $0.00 gas
- Facilitator paid: $0.003 gas
- Payment: 0.01 USD1 transferred instantly
Security
- EIP-712 signatures - Industry standard (Uniswap, OpenSea)
- Nonce system - Prevents replay attacks
- Time-bounded - Authorizations expire
- On-chain verification - Contract verifies signatures before transfer
Network Details
Mainnet
- Chain ID: 56
- RPC: https://bsc-dataseed1.binance.org
- B402 Relayer:
0xE1C2830d5DDd6B49E9c46EbE03a98Cb44CD8eA5a - USD1:
0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d - USDT:
0x55d398326f99059fF775485246999027B3197955 - USDC:
0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d
Testnet
- Chain ID: 97
- RPC: https://data-seed-prebsc-1-s1.binance.org:8545
- B402 Relayer:
0x62150F2c3A29fDA8bCf22c0F22Eb17270FCBb78A - USDT:
0x337610d27c682E347C9cD60BD4b3b107C9d34dDd
License
MIT
Links
- Homepage: https://b402.ai
- GitHub: https://github.com/vistara-labs/b402-protocol
- NPM: https://npmjs.com/package/b402-sdk
