Skip to content

Commit

Permalink
Revert "chore(voucher): use settlement amount to get voucher price (#…
Browse files Browse the repository at this point in the history
…4546)"

This reverts commit 1a49cdb.
  • Loading branch information
siddhart1o1 committed Jul 27, 2024
1 parent 23befab commit 50418cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 83 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.fromPrice({
amountCalculator.voucherAmountAfterPlatformFeesAndCommission({
voucherPrice: currencyConversion?.currencyConversionEstimation.usdCentAmount,
commissionPercentage: Number(commissionPercentage),
platformFeesInPpm,
Expand Down
43 changes: 5 additions & 38 deletions apps/voucher/graphql/resolvers/mutation/create-withdraw-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import {
getWalletDetails,
getWalletDetailsFromWalletId,
} from "@/utils/helpers"
import {
createWithdrawLinkMutation,
updateWithdrawLink,
updateWithdrawLinkStatus,
} from "@/services/db"
import { createWithdrawLinkMutation, updateWithdrawLinkStatus } from "@/services/db"

import { authOptions } from "@/app/api/auth/[...nextauth]/auth"
import { PaymentSendResult, Status, WalletCurrency } from "@/lib/graphql/generated"
Expand All @@ -22,7 +18,6 @@ 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 @@ -58,8 +53,8 @@ export const createWithdrawLink = async (

// amount that would be sent to user
const voucherAmountAfterPlatformFeesAndCommission = Number(
amountCalculator.voucherAmountAfterPlatformFeesAndCommission
.fromPrice({
amountCalculator
.voucherAmountAfterPlatformFeesAndCommission({
voucherPrice: salesAmountInCents,
commissionPercentage,
platformFeesInPpm,
Expand Down Expand Up @@ -270,38 +265,10 @@ 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 updateWithdrawLink({
const response = await updateWithdrawLinkStatus({
id: createWithdrawLinkResponse.id,
updates: {
status: Status.Active,
voucherAmountInCents: voucherAmountInCents,
},
status: Status.Active,
})
return response
}
Expand Down
39 changes: 13 additions & 26 deletions apps/voucher/lib/amount-calculator.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
export const amountCalculator = {
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)
},
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)
},
voucherAmountAfterCommission({
voucherPrice,
Expand Down
18 changes: 0 additions & 18 deletions apps/voucher/services/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,3 @@ 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 50418cb

Please sign in to comment.