exchange

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

exchange_do_reserve_open_deposit.sql (2162B)


      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 
     18 CREATE OR REPLACE FUNCTION exchange_do_reserve_open_deposit(
     19   IN in_coin_pub BYTEA,
     20   IN in_known_coin_id INT8,
     21   IN in_coin_sig BYTEA,
     22   IN in_reserve_sig BYTEA,
     23   IN in_reserve_pub BYTEA,
     24   IN in_coin_total taler_amount,
     25   OUT out_insufficient_funds BOOLEAN)
     26 LANGUAGE plpgsql
     27 AS $$
     28 BEGIN
     29 
     30 INSERT INTO exchange.reserves_open_deposits
     31   (reserve_sig
     32   ,reserve_pub
     33   ,coin_pub
     34   ,coin_sig
     35   ,contribution
     36   )
     37   VALUES
     38   (in_reserve_sig
     39   ,in_reserve_pub
     40   ,in_coin_pub
     41   ,in_coin_sig
     42   ,in_coin_total
     43   )
     44   ON CONFLICT DO NOTHING;
     45 
     46 IF NOT FOUND
     47 THEN
     48   -- Idempotent request known, return success.
     49   out_insufficient_funds=FALSE;
     50   RETURN;
     51 END IF;
     52 
     53 
     54 -- Check and update balance of the coin.
     55 UPDATE exchange.known_coins kc
     56   SET
     57     remaining.frac=(kc.remaining).frac-in_coin_total.frac
     58        + CASE
     59          WHEN (kc.remaining).frac < in_coin_total.frac
     60          THEN 100000000
     61          ELSE 0
     62          END,
     63     remaining.val=(kc.remaining).val-in_coin_total.val
     64        - CASE
     65          WHEN (kc.remaining).frac < in_coin_total.frac
     66          THEN 1
     67          ELSE 0
     68          END
     69   WHERE coin_pub=in_coin_pub
     70     AND ( ((kc.remaining).val > in_coin_total.val) OR
     71           ( ((kc.remaining).frac >= in_coin_total.frac) AND
     72             ((kc.remaining).val >= in_coin_total.val) ) );
     73 
     74 IF NOT FOUND
     75 THEN
     76   -- Insufficient balance.
     77   out_insufficient_funds=TRUE;
     78   RETURN;
     79 END IF;
     80 
     81 -- Everything fine, return success!
     82 out_insufficient_funds=FALSE;
     83 
     84 END $$;