From 1a49cdb57d7c77a1542a1cff35c7e66c22c6a324 Mon Sep 17 00:00:00 2001 From: Siddharth Date: Mon, 22 Jul 2024 17:58:05 +0530 Subject: [PATCH] chore(voucher): use settlement amount to get voucher price (#4546) --- apps/voucher/app/create/client-side-page.tsx | 2 +- .../mutation/create-withdraw-link.ts | 43 ++++++++++++++++--- apps/voucher/lib/amount-calculator.ts | 39 +++++++++++------ apps/voucher/services/db/index.ts | 18 ++++++++ 4 files changed, 83 insertions(+), 19 deletions(-) diff --git a/apps/voucher/app/create/client-side-page.tsx b/apps/voucher/app/create/client-side-page.tsx index 30c521a6af..3aa1859c60 100644 --- a/apps/voucher/app/create/client-side-page.tsx +++ b/apps/voucher/app/create/client-side-page.tsx @@ -70,7 +70,7 @@ export default function CreatePage({ platformFeesInPpm }: Props) { }, []) const voucherAmountInCents = - amountCalculator.voucherAmountAfterPlatformFeesAndCommission({ + amountCalculator.voucherAmountAfterPlatformFeesAndCommission.fromPrice({ voucherPrice: currencyConversion?.currencyConversionEstimation.usdCentAmount, commissionPercentage: Number(commissionPercentage), platformFeesInPpm, diff --git a/apps/voucher/graphql/resolvers/mutation/create-withdraw-link.ts b/apps/voucher/graphql/resolvers/mutation/create-withdraw-link.ts index e852526c10..32bca3b1af 100644 --- a/apps/voucher/graphql/resolvers/mutation/create-withdraw-link.ts +++ b/apps/voucher/graphql/resolvers/mutation/create-withdraw-link.ts @@ -6,7 +6,11 @@ import { getWalletDetails, getWalletDetailsFromWalletId, } from "@/utils/helpers" -import { createWithdrawLinkMutation, updateWithdrawLinkStatus } from "@/services/db" +import { + createWithdrawLinkMutation, + updateWithdrawLink, + updateWithdrawLinkStatus, +} from "@/services/db" import { authOptions } from "@/app/api/auth/[...nextauth]/auth" import { PaymentSendResult, Status, WalletCurrency } from "@/lib/graphql/generated" @@ -18,6 +22,7 @@ import { escrowApolloClient } from "@/services/galoy/client/escrow" import { amountCalculator } from "@/lib/amount-calculator" import { env } from "@/env" +import { convertCurrency } from "@/lib/utils" export const createWithdrawLink = async ( _: undefined, @@ -53,8 +58,8 @@ export const createWithdrawLink = async ( // amount that would be sent to user const voucherAmountAfterPlatformFeesAndCommission = Number( - amountCalculator - .voucherAmountAfterPlatformFeesAndCommission({ + amountCalculator.voucherAmountAfterPlatformFeesAndCommission + .fromPrice({ voucherPrice: salesAmountInCents, commissionPercentage, platformFeesInPpm, @@ -265,10 +270,38 @@ export const handleBtcWalletPayment = async ({ if (btcPaymentResponse.intraLedgerPaymentSend.errors.length > 0) return new Error(btcPaymentResponse.intraLedgerPaymentSend.errors[0].message) + // TODO handle case if settlementDisplayCurrency is changed for some reason + if ( + !btcPaymentResponse.intraLedgerPaymentSend.transaction?.settlementDisplayAmount || + btcPaymentResponse.intraLedgerPaymentSend.transaction.settlementDisplayCurrency !== + "USD" + ) { + console.error("error while verifying Settlement Amount and Settlement Currency") + return new Error("Something went wrong, please contact support if error persists") + } + + const amountPaidToEscrowInCents = convertCurrency.usdToCents({ + usd: Math.abs( + btcPaymentResponse.intraLedgerPaymentSend.transaction?.settlementDisplayAmount, + ), + }) + + const voucherAmountInCents = Number( + amountCalculator.voucherAmountAfterPlatformFeesAndCommission + .fromCommission({ + platformFeesInPpm: env.PLATFORM_FEES_IN_PPM, + voucherAmountAfterCommission: amountPaidToEscrowInCents, + }) + .toFixed(0), + ) + if (btcPaymentResponse.intraLedgerPaymentSend.status === PaymentSendResult.Success) { - const response = await updateWithdrawLinkStatus({ + const response = await updateWithdrawLink({ id: createWithdrawLinkResponse.id, - status: Status.Active, + updates: { + status: Status.Active, + voucherAmountInCents: voucherAmountInCents, + }, }) return response } diff --git a/apps/voucher/lib/amount-calculator.ts b/apps/voucher/lib/amount-calculator.ts index e54d698227..a390667f9a 100644 --- a/apps/voucher/lib/amount-calculator.ts +++ b/apps/voucher/lib/amount-calculator.ts @@ -1,17 +1,30 @@ export const amountCalculator = { - voucherAmountAfterPlatformFeesAndCommission({ - voucherPrice, - commissionPercentage, - platformFeesInPpm, - }: { - voucherPrice: number - commissionPercentage: number - platformFeesInPpm: number - }): number { - const commissionAmount = voucherPrice * (commissionPercentage / 100) - const platformFees = voucherPrice * (platformFeesInPpm / 1000000) - const result = voucherPrice - commissionAmount - platformFees - return Math.max(result, 0) + voucherAmountAfterPlatformFeesAndCommission: { + fromPrice: ({ + voucherPrice, + commissionPercentage, + platformFeesInPpm, + }: { + voucherPrice: number + commissionPercentage: number + platformFeesInPpm: number + }) => { + const commissionAmount = voucherPrice * (commissionPercentage / 100) + const platformFees = voucherPrice * (platformFeesInPpm / 1000000) + const result = voucherPrice - commissionAmount - platformFees + return Math.max(result, 0) + }, + fromCommission: ({ + voucherAmountAfterCommission, + platformFeesInPpm, + }: { + voucherAmountAfterCommission: number + platformFeesInPpm: number + }) => { + const platformFees = voucherAmountAfterCommission * (platformFeesInPpm / 1000000) + const result = voucherAmountAfterCommission - platformFees + return Math.max(result, 0) + }, }, voucherAmountAfterCommission({ voucherPrice, diff --git a/apps/voucher/services/db/index.ts b/apps/voucher/services/db/index.ts index 567b92f366..efeaa8833e 100644 --- a/apps/voucher/services/db/index.ts +++ b/apps/voucher/services/db/index.ts @@ -165,3 +165,21 @@ export async function updateWithdrawLinkStatus({ : new Error("Failed to update withdraw link status") } } + +export async function updateWithdrawLink({ + id, + updates, +}: { + id: string + updates: Partial +}): Promise { + try { + const [updatedWithdrawLink] = await knex("WithdrawLinks") + .where({ id }) + .update(updates) + .returning("*") + return updatedWithdrawLink + } catch (error) { + return error instanceof Error ? error : new Error("Failed to update withdraw link") + } +}