Refactor method to resolve pricing record lrn

This commit is contained in:
Shreerang Kale 2025-07-22 18:15:40 +05:30
parent 9633d49374
commit 3b570dbeb9
6 changed files with 46 additions and 40 deletions

View File

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

View File

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

View File

@ -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<DeploymentCostInfo>();
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,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<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

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