Update resolve-market to be a v2 function
This commit is contained in:
parent
c1765ca0cb
commit
c2038e07e9
|
@ -6,7 +6,6 @@ admin.initializeApp()
|
|||
// export * from './keep-awake'
|
||||
export * from './claim-manalink'
|
||||
export * from './transact'
|
||||
export * from './resolve-market'
|
||||
export * from './stripe'
|
||||
export * from './create-user'
|
||||
export * from './create-answer'
|
||||
|
@ -37,3 +36,4 @@ export * from './sell-shares'
|
|||
export * from './create-contract'
|
||||
export * from './withdraw-liquidity'
|
||||
export * from './create-group'
|
||||
export * from './resolve-market'
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import * as functions from 'firebase-functions'
|
||||
import * as admin from 'firebase-admin'
|
||||
import { z } from 'zod'
|
||||
import { difference, uniq, mapValues, groupBy, sumBy } from 'lodash'
|
||||
|
||||
import { Contract, resolution, RESOLUTIONS } from '../../common/contract'
|
||||
import { Contract, RESOLUTIONS } from '../../common/contract'
|
||||
import { User } from '../../common/user'
|
||||
import { Bet } from '../../common/bet'
|
||||
import { getUser, isProd, payUser } from './utils'
|
||||
|
@ -15,25 +15,24 @@ import {
|
|||
} from '../../common/payouts'
|
||||
import { removeUndefinedProps } from '../../common/util/object'
|
||||
import { LiquidityProvision } from '../../common/liquidity-provision'
|
||||
import { newEndpoint, validate } from './api'
|
||||
|
||||
export const resolveMarket = functions
|
||||
.runWith({ minInstances: 1, secrets: ['MAILGUN_KEY'] })
|
||||
.https.onCall(
|
||||
async (
|
||||
data: {
|
||||
outcome: resolution
|
||||
value?: number
|
||||
contractId: string
|
||||
probabilityInt?: number
|
||||
resolutions?: { [outcome: string]: number }
|
||||
},
|
||||
context
|
||||
) => {
|
||||
const userId = context?.auth?.uid
|
||||
const bodySchema = z.object({
|
||||
outcome: z.enum(RESOLUTIONS),
|
||||
value: z.number().optional(),
|
||||
contractId: z.string(),
|
||||
probabilityInt: z.number().gte(0).lt(100).optional(),
|
||||
resolutions: z.map(z.string(), z.number()).optional(),
|
||||
})
|
||||
|
||||
export const resolvemarket = newEndpoint(['POST'], async (req, auth) => {
|
||||
const { outcome, value, contractId, probabilityInt, resolutions } = validate(
|
||||
bodySchema,
|
||||
req.body
|
||||
)
|
||||
const userId = auth.uid
|
||||
if (!userId) return { status: 'error', message: 'Not authorized' }
|
||||
|
||||
const { outcome, contractId, probabilityInt, resolutions, value } = data
|
||||
|
||||
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||
const contractSnap = await contractDoc.get()
|
||||
if (!contractSnap.exists)
|
||||
|
@ -64,9 +63,7 @@ export const resolveMarket = functions
|
|||
if (
|
||||
outcomeType === 'BINARY' &&
|
||||
probabilityInt !== undefined &&
|
||||
(probabilityInt < 0 ||
|
||||
probabilityInt > 100 ||
|
||||
!isFinite(probabilityInt))
|
||||
(probabilityInt < 0 || probabilityInt > 100 || !isFinite(probabilityInt))
|
||||
)
|
||||
return { status: 'error', message: 'Invalid probability' }
|
||||
|
||||
|
@ -104,7 +101,7 @@ export const resolveMarket = functions
|
|||
const { payouts, creatorPayout, liquidityPayouts, collectedFees } =
|
||||
getPayouts(
|
||||
outcome,
|
||||
resolutions ?? {},
|
||||
Object.fromEntries(resolutions || []),
|
||||
contract,
|
||||
bets,
|
||||
liquidities,
|
||||
|
@ -139,10 +136,7 @@ export const resolveMarket = functions
|
|||
)
|
||||
|
||||
if (creatorPayout)
|
||||
await processPayouts(
|
||||
[{ userId: creatorId, payout: creatorPayout }],
|
||||
true
|
||||
)
|
||||
await processPayouts([{ userId: creatorId, payout: creatorPayout }], true)
|
||||
|
||||
await processPayouts(liquidityPayouts, true)
|
||||
|
||||
|
@ -158,12 +152,11 @@ export const resolveMarket = functions
|
|||
contract,
|
||||
outcome,
|
||||
resolutionProbability,
|
||||
resolutions
|
||||
Object.fromEntries(resolutions || [])
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const processPayouts = async (payouts: Payout[], isDeposit = false) => {
|
||||
const userPayouts = groupPayoutsByUser(payouts)
|
||||
|
|
Loading…
Reference in New Issue
Block a user