0x0x230x

Untitled

Aug 7th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import { Buffer } from 'buffer';
  2. import { paymentMiddleware } from 'x402-next';
  3. import { decodeXPaymentResponse } from 'x402-fetch';
  4. import { facilitator } from '@coinbase/x402';
  5.  
  6. const baseClient = createPublicClient({
  7. chain: base,
  8. transport: http(),
  9. });
  10.  
  11. const USDC_ADDRESS = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
  12.  
  13. const pathname = '/api/gigs/create/x402';
  14.  
  15. export async function POST(req: NextRequest) {
  16. const xPaymentHeader = req.headers.get('x-payment');
  17. const body = await req.json();
  18.  
  19.  
  20. const participants = await getTransactionParticipants(xPaymentHeader);
  21. const paymentResponse = await requestPayment(
  22. req,
  23. participants.gigWalletAddress,
  24. pathname,
  25. {
  26. address: validatedData.token_address as `0x${string}`,
  27. decimals: asset.decimals,
  28. amount: asset.tokenAmount!,
  29. },
  30. );
  31.  
  32. if (paymentResponse.status === 402) return paymentResponse;
  33.  
  34.  
  35. const gig = await createGig(validatedData, {
  36. address: participants.gigWalletAddress,
  37. id: participants.gigWalletId,
  38. });
  39.  
  40. await startGig((gig as { gig_id: number }).gig_id, participants.from);
  41.  
  42. return NextResponse.json({
  43. message: '✅ Access granted after payment',
  44. gig_id: (gig as { gig_id: number }).gig_id,
  45. });
  46. }
  47.  
  48.  
  49. async function requestPayment(
  50. req: NextRequest,
  51. gigWalletAddress: `0x${string}`,
  52. pathname: string,
  53. asset: {
  54. address: `0x${string}`;
  55. decimals: number;
  56. amount: number;
  57. },
  58. ) {
  59. const mw = paymentMiddleware(
  60. gigWalletAddress,
  61. {
  62. [pathname]: {
  63. price: {
  64. amount: parseUnits(
  65. asset.amount.toString(),
  66. asset.decimals,
  67. ).toString(),
  68. asset: {
  69. address: asset.address,
  70. decimals: asset.decimals,
  71. eip712: {
  72. name: 'USD Coin',
  73. version: '2',
  74. },
  75. },
  76. },
  77. network: 'base',
  78. config: {
  79. description: `Create gig on Gig.bot`,
  80. },
  81. },
  82. },
  83. facilitator,
  84. );
  85.  
  86. return await mw(req as any);
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment