-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete_demo.py
More file actions
56 lines (43 loc) · 1.42 KB
/
Copy pathcomplete_demo.py
File metadata and controls
56 lines (43 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Complete end-to-end B402 demo: Setup + Payment
This script shows the full workflow:
1. Check if approval is needed
2. Approve if needed (one-time)
3. Send payment (gasless)
"""
from b402 import B402
# Initialize B402 client
b402 = B402()
print("=== B402 Payment Demo ===\n")
# Step 1: Check approval
print("Step 1: Checking approval...")
approved, allowance = b402.check_approval("USD1")
if approved:
print(f"✅ Already approved! Allowance: {allowance / 10**18:.2f} USD1\n")
else:
print("❌ Not approved yet\n")
# Step 2: Setup (approve relayer)
print("Step 2: Approving relayer (one-time setup)...")
print("This will cost a small amount of BNB for gas...")
result = b402.setup("USD1", auto_approve=True)
if result["tx_hash"]:
print(f"✅ Approved!")
print(f"TX: https://bscscan.com/tx/{result['tx_hash']}\n")
else:
print("✅ Already approved\n")
# Step 3: Send payment (gasless!)
print("Step 3: Sending payment (gasless)...")
payment = b402.pay(
amount="0.01",
token="USD1",
recipient="0x96625c0e209b4e0d4741d8a3ffb6b91c0da6fa5f"
)
if payment.success:
print(f"✅ Payment successful!")
print(f"Paid: {payment.amount} {payment.token}")
print(f"From: {payment.payer}")
print(f"To: {payment.recipient}")
print(f"TX: https://bscscan.com/tx/{payment.tx_hash}")
else:
print(f"❌ Payment failed: {payment.error}")
print("\n=== Demo Complete ===")