Add profit of bets made within last week
This commit is contained in:
parent
46fab105d9
commit
cd7ddae133
|
@ -3,7 +3,6 @@ import * as admin from 'firebase-admin'
|
||||||
|
|
||||||
import { Contract, CPMMContract } from '../../common/contract'
|
import { Contract, CPMMContract } from '../../common/contract'
|
||||||
import {
|
import {
|
||||||
getAllPrivateUsers,
|
|
||||||
getPrivateUser,
|
getPrivateUser,
|
||||||
getUser,
|
getUser,
|
||||||
getValue,
|
getValue,
|
||||||
|
@ -20,6 +19,7 @@ import { sendWeeklyPortfolioUpdateEmail } from './emails'
|
||||||
import { contractUrl } from './utils'
|
import { contractUrl } from './utils'
|
||||||
import { Txn } from '../../common/txn'
|
import { Txn } from '../../common/txn'
|
||||||
import { formatMoney } from '../../common/util/format'
|
import { formatMoney } from '../../common/util/format'
|
||||||
|
import { getContractBetMetrics } from '../../common/calculate'
|
||||||
|
|
||||||
// TODO: reset weeklyPortfolioUpdateEmailSent to false for all users at the start of each week
|
// TODO: reset weeklyPortfolioUpdateEmailSent to false for all users at the start of each week
|
||||||
export const weeklyPortfolioUpdateEmails = functions
|
export const weeklyPortfolioUpdateEmails = functions
|
||||||
|
@ -36,12 +36,12 @@ const firestore = admin.firestore()
|
||||||
export async function sendPortfolioUpdateEmailsToAllUsers() {
|
export async function sendPortfolioUpdateEmailsToAllUsers() {
|
||||||
const privateUsers = isProd()
|
const privateUsers = isProd()
|
||||||
? // ian & stephen's ids
|
? // ian & stephen's ids
|
||||||
// ? filterDefined([
|
filterDefined([
|
||||||
// await getPrivateUser('AJwLWoo3xue32XIiAVrL5SyR1WB2'),
|
await getPrivateUser('AJwLWoo3xue32XIiAVrL5SyR1WB2'),
|
||||||
// await getPrivateUser('tlmGNz9kjXc2EteizMORes4qvWl2'),
|
// await getPrivateUser('tlmGNz9kjXc2EteizMORes4qvWl2'),
|
||||||
// ])
|
])
|
||||||
await getAllPrivateUsers()
|
: // await getAllPrivateUsers()
|
||||||
: filterDefined([await getPrivateUser('6hHpzvRG0pMq8PNJs7RZj2qlZGn2')])
|
filterDefined([await getPrivateUser('6hHpzvRG0pMq8PNJs7RZj2qlZGn2')])
|
||||||
// get all users that haven't unsubscribed from weekly emails
|
// get all users that haven't unsubscribed from weekly emails
|
||||||
const privateUsersToSendEmailsTo = privateUsers
|
const privateUsersToSendEmailsTo = privateUsers
|
||||||
.filter((user) => {
|
.filter((user) => {
|
||||||
|
@ -165,28 +165,42 @@ export async function sendPortfolioUpdateEmailsToAllUsers() {
|
||||||
const bets = userBets.filter(
|
const bets = userBets.filter(
|
||||||
(bet) => bet.contractId === contract.id
|
(bet) => bet.contractId === contract.id
|
||||||
)
|
)
|
||||||
|
const previousBets = bets.filter(
|
||||||
|
(b) => b.createdTime < Date.now() - 7 * DAY_MS
|
||||||
|
)
|
||||||
|
|
||||||
|
const betsInLastWeek = bets.filter(
|
||||||
|
(b) => b.createdTime >= Date.now() - 7 * DAY_MS
|
||||||
|
)
|
||||||
|
|
||||||
const marketProbabilityAWeekAgo =
|
const marketProbabilityAWeekAgo =
|
||||||
cpmmContract.prob - cpmmContract.probChanges.week
|
cpmmContract.prob - cpmmContract.probChanges.week
|
||||||
const currentMarketProbability = cpmmContract.resolutionProbability
|
const currentMarketProbability = cpmmContract.resolutionProbability
|
||||||
? cpmmContract.resolutionProbability
|
? cpmmContract.resolutionProbability
|
||||||
: cpmmContract.prob
|
: cpmmContract.prob
|
||||||
const betsValueAWeekAgo = computeInvestmentValueCustomProb(
|
const betsMadeAWeekAgoValue = computeInvestmentValueCustomProb(
|
||||||
bets.filter((b) => b.createdTime < Date.now() - 7 * DAY_MS),
|
previousBets,
|
||||||
contract,
|
contract,
|
||||||
marketProbabilityAWeekAgo
|
marketProbabilityAWeekAgo
|
||||||
)
|
)
|
||||||
const currentBetsValue = computeInvestmentValueCustomProb(
|
const currentBetsMadeAWeekAgoValue =
|
||||||
bets,
|
computeInvestmentValueCustomProb(
|
||||||
|
previousBets,
|
||||||
contract,
|
contract,
|
||||||
currentMarketProbability
|
currentMarketProbability
|
||||||
)
|
)
|
||||||
|
const betsMadeInLastWeekProfit = getContractBetMetrics(
|
||||||
|
contract,
|
||||||
|
betsInLastWeek
|
||||||
|
).profit
|
||||||
const marketChange =
|
const marketChange =
|
||||||
currentMarketProbability - marketProbabilityAWeekAgo
|
currentMarketProbability - marketProbabilityAWeekAgo
|
||||||
return {
|
return {
|
||||||
currentValue: currentBetsValue,
|
currentValue: currentBetsMadeAWeekAgoValue,
|
||||||
pastValue: betsValueAWeekAgo,
|
pastValue: betsMadeAWeekAgoValue,
|
||||||
difference: currentBetsValue - betsValueAWeekAgo,
|
difference:
|
||||||
|
betsMadeInLastWeekProfit +
|
||||||
|
(currentBetsMadeAWeekAgoValue - betsMadeAWeekAgoValue),
|
||||||
contractSlug: contract.slug,
|
contractSlug: contract.slug,
|
||||||
marketProbAWeekAgo: marketProbabilityAWeekAgo,
|
marketProbAWeekAgo: marketProbabilityAWeekAgo,
|
||||||
questionTitle: contract.question,
|
questionTitle: contract.question,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user