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