predikt/oracle/backend/scripts/backfill-answer-resolution-times.ts
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

46 lines
1.6 KiB
TypeScript

import { runScript } from 'run-script'
import { tsToMillis } from 'common/supabase/utils'
import { removeUndefinedProps } from 'common/util/object'
import { SafeBulkWriter } from 'shared/safe-bulk-writer'
import { log } from 'shared/monitoring/log'
if (require.main === module) {
runScript(async ({ firestore, pg }) => {
const resolvedAnswers = await pg.map(
`select answers.id, answers.contract_id, contracts.resolution_time, contracts.data->>'resolverId' as resolver_id from answers
join contracts on answers.contract_id = contracts.id
where resolution_time is not null
and answers.data->>'resolutionTime' is null
and mechanism = 'cpmm-multi-1'
`,
[],
(row) => ({
id: row.id as string,
resolutionTime: tsToMillis(row.resolution_time),
contractId: row.contract_id as string,
resolverId: row.resolver_id ? (row.resolver_id as string) : undefined,
})
)
const toUpdate = resolvedAnswers.length
let total = 0
const writer = new SafeBulkWriter(undefined, firestore)
for (const answer of resolvedAnswers) {
writer.update(
firestore.doc(
`contracts/${answer.contractId}/answersCpmm/${answer.id}`
),
removeUndefinedProps({
resolutionTime: answer.resolutionTime,
resolverId: answer.resolverId,
})
)
total++
console.log(`Updated ${total}/${toUpdate} answers`)
}
await writer
.close()
.catch((e) => log.error('Error bulk writing answer updates', e))
.then(() => log('Committed Firestore writes.'))
})
}