summaryrefslogtreecommitdiff
path: root/src/exchangedb/exchange_do_recoup_to_reserve.sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/exchangedb/exchange_do_recoup_to_reserve.sql')
-rw-r--r--src/exchangedb/exchange_do_recoup_to_reserve.sql50
1 files changed, 30 insertions, 20 deletions
diff --git a/src/exchangedb/exchange_do_recoup_to_reserve.sql b/src/exchangedb/exchange_do_recoup_to_reserve.sql
index 39baba8fa..71c1d51b9 100644
--- a/src/exchangedb/exchange_do_recoup_to_reserve.sql
+++ b/src/exchangedb/exchange_do_recoup_to_reserve.sql
@@ -31,9 +31,10 @@ CREATE OR REPLACE FUNCTION exchange_do_recoup_to_reserve(
LANGUAGE plpgsql
AS $$
DECLARE
- tmp_val INT8; -- amount recouped
-DECLARE
- tmp_frac INT8; -- amount recouped
+ tmp taler_amount; -- amount recouped
+ balance taler_amount; -- current balance of the reserve
+ new_balance taler_amount; -- new balance of the reserve
+ reserve RECORD;
BEGIN
-- Shards: SELECT known_coins (by coin_pub)
-- SELECT recoup (by coin_pub)
@@ -49,8 +50,8 @@ SELECT
remaining_frac
,remaining_val
INTO
- tmp_frac
- ,tmp_val
+ tmp.frac
+ ,tmp.val
FROM exchange.known_coins
WHERE coin_pub=in_coin_pub;
@@ -61,7 +62,7 @@ THEN
RETURN;
END IF;
-IF tmp_val + tmp_frac = 0
+IF tmp.val + tmp.frac = 0
THEN
-- Check for idempotency
SELECT
@@ -83,22 +84,31 @@ UPDATE known_coins
,remaining_val=0
WHERE coin_pub=in_coin_pub;
+-- Get current balance
+SELECT *
+ INTO reserve
+ FROM reserves
+ WHERE reserve_pub=in_reserve_pub;
+
+balance = reserve.current_balance;
+new_balance.frac=balance.frac+tmp.frac
+ - CASE
+ WHEN balance.frac+tmp.frac >= 100000000
+ THEN 100000000
+ ELSE 0
+ END;
+
+new_balance.val=balance.val+tmp.val
+ + CASE
+ WHEN balance.frac+tmp.frac >= 100000000
+ THEN 1
+ ELSE 0
+ END;
-- Credit the reserve and update reserve timers.
UPDATE reserves
SET
- current_balance_frac=current_balance_frac+tmp_frac
- - CASE
- WHEN current_balance_frac+tmp_frac >= 100000000
- THEN 100000000
- ELSE 0
- END,
- current_balance_val=current_balance_val+tmp_val
- + CASE
- WHEN current_balance_frac+tmp_frac >= 100000000
- THEN 1
- ELSE 0
- END,
+ current_balance = new_balance,
gc_date=GREATEST(gc_date, in_reserve_gc),
expiration_date=GREATEST(expiration_date, in_reserve_expiration)
WHERE reserve_pub=in_reserve_pub;
@@ -126,8 +136,8 @@ VALUES
(in_coin_pub
,in_coin_sig
,in_coin_blind
- ,tmp_val
- ,tmp_frac
+ ,tmp.val
+ ,tmp.frac
,in_recoup_timestamp
,in_reserve_out_serial_id);