1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
--
-- This file is part of TALER
-- Copyright (C) 2014--2022 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
CREATE OR REPLACE FUNCTION exchange_do_history_request(
IN in_reserve_pub BYTEA,
IN in_reserve_sig BYTEA,
IN in_request_timestamp INT8,
IN in_history_fee_val INT8,
IN in_history_fee_frac INT4,
OUT out_balance_ok BOOLEAN,
OUT out_idempotent BOOLEAN)
LANGUAGE plpgsql
AS $$
BEGIN
-- Insert and check for idempotency.
INSERT INTO exchange.history_requests
(reserve_pub
,request_timestamp
,reserve_sig
,history_fee_val
,history_fee_frac)
VALUES
(in_reserve_pub
,in_request_timestamp
,in_reserve_sig
,in_history_fee_val
,in_history_fee_frac)
ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
out_balance_ok=TRUE;
out_idempotent=TRUE;
RETURN;
END IF;
out_idempotent=FALSE;
-- Update reserve balance.
UPDATE exchange.reserves
SET
current_balance_frac=current_balance_frac-in_history_fee_frac
+ CASE
WHEN current_balance_frac < in_history_fee_frac
THEN 100000000
ELSE 0
END,
current_balance_val=current_balance_val-in_history_fee_val
- CASE
WHEN current_balance_frac < in_history_fee_frac
THEN 1
ELSE 0
END
WHERE
reserve_pub=in_reserve_pub
AND ( (current_balance_val > in_history_fee_val) OR
( (current_balance_frac >= in_history_fee_frac) AND
(current_balance_val >= in_history_fee_val) ) );
IF NOT FOUND
THEN
-- Either reserve does not exist, or balance insufficient.
-- Both we treat the same here as balance insufficient.
out_balance_ok=FALSE;
RETURN;
END IF;
out_balance_ok=TRUE;
END $$;
|