exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_do_expire_purse.sql (2692B)


      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_expire_purse(
     18   IN in_start_time INT8,
     19   IN in_end_time INT8,
     20   IN in_now INT8,
     21   OUT out_found BOOLEAN)
     22 LANGUAGE plpgsql
     23 AS $$
     24 DECLARE
     25   my_purse_pub BYTEA;
     26 DECLARE
     27   my_deposit record;
     28 DECLARE
     29   my_in_reserve_quota BOOLEAN;
     30 BEGIN
     31 
     32 -- FIXME: we should probably do this in a loop
     33 -- and expire all at once, instead of one per query
     34 SELECT purse_pub
     35       ,in_reserve_quota
     36   INTO my_purse_pub
     37       ,my_in_reserve_quota
     38   FROM purse_requests
     39  WHERE (purse_expiration >= in_start_time) AND
     40        (purse_expiration < in_end_time) AND
     41        NOT was_decided
     42   ORDER BY purse_expiration ASC
     43  LIMIT 1;
     44 out_found = FOUND;
     45 IF NOT FOUND
     46 THEN
     47   RETURN;
     48 END IF;
     49 
     50 INSERT INTO purse_decision
     51   (purse_pub
     52   ,action_timestamp
     53   ,refunded)
     54 VALUES
     55   (my_purse_pub
     56   ,in_now
     57   ,TRUE);
     58 
     59 -- Code for 'TALER_DBEVENT_EXCHANGE_PURSE_REFUNDED'
     60 NOTIFY X8DJSPNYJMNZDAP7GN6YQ4EZVSQXMF3HRP4VAR347WP9SZYP1C200;
     61 
     62 IF (my_in_reserve_quota)
     63 THEN
     64   UPDATE reserves
     65     SET purses_active=purses_active-1
     66   WHERE reserve_pub IN
     67     (SELECT reserve_pub
     68        FROM exchange.purse_merges
     69       WHERE purse_pub=my_purse_pub
     70      LIMIT 1);
     71 END IF;
     72 
     73 -- restore balance to each coin deposited into the purse
     74 FOR my_deposit IN
     75   SELECT coin_pub
     76         ,amount_with_fee
     77     FROM purse_deposits
     78   WHERE purse_pub = my_purse_pub
     79 LOOP
     80   UPDATE known_coins kc SET
     81     remaining.frac=(kc.remaining).frac+(my_deposit.amount_with_fee).frac
     82      - CASE
     83        WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000
     84        THEN 100000000
     85        ELSE 0
     86        END,
     87     remaining.val=(kc.remaining).val+(my_deposit.amount_with_fee).val
     88      + CASE
     89        WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000
     90        THEN 1
     91        ELSE 0
     92        END
     93     WHERE coin_pub = my_deposit.coin_pub;
     94   END LOOP;
     95 END $$;
     96 
     97 COMMENT ON FUNCTION exchange_do_expire_purse(INT8,INT8,INT8)
     98   IS 'Finds an expired purse in the given time range and refunds the coins (if any).';