From 26f12103a90fce033614ffed20f8aae1533f8c37 Mon Sep 17 00:00:00 2001 From: binodnpm Date: Thu, 6 Jun 2024 14:18:30 +0530 Subject: [PATCH] # Fix for Datewise Liquidity Summary - Refactored function `count_cover_purchase_during`, `get_total_capacity_by_date`, `get_total_covered_till_date`, `sum_cover_fee_earned_during` and their respective overloads - Fixed mistakes of the `get_datewise_liquidity_summary` function --- build/db.sql | 150 +++++++++--------- package.json | 7 + .../home/get_datewise_liquidity_summary.sql | 11 +- .../count_cover_purchase_during.sql | 48 +++--- .../003-functions/get_capacity_till_date.sql | 25 ++- .../get_total_covered_till_date.sql | 30 ++-- .../sum_cover_fee_earned_during.sql | 35 ++-- 7 files changed, 158 insertions(+), 148 deletions(-) diff --git a/build/db.sql b/build/db.sql index 2e8bf25..541c0b7 100644 --- a/build/db.sql +++ b/build/db.sql @@ -5677,6 +5677,9 @@ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION count_cover_purchase_during ( + _chain_id uint256, + _cover_key bytes32, + _product_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -5689,17 +5692,21 @@ BEGIN SELECT COUNT(*) INTO _result FROM policy.cover_purchased - WHERE to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; + WHERE 1 = 1 + AND (_chain_id IS NULL OR policy.cover_purchased.chain_id = _chain_id) + AND (_cover_key IS NULL OR policy.cover_purchased.cover_key = _cover_key) + AND (_product_key IS NULL OR policy.cover_purchased.product_key = _product_key) + AND to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; RETURN COALESCE(_result, 0); END $$ LANGUAGE plpgsql; - CREATE OR REPLACE FUNCTION count_cover_purchase_during ( _chain_id uint256, + _cover_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -5707,23 +5714,16 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT COUNT(*) - INTO _result - FROM policy.cover_purchased - WHERE policy.cover_purchased.chain_id = _chain_id - AND to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; - - RETURN COALESCE(_result, 0); + RETURN count_cover_purchase_during(_chain_id, _cover_key, NULL, _start, _end); END $$ LANGUAGE plpgsql; + CREATE OR REPLACE FUNCTION count_cover_purchase_during ( _chain_id uint256, - _cover_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -5731,16 +5731,25 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT COUNT(*) - INTO _result - FROM policy.cover_purchased - WHERE policy.cover_purchased.chain_id = _chain_id - AND policy.cover_purchased.cover_key = _cover_key - AND to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; + RETURN count_cover_purchase_during(_chain_id, NULL, NULL, _start, _end); +END +$$ +LANGUAGE plpgsql; - RETURN COALESCE(_result, 0); + +CREATE OR REPLACE FUNCTION count_cover_purchase_during +( + _start TIMESTAMP WITH TIME ZONE, + _end TIMESTAMP WITH TIME ZONE +) +RETURNS numeric +STABLE +AS +$$ + DECLARE _result numeric; +BEGIN + RETURN count_cover_purchase_during(NULL, NULL, NULL, _start, _end); END $$ LANGUAGE plpgsql; @@ -5749,6 +5758,7 @@ LANGUAGE plpgsql; ALTER FUNCTION count_cover_purchase_during(TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; ALTER FUNCTION count_cover_purchase_during(uint256, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; ALTER FUNCTION count_cover_purchase_during(uint256, bytes32, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; +ALTER FUNCTION count_cover_purchase_during(uint256, bytes32, bytes32, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; DROP FUNCTION IF EXISTS get_active_incident_date ( @@ -5895,9 +5905,9 @@ LANGUAGE plpgsql; --SELECT * FROM get_active_product_status(84531, string_to_bytes32('huobi'), NULL); - CREATE OR REPLACE FUNCTION get_total_capacity_by_date ( + _chain_id uint256, _date TIMESTAMP WITH TIME ZONE ) RETURNS numeric @@ -5911,6 +5921,8 @@ BEGIN ( SELECT DISTINCT core.transactions.chain_id FROM core.transactions + WHERE 1 = 1 + AND (_chain_id IS NULL OR core.transactions.chain_id = _chain_id) ), unfiltered AS @@ -5960,9 +5972,29 @@ END $$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION get_total_capacity_by_date +( + _date TIMESTAMP WITH TIME ZONE +) +RETURNS numeric +STABLE +AS +$$ + DECLARE _capacity uint256; +BEGIN + RETURN get_total_capacity_by_date(NULL, _date); +END +$$ +LANGUAGE plpgsql; + + + ALTER FUNCTION get_total_capacity_by_date(TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; +ALTER FUNCTION get_total_capacity_by_date(uint256, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; + + ---SELECT get_total_capacity_by_date('infinity') CREATE OR REPLACE FUNCTION get_claim_platform_fee(_chain_id uint256) RETURNS numeric STABLE @@ -6987,6 +7019,8 @@ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION get_total_covered_till_date ( + _chain_id uint256, + _cover_key bytes32, _date TIMESTAMP WITH TIME ZONE ) RETURNS numeric @@ -6998,13 +7032,17 @@ BEGIN SELECT SUM(get_stablecoin_value(chain_id, amount_to_cover)) INTO _result FROM policy.cover_purchased - WHERE to_timestamp(expires_on) <= _date; + WHERE 1 = 1 + AND (_chain_id IS NULL OR chain_id = _chain_id) + AND (_cover_key IS NULL OR cover_key = _cover_key) + AND to_timestamp(expires_on) <= _date; RETURN COALESCE(_result, 0); END $$ LANGUAGE plpgsql; + CREATE OR REPLACE FUNCTION get_total_covered_till_date ( _chain_id uint256, @@ -7014,40 +7052,22 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT SUM(get_stablecoin_value(chain_id, amount_to_cover)) - INTO _result - FROM policy.cover_purchased - WHERE chain_id = _chain_id - AND to_timestamp(expires_on) <= _date; - - RETURN COALESCE(_result, 0); + RETURN get_total_covered_till_date(_chain_id, NULL, _date); END $$ LANGUAGE plpgsql; - CREATE OR REPLACE FUNCTION get_total_covered_till_date ( - _chain_id uint256, - _cover_key bytes32, _date TIMESTAMP WITH TIME ZONE ) RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT SUM(get_stablecoin_value(chain_id, amount_to_cover)) - INTO _result - FROM policy.cover_purchased - WHERE chain_id = _chain_id - AND cover_key = _cover_key - AND to_timestamp(expires_on) <= _date; - - RETURN COALESCE(_result, 0); + RETURN get_total_covered_till_date(NULL, NULL, _date); END $$ LANGUAGE plpgsql; @@ -7219,6 +7239,8 @@ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION sum_cover_fee_earned_during ( + _chain_id uint256, + _cover_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -7233,7 +7255,10 @@ BEGIN INTO _result FROM policy.cover_purchased - WHERE to_timestamp(policy.cover_purchased.block_timestamp) + WHERE 1 = 1 + AND (_chain_id IS NULL OR policy.cover_purchased.chain_id = _chain_id) + AND (_cover_key IS NULL OR policy.cover_purchased.cover_key = _cover_key) + AND to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; RETURN COALESCE(_result, 0); @@ -7252,26 +7277,15 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT - SUM(get_stablecoin_value(policy.cover_purchased.chain_id, policy.cover_purchased.fee)) - INTO - _result - FROM policy.cover_purchased - WHERE policy.cover_purchased.chain_id = _chain_id - AND to_timestamp(policy.cover_purchased.block_timestamp) - BETWEEN _start AND _end; - - RETURN COALESCE(_result, 0); + RETURN sum_cover_fee_earned_during(_chain_id, NULL, _start, _end); END $$ LANGUAGE plpgsql; + CREATE OR REPLACE FUNCTION sum_cover_fee_earned_during ( - _chain_id uint256, - _cover_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -7279,19 +7293,8 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT - SUM(get_stablecoin_value(policy.cover_purchased.chain_id, policy.cover_purchased.fee)) - INTO - _result - FROM policy.cover_purchased - WHERE policy.cover_purchased.chain_id = _chain_id - AND policy.cover_purchased.cover_key = _cover_key - AND to_timestamp(policy.cover_purchased.block_timestamp) - BETWEEN _start AND _end; - - RETURN COALESCE(_result, 0); + RETURN sum_cover_fee_earned_during(NULL, NULL, _start, _end); END $$ LANGUAGE plpgsql; @@ -7919,7 +7922,7 @@ BEGIN CREATE UNLOGGED TABLE IF NOT EXISTS public.datewise_liquidity_summary ( id BIGSERIAL, - chain_id integer, + chain_id uint256, date TIMESTAMP WITH TIME ZONE, total_liquidity numeric, total_capacity numeric, @@ -7947,7 +7950,6 @@ BEGIN SELECT * FROM stale_data ); - WITH date_ranges AS ( @@ -8003,19 +8005,19 @@ BEGIN WHERE public.datewise_liquidity_summary.total_liquidity IS NULL; UPDATE public.datewise_liquidity_summary - SET total_capacity = get_total_capacity_by_date(public.datewise_liquidity_summary.date) + SET total_capacity = get_total_capacity_by_date(public.datewise_liquidity_summary.chain_id, public.datewise_liquidity_summary.date) WHERE public.datewise_liquidity_summary.total_capacity IS NULL; UPDATE public.datewise_liquidity_summary - SET total_covered = get_total_covered_till_date(public.datewise_liquidity_summary.date) + SET total_covered = get_total_covered_till_date(public.datewise_liquidity_summary.chain_id, public.datewise_liquidity_summary.date) WHERE public.datewise_liquidity_summary.total_covered IS NULL; UPDATE public.datewise_liquidity_summary - SET total_cover_fee = sum_cover_fee_earned_during('-infinity', public.datewise_liquidity_summary.date) + SET total_cover_fee = sum_cover_fee_earned_during(public.datewise_liquidity_summary.chain_id, '-infinity', public.datewise_liquidity_summary.date) WHERE public.datewise_liquidity_summary.total_cover_fee IS NULL; UPDATE public.datewise_liquidity_summary - SET total_purchase_count = count_cover_purchase_during('-infinity', public.datewise_liquidity_summary.date) + SET total_purchase_count = count_cover_purchase_during(public.datewise_liquidity_summary.chain_id, '-infinity', public.datewise_liquidity_summary.date) WHERE public.datewise_liquidity_summary.total_purchase_count IS NULL; RETURN QUERY diff --git a/package.json b/package.json index 0d87938..6379019 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,12 @@ "ethers": "5.5.2", "husky": "^9.0.11", "standard": "^17.0.0" + }, + "engines": { + "node": ">=20" + }, + "volta": { + "node": "20.11.1", + "yarn": "1.22.19" } } \ No newline at end of file diff --git a/sql/app/home/get_datewise_liquidity_summary.sql b/sql/app/home/get_datewise_liquidity_summary.sql index 22a4553..97f2f7f 100644 --- a/sql/app/home/get_datewise_liquidity_summary.sql +++ b/sql/app/home/get_datewise_liquidity_summary.sql @@ -18,7 +18,7 @@ BEGIN CREATE UNLOGGED TABLE IF NOT EXISTS public.datewise_liquidity_summary ( id BIGSERIAL, - chain_id integer, + chain_id uint256, date TIMESTAMP WITH TIME ZONE, total_liquidity numeric, total_capacity numeric, @@ -46,7 +46,6 @@ BEGIN SELECT * FROM stale_data ); - WITH date_ranges AS ( @@ -102,19 +101,19 @@ BEGIN WHERE public.datewise_liquidity_summary.total_liquidity IS NULL; UPDATE public.datewise_liquidity_summary - SET total_capacity = get_total_capacity_by_date(public.datewise_liquidity_summary.date) + SET total_capacity = get_total_capacity_by_date(public.datewise_liquidity_summary.chain_id, public.datewise_liquidity_summary.date) WHERE public.datewise_liquidity_summary.total_capacity IS NULL; UPDATE public.datewise_liquidity_summary - SET total_covered = get_total_covered_till_date(public.datewise_liquidity_summary.date) + SET total_covered = get_total_covered_till_date(public.datewise_liquidity_summary.chain_id, public.datewise_liquidity_summary.date) WHERE public.datewise_liquidity_summary.total_covered IS NULL; UPDATE public.datewise_liquidity_summary - SET total_cover_fee = sum_cover_fee_earned_during('-infinity', public.datewise_liquidity_summary.date) + SET total_cover_fee = sum_cover_fee_earned_during(public.datewise_liquidity_summary.chain_id, '-infinity', public.datewise_liquidity_summary.date) WHERE public.datewise_liquidity_summary.total_cover_fee IS NULL; UPDATE public.datewise_liquidity_summary - SET total_purchase_count = count_cover_purchase_during('-infinity', public.datewise_liquidity_summary.date) + SET total_purchase_count = count_cover_purchase_during(public.datewise_liquidity_summary.chain_id, '-infinity', public.datewise_liquidity_summary.date) WHERE public.datewise_liquidity_summary.total_purchase_count IS NULL; RETURN QUERY diff --git a/sql/base/003-functions/count_cover_purchase_during.sql b/sql/base/003-functions/count_cover_purchase_during.sql index a1ee2c4..1871504 100644 --- a/sql/base/003-functions/count_cover_purchase_during.sql +++ b/sql/base/003-functions/count_cover_purchase_during.sql @@ -1,5 +1,8 @@ CREATE OR REPLACE FUNCTION count_cover_purchase_during ( + _chain_id uint256, + _cover_key bytes32, + _product_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -12,17 +15,21 @@ BEGIN SELECT COUNT(*) INTO _result FROM policy.cover_purchased - WHERE to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; + WHERE 1 = 1 + AND (_chain_id IS NULL OR policy.cover_purchased.chain_id = _chain_id) + AND (_cover_key IS NULL OR policy.cover_purchased.cover_key = _cover_key) + AND (_product_key IS NULL OR policy.cover_purchased.product_key = _product_key) + AND to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; RETURN COALESCE(_result, 0); END $$ LANGUAGE plpgsql; - CREATE OR REPLACE FUNCTION count_cover_purchase_during ( _chain_id uint256, + _cover_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -30,23 +37,16 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT COUNT(*) - INTO _result - FROM policy.cover_purchased - WHERE policy.cover_purchased.chain_id = _chain_id - AND to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; - - RETURN COALESCE(_result, 0); + RETURN count_cover_purchase_during(_chain_id, _cover_key, NULL, _start, _end); END $$ LANGUAGE plpgsql; + CREATE OR REPLACE FUNCTION count_cover_purchase_during ( _chain_id uint256, - _cover_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -54,16 +54,25 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT COUNT(*) - INTO _result - FROM policy.cover_purchased - WHERE policy.cover_purchased.chain_id = _chain_id - AND policy.cover_purchased.cover_key = _cover_key - AND to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; + RETURN count_cover_purchase_during(_chain_id, NULL, NULL, _start, _end); +END +$$ +LANGUAGE plpgsql; - RETURN COALESCE(_result, 0); + +CREATE OR REPLACE FUNCTION count_cover_purchase_during +( + _start TIMESTAMP WITH TIME ZONE, + _end TIMESTAMP WITH TIME ZONE +) +RETURNS numeric +STABLE +AS +$$ + DECLARE _result numeric; +BEGIN + RETURN count_cover_purchase_during(NULL, NULL, NULL, _start, _end); END $$ LANGUAGE plpgsql; @@ -72,3 +81,4 @@ LANGUAGE plpgsql; ALTER FUNCTION count_cover_purchase_during(TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; ALTER FUNCTION count_cover_purchase_during(uint256, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; ALTER FUNCTION count_cover_purchase_during(uint256, bytes32, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; +ALTER FUNCTION count_cover_purchase_during(uint256, bytes32, bytes32, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; diff --git a/sql/base/003-functions/get_capacity_till_date.sql b/sql/base/003-functions/get_capacity_till_date.sql index 1da1685..24a96fe 100644 --- a/sql/base/003-functions/get_capacity_till_date.sql +++ b/sql/base/003-functions/get_capacity_till_date.sql @@ -1,6 +1,6 @@ - CREATE OR REPLACE FUNCTION get_total_capacity_by_date ( + _chain_id uint256, _date TIMESTAMP WITH TIME ZONE ) RETURNS numeric @@ -14,6 +14,8 @@ BEGIN ( SELECT DISTINCT core.transactions.chain_id FROM core.transactions + WHERE 1 = 1 + AND (_chain_id IS NULL OR core.transactions.chain_id = _chain_id) ), unfiltered AS @@ -63,6 +65,25 @@ END $$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION get_total_capacity_by_date +( + _date TIMESTAMP WITH TIME ZONE +) +RETURNS numeric +STABLE +AS +$$ + DECLARE _capacity uint256; +BEGIN + RETURN get_total_capacity_by_date(NULL, _date); +END +$$ +LANGUAGE plpgsql; + + + ALTER FUNCTION get_total_capacity_by_date(TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; +ALTER FUNCTION get_total_capacity_by_date(uint256, TIMESTAMP WITH TIME ZONE) OWNER TO writeuser; + ---SELECT get_total_capacity_by_date('infinity') \ No newline at end of file diff --git a/sql/base/003-functions/get_total_covered_till_date.sql b/sql/base/003-functions/get_total_covered_till_date.sql index 78d5686..a205522 100644 --- a/sql/base/003-functions/get_total_covered_till_date.sql +++ b/sql/base/003-functions/get_total_covered_till_date.sql @@ -1,5 +1,7 @@ CREATE OR REPLACE FUNCTION get_total_covered_till_date ( + _chain_id uint256, + _cover_key bytes32, _date TIMESTAMP WITH TIME ZONE ) RETURNS numeric @@ -11,13 +13,17 @@ BEGIN SELECT SUM(get_stablecoin_value(chain_id, amount_to_cover)) INTO _result FROM policy.cover_purchased - WHERE to_timestamp(expires_on) <= _date; + WHERE 1 = 1 + AND (_chain_id IS NULL OR chain_id = _chain_id) + AND (_cover_key IS NULL OR cover_key = _cover_key) + AND to_timestamp(expires_on) <= _date; RETURN COALESCE(_result, 0); END $$ LANGUAGE plpgsql; + CREATE OR REPLACE FUNCTION get_total_covered_till_date ( _chain_id uint256, @@ -27,40 +33,22 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT SUM(get_stablecoin_value(chain_id, amount_to_cover)) - INTO _result - FROM policy.cover_purchased - WHERE chain_id = _chain_id - AND to_timestamp(expires_on) <= _date; - - RETURN COALESCE(_result, 0); + RETURN get_total_covered_till_date(_chain_id, NULL, _date); END $$ LANGUAGE plpgsql; - CREATE OR REPLACE FUNCTION get_total_covered_till_date ( - _chain_id uint256, - _cover_key bytes32, _date TIMESTAMP WITH TIME ZONE ) RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT SUM(get_stablecoin_value(chain_id, amount_to_cover)) - INTO _result - FROM policy.cover_purchased - WHERE chain_id = _chain_id - AND cover_key = _cover_key - AND to_timestamp(expires_on) <= _date; - - RETURN COALESCE(_result, 0); + RETURN get_total_covered_till_date(NULL, NULL, _date); END $$ LANGUAGE plpgsql; diff --git a/sql/base/003-functions/sum_cover_fee_earned_during.sql b/sql/base/003-functions/sum_cover_fee_earned_during.sql index d003e2d..19ce189 100644 --- a/sql/base/003-functions/sum_cover_fee_earned_during.sql +++ b/sql/base/003-functions/sum_cover_fee_earned_during.sql @@ -1,5 +1,7 @@ CREATE OR REPLACE FUNCTION sum_cover_fee_earned_during ( + _chain_id uint256, + _cover_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -14,7 +16,10 @@ BEGIN INTO _result FROM policy.cover_purchased - WHERE to_timestamp(policy.cover_purchased.block_timestamp) + WHERE 1 = 1 + AND (_chain_id IS NULL OR policy.cover_purchased.chain_id = _chain_id) + AND (_cover_key IS NULL OR policy.cover_purchased.cover_key = _cover_key) + AND to_timestamp(policy.cover_purchased.block_timestamp) BETWEEN _start AND _end; RETURN COALESCE(_result, 0); @@ -33,26 +38,15 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT - SUM(get_stablecoin_value(policy.cover_purchased.chain_id, policy.cover_purchased.fee)) - INTO - _result - FROM policy.cover_purchased - WHERE policy.cover_purchased.chain_id = _chain_id - AND to_timestamp(policy.cover_purchased.block_timestamp) - BETWEEN _start AND _end; - - RETURN COALESCE(_result, 0); + RETURN sum_cover_fee_earned_during(_chain_id, NULL, _start, _end); END $$ LANGUAGE plpgsql; + CREATE OR REPLACE FUNCTION sum_cover_fee_earned_during ( - _chain_id uint256, - _cover_key bytes32, _start TIMESTAMP WITH TIME ZONE, _end TIMESTAMP WITH TIME ZONE ) @@ -60,19 +54,8 @@ RETURNS numeric STABLE AS $$ - DECLARE _result numeric; BEGIN - SELECT - SUM(get_stablecoin_value(policy.cover_purchased.chain_id, policy.cover_purchased.fee)) - INTO - _result - FROM policy.cover_purchased - WHERE policy.cover_purchased.chain_id = _chain_id - AND policy.cover_purchased.cover_key = _cover_key - AND to_timestamp(policy.cover_purchased.block_timestamp) - BETWEEN _start AND _end; - - RETURN COALESCE(_result, 0); + RETURN sum_cover_fee_earned_during(NULL, NULL, _start, _end); END $$ LANGUAGE plpgsql;