Refactor method to resolve pricing record lrn

This commit is contained in:
Shreerang Kale 2025-07-22 18:15:40 +05:30
parent 12510b0371
commit 7fcde7b30d
6 changed files with 44 additions and 34 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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!,
},
};
};

View File

@ -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<PricingRecordAttributes> => {
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;
}

View File

@ -72,3 +72,11 @@ export interface LaconicTransferResult {
transactionHash?: string;
error?: string;
}
export interface PricingRecordAttributes {
amount: string;
currency: string;
for: string;
type: string;
version: string;
}