Merge branch 'main' into fast-fold-following
This commit is contained in:
commit
c5218cff98
|
@ -24,17 +24,15 @@ export const updateUserMetrics = functions.pubsub
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
users.map(async (user) => {
|
users.map(async (user) => {
|
||||||
const investmentValue = await computeInvestmentValue(
|
const [investmentValue, creatorVolume] = await Promise.all([
|
||||||
user,
|
computeInvestmentValue(user, contractsDict),
|
||||||
contractsDict
|
computeTotalPool(user, contractsDict),
|
||||||
)
|
])
|
||||||
const totalValue = user.balance + investmentValue
|
|
||||||
|
|
||||||
|
const totalValue = user.balance + investmentValue
|
||||||
const totalPnL = totalValue - user.totalDeposits
|
const totalPnL = totalValue - user.totalDeposits
|
||||||
|
|
||||||
const creatorVolume = await computeTotalVolume(user, contractsDict)
|
await firestore.collection('users').doc(user.id).update({
|
||||||
|
|
||||||
return firestore.collection('users').doc(user.id).update({
|
|
||||||
totalPnLCached: totalPnL,
|
totalPnLCached: totalPnL,
|
||||||
creatorVolumeCached: creatorVolume,
|
creatorVolumeCached: creatorVolume,
|
||||||
})
|
})
|
||||||
|
@ -58,15 +56,17 @@ const computeInvestmentValue = async (
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const computeTotalVolume = async (
|
const computeTotalPool = async (
|
||||||
user: User,
|
user: User,
|
||||||
contractsDict: _.Dictionary<Contract>
|
contractsDict: _.Dictionary<Contract>
|
||||||
) => {
|
) => {
|
||||||
const creatorContracts = Object.values(contractsDict).filter(
|
const creatorContracts = Object.values(contractsDict).filter(
|
||||||
(contract) => contract.creatorId === user.id
|
(contract) => contract.creatorId === user.id
|
||||||
)
|
)
|
||||||
const volumes = await Promise.all(creatorContracts.map(computeVolume))
|
const pools = creatorContracts.map((contract) =>
|
||||||
return _.sum(volumes)
|
_.sum(Object.values(contract.pool))
|
||||||
|
)
|
||||||
|
return _.sum(pools)
|
||||||
}
|
}
|
||||||
|
|
||||||
const computeVolume = async (contract: Contract) => {
|
const computeVolume = async (contract: Contract) => {
|
||||||
|
|
|
@ -350,6 +350,7 @@ function AnswerBetPanel(props: {
|
||||||
|
|
||||||
function CreateAnswerInput(props: { contract: Contract }) {
|
function CreateAnswerInput(props: { contract: Contract }) {
|
||||||
const { contract } = props
|
const { contract } = props
|
||||||
|
const user = useUser()
|
||||||
const [text, setText] = useState('')
|
const [text, setText] = useState('')
|
||||||
const [betAmount, setBetAmount] = useState<number | undefined>(10)
|
const [betAmount, setBetAmount] = useState<number | undefined>(10)
|
||||||
const [amountError, setAmountError] = useState<string | undefined>()
|
const [amountError, setAmountError] = useState<string | undefined>()
|
||||||
|
@ -451,17 +452,28 @@ function CreateAnswerInput(props: { contract: Contract }) {
|
||||||
</Col>
|
</Col>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<button
|
{user ? (
|
||||||
className={clsx(
|
<button
|
||||||
'btn self-end mt-2',
|
className={clsx(
|
||||||
canSubmit ? 'btn-outline' : 'btn-disabled',
|
'btn self-end mt-2',
|
||||||
isSubmitting && 'loading'
|
canSubmit ? 'btn-outline' : 'btn-disabled',
|
||||||
)}
|
isSubmitting && 'loading'
|
||||||
disabled={!canSubmit}
|
)}
|
||||||
onClick={submitAnswer}
|
disabled={!canSubmit}
|
||||||
>
|
onClick={submitAnswer}
|
||||||
Submit answer & bet
|
>
|
||||||
</button>
|
Submit answer & bet
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
text && (
|
||||||
|
<button
|
||||||
|
className="btn self-end whitespace-nowrap border-none bg-gradient-to-r from-teal-500 to-green-500 px-10 text-lg font-medium normal-case hover:from-teal-600 hover:to-green-600"
|
||||||
|
onClick={firebaseLogin}
|
||||||
|
>
|
||||||
|
Sign in
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
)}
|
||||||
</Col>
|
</Col>
|
||||||
</Col>
|
</Col>
|
||||||
</Col>
|
</Col>
|
||||||
|
|
|
@ -5,8 +5,8 @@ import _ from 'lodash'
|
||||||
import { Contract, listAllContracts } from '../lib/firebase/contracts'
|
import { Contract, listAllContracts } from '../lib/firebase/contracts'
|
||||||
import { Page } from '../components/page'
|
import { Page } from '../components/page'
|
||||||
import { ActivityFeed, findActiveContracts } from './activity'
|
import { ActivityFeed, findActiveContracts } from './activity'
|
||||||
import { Comment, listAllComments } from '../lib/firebase/comments'
|
import { Comment, getRecentComments } from '../lib/firebase/comments'
|
||||||
import { Bet, listAllBets } from '../lib/firebase/bets'
|
import { Bet, getRecentBets } 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'
|
||||||
|
@ -28,16 +28,16 @@ export async function getStaticProps() {
|
||||||
listAllFolds().catch(() => []),
|
listAllFolds().catch(() => []),
|
||||||
])
|
])
|
||||||
|
|
||||||
const [contractBets, contractComments] = await Promise.all([
|
const [recentBets, recentComments] = await Promise.all([
|
||||||
Promise.all(contracts.map((contract) => listAllBets(contract.id))),
|
getRecentBets(),
|
||||||
Promise.all(contracts.map((contract) => listAllComments(contract.id))),
|
getRecentComments(),
|
||||||
])
|
])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
contracts,
|
contracts,
|
||||||
contractBets,
|
recentBets,
|
||||||
contractComments,
|
recentComments,
|
||||||
folds,
|
folds,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -47,18 +47,15 @@ export async function getStaticProps() {
|
||||||
|
|
||||||
const Home = (props: {
|
const Home = (props: {
|
||||||
contracts: Contract[]
|
contracts: Contract[]
|
||||||
contractBets: Bet[][]
|
|
||||||
contractComments: Comment[][]
|
|
||||||
folds: Fold[]
|
folds: Fold[]
|
||||||
|
recentBets: Bet[]
|
||||||
|
recentComments: Comment[]
|
||||||
}) => {
|
}) => {
|
||||||
const { contractBets, contractComments, folds } = props
|
const { folds, recentBets, recentComments } = props
|
||||||
|
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
|
|
||||||
const contracts = useUpdatedContracts(props.contracts)
|
const contracts = useUpdatedContracts(props.contracts)
|
||||||
const contractIdToIndex = _.fromPairs(
|
|
||||||
contracts.map((contract, index) => [contract.id, index])
|
|
||||||
)
|
|
||||||
|
|
||||||
const followedFoldIds = useFollowedFolds(user)
|
const followedFoldIds = useFollowedFolds(user)
|
||||||
const followedFolds = filterDefined(
|
const followedFolds = filterDefined(
|
||||||
|
@ -86,29 +83,25 @@ const Home = (props: {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const oneDayMS = 24 * 60 * 60 * 1000
|
const activeContracts = findActiveContracts(
|
||||||
const recentBets = (feedContracts ?? [])
|
feedContracts,
|
||||||
.map((c) => contractBets[contractIdToIndex[c.id]])
|
recentComments,
|
||||||
.flat()
|
recentBets,
|
||||||
.filter((bet) => bet.createdTime > Date.now() - oneDayMS)
|
365
|
||||||
const feedComments = (feedContracts ?? [])
|
)
|
||||||
.map((c) => contractComments[contractIdToIndex[c.id]])
|
|
||||||
.flat()
|
|
||||||
|
|
||||||
const activeContracts =
|
const betsByContract = _.groupBy(recentBets, (bet) => bet.contractId)
|
||||||
feedContracts &&
|
const activeBets = activeContracts.map(
|
||||||
findActiveContracts(feedContracts, feedComments, recentBets, 365)
|
(contract) => betsByContract[contract.id] ?? []
|
||||||
|
)
|
||||||
|
|
||||||
const activeBets = activeContracts
|
const commentsByContract = _.groupBy(
|
||||||
? activeContracts.map(
|
recentComments,
|
||||||
(contract) => contractBets[contractIdToIndex[contract.id]]
|
(comment) => comment.contractId
|
||||||
)
|
)
|
||||||
: []
|
const activeComments = activeContracts.map(
|
||||||
const activeComments = activeContracts
|
(contract) => commentsByContract[contract.id] ?? []
|
||||||
? activeContracts.map(
|
)
|
||||||
(contract) => contractComments[contractIdToIndex[contract.id]]
|
|
||||||
)
|
|
||||||
: []
|
|
||||||
|
|
||||||
if (user === null) {
|
if (user === null) {
|
||||||
Router.replace('/')
|
Router.replace('/')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user