From a1dae0199f3bc3e9f66fc1375c652c6f99b26b2c Mon Sep 17 00:00:00 2001
From: Özgür Kesim <oec-taler@kesim.org>
Date: Fri, 28 Jul 2023 19:01:57 +0200
Subject: FAILING attempt to make echange.reserve use taler_amount

Fails with

> TALER_PREFIX=/usr/local ./test-exchangedb-postgres
2023-07-28T19:02:25.150845+0200 /home/oec/projects/taler/exchange/src/exchangedb/.libs/test-exchangedb-postgres-2559159 WARNING Could not run PSQL on file /usr/local/share/taler//sql/exchange/drop.sql: psql exit code was 3
2023-07-28T19:02:32.488085+0200 pq-2559159 ERROR Query `call_withdraw' failed with result: invalid input syntax for type bigint:
"(1,1000)"/(null)/ERROR:  invalid input syntax for type bigint: "(1,1000)"
CONTEXT:  PL/pgSQL function exchange_do_withdraw(bytea,taler_amount,bytea,bytea,bytea,bytea,bytea,bigint,bigint,boolean) line 23 at SQL statement
/PGRES_FATAL_ERROR/ERROR:  invalid input syntax for type bigint: "(1,1000)"
CONTEXT:  PL/pgSQL function exchange_do_withdraw(bytea,taler_amount,bytea,bytea,bytea,bytea,bytea,bigint,bigint,boolean) line 23 at SQL statement
2023-07-28T19:02:32.488146+0200 /home/oec/projects/taler/exchange/src/exchangedb/.libs/test-exchangedb-postgres-2559159 ERROR Assertion failed at test_exchangedb.c:1428.
---
 src/exchangedb/exchange_do_batch_withdraw.sql | 37 ++++++++++++---------------
 1 file changed, 17 insertions(+), 20 deletions(-)

(limited to 'src/exchangedb/exchange_do_batch_withdraw.sql')

diff --git a/src/exchangedb/exchange_do_batch_withdraw.sql b/src/exchangedb/exchange_do_batch_withdraw.sql
index 53e90844..ba364dda 100644
--- a/src/exchangedb/exchange_do_batch_withdraw.sql
+++ b/src/exchangedb/exchange_do_batch_withdraw.sql
@@ -17,8 +17,7 @@
 -- @author Özgür Kesim
 
 CREATE OR REPLACE FUNCTION exchange_do_batch_withdraw(
-  IN amount_val INT8,
-  IN amount_frac INT4,
+  IN amount taler_amount,
   IN rpub BYTEA,
   IN now INT8,
   IN min_reserve_gc INT8,
@@ -32,8 +31,7 @@ LANGUAGE plpgsql
 AS $$
 DECLARE
   reserve_gc INT8;
-  reserve_val INT8;
-  reserve_frac INT4;
+  reserve taler_amount;
   reserve_birthday INT4;
   not_before date;
 BEGIN
@@ -45,14 +43,14 @@ BEGIN
 
 
 SELECT
-   current_balance_val
-  ,current_balance_frac
+   current_balance.val
+  ,current_balance.frac
   ,gc_date
   ,birthday
   ,reserve_uuid
  INTO
-   reserve_val
-  ,reserve_frac
+   reserve.val
+  ,reserve.frac
   ,reserve_gc
   ,reserve_birthday
   ,ruuid
@@ -92,21 +90,21 @@ ELSE
 END IF;
 
 -- Check reserve balance is sufficient.
-IF (reserve_val > amount_val)
+IF (reserve.val > amount.val)
 THEN
-  IF (reserve_frac >= amount_frac)
+  IF (reserve.frac >= amount.frac)
   THEN
-    reserve_val=reserve_val - amount_val;
-    reserve_frac=reserve_frac - amount_frac;
+    reserve.val=reserve.val - amount.val;
+    reserve.frac=reserve.frac - amount.frac;
   ELSE
-    reserve_val=reserve_val - amount_val - 1;
-    reserve_frac=reserve_frac + 100000000 - amount_frac;
+    reserve.val=reserve.val - amount.val - 1;
+    reserve.frac=reserve.frac + 100000000 - amount.frac;
   END IF;
 ELSE
-  IF (reserve_val = amount_val) AND (reserve_frac >= amount_frac)
+  IF (reserve.val = amount.val) AND (reserve.frac >= amount.frac)
   THEN
-    reserve_val=0;
-    reserve_frac=reserve_frac - amount_frac;
+    reserve.val=0;
+    reserve.frac=reserve.frac - amount.frac;
   ELSE
     balance_ok=FALSE;
     RETURN;
@@ -119,8 +117,7 @@ min_reserve_gc=GREATEST(min_reserve_gc,reserve_gc);
 -- Update reserve balance.
 UPDATE reserves SET
   gc_date=min_reserve_gc
- ,current_balance_val=reserve_val
- ,current_balance_frac=reserve_frac
+ ,current_balance=reserve
 WHERE
   reserves.reserve_pub=rpub;
 
@@ -129,6 +126,6 @@ balance_ok=TRUE;
 
 END $$;
 
-COMMENT ON FUNCTION exchange_do_batch_withdraw(INT8, INT4, BYTEA, INT8, INT8, BOOLEAN)
+COMMENT ON FUNCTION exchange_do_batch_withdraw(taler_amount, BYTEA, INT8, INT8, BOOLEAN)
   IS 'Checks whether the reserve has sufficient balance for a withdraw operation (or the request is repeated and was previously approved) and that age requirements are formally met. If so updates the database with the result. Excludes storing the planchets.';
 
-- 
cgit v1.2.3

