-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ingawei/man 1823 shift market tiers down a level (#2933)
- Loading branch information
Showing
15 changed files
with
171 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-- Create a function to update contracts in batches | ||
drop function if exists update_contract_tiers_efficient(batch_size INTEGER, start_time TIMESTAMP ); | ||
create | ||
or replace function update_contract_tiers_efficient (batch_size integer, start_time timestamp) returns table ( | ||
updated_count integer, | ||
last_updated_time timestamp | ||
) as $$ | ||
DECLARE | ||
batch_updated INTEGER; | ||
last_updated_timestamp TIMESTAMP; | ||
BEGIN | ||
WITH to_update AS ( | ||
SELECT | ||
id, | ||
created_time, | ||
get_tier_from_liquidity_efficient( | ||
outcome_type, | ||
CASE WHEN data ? 'answers' THEN jsonb_array_length(data->'answers') ELSE NULL END, | ||
COALESCE((data->>'totalLiquidity')::NUMERIC, 0) | ||
) AS new_tier | ||
FROM contracts | ||
WHERE created_time > start_time | ||
AND (data->>'totalLiquidity' IS NOT NULL) | ||
ORDER BY created_time | ||
LIMIT batch_size | ||
) | ||
UPDATE contracts c | ||
SET data = c.data || jsonb_build_object('marketTier', tu.new_tier) | ||
FROM to_update tu | ||
WHERE c.id = tu.id | ||
AND (c.tier IS DISTINCT FROM tu.new_tier OR c.tier IS NULL); | ||
|
||
GET DIAGNOSTICS batch_updated = ROW_COUNT; | ||
|
||
IF batch_updated > 0 THEN | ||
SELECT created_time INTO last_updated_timestamp | ||
FROM contracts | ||
WHERE created_time > start_time | ||
ORDER BY created_time | ||
LIMIT 1 OFFSET (batch_size - 1); | ||
ELSE | ||
last_updated_timestamp := start_time; | ||
END IF; | ||
|
||
RETURN QUERY SELECT batch_updated, last_updated_timestamp; | ||
END; | ||
$$ language plpgsql; | ||
|
||
-- Commit the transaction to save the functions | ||
commit; | ||
|
48 changes: 48 additions & 0 deletions
48
backend/scripts/shift-tiers/get-tier-from-liquidity-function.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
-- Create a more efficient function to get the tier from liquidity | ||
create | ||
or replace function get_tier_from_liquidity_efficient ( | ||
outcome_type text, | ||
num_answers integer, | ||
liquidity numeric | ||
) returns text as $$ | ||
DECLARE | ||
ante NUMERIC; | ||
tier_liquidity NUMERIC; | ||
fixed_ante CONSTANT NUMERIC := 1000; | ||
multiple_choice_minimum_cost CONSTANT NUMERIC := 1000; | ||
BEGIN | ||
IF outcome_type IN ('POLL', 'BOUNTIED_QUESTION') THEN | ||
RETURN NULL; | ||
END IF; | ||
|
||
ante := CASE outcome_type | ||
WHEN 'BINARY' THEN fixed_ante | ||
WHEN 'MULTIPLE_CHOICE' THEN GREATEST(fixed_ante / 10 * COALESCE(num_answers, 0), multiple_choice_minimum_cost) | ||
WHEN 'FREE_RESPONSE' THEN fixed_ante / 10 | ||
WHEN 'PSEUDO_NUMERIC' THEN fixed_ante * 2.5 | ||
WHEN 'STONK' THEN fixed_ante | ||
WHEN 'NUMBER' THEN fixed_ante * 10 | ||
ELSE fixed_ante | ||
END; | ||
|
||
-- Special handling for NUMBER type | ||
IF outcome_type = 'NUMBER' THEN | ||
IF liquidity >= (ante * 10) THEN | ||
RETURN 'crystal'; | ||
ELSIF liquidity >= ante THEN | ||
RETURN 'premium'; | ||
ELSIF liquidity >= (ante / 10) THEN | ||
RETURN 'plus'; | ||
ELSE | ||
RETURN 'play'; | ||
END IF; | ||
END IF; | ||
|
||
-- For other types | ||
IF liquidity >= ante * 100 THEN RETURN 'crystal'; | ||
ELSIF liquidity >= ante * 10 THEN RETURN 'premium'; | ||
ELSIF liquidity >= ante THEN RETURN 'plus'; | ||
ELSE RETURN 'play'; | ||
END IF; | ||
END; | ||
$$ language plpgsql immutable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.