summaryrefslogtreecommitdiff
path: root/src/exchangedb/plugin_exchangedb_postgres.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-07-09 12:14:20 +0200
committerChristian Grothoff <christian@grothoff.org>2022-07-09 12:14:20 +0200
commit4e5193a21fd6084e773e00ed8f638f193a525bab (patch)
treeb96f07d1b0caec239c22a6dc094c0b3537831989 /src/exchangedb/plugin_exchangedb_postgres.c
parent45f43fcde72554f21fe384c4bbb199c010ef46c9 (diff)
downloadexchange-4e5193a21fd6084e773e00ed8f638f193a525bab.tar.gz
exchange-4e5193a21fd6084e773e00ed8f638f193a525bab.tar.bz2
exchange-4e5193a21fd6084e773e00ed8f638f193a525bab.zip
-fix full refund deposit fee computation in aggregator
Diffstat (limited to 'src/exchangedb/plugin_exchangedb_postgres.c')
-rw-r--r--src/exchangedb/plugin_exchangedb_postgres.c32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/exchangedb/plugin_exchangedb_postgres.c b/src/exchangedb/plugin_exchangedb_postgres.c
index ee120a01c..277e3bc4f 100644
--- a/src/exchangedb/plugin_exchangedb_postgres.c
+++ b/src/exchangedb/plugin_exchangedb_postgres.c
@@ -1778,10 +1778,31 @@ prepare_statements (struct PostgresClosure *pg)
" FROM refunds"
" WHERE coin_pub IN (SELECT coin_pub FROM dep)"
" AND deposit_serial_id IN (SELECT deposit_serial_id FROM dep))"
- " ,fees AS (" /* find deposit fees for non-refunded deposits */
- // FIXME: this is wrong, the deposit fee is waived IF the
- // refunds were for 100% of the deposit value. This logic
- // ignores this detail :-(.
+ " ,ref_by_coin AS (" /* total up refunds by coin */
+ " SELECT"
+ " SUM(refund_val) AS sum_refund_val"
+ " ,SUM(refund_frac) AS sum_refund_frac"
+ " ,coin_pub"
+ " ,deposit_serial_id" /* theoretically, coin could be in multiple refunded transactions */
+ " FROM ref"
+ " GROUP BY coin_pub, deposit_serial_id)"
+ " ,norm_ref_by_coin AS (" /* normalize */
+ " SELECT"
+ " sum_refund_val + sum_refund_frac / 100000000 AS norm_refund_val"
+ " ,sum_refund_frac % 100000000 AS norm_refund_frac"
+ " ,coin_pub"
+ " ,deposit_serial_id" /* theoretically, coin could be in multiple refunded transactions */
+ " FROM ref_by_coin)"
+ " ,fully_refunded_coins AS (" /* find applicable refunds -- NOTE: may do a full join on the master, maybe find a left-join way to integrate with query above to push it to the shards? */
+ " SELECT"
+ " dep.coin_pub"
+ " FROM norm_ref_by_coin norm"
+ " JOIN dep"
+ " ON (norm.coin_pub = dep.coin_pub"
+ " AND norm.deposit_serial_id = dep.deposit_Serial_id"
+ " AND norm.norm_refund_val = dep.amount_val"
+ " AND norm.norm_refund_frac = dep.amount_frac))"
+ " ,fees AS (" /* find deposit fees for not fully refunded deposits */
" SELECT"
" denom.fee_deposit_val AS fee_val"
" ,denom.fee_deposit_frac AS fee_frac"
@@ -1790,7 +1811,8 @@ prepare_statements (struct PostgresClosure *pg)
" JOIN known_coins kc" /* NOTE: may do a full join on the master, maybe find a left-join way to integrate with query above to push it to the shards? */
" USING (coin_pub)"
" JOIN denominations denom"
- " USING (denominations_serial))"
+ " USING (denominations_serial)"
+ " WHERE coin_pub NOT IN (SELECT coin_pub FROM fully_refunded_coins))"
" ,dummy AS (" /* add deposits to aggregation_tracking */
" INSERT INTO aggregation_tracking"
" (deposit_serial_id"