Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo contract resolution #1515

Merged
merged 4 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions backend/functions/src/api/resolve-market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { runTxn, TxnData } from 'shared/run-txn'
import { ContractResolutionPayoutTxn } from 'common/txn'
import { runContractPayoutTxn } from 'shared/run-txn'

export const payUsersTransactions = async (
const payUsersTransactions = async (
payouts: {
userId: string
payout: number
Expand All @@ -64,9 +64,10 @@ export const payUsersTransactions = async (
toId: userId,
amount: payout,
token: 'M$',
data: { deposit: deposit ?? 0 },
description: 'Contract payout for resolution: ' + contractId,
} as ContractResolutionPayoutTxn
runContractPayoutTxn(transaction, payoutTxn, deposit ?? 0)
runContractPayoutTxn(transaction, payoutTxn)
})
})
}
Expand Down
51 changes: 51 additions & 0 deletions backend/scripts/undo-resolution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as admin from 'firebase-admin'

import { initAdmin } from 'shared/init-admin'
initAdmin()
import { ContractResolutionPayoutTxn } from 'common/txn'
import { chunk } from 'lodash'
import { undoContractPayoutTxn } from 'shared/run-txn'

const firestore = admin.firestore()

const undoResolution = async (contractId: string) => {
const txns = await firestore
.collection('txns')
.where('category', '==', 'CONTRACT_RESOLUTION_PAYOUT')
.where('fromType', '==', 'CONTRACT')
.where('fromId', '==', contractId)
.get()
.then((snapshot) =>
snapshot.docs.map((doc) => doc.data() as ContractResolutionPayoutTxn)
)
console.log('reverting txns', txns.length)
const chunkedTxns = chunk(txns, 250)
for (const chunk of chunkedTxns) {
await firestore.runTransaction(async (transaction) => {
for (const txn of chunk) {
undoContractPayoutTxn(transaction, txn)
}
})
}
console.log('reverted txns')

await firestore.doc(`contracts/${contractId}`).update({
isResolved: false,
IanPhilips marked this conversation as resolved.
Show resolved Hide resolved
resolutionTime: admin.firestore.FieldValue.delete(),
resolution: admin.firestore.FieldValue.delete(),
resolutionProbability: admin.firestore.FieldValue.delete(),
})
console.log('updated contract')
}

if (require.main === module) {
const contractId = process.argv[2]
if (!contractId) {
console.log('Usage: ts-node undo-resolution.ts <contractId>')
process.exit()
}
console.log('reverting resolution for contract:', contractId)
undoResolution(contractId)
.then(() => process.exit())
.catch(console.log)
}
33 changes: 28 additions & 5 deletions backend/shared/src/run-txn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,43 @@ export async function runTxn(

export function runContractPayoutTxn(
fbTransaction: admin.firestore.Transaction,
data: Omit<ContractResolutionPayoutTxn, 'id' | 'createdTime'>,
deposit: number
txnData: Omit<ContractResolutionPayoutTxn, 'id' | 'createdTime'>
) {
const { amount, toId } = data
const { amount, toId, data } = txnData
const { deposit } = data
const toDoc = firestore.doc(`users/${toId}`)
fbTransaction.update(toDoc, {
balance: FieldValue.increment(amount),
totalDeposits: FieldValue.increment(deposit),
totalDeposits: FieldValue.increment(deposit ?? 0),
})

const newTxnDoc = firestore.collection(`txns/`).doc()
const txn = { id: newTxnDoc.id, createdTime: Date.now(), ...data }
const txn = { id: newTxnDoc.id, createdTime: Date.now(), ...txnData }
fbTransaction.create(newTxnDoc, removeUndefinedProps(txn))

return { status: 'success', txn }
}

export function undoContractPayoutTxn(
fbTransaction: admin.firestore.Transaction,
txnData: ContractResolutionPayoutTxn
) {
const { amount, toId, id, data } = txnData
const { deposit } = data ?? {}
const toDoc = firestore.doc(`users/${toId}`)
fbTransaction.update(toDoc, {
balance: FieldValue.increment(-amount),
totalDeposits: FieldValue.increment(-(deposit ?? 0)),
})
const txnDoc = firestore.doc(`txns/${id}`)

fbTransaction.update(txnDoc, {
data: {
...(data ?? {}),
IanPhilips marked this conversation as resolved.
Show resolved Hide resolved
reverted: true,
},
})

return { status: 'success', data: txnData }
}
const firestore = admin.firestore()
4 changes: 4 additions & 0 deletions common/src/txn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ type ContractResolutionPayout = {
toType: 'USER'
category: 'CONTRACT_RESOLUTION_PAYOUT'
token: 'M$'
data: {
reverted?: boolean
deposit?: number
}
}

type QfId = {
Expand Down