Skip to content

Commit

Permalink
Ingawei/man 1823 shift market tiers down a level (#2933)
Browse files Browse the repository at this point in the history
  • Loading branch information
ingawei authored Oct 21, 2024
1 parent b695eba commit 48f8099
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 112 deletions.
7 changes: 1 addition & 6 deletions backend/scripts/backfill-tiers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// We have many old contracts without a collectedFees data structure. Let's fill them in.

import { MarketContract } from 'common/contract'
import { getTierFromLiquidity } from 'common/tier'
import * as admin from 'firebase-admin'
Expand All @@ -13,10 +11,7 @@ if (require.main === module) {
contractsRef.get().then(async (contractsSnaps) => {
console.log(`Loaded ${contractsSnaps.size} contracts.`)
const needsFilling = contractsSnaps.docs.filter((ct) => {
return (
!('marketTier' in ct.data()) &&
'totalLiquidity' in ct.data()
)
return !('marketTier' in ct.data()) && 'totalLiquidity' in ct.data()
})
console.log(`Found ${needsFilling.length} contracts to update.`)
await Promise.all(
Expand Down
51 changes: 51 additions & 0 deletions backend/scripts/shift-tiers/backfill-tier-column.sql
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 backend/scripts/shift-tiers/get-tier-from-liquidity-function.sql
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;
2 changes: 1 addition & 1 deletion common/src/economy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const getTieredCost = (
? baseCost * 10 ** (tiers.indexOf(tier) - 1)
: baseCost

if (outcomeType == 'NUMBER' && tier != 'basic' && tier != 'play') {
if (outcomeType == 'NUMBER' && tier != 'play') {
return tieredCost / 10
}

Expand Down
2 changes: 1 addition & 1 deletion common/src/tier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MarketContract } from './contract'
import { getAnte, getTieredCost } from './economy'

// Array of tiers in order
export const tiers = ['play', 'basic', 'plus', 'premium', 'crystal'] as const
export const tiers = ['play', 'plus', 'premium', 'crystal'] as const

export type BinaryDigit = '0' | '1'

Expand Down
64 changes: 22 additions & 42 deletions web/components/contract/contract-table-col-formats.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ChatIcon, UserIcon } from '@heroicons/react/solid'
import { Contract } from 'common/contract'
import { SWEEPIES_MARKET_TOOLTIP } from 'common/envs/constants'
import { useNumContractComments } from 'web/hooks/use-comments'
import { shortenNumber } from 'web/lib/util/formatNumber'
import { SweepiesCoin } from 'web/public/custom-components/sweepiesCoin'
import { Row } from '../layout/row'
import { TierTooltip } from '../tiers/tier-tooltip'
import { Tooltip } from '../widgets/tooltip'
import { Action } from './contract-table-action'
import { ContractStatusLabel } from './contracts-table'
import { Tooltip } from '../widgets/tooltip'
import { SWEEPIES_MARKET_TOOLTIP } from 'common/envs/constants'
import { SweepiesCoin } from 'web/public/custom-components/sweepiesCoin'
import { TierTooltip } from '../tiers/tier-tooltip'

export type ColumnFormat = {
header: string
Expand Down Expand Up @@ -37,44 +37,6 @@ export const traderColumn = {
width: 'w-[90px]',
}

export const coinOrTierColumn = {
header: 'Coin/Tier',
content: (contract: Contract) => {
const { marketTier, token } = contract
return (
<>
{token == 'CASH' && (
<span>
<Tooltip
text={SWEEPIES_MARKET_TOOLTIP}
className="relative inline-flex h-[1em] w-[1.1em] items-baseline"
>
<SweepiesCoin className="absolute inset-0 top-[0.2em]" />
</Tooltip>
</span>
)}

{!!marketTier &&
marketTier !== 'play' &&
marketTier !== 'basic' &&
token != 'CASH' && (
<span>
<TierTooltip
placement={'top'}
tier={marketTier}
contract={contract}
noTitle
className="relative mr-0.5 inline-flex h-[1em] w-[1.1em] items-baseline"
iconClassName="absolute inset-0 top-[0.2em]"
/>
</span>
)}
</>
)
},
width: 'w-[10px]',
}

export const probColumn = {
header: 'Stat',
content: (contract: Contract) => (
Expand All @@ -92,6 +54,24 @@ export const probColumn = {
width: 'w-[80px]',
}

export const tierColumn = {
header: 'Tier',
content: (contract: Contract) => {
const marketTier = contract.marketTier
return (
<TierTooltip
placement={'top'}
tier={marketTier!}
contract={contract}
noTitle
className="relative mr-0.5 inline-flex h-[1em] w-[1.1em] items-baseline"
iconClassName="absolute inset-0 top-[0.2em]"
/>
)
},
width: 'w-8',
}

export const actionColumn = {
header: 'Action',
content: (contract: Contract) => <Action contract={contract} />,
Expand Down
38 changes: 34 additions & 4 deletions web/components/contract/contracts-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { EyeOffIcon } from '@heroicons/react/solid'
import clsx from 'clsx'
import { getDisplayProbability } from 'common/calculate'
import { Contract, contractPath } from 'common/contract'
import { ENV_CONFIG } from 'common/envs/constants'
import {
ENV_CONFIG,
SPICE_MARKET_TOOLTIP,
SWEEPIES_MARKET_TOOLTIP,
} from 'common/envs/constants'
import { getFormattedMappedValue } from 'common/pseudo-numeric'
import { formatMoney, formatPercentShort } from 'common/util/format'
import Link from 'next/link'
Expand All @@ -19,12 +23,15 @@ import {
probColumn,
traderColumn,
ColumnFormat,
coinOrTierColumn,
tierColumn,
} from './contract-table-col-formats'
import { UserHovercard } from '../user/user-hovercard'
import { getFormattedExpectedValue } from 'common/multi-numeric'
import { removeEmojis } from 'common/util/string'
import { track } from 'web/lib/service/analytics'
import { Tooltip } from '../widgets/tooltip'
import { SpiceCoin } from 'web/public/custom-components/spiceCoin'
import { SweepiesCoin } from 'web/public/custom-components/sweepiesCoin'

export function ContractsTable(props: {
contracts: Contract[]
Expand All @@ -37,7 +44,7 @@ export function ContractsTable(props: {
contracts,
onContractClick,
highlightContractIds,
columns = [coinOrTierColumn, traderColumn, probColumn, actionColumn],
columns = [traderColumn, tierColumn, probColumn, actionColumn],
hideAvatar,
} = props

Expand Down Expand Up @@ -263,7 +270,30 @@ function ContractQuestion(props: {
/>
</UserHovercard>
)}
<span>{removeEmojis(contract.question)}</span>
<span>
{/* <VisibilityIcon contract={contract} /> */}
{contract.token == 'CASH' && (
<span>
<Tooltip
text={SWEEPIES_MARKET_TOOLTIP}
className=" relative mr-0.5 inline-flex h-[1em] w-[1.1em] items-baseline"
>
<SweepiesCoin className="absolute inset-0 top-[0.2em]" />
</Tooltip>
</span>
)}
{!!contract.isSpicePayout && (
<span>
<Tooltip
text={SPICE_MARKET_TOOLTIP}
className=" relative mr-0.5 inline-flex h-[1em] w-[1.1em] items-baseline"
>
<SpiceCoin className="absolute inset-0 top-[0.2em]" />
</Tooltip>
</span>
)}
{removeEmojis(contract.question)}
</span>
</Row>
)
}
Expand Down
2 changes: 1 addition & 1 deletion web/components/contract/feed-contract-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export function FeedContractCard(props: {
Ad {adSecondsLeft ? adSecondsLeft + 's' : ''}
</div>
)}
{marketTier && marketTier !== 'basic' ? (
{marketTier ? (
<TierTooltip tier={marketTier} contract={contract} />
) : feedReason ? (
<CardReason
Expand Down
4 changes: 2 additions & 2 deletions web/components/contract/upgrade-tier-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ function UpgradeTier(props: {
<Col
className={clsx(
currentAmount == additionalAmount
? tier == 'basic'
? tier == 'play'
? 'outline-ink-500'
: tier == 'plus'
? 'outline-blue-500'
: tier == 'premium'
? 'outline-purple-400'
: 'outline-pink-500'
: tier == 'basic'
: tier == 'play'
? 'hover:outline-ink-500/50 opacity-50 outline-transparent'
: tier == 'plus'
? 'opacity-50 outline-transparent hover:outline-purple-500/50'
Expand Down
6 changes: 3 additions & 3 deletions web/components/new-contract/contract-params-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ export function ContractParamsForm(props: {
outcomeType
)
? undefined
: outcomeType == 'NUMBER'
? 'plus'
: 'basic'
: 'plus'

const [marketTier, setMarketTier] = useState<MarketTierType | undefined>(
DEFAULT_TIER
)

const paramsKey =
(params?.q ?? '') +
(params?.groupSlugs?.join('') ?? '') +
Expand Down
Loading

0 comments on commit 48f8099

Please sign in to comment.