Skip to content

Commit

Permalink
Calculate payouts with contract metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Dec 5, 2024
1 parent 64b13c6 commit 510aefa
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 242 deletions.
13 changes: 3 additions & 10 deletions backend/api/src/request-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from 'shared/supabase/init'
import { createLoanIncomeNotification } from 'shared/create-notification'
import { getUser, log } from 'shared/utils'
import { PortfolioMetrics } from 'common/portfolio-metrics'
import { getUserLoanUpdates, isUserEligibleForLoan } from 'common/loans'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
Expand All @@ -18,6 +17,7 @@ import { runTxnFromBank } from 'shared/txn/run-txn'
import { filterDefined } from 'common/util/array'
import { getUnresolvedContractMetricsContractsAnswers } from 'shared/update-user-portfolio-histories-core'
import { keyBy } from 'lodash'
import { convertPortfolioHistory } from 'common/supabase/portfolio-metrics'

export const requestLoan: APIHandler<'request-loan'> = async (_, auth) => {
const pg = createSupabaseDirectClient()
Expand Down Expand Up @@ -92,18 +92,11 @@ export const getNextLoanAmountResults = async (userId: string) => {
const pg = createSupabaseDirectClient()

const portfolioMetric = await pg.oneOrNone(
`select user_id, ts, investment_value, balance, total_deposits
`select *
from user_portfolio_history_latest
where user_id = $1`,
[userId],
(r) =>
({
userId: r.user_id as string,
timestamp: Date.parse(r.ts as string),
investmentValue: parseFloat(r.investment_value as string),
balance: parseFloat(r.balance as string),
totalDeposits: parseFloat(r.total_deposits as string),
} as PortfolioMetrics & { userId: string })
convertPortfolioHistory
)
if (!portfolioMetric) {
throw new APIError(404, `No portfolio found for user ${userId}`)
Expand Down
18 changes: 9 additions & 9 deletions backend/api/src/unresolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,21 @@ const undoResolution = async (
} else if (answerId) {
const answer = await pg.one(
`
with last_bet as (
select prob_after from contract_bets
where answer_id = $1
and contract_id = $2
order by created_time desc
limit 1
)
update answers
set
resolution = null,
resolution_time = null,
resolution_probability = null,
prob = coalesce(last_bet.prob_after,0.5),
prob = coalesce(
(select prob_after
from contract_bets
where answer_id = $1
and contract_id = $2
order by created_time desc
limit 1),
0.5
),
resolver_id = null
from last_bet
where id = $1
returning *`,
[answerId, contractId],
Expand Down
8 changes: 4 additions & 4 deletions backend/shared/src/create-notification.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getContractBetMetrics } from 'common/calculate'
import {
BetFillData,
BetReplyNotificationData,
Expand Down Expand Up @@ -91,6 +90,7 @@ import {
answerToMidpoint,
} from 'common/multi-numeric'
import { floatingEqual } from 'common/util/math'
import { ContractMetric } from 'common/contract-metric'

type recipients_to_reason_texts = {
[userId: string]: { reason: notification_reason_types }
Expand Down Expand Up @@ -1213,8 +1213,8 @@ export const createContractResolvedNotifications = async (
resolutionValue: number | undefined,
answerId: string | undefined,
resolutionData: {
userIdToContractMetrics: {
[userId: string]: ReturnType<typeof getContractBetMetrics>
userIdToContractMetric: {
[userId: string]: Omit<ContractMetric, 'id'>
}
userPayouts: { [userId: string]: number }
creatorPayout: number
Expand Down Expand Up @@ -1261,7 +1261,7 @@ export const createContractResolvedNotifications = async (
const bulkPushNotifications: [PrivateUser, Notification, string, string][] =
[]
const {
userIdToContractMetrics,
userIdToContractMetric: userIdToContractMetrics,
userPayouts,
creatorPayout,
resolutionProbability,
Expand Down
3 changes: 3 additions & 0 deletions backend/shared/src/helpers/user-contract-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export async function bulkUpdateContractMetrics(
export function bulkUpdateContractMetricsQuery(
metrics: Omit<ContractMetric, 'id'>[]
) {
if (metrics.length === 0) {
return 'select 1 where false'
}
return bulkUpsertQuery(
'user_contract_metrics',
[],
Expand Down
Loading

0 comments on commit 510aefa

Please sign in to comment.