do_expire_purse.sql (2816B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2014--2022 Taler Systems SA 4 -- 5 -- TALER is free software; you can redistribute it and/or modify it under the 6 -- terms of the GNU General Public License as published by the Free Software 7 -- Foundation; either version 3, or (at your option) any later version. 8 -- 9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 -- A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 -- 13 -- You should have received a copy of the GNU General Public License along with 14 -- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 -- 16 17 DROP FUNCTION IF EXISTS exchange_do_expire_purse; 18 CREATE FUNCTION exchange_do_expire_purse( 19 IN in_start_time INT8, 20 IN in_end_time INT8, 21 IN in_now INT8, 22 OUT out_found BOOLEAN, 23 OUT out_purse_expiration INT8) 24 LANGUAGE plpgsql 25 AS $$ 26 DECLARE 27 my_purse_pub BYTEA; 28 DECLARE 29 my_deposit record; 30 DECLARE 31 my_in_reserve_quota BOOLEAN; 32 BEGIN 33 34 -- FIXME: we should probably do this in a loop 35 -- and expire all at once, instead of one per query 36 SELECT purse_pub 37 ,in_reserve_quota 38 ,purse_expiration 39 INTO my_purse_pub 40 ,my_in_reserve_quota 41 ,out_purse_expiration 42 FROM purse_requests 43 WHERE (purse_expiration >= in_start_time) AND 44 (purse_expiration < in_end_time) AND 45 NOT was_decided 46 ORDER BY purse_expiration ASC 47 LIMIT 1; 48 out_found = FOUND; 49 IF NOT FOUND 50 THEN 51 RETURN; 52 END IF; 53 54 INSERT INTO purse_decision 55 (purse_pub 56 ,action_timestamp 57 ,refunded) 58 VALUES 59 (my_purse_pub 60 ,in_now 61 ,TRUE); 62 63 -- Code for 'TALER_DBEVENT_EXCHANGE_PURSE_REFUNDED' 64 NOTIFY X8DJSPNYJMNZDAP7GN6YQ4EZVSQXMF3HRP4VAR347WP9SZYP1C200; 65 66 IF (my_in_reserve_quota) 67 THEN 68 UPDATE reserves 69 SET purses_active=purses_active-1 70 WHERE reserve_pub IN 71 (SELECT reserve_pub 72 FROM exchange.purse_merges 73 WHERE purse_pub=my_purse_pub 74 LIMIT 1); 75 END IF; 76 77 -- restore balance to each coin deposited into the purse 78 FOR my_deposit IN 79 SELECT coin_pub 80 ,amount_with_fee 81 FROM purse_deposits 82 WHERE purse_pub = my_purse_pub 83 LOOP 84 UPDATE known_coins kc SET 85 remaining.frac=(kc.remaining).frac+(my_deposit.amount_with_fee).frac 86 - CASE 87 WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000 88 THEN 100000000 89 ELSE 0 90 END, 91 remaining.val=(kc.remaining).val+(my_deposit.amount_with_fee).val 92 + CASE 93 WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000 94 THEN 1 95 ELSE 0 96 END 97 WHERE coin_pub = my_deposit.coin_pub; 98 END LOOP; 99 END $$; 100 101 COMMENT ON FUNCTION exchange_do_expire_purse(INT8,INT8,INT8) 102 IS 'Finds an expired purse in the given time range and refunds the coins (if any).';