From 3b570dbeb91184883fa69c4838d8ae4cba685ab4 Mon Sep 17 00:00:00 2001 From: Shreerang Kale Date: Tue, 22 Jul 2025 18:15:40 +0530 Subject: [PATCH] Refactor method to resolve pricing record lrn --- .env.example | 1 - src/app/api/registry/route.ts | 12 +++++------ src/components/PaymentModal.tsx | 35 ++++++--------------------------- src/config/index.ts | 2 +- src/services/registry.ts | 28 ++++++++++++++++++++++++-- src/types/index.ts | 8 ++++++++ 6 files changed, 46 insertions(+), 40 deletions(-) diff --git a/.env.example b/.env.example index 51a6106..df4e429 100644 --- a/.env.example +++ b/.env.example @@ -5,7 +5,6 @@ NEXT_PUBLIC_SOLANA_RPC_URL=https://skilled-prettiest-seed.solana-mainnet.quiknode.pro/eeecfebd04e345f69f1900cc3483cbbfea02a158 NEXT_PUBLIC_SOLANA_TOKEN_MINT_ADDRESS=71Jvq4Epe2FCJ7JFSF7jLXdNk1Wy4Bhqd9iL6bEFELvg NEXT_PUBLIC_SOLANA_TOKEN_SYMBOL=GOR -NEXT_PUBLIC_SOLANA_PAYMENT_AMOUNT_USD=5 # Gorbagana Chain Configuration NEXT_PUBLIC_GORBAGANA_RPC_URL=https://rpc.gorbagana.wtf diff --git a/src/app/api/registry/route.ts b/src/app/api/registry/route.ts index 1f14775..a65c5b8 100644 --- a/src/app/api/registry/route.ts +++ b/src/app/api/registry/route.ts @@ -13,6 +13,7 @@ import { getRegistry, getRegistryConfig } from '@/config'; import { getRequiredTokenInfo, RequiredTokenInfo } from '@/services/jupiter-price'; import { IS_NAT_GOR_TRANSFER_ENABLED, SOLANA_GOR_MINT_ADDRESS } from '@/constants/payments'; import { PaymentMethod } from '@/types'; +import { resolvePricingRecordLrn } from '@/services/registry'; assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required'); assert(!IS_NAT_GOR_TRANSFER_ENABLED || process.env.NEXT_PUBLIC_GORBAGANA_RPC_URL, 'GORBAGANA_RPC_URL is required when NAT GOR transfer is enabled'); @@ -215,14 +216,11 @@ export async function POST(request: NextRequest) { }, { status: 400 }); } - // Verify Solana payment based on method - console.log(`Step 0: Verifying Solana ${paymentMethod} payment...`); - - - // Calculate expected token amount based on current price + // Verify Solana payment + console.log('Step 0: Verifying Solana token payment...'); let requiredTokenInfo: RequiredTokenInfo; - - const targetUsdAmount = parseFloat(process.env.NEXT_PUBLIC_SOLANA_PAYMENT_AMOUNT_USD!); + const pricingRecordAttributes = await resolvePricingRecordLrn(); + const targetUsdAmount = parseInt(pricingRecordAttributes.amount, 10); const mintAddress = process.env.NEXT_PUBLIC_SOLANA_TOKEN_MINT_ADDRESS!; try { diff --git a/src/components/PaymentModal.tsx b/src/components/PaymentModal.tsx index cb00475..47be3e8 100644 --- a/src/components/PaymentModal.tsx +++ b/src/components/PaymentModal.tsx @@ -6,12 +6,12 @@ import assert from 'assert'; import { Connection } from '@solana/web3.js'; import { useConnection, useWallet } from '@solana/wallet-adapter-react'; -import { getRegistry } from '@/config'; import { sendSolanaPayment } from '@/services/solana'; import { getRequiredTokenInfo, RequiredTokenInfo } from '@/services/jupiter-price'; import { PaymentMethod, PaymentModalProps, PaymentRequest } from '@/types'; import { IS_NAT_GOR_TRANSFER_ENABLED, PAYMENT_METHOD_LABELS, SOLANA_GOR_MINT_ADDRESS } from '@/constants/payments'; import { usePaymentMethod } from '@/contexts/PaymentMethodContext'; +import { resolvePricingRecordLrn } from '@/services/registry'; assert(!IS_NAT_GOR_TRANSFER_ENABLED || process.env.NEXT_PUBLIC_GORBAGANA_RPC_URL, 'GORBAGANA_RPC_URL is required when NAT GOR transfer is enabled'); @@ -20,23 +20,11 @@ const GORBAGANA_RPC_URL = process.env.NEXT_PUBLIC_GORBAGANA_RPC_URL; assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required'); assert(process.env.NEXT_PUBLIC_PRICING_RECORD_LRN, 'DEPLOYMENT_RECORD_LRN is required'); -const PRICING_RECORD_LRN = process.env.NEXT_PUBLIC_PRICING_RECORD_LRN; -const SUPPORTED_CURRENCY = "USD"; -const VALID_PRICING_RECORD_FOR = "webapp-deployment"; - interface DeploymentCostInfo { amount: string; currency: string; } -interface PricingRecordAttributes { - amount: string; - currency: string; - for: string; - type: string; - version: string; -} - export default function PaymentModal({ isOpen, onClose, @@ -55,27 +43,16 @@ export default function PaymentModal({ const [deploymentCostInfo, setDeploymentCostInfo] = useState(); useEffect(() => { - const registry = getRegistry(); - - const resolveDeploymentCostInfo = async () => { - const result = await registry.resolveNames([PRICING_RECORD_LRN!]) - const PricingRecordAttributes: PricingRecordAttributes = result[0].attributes; - - if (PricingRecordAttributes.for !== VALID_PRICING_RECORD_FOR) { - throw new Error(`Incorrect pricing record type: ${PricingRecordAttributes.type}. Please provide correct pricing record lrn`) - } - - if (PricingRecordAttributes.currency !== SUPPORTED_CURRENCY) { - throw new Error(`Unsupported currency found in pricing record: ${PricingRecordAttributes.currency}`) - } + const getDeploymentCostInfo = async () => { + const pricingRecordAttributes = await resolvePricingRecordLrn(); setDeploymentCostInfo({ - amount: PricingRecordAttributes.amount, - currency:PricingRecordAttributes.currency + amount: pricingRecordAttributes.amount, + currency:pricingRecordAttributes.currency }) } - resolveDeploymentCostInfo(); + getDeploymentCostInfo(); }, []); // Get configuration from environment variables diff --git a/src/config/index.ts b/src/config/index.ts index 9afd1ea..0a9954b 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -25,7 +25,7 @@ export const getClientRegistryConfig = () => { rpcEndpoint: process.env.NEXT_PUBLIC_REGISTRY_RPC_ENDPOINT!, gqlEndpoint: process.env.NEXT_PUBLIC_REGISTRY_GQL_ENDPOINT!, fee: { - gasPrice: process.env.NEXT_PUBLIC_REGISTRY_GAS_PRICE || '0.001', + gasPrice: process.env.NEXT_PUBLIC_REGISTRY_GAS_PRICE!, }, }; }; diff --git a/src/services/registry.ts b/src/services/registry.ts index 7b65499..75a6b6a 100644 --- a/src/services/registry.ts +++ b/src/services/registry.ts @@ -1,5 +1,14 @@ -import { CreateRecordResponse } from '../types'; -import { PaymentMethod } from '../types'; +import assert from 'assert'; + +import { getRegistry } from '@/config'; +import { CreateRecordResponse, PricingRecordAttributes, PaymentMethod } from '../types'; + +assert(process.env.NEXT_PUBLIC_PRICING_RECORD_LRN, 'DEPLOYMENT_RECORD_LRN is required'); +const PRICING_RECORD_LRN = process.env.NEXT_PUBLIC_PRICING_RECORD_LRN; + +const SUPPORTED_CURRENCY = "USD"; +const VALID_PRICING_RECORD_FOR = "webapp-deployment"; + export const createApplicationDeploymentRequest = async ( url: string, @@ -51,3 +60,18 @@ export const createApplicationDeploymentRequest = async ( } }; +export const resolvePricingRecordLrn = async (): Promise => { + const registry = getRegistry(); + const result = await registry.resolveNames([PRICING_RECORD_LRN]) + const pricingRecordAttributes: PricingRecordAttributes = result[0].attributes; + + if (pricingRecordAttributes.for !== VALID_PRICING_RECORD_FOR) { + throw new Error(`Incorrect pricing record type: ${pricingRecordAttributes.type}. Please provide correct pricing record lrn`) + } + + if (pricingRecordAttributes.currency !== SUPPORTED_CURRENCY) { + throw new Error(`Unsupported currency found in pricing record: ${pricingRecordAttributes.currency}`) + } + + return pricingRecordAttributes; +} diff --git a/src/types/index.ts b/src/types/index.ts index 86203f5..9dab16e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -53,3 +53,11 @@ export interface LaconicTransferResult { transactionHash?: string; error?: string; } + +export interface PricingRecordAttributes { + amount: string; + currency: string; + for: string; + type: string; + version: string; +}