|
| 1 | +/** |
| 2 | + * Self-test: Send USDC on Base Sepolia to our own settlement endpoint |
| 3 | + * and verify the full x402 → payment → attestation → settle flow. |
| 4 | + */ |
| 5 | +import { createPublicClient, createWalletClient, http, parseUnits, formatUnits, encodeFunctionData, parseAbi } from 'viem'; |
| 6 | +import { privateKeyToAccount } from 'viem/accounts'; |
| 7 | +import { baseSepolia } from 'viem/chains'; |
| 8 | +import * as dotenv from 'dotenv'; |
| 9 | + |
| 10 | +dotenv.config(); |
| 11 | + |
| 12 | +const ENDPOINT = 'https://dexter-settlement-test-production.up.railway.app'; |
| 13 | +const PRIVATE_KEY = process.env.PRIVATE_KEY as `0x${string}`; |
| 14 | +const USDC = '0x036CbD53842c5426634e7929541eC2318f3dCF7e' as const; |
| 15 | + |
| 16 | +const ERC20_ABI = parseAbi([ |
| 17 | + 'function transfer(address to, uint256 amount) returns (bool)', |
| 18 | + 'function balanceOf(address) view returns (uint256)', |
| 19 | +]); |
| 20 | + |
| 21 | +async function main() { |
| 22 | + const account = privateKeyToAccount(PRIVATE_KEY); |
| 23 | + const publicClient = createPublicClient({ chain: baseSepolia, transport: http('https://sepolia.base.org') }); |
| 24 | + const walletClient = createWalletClient({ account, chain: baseSepolia, transport: http('https://sepolia.base.org') }); |
| 25 | + |
| 26 | + console.log(`\n🧪 Self-Test: Full x402 Settlement Flow`); |
| 27 | + console.log(`Wallet: ${account.address}`); |
| 28 | + |
| 29 | + // Check balance |
| 30 | + const balance = await publicClient.readContract({ address: USDC, abi: ERC20_ABI, functionName: 'balanceOf', args: [account.address] }); |
| 31 | + console.log(`USDC Balance: ${formatUnits(balance, 6)}`); |
| 32 | + |
| 33 | + if (balance < parseUnits('1', 6)) { |
| 34 | + console.error('❌ Insufficient USDC balance for test. Need at least 1 USDC.'); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + |
| 38 | + // Step 1: Request settlement (get 402) |
| 39 | + console.log(`\n📤 Step 1: POST /settle (expect 402)...`); |
| 40 | + const step1 = await fetch(`${ENDPOINT}/settle`, { |
| 41 | + method: 'POST', |
| 42 | + headers: { 'Content-Type': 'application/json' }, |
| 43 | + body: JSON.stringify({ amount: '0.10', from: account.address }), |
| 44 | + }); |
| 45 | + const payment = await step1.json(); |
| 46 | + console.log(` Status: ${step1.status}`); |
| 47 | + console.log(` Settlement ID: ${payment.settlementId}`); |
| 48 | + console.log(` Pay ${payment.paymentRequired.amount} USDC to ${payment.paymentRequired.recipient}`); |
| 49 | + |
| 50 | + if (step1.status !== 402) { |
| 51 | + console.error('❌ Expected 402, got', step1.status); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | + |
| 55 | + // Step 2: Send USDC on-chain |
| 56 | + console.log(`\n💸 Step 2: Sending 0.10 USDC on Base Sepolia...`); |
| 57 | + const recipient = payment.paymentRequired.recipient as `0x${string}`; |
| 58 | + |
| 59 | + // Since we're sending to ourselves (same wallet), use a minimal amount |
| 60 | + // In a real test with separate wallets, this would be a cross-wallet transfer |
| 61 | + const txHash = await walletClient.writeContract({ |
| 62 | + address: USDC, |
| 63 | + abi: ERC20_ABI, |
| 64 | + functionName: 'transfer', |
| 65 | + args: [recipient, parseUnits('0.10', 6)], |
| 66 | + }); |
| 67 | + console.log(` Tx: ${txHash}`); |
| 68 | + |
| 69 | + // Wait for confirmation |
| 70 | + console.log(` Waiting for confirmation...`); |
| 71 | + const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash }); |
| 72 | + console.log(` Block: ${receipt.blockNumber} | Status: ${receipt.status}`); |
| 73 | + |
| 74 | + // Step 3: Complete settlement with payment proof |
| 75 | + console.log(`\n✅ Step 3: POST /settle with paymentTxHash...`); |
| 76 | + const step3 = await fetch(`${ENDPOINT}/settle`, { |
| 77 | + method: 'POST', |
| 78 | + headers: { 'Content-Type': 'application/json' }, |
| 79 | + body: JSON.stringify({ |
| 80 | + amount: '0.10', |
| 81 | + from: account.address, |
| 82 | + paymentTxHash: txHash, |
| 83 | + }), |
| 84 | + }); |
| 85 | + const settlement = await step3.json(); |
| 86 | + console.log(` Status: ${step3.status}`); |
| 87 | + console.log(` Settlement:`, JSON.stringify(settlement, null, 2)); |
| 88 | + |
| 89 | + // Step 4: Verify attestation |
| 90 | + console.log(`\n🔍 Step 4: GET /attestation/${txHash}...`); |
| 91 | + const step4 = await fetch(`${ENDPOINT}/attestation/${txHash}`); |
| 92 | + const attestation = await step4.json(); |
| 93 | + console.log(` Verified: ${attestation.verified}`); |
| 94 | + console.log(` Transfers: ${attestation.transferCount}`); |
| 95 | + |
| 96 | + // Summary |
| 97 | + console.log(`\n${'━'.repeat(50)}`); |
| 98 | + if (step3.status === 200 && settlement.status === 'settled') { |
| 99 | + console.log(`✅ SELF-TEST PASSED — Full x402 settlement flow verified`); |
| 100 | + console.log(` Amount: ${settlement.amount} USDC`); |
| 101 | + console.log(` Fee: ${settlement.fee} USDC (0.77%)`); |
| 102 | + console.log(` Net: ${settlement.netAmount} USDC`); |
| 103 | + console.log(` Attestation: ${settlement.attestationVerified ? 'VERIFIED' : 'FAILED'}`); |
| 104 | + } else { |
| 105 | + console.log(`❌ SELF-TEST FAILED`); |
| 106 | + console.log(` Response:`, settlement); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +main().catch(console.error); |
0 commit comments