gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] 07/10: Introduce debit check helper.


From: gnunet
Subject: [libeufin] 07/10: Introduce debit check helper.
Date: Sat, 07 Jan 2023 13:49:06 +0100

This is an automated email from the git hooks/post-receive script.

ms pushed a commit to branch master
in repository libeufin.

commit f23911edb73eddbbedca29d03196c95a10e419ed
Author: MS <ms@taler.net>
AuthorDate: Sat Jan 7 13:40:55 2023 +0100

    Introduce debit check helper.
---
 .../kotlin/tech/libeufin/sandbox/bankAccount.kt    | 51 +++++++++++++---------
 1 file changed, 30 insertions(+), 21 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
index eacae3c3..50963695 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
@@ -6,6 +6,32 @@ import org.jetbrains.exposed.sql.transactions.transaction
 import tech.libeufin.util.*
 import java.math.BigDecimal
 
+/**
+ * Check whether the given bank account would surpass the
+ * debit threshold, in case the potential amount gets transferred.
+ * Returns true when the debit WOULD be surpassed.  */
+fun maybeDebit(
+    accountLabel: String,
+    requestedAmount: BigDecimal,
+    demobankName: String = "default"
+): Boolean {
+    val demobank = getDemobank(demobankName) ?: throw notFound(
+        "Demobank '${demobankName}' not found when trying to check the debit 
threshold" +
+                " for user $accountLabel"
+    )
+    val balance = getBalance(accountLabel, withPending = true)
+    val maxDebt = if (accountLabel == "admin") {
+        demobank.bankDebtLimit
+    } else demobank.usersDebtLimit
+    val balanceCheck = balance - requestedAmount
+    if (balanceCheck < BigDecimal.ZERO && balanceCheck.abs() > 
BigDecimal.valueOf(maxDebt.toLong())) {
+        logger.warn("User '$accountLabel' would surpass the debit" +
+                " threshold of $maxDebt, given the requested amount of 
${requestedAmount.toPlainString()}")
+        return true
+    }
+    return false
+}
+
 /**
  * The last balance is the one mentioned in the bank account's
  * last statement.  If the bank account does not have any statement
@@ -54,7 +80,7 @@ fun getBalance(
 }
 
 // Wrapper offering to get bank accounts from a string.
-fun getBalance(accountLabel: String, withPending: Boolean = false): BigDecimal 
{
+fun getBalance(accountLabel: String, withPending: Boolean = true): BigDecimal {
     val defaultDemobank = getDefaultDemobank()
     val account = getBankAccountFromLabel(accountLabel, defaultDemobank)
     return getBalance(account, withPending)
@@ -90,16 +116,8 @@ fun wireTransfer(
         pmtInfId
     )
 }
-/**
- * Book a CRDT and a DBIT transaction and return the unique reference thereof.
- *
- * At the moment there is redundancy because all the creditor / debtor details
- * are contained (directly or indirectly) already in the BankAccount 
parameters.
- *
- * This is kept both not to break the existing tests and to allow future 
versions
- * where one party of the transaction is not a customer of the running Sandbox.
- */
 
+// Book a CRDT and a DBIT transaction and return the unique reference thereof.
 fun wireTransfer(
     debitAccount: BankAccountEntity,
     creditAccount: BankAccountEntity,
@@ -118,17 +136,8 @@ fun wireTransfer(
                     "  Only ${demobank.currency} allowed."
         )
     // Check funds are sufficient.
-    /**
-     * Using 'pending' balance because Libeufin never books.  The
-     * reason is that booking is not Taler-relevant.
-     */
-    val pendingBalance = getBalance(debitAccount, withPending = true)
-    val maxDebt = if (debitAccount.label == "admin") {
-        demobank.bankDebtLimit
-    } else demobank.usersDebtLimit
-    val balanceCheck = pendingBalance - amountAsNumber
-    if (balanceCheck < BigDecimal.ZERO && balanceCheck.abs() > 
BigDecimal.valueOf(maxDebt.toLong())) {
-        logger.info("Account ${debitAccount.label} would surpass debit 
threshold of $maxDebt.  Rollback wire transfer")
+    if (maybeDebit(debitAccount.label, amountAsNumber)) {
+        logger.error("Account ${debitAccount.label} would surpass debit 
threshold.  Rollback wire transfer")
         throw SandboxError(HttpStatusCode.PreconditionFailed, "Insufficient 
funds")
     }
     val timeStamp = getUTCnow().toInstant().toEpochMilli()

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]