Simplify function to display price with decimal point

This commit is contained in:
Adw8 2025-01-28 19:56:41 +05:30
parent 5fd10ae15f
commit 771656ae5d
5 changed files with 12 additions and 13 deletions

View File

@ -22,6 +22,7 @@
"typeorm": "^0.3.12" "typeorm": "^0.3.12"
}, },
"devDependencies": { "devDependencies": {
"@types/bn.js": "^5.1.6",
"@types/node": "^20", "@types/node": "^20",
"@types/react": "^18", "@types/react": "^18",
"@types/react-dom": "^18", "@types/react-dom": "^18",

View File

@ -49,12 +49,12 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
) )
} }
const MTMFor1USDC = await getUSDCToMTMQuote(); const mtmFor1USDC = await getUSDCToMTMQuote();
const scale = new BN(100); const scale = new BN(100);
const scaledCost = new BN(model.cost * 100); const scaledCost = new BN(model.cost * 100);
const expectedAmount = scaledCost.mul(MTMFor1USDC).div(scale); const expectedAmount = scaledCost.mul(mtmFor1USDC).div(scale);
const isPaymentVerified = await verifyPayment(transactionSignature, expectedAmount) const isPaymentVerified = await verifyPayment(transactionSignature, expectedAmount)
if (!isPaymentVerified) { if (!isPaymentVerified) {

View File

@ -17,13 +17,13 @@ const Page: React.FC = (): React.ReactElement => {
type: null type: null
}) })
const [MTMFor1USDC, setMTMFor1USDC] = useState<BN>(new BN(0)); const [mtmFor1USDC, setMtmFor1USDC] = useState<BN>(new BN(0));
useEffect(() => { useEffect(() => {
const fetchPrice = async () => { const fetchPrice = async () => {
try { try {
const price = await getUSDCToMTMQuote(); const price = await getUSDCToMTMQuote();
setMTMFor1USDC(price); setMtmFor1USDC(price);
} catch (error) { } catch (error) {
console.error('Failed to fetch price:', error); console.error('Failed to fetch price:', error);
} }
@ -121,7 +121,7 @@ const Page: React.FC = (): React.ReactElement => {
{FLUX_MODELS.map((model) => { {FLUX_MODELS.map((model) => {
// Convert cost from number to BN // Convert cost from number to BN
const costBN = new BN(model.cost * 100); const costBN = new BN(model.cost * 100);
const mtmPriceBN = costBN.mul(MTMFor1USDC).div(new BN(100)); const mtmPriceBN = costBN.mul(mtmFor1USDC).div(new BN(100));
return ( return (
<AIServiceCard <AIServiceCard

View File

@ -19,10 +19,12 @@ interface GenerationState {
} }
const formatBNWithDecimals = (value: BN, decimals: number): string => { const formatBNWithDecimals = (value: BN, decimals: number): string => {
const quotient = value.div(new BN(10).pow(new BN(decimals))); const factor = new BN(10).pow(new BN(decimals));
const remainder = value.mod(new BN(10).pow(new BN(decimals))); // Part before decimal point
const decimalPart = remainder.mul(new BN(10).pow(new BN(6))).div(new BN(10).pow(new BN(decimals))); const quotient = value.div(factor);
return `${quotient.toString()}.${decimalPart.toString().padStart(6, '0')}`; // Part after decimal point
const remainder = value.mod(factor);
return `${quotient.toString()}.${remainder.toString().padStart(decimals, '0')}`;
} }
const AIServiceCard: React.FC<AIServiceCardProps> = ({ const AIServiceCard: React.FC<AIServiceCardProps> = ({

View File

@ -72,10 +72,6 @@ export async function verifyPayment(
const { info } = parsed; const { info } = parsed;
const { amount } = info; const { amount } = info;
console.log('Parsed transfer instruction:', parsed);
console.log('Transfer info:', info);
console.log('Transfer amount:', amount);
const transactionAmount = new BN(amount); const transactionAmount = new BN(amount);
// Transaction amount should be within 10% of the expected amount // Transaction amount should be within 10% of the expected amount