|
1 | 1 | import { serve } from '@hono/node-server'; |
2 | 2 | import { readFileSync } from 'fs'; |
3 | 3 | import { createSolanaRpc } from '@solana/kit'; |
| 4 | +import { HTTPFacilitatorClient } from '@x402/core/http'; |
4 | 5 | import { createApp } from './app.js'; |
5 | 6 | import { TreasuryService } from './services/treasury.js'; |
6 | 7 | import { X402Service } from './services/x402.js'; |
| 8 | +import { PayoutService } from './services/payout.js'; |
7 | 9 | import { DepositDetector } from './services/deposit.js'; |
8 | 10 | import { config } from './config.js'; |
9 | 11 |
|
10 | 12 | async function main() { |
| 13 | + // Devnet treasury (SOL) |
11 | 14 | const keypairJson = readFileSync(config.treasuryKeypair, 'utf-8'); |
12 | 15 | const keypairBytes = new Uint8Array(JSON.parse(keypairJson)); |
13 | | - |
14 | 16 | const treasury = await TreasuryService.create({ |
15 | 17 | rpcUrl: config.devnetRpc, |
16 | 18 | wssUrl: config.devnetWss, |
17 | 19 | keypairBytes, |
18 | 20 | }); |
19 | 21 |
|
| 22 | + // x402 facilitator (real) |
| 23 | + const facilitator = new HTTPFacilitatorClient({ url: config.facilitatorUrl }); |
20 | 24 | const x402 = new X402Service({ |
21 | | - facilitator: { |
22 | | - verify: async () => ({ isValid: true }), |
23 | | - settle: async () => ({ success: true, transaction: '', network: config.svmNetwork as `${string}:${string}` }), |
24 | | - getSupported: async () => ({ kinds: [], extensions: [], signers: {} }), |
25 | | - }, |
| 25 | + facilitator, |
26 | 26 | payTo: treasury.address, |
27 | 27 | network: config.svmNetwork, |
28 | 28 | }); |
29 | | - console.warn('WARNING: x402 facilitator is STUBBED — wire HTTPFacilitatorClient for production'); |
30 | 29 |
|
31 | | - const { app, db } = createApp({ treasury, x402 }); |
| 30 | + // Mainnet payout (USDC) — optional, enables sell flow |
| 31 | + let payout: PayoutService | undefined; |
| 32 | + if (config.mainnetKeypair) { |
| 33 | + const mainnetJson = readFileSync(config.mainnetKeypair, 'utf-8'); |
| 34 | + const mainnetBytes = new Uint8Array(JSON.parse(mainnetJson)); |
| 35 | + payout = await PayoutService.create({ |
| 36 | + rpcUrl: config.mainnetRpc, |
| 37 | + wssUrl: config.mainnetWss, |
| 38 | + keypairBytes: mainnetBytes, |
| 39 | + maxPayoutUsdc: config.maxPayoutUsdc, |
| 40 | + minReserveUsdc: config.minReserveUsdc, |
| 41 | + }); |
| 42 | + console.log(`Payout wallet: ${payout.walletAddress}`); |
| 43 | + } else { |
| 44 | + console.warn('WARNING: No mainnet keypair configured — sell payouts disabled'); |
| 45 | + } |
| 46 | + |
| 47 | + const { app, db } = createApp({ treasury, x402, payout }); |
32 | 48 |
|
| 49 | + // Deposit detector with payout callback |
33 | 50 | const devnetRpc = createSolanaRpc(config.devnetRpc); |
34 | 51 | const depositDetector = new DepositDetector({ |
35 | 52 | db, |
36 | 53 | rpc: devnetRpc as any, |
37 | 54 | treasuryAddress: treasury.address, |
38 | | - onDeposit: async (tx, sig) => { |
39 | | - console.log(`Deposit confirmed for sell ${tx.id}: ${sig}`); |
40 | | - // TODO: trigger USDC payout on mainnet |
| 55 | + onDeposit: async (tx, devnetSig) => { |
| 56 | + console.log(`Deposit confirmed for sell ${tx.id}: ${devnetSig}`); |
| 57 | + if (!payout) { |
| 58 | + console.warn(`No payout service — sell ${tx.id} completed without USDC payout`); |
| 59 | + return; |
| 60 | + } |
| 61 | + try { |
| 62 | + const mainnetSig = await payout.sendUsdc(tx.wallet, tx.usdc_amount); |
| 63 | + db.update(tx.id, { mainnet_payout_tx: mainnetSig }); |
| 64 | + console.log(`USDC payout sent for sell ${tx.id}: ${mainnetSig}`); |
| 65 | + } catch (err) { |
| 66 | + console.error(`USDC payout failed for sell ${tx.id}:`, err); |
| 67 | + // Refund devnet SOL |
| 68 | + try { |
| 69 | + const refundSig = await treasury.sendSol(tx.wallet, tx.sol_amount); |
| 70 | + db.update(tx.id, { status: 'refunded', devnet_tx: refundSig }); |
| 71 | + console.log(`Refunded ${tx.sol_amount} SOL to ${tx.wallet}: ${refundSig}`); |
| 72 | + } catch (refundErr) { |
| 73 | + console.error(`CRITICAL: Refund also failed for sell ${tx.id}:`, refundErr); |
| 74 | + db.update(tx.id, { status: 'failed' }); |
| 75 | + } |
| 76 | + } |
41 | 77 | }, |
42 | 78 | }); |
43 | 79 | depositDetector.start(); |
44 | 80 |
|
| 81 | + // Graceful shutdown |
45 | 82 | const shutdown = () => { |
46 | 83 | depositDetector.stop(); |
47 | 84 | db.close(); |
|
0 commit comments