From e2dc4c6b8f1cf6d73af4b964f4d78abf05e806bc Mon Sep 17 00:00:00 2001 From: James Grugett Date: Wed, 12 Oct 2022 17:30:19 -0500 Subject: [PATCH] Resolve markets again script --- functions/src/resolve-market.ts | 23 ++++++-- .../src/scripts/resolve-markets-again.ts | 59 +++++++++++++++++++ 2 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 functions/src/scripts/resolve-markets-again.ts diff --git a/functions/src/resolve-market.ts b/functions/src/resolve-market.ts index f29ff124..347bf055 100644 --- a/functions/src/resolve-market.ts +++ b/functions/src/resolve-market.ts @@ -36,6 +36,7 @@ import { DEV_HOUSE_LIQUIDITY_PROVIDER_ID, HOUSE_LIQUIDITY_PROVIDER_ID, } from '../../common/antes' +import { User } from 'common/user' const bodySchema = z.object({ contractId: z.string(), @@ -89,13 +90,10 @@ export const resolvemarket = newEndpoint(opts, async (req, auth) => { if (!contractSnap.exists) throw new APIError(404, 'No contract exists with the provided ID') const contract = contractSnap.data() as Contract - const { creatorId, closeTime } = contract + const { creatorId } = contract const firebaseUser = await admin.auth().getUser(auth.uid) - const { value, resolutions, probabilityInt, outcome } = getResolutionParams( - contract, - req.body - ) + const resolutionParams = getResolutionParams(contract, req.body) if ( creatorId !== auth.uid && @@ -109,6 +107,16 @@ export const resolvemarket = newEndpoint(opts, async (req, auth) => { const creator = await getUser(creatorId) if (!creator) throw new APIError(500, 'Creator not found') + return await resolveMarket(contract, creator, resolutionParams) +}) + +export const resolveMarket = async ( + contract: Contract, + creator: User, + { value, resolutions, probabilityInt, outcome }: ResolutionParams +) => { + const { creatorId, closeTime, id: contractId } = contract + const resolutionProbability = probabilityInt !== undefined ? probabilityInt / 100 : undefined @@ -183,6 +191,7 @@ export const resolvemarket = newEndpoint(opts, async (req, auth) => { ) const userCount = uniqBy(payouts, 'userId').length + const contractDoc = firestore.doc(`contracts/${contractId}`) if (userCount <= 499) { await firestore.runTransaction(async (transaction) => { @@ -226,7 +235,7 @@ export const resolvemarket = newEndpoint(opts, async (req, auth) => { ) return updatedContract -}) +} function getResolutionParams(contract: Contract, body: string) { const { outcomeType } = contract @@ -292,6 +301,8 @@ function getResolutionParams(contract: Contract, body: string) { throw new APIError(500, `Invalid outcome type: ${outcomeType}`) } +type ResolutionParams = ReturnType + function validateAnswer( contract: FreeResponseContract | MultipleChoiceContract, answer: number diff --git a/functions/src/scripts/resolve-markets-again.ts b/functions/src/scripts/resolve-markets-again.ts new file mode 100644 index 00000000..c1ff3156 --- /dev/null +++ b/functions/src/scripts/resolve-markets-again.ts @@ -0,0 +1,59 @@ +import { initAdmin } from './script-init' +initAdmin() + +import { zip } from 'lodash' +import { filterDefined } from 'common/util/array' +import { resolveMarket } from '../resolve-market' +import { getContract, getUser } from '../utils' + +if (require.main === module) { + const contractIds = process.argv.slice(2) + if (contractIds.length === 0) { + throw new Error('No contract ids provided') + } + resolveMarketsAgain(contractIds).then(() => process.exit(0)) +} + +async function resolveMarketsAgain(contractIds: string[]) { + const maybeContracts = await Promise.all(contractIds.map(getContract)) + if (maybeContracts.some((c) => !c)) { + throw new Error('Invalid contract id') + } + const contracts = filterDefined(maybeContracts) + + const maybeCreators = await Promise.all( + contracts.map((c) => getUser(c.creatorId)) + ) + if (maybeCreators.some((c) => !c)) { + throw new Error('No creator found') + } + const creators = filterDefined(maybeCreators) + + if ( + !contracts.every((c) => c.resolution === 'YES' || c.resolution === 'NO') + ) { + throw new Error('Only YES or NO resolutions supported') + } + + const resolutionParams = contracts.map((c) => ({ + outcome: c.resolution as string, + value: undefined, + probabilityInt: undefined, + resolutions: undefined, + })) + + const params = zip(contracts, creators, resolutionParams) + + for (const [contract, creator, resolutionParams] of params) { + if (contract && creator && resolutionParams) { + console.log('Resolving', contract.question) + try { + await resolveMarket(contract, creator, resolutionParams) + } catch (e) { + console.log(e) + } + } + } + + console.log(`Resolved all contracts.`) +}