From 7fcde7b30d6944a85d56b654cff86567fcbe2e96 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 | 4 +++- src/components/PaymentModal.tsx | 36 ++++++--------------------------- src/config/index.ts | 2 +- src/services/registry.ts | 27 ++++++++++++++++++++++++- src/types/index.ts | 8 ++++++++ 6 files changed, 44 insertions(+), 34 deletions(-) diff --git a/.env.example b/.env.example index ce73351..d6677a0 100644 --- a/.env.example +++ b/.env.example @@ -8,7 +8,6 @@ NEXT_PUBLIC_SOLANA_TOKEN_MINT_ADDRESS=71Jvq4Epe2FCJ7JFSF7jLXdNk1Wy4Bhqd9iL6bEFEL # Multisig address NEXT_PUBLIC_SOLANA_TOKEN_RECIPIENT_ADDRESS=FFDx3SdAEeXrp6BTmStB4BDHpctGsaasZq4FFcowRobY NEXT_PUBLIC_SOLANA_TOKEN_SYMBOL=GOR -NEXT_PUBLIC_SOLANA_PAYMENT_AMOUNT_USD=5 # Payment amount in USD # UI Configuration NEXT_PUBLIC_EXAMPLE_URL=https://git.vdb.to/cerc-io/test-progressive-web-app diff --git a/src/app/api/registry/route.ts b/src/app/api/registry/route.ts index 561e41d..ac5743d 100644 --- a/src/app/api/registry/route.ts +++ b/src/app/api/registry/route.ts @@ -11,6 +11,7 @@ import { verifyUnusedSolanaPayment } from '@/utils/solana-verify'; import { transferLNTTokens } from '@/services/laconic-transfer'; import { getRegistry, getRegistryConfig } from '@/config'; import { getRequiredTokenInfo } from '@/services/jupiter-price'; +import { resolvePricingRecordLrn } from '@/services/registry'; assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required'); const SOLANA_RPC_URL = process.env.NEXT_PUBLIC_SOLANA_RPC_URL; @@ -182,7 +183,8 @@ export async function POST(request: NextRequest) { // Verify Solana payment console.log('Step 0: Verifying Solana token payment...'); - 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!; // Calculate expected token amount based on current price diff --git a/src/components/PaymentModal.tsx b/src/components/PaymentModal.tsx index cb39092..e86c2f0 100644 --- a/src/components/PaymentModal.tsx +++ b/src/components/PaymentModal.tsx @@ -9,29 +9,16 @@ import { Connection } from '@solana/web3.js'; import { sendSolanaTokenPayment } from '@/services/solana'; import { getRequiredTokenInfo } from '@/services/jupiter-price'; import { PaymentModalProps } from '@/types'; -import { getRegistry } from '@/config'; +import { resolvePricingRecordLrn } from '@/services/registry'; 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 SOLANA_RPC_URL = process.env.NEXT_PUBLIC_SOLANA_RPC_URL; -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, @@ -49,27 +36,16 @@ export default function PaymentModal({ const connection = useMemo(() => new Connection(SOLANA_RPC_URL), []); 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 3206ce9..bc7c2e3 100644 --- a/src/services/registry.ts +++ b/src/services/registry.ts @@ -1,4 +1,14 @@ -import { CreateRecordResponse } from '../types'; +import assert from 'assert'; + +import { getRegistry } from '@/config'; +import { CreateRecordResponse, PricingRecordAttributes } 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, @@ -49,3 +59,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 7bc2645..72615d6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -72,3 +72,11 @@ export interface LaconicTransferResult { transactionHash?: string; error?: string; } + +export interface PricingRecordAttributes { + amount: string; + currency: string; + for: string; + type: string; + version: string; +}