Skip to content

Commit

Permalink
chore(voucher): use settlement amount to get voucher price (#4546)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhart1o1 authored Jul 22, 2024
1 parent 50d45fa commit 1a49cdb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apps/voucher/app/create/client-side-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 38 additions & 5 deletions apps/voucher/graphql/resolvers/mutation/create-withdraw-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
39 changes: 26 additions & 13 deletions apps/voucher/lib/amount-calculator.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
18 changes: 18 additions & 0 deletions apps/voucher/services/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WithdrawLink>
}): Promise<WithdrawLink | Error> {
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")
}
}

0 comments on commit 1a49cdb

Please sign in to comment.