Don't run share redemption after adding liquidity (#631)

This commit is contained in:
Marshall Polaris 2022-07-12 12:36:31 -07:00 committed by GitHub
parent 43b30e6d04
commit 5fd42df1ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,6 @@ import { z } from 'zod'
import { Contract } from '../../common/contract' import { Contract } from '../../common/contract'
import { User } from '../../common/user' import { User } from '../../common/user'
import { removeUndefinedProps } from '../../common/util/object' import { removeUndefinedProps } from '../../common/util/object'
import { redeemShares } from './redeem-shares'
import { getNewLiquidityProvision } from '../../common/add-liquidity' import { getNewLiquidityProvision } from '../../common/add-liquidity'
import { APIError, newEndpoint, validate } from './api' import { APIError, newEndpoint, validate } from './api'
@ -19,78 +18,73 @@ export const addliquidity = newEndpoint({}, async (req, auth) => {
if (!isFinite(amount)) throw new APIError(400, 'Invalid amount') if (!isFinite(amount)) throw new APIError(400, 'Invalid amount')
// run as transaction to prevent race conditions // run as transaction to prevent race conditions
return await firestore return await firestore.runTransaction(async (transaction) => {
.runTransaction(async (transaction) => { const userDoc = firestore.doc(`users/${auth.uid}`)
const userDoc = firestore.doc(`users/${auth.uid}`) const userSnap = await transaction.get(userDoc)
const userSnap = await transaction.get(userDoc) if (!userSnap.exists) throw new APIError(400, 'User not found')
if (!userSnap.exists) throw new APIError(400, 'User not found') const user = userSnap.data() as User
const user = userSnap.data() as User
const contractDoc = firestore.doc(`contracts/${contractId}`) const contractDoc = firestore.doc(`contracts/${contractId}`)
const contractSnap = await transaction.get(contractDoc) const contractSnap = await transaction.get(contractDoc)
if (!contractSnap.exists) throw new APIError(400, 'Invalid contract') if (!contractSnap.exists) throw new APIError(400, 'Invalid contract')
const contract = contractSnap.data() as Contract const contract = contractSnap.data() as Contract
if ( if (
contract.mechanism !== 'cpmm-1' || contract.mechanism !== 'cpmm-1' ||
(contract.outcomeType !== 'BINARY' && (contract.outcomeType !== 'BINARY' &&
contract.outcomeType !== 'PSEUDO_NUMERIC') contract.outcomeType !== 'PSEUDO_NUMERIC')
) )
throw new APIError(400, 'Invalid contract') throw new APIError(400, 'Invalid contract')
const { closeTime } = contract const { closeTime } = contract
if (closeTime && Date.now() > closeTime) if (closeTime && Date.now() > closeTime)
throw new APIError(400, 'Trading is closed') throw new APIError(400, 'Trading is closed')
if (user.balance < amount) throw new APIError(400, 'Insufficient balance') if (user.balance < amount) throw new APIError(400, 'Insufficient balance')
const newLiquidityProvisionDoc = firestore const newLiquidityProvisionDoc = firestore
.collection(`contracts/${contractId}/liquidity`) .collection(`contracts/${contractId}/liquidity`)
.doc() .doc()
const { newLiquidityProvision, newPool, newP, newTotalLiquidity } = const { newLiquidityProvision, newPool, newP, newTotalLiquidity } =
getNewLiquidityProvision( getNewLiquidityProvision(
user, user,
amount, amount,
contract, contract,
newLiquidityProvisionDoc.id newLiquidityProvisionDoc.id
)
if (newP !== undefined && !isFinite(newP)) {
return {
status: 'error',
message: 'Liquidity injection rejected due to overflow error.',
}
}
transaction.update(
contractDoc,
removeUndefinedProps({
pool: newPool,
p: newP,
totalLiquidity: newTotalLiquidity,
})
) )
const newBalance = user.balance - amount if (newP !== undefined && !isFinite(newP)) {
const newTotalDeposits = user.totalDeposits - amount return {
status: 'error',
if (!isFinite(newBalance)) { message: 'Liquidity injection rejected due to overflow error.',
throw new APIError(500, 'Invalid user balance for ' + user.username)
} }
}
transaction.update(userDoc, { transaction.update(
balance: newBalance, contractDoc,
totalDeposits: newTotalDeposits, removeUndefinedProps({
pool: newPool,
p: newP,
totalLiquidity: newTotalLiquidity,
}) })
)
transaction.create(newLiquidityProvisionDoc, newLiquidityProvision) const newBalance = user.balance - amount
const newTotalDeposits = user.totalDeposits - amount
return newLiquidityProvision if (!isFinite(newBalance)) {
}) throw new APIError(500, 'Invalid user balance for ' + user.username)
.then(async (result) => { }
await redeemShares(auth.uid, contractId)
return result transaction.update(userDoc, {
balance: newBalance,
totalDeposits: newTotalDeposits,
}) })
transaction.create(newLiquidityProvisionDoc, newLiquidityProvision)
return newLiquidityProvision
})
}) })
const firestore = admin.firestore() const firestore = admin.firestore()