exchange_do_purse_delete.sql (2839B)
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 CREATE OR REPLACE FUNCTION exchange_do_purse_delete( 18 IN in_purse_pub BYTEA, 19 IN in_purse_sig BYTEA, 20 IN in_now INT8, 21 OUT out_decided BOOLEAN, 22 OUT out_found BOOLEAN) 23 LANGUAGE plpgsql 24 AS $$ 25 DECLARE 26 my_deposit record; 27 DECLARE 28 my_in_reserve_quota BOOLEAN; 29 BEGIN 30 31 PERFORM refunded FROM purse_decision 32 WHERE purse_pub=in_purse_pub; 33 IF FOUND 34 THEN 35 out_found=TRUE; 36 out_decided=TRUE; 37 RETURN; 38 END IF; 39 out_decided=FALSE; 40 41 SELECT in_reserve_quota 42 INTO my_in_reserve_quota 43 FROM exchange.purse_requests 44 WHERE purse_pub=in_purse_pub; 45 out_found=FOUND; 46 IF NOT FOUND 47 THEN 48 RETURN; 49 END IF; 50 51 -- store reserve deletion 52 INSERT INTO exchange.purse_deletion 53 (purse_pub 54 ,purse_sig) 55 VALUES 56 (in_purse_pub 57 ,in_purse_sig) 58 ON CONFLICT DO NOTHING; 59 60 IF NOT FOUND 61 THEN 62 RETURN; 63 END IF; 64 65 -- Delete contract associated with purse, if it exists. 66 DELETE FROM contracts 67 WHERE purse_pub=in_purse_pub; 68 69 -- store purse decision 70 INSERT INTO purse_decision 71 (purse_pub 72 ,action_timestamp 73 ,refunded) 74 VALUES 75 (in_purse_pub 76 ,in_now 77 ,TRUE); 78 79 -- update purse quota at reserve 80 IF (my_in_reserve_quota) 81 THEN 82 UPDATE reserves 83 SET purses_active=purses_active-1 84 WHERE reserve_pub IN 85 (SELECT reserve_pub 86 FROM exchange.purse_merges 87 WHERE purse_pub=in_purse_pub 88 LIMIT 1); 89 END IF; 90 91 -- restore balance to each coin deposited into the purse 92 FOR my_deposit IN 93 SELECT coin_pub 94 ,amount_with_fee 95 FROM exchange.purse_deposits 96 WHERE purse_pub = in_purse_pub 97 LOOP 98 UPDATE known_coins kc SET 99 remaining.frac=(kc.remaining).frac+(my_deposit.amount_with_fee).frac 100 - CASE 101 WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000 102 THEN 100000000 103 ELSE 0 104 END, 105 remaining.val=(kc.remaining).val+(my_deposit.amount_with_fee).val 106 + CASE 107 WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000 108 THEN 1 109 ELSE 0 110 END 111 WHERE coin_pub = my_deposit.coin_pub; 112 END LOOP; 113 114 115 END $$; 116 117 COMMENT ON FUNCTION exchange_do_purse_delete(BYTEA,BYTEA,INT8) 118 IS 'Delete a previously undecided purse and refund the coins (if any).';