From d827d155c9fc7b4008bc8f8ed8b9c5158eb1f9b3 Mon Sep 17 00:00:00 2001 From: MS Date: Tue, 23 May 2023 12:03:42 +0200 Subject: Conversion service. Checking that conversion never produces negative amounts. --- sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt | 13 +++++++++++-- .../main/kotlin/tech/libeufin/sandbox/ConversionService.kt | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'sandbox/src/main/kotlin/tech/libeufin') diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt index 70642899..e44dc227 100644 --- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt +++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt @@ -166,10 +166,19 @@ fun applyCashoutRatioAndFee( ): BigDecimal { // Normal case, when the calculation starts from the regional amount. if (!fromCredit) { - return ((amount * ratiosAndFees.sell_at_ratio.toBigDecimal()) - + val maybeCashoutAmount = ((amount * ratiosAndFees.sell_at_ratio.toBigDecimal()) - ratiosAndFees.sell_out_fee.toBigDecimal()).roundToTwoDigits() + // throws 500, since bank should not allow to get negative fiat amounts. + if (maybeCashoutAmount < BigDecimal.ZERO) { + logger.error("Cash-out operation caused a negative fiat output." + + " Regional amount was '$amount', cash-out ratio is '${ratiosAndFees.sell_at_ratio}," + + " cash-out fee is '${ratiosAndFees.sell_out_fee}''" + ) + throw internalServerError("Applying cash-out fees yielded negative fiat amount.") + } + return maybeCashoutAmount } - // UI convenient case, when the calculation start from the + // UI convenient case, when the calculation starts from the // desired fiat amount that the user wants eventually be paid. return ((amount + ratiosAndFees.sell_out_fee.toBigDecimal()) / ratiosAndFees.sell_at_ratio.toBigDecimal()).roundToTwoDigits() diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/ConversionService.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/ConversionService.kt index f19718fa..a080dc80 100644 --- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/ConversionService.kt +++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/ConversionService.kt @@ -110,9 +110,18 @@ fun downloadLoop(block: () -> Unit) { private fun applyBuyinRatioAndFees( amount: BigDecimal, ratiosAndFees: RatioAndFees -): BigDecimal = - ((amount * ratiosAndFees.buy_at_ratio.toBigDecimal()) +): BigDecimal { + val maybeBuyinAmount = ((amount * ratiosAndFees.buy_at_ratio.toBigDecimal()) - ratiosAndFees.buy_in_fee.toBigDecimal()).roundToTwoDigits() + // Bank's fault, as buying in should never lead to negative. + if (maybeBuyinAmount < BigDecimal.ZERO) { + logger.error("Negative buy-in scenario: input fiat amount was '${amount}'" + + ", buy-in ratio was '${ratiosAndFees.buy_at_ratio}'," + + " buy-in fee was '${ratiosAndFees.buy_in_fee}'") + throw internalServerError("Applying buy-in fees yielded negative regional amount") + } + return maybeBuyinAmount +} private fun ensureDisabledRedirects(client: HttpClient) { client.config { -- cgit v1.2.3