share redemption

This commit is contained in:
mantikoros 2022-03-09 11:35:11 -06:00
parent 90888a2cc6
commit 3145966a3c
3 changed files with 156 additions and 68 deletions

View File

@ -20,6 +20,7 @@ export type Bet = {
isSold?: boolean // true if this BUY bet has been sold isSold?: boolean // true if this BUY bet has been sold
isAnte?: boolean isAnte?: boolean
isLiquidityProvision?: boolean isLiquidityProvision?: boolean
isRedemption?: boolean
createdTime: number createdTime: number
} }

View File

@ -11,6 +11,7 @@ import {
} from '../../common/new-bet' } from '../../common/new-bet'
import { removeUndefinedProps } from '../../common/util/object' import { removeUndefinedProps } from '../../common/util/object'
import { Bet } from '../../common/bet' import { Bet } from '../../common/bet'
import { redeemShares } from './redeem-shares'
export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
async ( async (
@ -33,7 +34,8 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
return { status: 'error', message: 'Invalid outcome' } return { status: 'error', message: 'Invalid outcome' }
// run as transaction to prevent race conditions // run as transaction to prevent race conditions
return await firestore.runTransaction(async (transaction) => { return await firestore
.runTransaction(async (transaction) => {
const userDoc = firestore.doc(`users/${userId}`) const userDoc = firestore.doc(`users/${userId}`)
const userSnap = await transaction.get(userDoc) const userSnap = await transaction.get(userDoc)
if (!userSnap.exists) if (!userSnap.exists)
@ -114,6 +116,10 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
return { status: 'success', betId: newBetDoc.id } return { status: 'success', betId: newBetDoc.id }
}) })
.then(async (result) => {
await redeemShares(userId, contractId)
return result
})
} }
) )

View File

@ -0,0 +1,81 @@
import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { Bet } from '../../common/bet'
import { getProbability } from '../../common/calculate'
import { Binary, CPMM, FullContract } from '../../common/contract'
import { User } from '../../common/user'
export const redeemShares = async (userId: string, contractId: string) => {
return await firestore.runTransaction(async (transaction) => {
const contractDoc = firestore.doc(`contracts/${contractId}`)
const contractSnap = await transaction.get(contractDoc)
if (!contractSnap.exists)
return { status: 'error', message: 'Invalid contract' }
const contract = contractSnap.data() as FullContract<CPMM, Binary>
if (contract.outcomeType !== 'BINARY' || contract.mechanism !== 'cpmm-1')
return { status: 'success' }
const betsSnap = await transaction.get(
firestore
.collection(`contracts/${contract.id}/bets`)
.where('userId', '==', userId)
)
const bets = betsSnap.docs.map((doc) => doc.data() as Bet)
const [yesBets, noBets] = _.partition(bets, (b) => b.outcome === 'YES')
const yesShares = _.sumBy(yesBets, (b) => b.shares)
const noShares = _.sumBy(noBets, (b) => b.shares)
const amount = Math.min(yesShares, noShares)
if (amount <= 0) return
const p = getProbability(contract)
const createdTime = Date.now()
const yesDoc = firestore.collection(`contracts/${contract.id}/bets`).doc()
const yesBet: Bet = {
id: yesDoc.id,
userId: userId,
contractId: contract.id,
amount: p * -amount,
shares: -amount,
outcome: 'YES',
probBefore: p,
probAfter: p,
createdTime,
isRedemption: true,
}
const noDoc = firestore.collection(`contracts/${contract.id}/bets`).doc()
const noBet: Bet = {
id: noDoc.id,
userId: userId,
contractId: contract.id,
amount: (1 - p) * -amount,
shares: -amount,
outcome: 'NO',
probBefore: p,
probAfter: p,
createdTime,
isRedemption: true,
}
const userDoc = firestore.doc(`users/${userId}`)
const userSnap = await transaction.get(userDoc)
if (!userSnap.exists) return { status: 'error', message: 'User not found' }
const user = userSnap.data() as User
const newBalance = user.balance + amount
transaction.update(userDoc, { balance: newBalance })
transaction.create(yesDoc, yesBet)
transaction.create(noDoc, noBet)
return { status: 'success' }
})
}
const firestore = admin.firestore()