Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/features/sponsor-dashboard/utils/paymentRPCValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface ValidatePaymentParams {
expectedAmount: number;
tokenMint: Token;
tokenPriceUSD?: number;
allowPartialPayment?: boolean;
}

export interface ValidationResult {
Expand All @@ -32,7 +33,15 @@ export async function validatePayment({
expectedAmount,
tokenMint,
tokenPriceUSD,
allowPartialPayment = false,
}: ValidatePaymentParams): Promise<ValidationResult> {
if (expectedAmount <= 0) {
return {
isValid: false,
error: 'Expected payment amount must be greater than 0',
};
}

const rpc = getRpc();
const maxRetries = 3;
const delayMs = 5000;
Expand Down Expand Up @@ -129,7 +138,7 @@ export async function validatePayment({

const allowedDiff = getAllowedDifference(expectedAmount);
if (
expectedAmount > 0 &&
!allowPartialPayment &&
Math.abs(actualTransferAmount - expectedAmount) > allowedDiff
) {
return {
Expand All @@ -138,6 +147,13 @@ export async function validatePayment({
};
}

if (allowPartialPayment && actualTransferAmount <= 0) {
return {
isValid: false,
error: 'Transferred amount must be greater than 0',
};
}

return { isValid: true, actualAmount: actualTransferAmount };
}

Expand Down Expand Up @@ -179,7 +195,7 @@ export async function validatePayment({

const allowedDiff = getAllowedDifference(expectedAmount);
if (
expectedAmount > 0 &&
!allowPartialPayment &&
Math.abs(actualTransferAmount - expectedAmount) > allowedDiff
) {
return {
Expand All @@ -188,6 +204,13 @@ export async function validatePayment({
};
}

if (allowPartialPayment && actualTransferAmount <= 0) {
return {
isValid: false,
error: 'Transferred amount must be greater than 0',
};
}

return { isValid: true, actualAmount: actualTransferAmount };
} catch (error: any) {
return { isValid: false, error: error.message };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function handler(req: NextApiRequestWithSponsor, res: NextApiResponse) {
id: {
in: paymentLinks.map((d) => d.submissionId),
},
listingId,
isPaid: false,
},
include: {
Expand Down Expand Up @@ -178,17 +179,15 @@ async function handler(req: NextApiRequestWithSponsor, res: NextApiResponse) {
throw new Error('This submission is already fully paid');
}

let expectedAmount = winnerReward;
if (isProject) {
expectedAmount = 0;
}
const expectedAmount = isProject ? remainingAmount : winnerReward;

const rpcValidationResult: ValidationResult = await validatePayment({
txId: paymentLink.txId,
recipientPublicKey: walletAddress!,
expectedAmount,
tokenMint: dbToken,
tokenPriceUSD,
allowPartialPayment: isProject,
});

if (!rpcValidationResult.isValid) {
Expand Down