diff options
author | MS <ms@taler.net> | 2023-05-23 12:03:42 +0200 |
---|---|---|
committer | MS <ms@taler.net> | 2023-05-23 12:03:42 +0200 |
commit | d827d155c9fc7b4008bc8f8ed8b9c5158eb1f9b3 (patch) | |
tree | 794d7f1bd6c84851e7403d7ed6e7ccd4825fc4cf /sandbox/src/main/kotlin/tech/libeufin | |
parent | 26d9e2a804f50d27d3e610b40fa77b0729a442e0 (diff) | |
download | libeufin-d827d155c9fc7b4008bc8f8ed8b9c5158eb1f9b3.tar.gz libeufin-d827d155c9fc7b4008bc8f8ed8b9c5158eb1f9b3.tar.bz2 libeufin-d827d155c9fc7b4008bc8f8ed8b9c5158eb1f9b3.zip |
Conversion service.
Checking that conversion never produces negative amounts.
Diffstat (limited to 'sandbox/src/main/kotlin/tech/libeufin')
-rw-r--r-- | sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt | 13 | ||||
-rw-r--r-- | sandbox/src/main/kotlin/tech/libeufin/sandbox/ConversionService.kt | 13 |
2 files changed, 22 insertions, 4 deletions
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 { |