Skip to main content

b402 payments on BSC

Project description

b402

Gasless crypto payments on BSC. One line of code.

Install

pip install b402

Fastest Way to Get Started

# 1. Set your private key
export PRIVATE_KEY="0x..."

# 2. Run Python
python3
# 3. One line to pay!
from b402 import pay

result = pay(amount="0.01", token="USD1", recipient="0x...")
# Done! ✨

That's it!

  • First payment: Auto-approves $10k cap + sends payment (~5 sec)
  • Future payments: Just pays instantly (<3 sec, gasless!)

What Just Happened?

  1. Checked if USD1 is approved for B402 relayer
  2. Approved $10,000 worth (one-time, costs ~$0.10 gas)
  3. Sent your payment (gasless - relayer pays!)
  4. Next 1000+ payments will be instant (no approval needed)

Defaults to BSC mainnet. Set network="testnet" for testnet.

Manual Control (Optional)

from b402 import B402

b402 = B402()

# Check approval status
approved, allowance = b402.check_approval("USD1")

# Manual setup if you prefer
if not approved:
    b402.setup("USD1")

# Send payment
result = b402.pay(amount="0.01", token="USD1", recipient="0x...")

How It Works

First Payment:

  1. Checks if token has sufficient allowance for this payment
  2. If not, automatically approves up to $10,000 worth (costs ~$0.10 gas, one-time)
  3. Sends payment (gasless!)

Subsequent Payments:

  1. Uses existing allowance ✅
  2. Sends payment immediately (gasless!)
  3. Re-approves automatically if allowance runs low

All payments are gasless for the user - the relayer pays all gas fees.

Security

Smart Approval Limits:

  • Never approves infinite amounts
  • Default cap: $10,000 worth of tokens
  • Checks allowance before each payment
  • Only approves when needed

Before (unsafe):

# Many SDKs approve: 2^256-1 (infinite!)
approve(relayer, 115792089237316195423570985008687907853269984665640564039457584007913129639935)

B402 (safe):

# Approves: $10,000 worth (reasonable limit)
approve(relayer, 10000000000000000000000)  # 10,000 * 10^18

Examples

Simplest (auto-approval):

from b402 import pay

result = pay("0.01", "USD1", "0x...")
# First time: approves + pays
# After that: just pays (gasless)

Disable auto-approval:

result = pay("0.01", "USD1", "0x...", auto_approve=False)
# Will return error if not approved, prompting manual setup

Manual control:

from b402 import B402

b402 = B402()
b402.setup("USD1")  # Explicit one-time setup
result = b402.pay("0.01", "USD1", "0x...")

Run the complete demo: python examples/complete_demo.py

Supported Tokens

Mainnet: USD1, USDT, USDC Testnet: USDT

30-Second Tutorial

# Install
# pip install b402

# Set key
import os
os.environ["PRIVATE_KEY"] = "0x..."

# Pay (that's it!)
from b402 import pay
result = pay("0.01", "USD1", "0x...")

# Check result
print(result.tx_hash if result.success else result.error)

Copy, paste, done. 🚀

API Reference

pay() - Simplest

from b402 import pay

result = pay(
    amount="0.01",           # Amount to send
    token="USD1",            # USD1, USDT, or USDC
    recipient="0x...",       # Recipient address
    network="mainnet",       # Optional: "mainnet" or "testnet"
    auto_approve=True,       # Optional: Auto-approve if needed
    debug=False              # Optional: Show debug logs
)

B402() - Advanced

Create B402 client for manual control. Defaults to mainnet.

b402 = B402()  # mainnet
b402_test = B402(network="testnet")

b402.setup(token)

One-time setup: approve relayer to spend tokens.

result = b402.setup("USD1")
# Returns: {"approved": True, "allowance": int, "tx_hash": "0x..."}

b402.check_approval(token)

Check if token is approved.

approved, allowance = b402.check_approval("USD1")

b402.pay(amount, token, recipient)

Send gasless payment.

result = b402.pay(amount="0.01", token="USD1", recipient="0x...")
# Returns: PaymentResult(success, tx_hash, error, payer, recipient, amount, token)

pay() - One-liner

Factory function for simple usage (after setup).

from b402 import pay

result = pay(amount="0.01", token="USD1", recipient="0x...")

Environment Variables

  • PRIVATE_KEY - Required. Your wallet private key (with 0x prefix)

Examples

Network Details

BSC Mainnet (Chain ID: 56)

  • Relayer: 0xE1C2830d5DDd6B49E9c46EbE03a98Cb44CD8eA5a
  • USD1: 0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d
  • USDT: 0x55d398326f99059fF775485246999027B3197955
  • USDC: 0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d

BSC Testnet (Chain ID: 97)

  • Relayer: 0x62150F2c3A29fDA8bCf22c0F22Eb17270FCBb78A
  • USDT: 0x337610d27c682E347C9cD60BD4b3b107C9d34dDd

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

b402-1.2.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

b402-1.2.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file b402-1.2.0.tar.gz.

File metadata

  • Download URL: b402-1.2.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for b402-1.2.0.tar.gz
Algorithm Hash digest
SHA256 c35ffc811803e6c45b56df7fea64765f5cbe7f71a4fb85fbf9607dc849846beb
MD5 1d8ccb4992f7e5cf93bc313667cc0880
BLAKE2b-256 d661fb17b327be0a6513c778a199eb0ecb33ca1b9ecfff33738673c4acabbb32

See more details on using hashes here.

File details

Details for the file b402-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: b402-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for b402-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9effc02bbc48f8cac7ebd52ac10edfeb34e39c45762ed3f66db56bb1b919f064
MD5 ebfe8ffd38e10a1797e5fbfafc6c3eb8
BLAKE2b-256 6fbae8cd64968a2dc0d0b6ea832eb55b44a5e2c3afc6eb7ad8fabac7c3b2903e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page