From 55ebf76c37a422bc98363efb6ac7525259df9c80 Mon Sep 17 00:00:00 2001 From: Milli Date: Sat, 4 Jun 2022 22:34:46 +0200 Subject: [PATCH] script to add auto resolution to all markets --- functions/src/scripts/add-auto-resolution.ts | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 functions/src/scripts/add-auto-resolution.ts diff --git a/functions/src/scripts/add-auto-resolution.ts b/functions/src/scripts/add-auto-resolution.ts new file mode 100644 index 00000000..9a4d7310 --- /dev/null +++ b/functions/src/scripts/add-auto-resolution.ts @@ -0,0 +1,56 @@ +import * as admin from 'firebase-admin' + +import { initAdmin } from './script-init' +initAdmin() + +import { getValues } from '../utils' +import { Contract } from '../../../common/contract' +import { DAY_MS } from 'common/util/time' + +const firestore = admin.firestore() + +async function addAutoResolutionToContracts() { + console.log('Adding auto resolution to existing contracts') + + const contracts = await getValues(firestore.collection('contracts')) + + console.log('Loaded', contracts.length, 'contracts') + + for (const contract of contracts.filter((c) => !c.isResolved)) { + addAutoResolutionToContract(contract) + } +} + +async function addAutoResolutionToContract(contract: Contract) { + const contractRef = firestore.doc(`folds/${contract.id}`) + if (contract.autoResolutionTime != null && contract.autoResolution != null) { + console.log('Skipping, already has auto resolution', contract.slug) + return + } + if (contract.autoResolutionTime != null || contract.autoResolution != null) { + console.error( + 'Has partial auto resolution, please check manually', + contract.slug + ) + return + } + if (contract.closeTime == null) { + console.error('Has no close time, please check manually', contract.slug) + return + } + + const autoResolutionTime = + contract.closeTime > Date.now() + ? contract.closeTime + 7 * DAY_MS + : Date.now() + 14 * DAY_MS + + console.log('Adding auto resolution', contract.slug) + + await contractRef.update({ + autoResolution: 'MKT', + autoResolutionTime: autoResolutionTime, + } as Partial) +} + +if (require.main === module) + addAutoResolutionToContracts().then(() => process.exit())