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.sql64
1 files changed, 35 insertions, 29 deletions
diff --git a/src/exchangedb/exchange_do_recoup_to_reserve.sql b/src/exchangedb/exchange_do_recoup_to_reserve.sql
index 39baba8fa..10ae063bd 100644
--- a/src/exchangedb/exchange_do_recoup_to_reserve.sql
+++ b/src/exchangedb/exchange_do_recoup_to_reserve.sql
@@ -31,9 +31,11 @@ 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;
+ rval RECORD;
BEGIN
-- Shards: SELECT known_coins (by coin_pub)
-- SELECT recoup (by coin_pub)
@@ -46,11 +48,9 @@ out_internal_failure=FALSE;
-- Check remaining balance of the coin.
SELECT
- remaining_frac
- ,remaining_val
+ remaining
INTO
- tmp_frac
- ,tmp_val
+ rval
FROM exchange.known_coins
WHERE coin_pub=in_coin_pub;
@@ -61,7 +61,9 @@ THEN
RETURN;
END IF;
-IF tmp_val + tmp_frac = 0
+tmp := rval.remaining;
+
+IF tmp.val + tmp.frac = 0
THEN
-- Check for idempotency
SELECT
@@ -79,26 +81,35 @@ END IF;
-- Update balance of the coin.
UPDATE known_coins
SET
- remaining_frac=0
- ,remaining_val=0
+ remaining.val = 0
+ ,remaining.frac = 0
WHERE coin_pub=in_coin_pub;
+-- Get current balance
+SELECT current_balance
+ 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;
@@ -117,8 +128,7 @@ INSERT INTO exchange.recoup
(coin_pub
,coin_sig
,coin_blind
- ,amount_val
- ,amount_frac
+ ,amount
,recoup_timestamp
,reserve_out_serial_id
)
@@ -126,8 +136,7 @@ VALUES
(in_coin_pub
,in_coin_sig
,in_coin_blind
- ,tmp_val
- ,tmp_frac
+ ,tmp
,in_recoup_timestamp
,in_reserve_out_serial_id);
@@ -139,6 +148,3 @@ END $$;
-- COMMENT ON FUNCTION exchange_do_recoup_to_reserve(INT8, INT4, BYTEA, BOOLEAN, BOOLEAN)
-- IS 'Executes a recoup of a coin that was withdrawn from a reserve';
-
-
-