Add hot markets to home feed

This commit is contained in:
James Grugett 2022-02-02 00:40:46 -06:00
parent 75fd870d69
commit f80ef1cd34
4 changed files with 46 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import {
query, query,
onSnapshot, onSnapshot,
where, where,
orderBy,
} from 'firebase/firestore' } from 'firebase/firestore'
import _ from 'lodash' import _ from 'lodash'
@ -23,6 +24,19 @@ export async function listAllBets(contractId: string) {
return bets return bets
} }
const DAY_IN_MS = 24 * 60 * 60 * 1000
// Define "recent" as "<24 hours ago" for now
const recentBetsQuery = query(
collectionGroup(db, 'bets'),
where('createdTime', '>', Date.now() - DAY_IN_MS),
orderBy('createdTime', 'desc')
)
export async function getRecentBets() {
return getValues<Bet>(recentBetsQuery)
}
export function listenForBets( export function listenForBets(
contractId: string, contractId: string,
setBets: (bets: Bet[]) => void setBets: (bets: Bet[]) => void

View File

@ -7,6 +7,7 @@ import { Col } from '../components/layout/col'
import { Bet } from '../../common/bet' import { Bet } from '../../common/bet'
const MAX_ACTIVE_CONTRACTS = 75 const MAX_ACTIVE_CONTRACTS = 75
const MAX_HOT_MARKETS = 10
// This does NOT include comment times, since those aren't part of the contract atm. // This does NOT include comment times, since those aren't part of the contract atm.
// TODO: Maybe store last activity time directly in the contract? // TODO: Maybe store last activity time directly in the contract?
@ -23,9 +24,11 @@ function lastActivityTime(contract: Contract) {
// - Comment on a market // - Comment on a market
// - New market created // - New market created
// - Market resolved // - Market resolved
// - Markets with most betting in last 24 hours
export function findActiveContracts( export function findActiveContracts(
allContracts: Contract[], allContracts: Contract[],
recentComments: Comment[], recentComments: Comment[],
recentBets: Bet[],
daysAgo = 3 daysAgo = 3
) { ) {
const idToActivityTime = new Map<string, number>() const idToActivityTime = new Map<string, number>()
@ -56,6 +59,26 @@ export function findActiveContracts(
} }
} }
// Add recent top-trading contracts, ordered by last bet.
const contractBets = _.groupBy(recentBets, (bet) => bet.contractId)
const contractTotalBets = _.mapValues(contractBets, (bets) =>
_.sumBy(bets, (bet) => bet.amount)
)
const topTradedContracts = _.sortBy(
_.toPairs(contractTotalBets),
([_, total]) => -1 * total
)
.map(([id]) => contractsById.get(id) as Contract)
.slice(0, MAX_HOT_MARKETS)
for (const contract of topTradedContracts) {
const bet = recentBets.find((bet) => bet.contractId === contract.id)
if (bet) {
contracts.push(contract)
record(contract.id, bet.createdTime)
}
}
contracts = _.uniqBy(contracts, (c) => c.id) contracts = _.uniqBy(contracts, (c) => c.id)
contracts = contracts.filter((contract) => contract.visibility === 'public') contracts = contracts.filter((contract) => contract.visibility === 'public')
contracts = _.sortBy(contracts, (c) => -(idToActivityTime.get(c.id) ?? 0)) contracts = _.sortBy(contracts, (c) => -(idToActivityTime.get(c.id) ?? 0))

View File

@ -50,6 +50,7 @@ export async function getStaticProps(props: { params: { slugs: string[] } }) {
let activeContracts = findActiveContracts( let activeContracts = findActiveContracts(
contracts, contracts,
_.flatten(contractComments), _.flatten(contractComments),
[],
365 365
) )
const [resolved, unresolved] = _.partition( const [resolved, unresolved] = _.partition(

View File

@ -10,7 +10,7 @@ import {
Comment, Comment,
listAllComments, listAllComments,
} from '../lib/firebase/comments' } from '../lib/firebase/comments'
import { Bet, listAllBets } from '../lib/firebase/bets' import { Bet, getRecentBets, listAllBets } from '../lib/firebase/bets'
import FeedCreate from '../components/feed-create' import FeedCreate from '../components/feed-create'
import { Spacer } from '../components/layout/spacer' import { Spacer } from '../components/layout/spacer'
import { Col } from '../components/layout/col' import { Col } from '../components/layout/col'
@ -18,12 +18,17 @@ import { useUser } from '../hooks/use-user'
import { useContracts } from '../hooks/use-contracts' import { useContracts } from '../hooks/use-contracts'
export async function getStaticProps() { export async function getStaticProps() {
const [contracts, recentComments] = await Promise.all([ const [contracts, recentComments, recentBets] = await Promise.all([
listAllContracts().catch((_) => []), listAllContracts().catch((_) => []),
getRecentComments().catch(() => []), getRecentComments().catch(() => []),
getRecentBets().catch(() => []),
]) ])
const activeContracts = findActiveContracts(contracts, recentComments) const activeContracts = findActiveContracts(
contracts,
recentComments,
recentBets
)
const activeContractBets = await Promise.all( const activeContractBets = await Promise.all(
activeContracts.map((contract) => listAllBets(contract.id).catch((_) => [])) activeContracts.map((contract) => listAllBets(contract.id).catch((_) => []))
) )