2022-04-21 17:58:12 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
2022-07-08 22:08:17 +00:00
|
|
|
import { z } from 'zod'
|
2022-04-21 17:58:12 +00:00
|
|
|
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { removeUndefinedProps } from '../../common/util/object'
|
|
|
|
import { getNewLiquidityProvision } from '../../common/add-liquidity'
|
2022-07-08 22:08:17 +00:00
|
|
|
import { APIError, newEndpoint, validate } from './api'
|
|
|
|
|
|
|
|
const bodySchema = z.object({
|
|
|
|
contractId: z.string(),
|
|
|
|
amount: z.number().gt(0),
|
|
|
|
})
|
|
|
|
|
|
|
|
export const addliquidity = newEndpoint({}, async (req, auth) => {
|
|
|
|
const { amount, contractId } = validate(bodySchema, req.body)
|
|
|
|
|
|
|
|
if (!isFinite(amount)) throw new APIError(400, 'Invalid amount')
|
|
|
|
|
|
|
|
// run as transaction to prevent race conditions
|
2022-07-12 19:36:31 +00:00
|
|
|
return await firestore.runTransaction(async (transaction) => {
|
|
|
|
const userDoc = firestore.doc(`users/${auth.uid}`)
|
|
|
|
const userSnap = await transaction.get(userDoc)
|
|
|
|
if (!userSnap.exists) throw new APIError(400, 'User not found')
|
|
|
|
const user = userSnap.data() as User
|
|
|
|
|
|
|
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
|
|
|
const contractSnap = await transaction.get(contractDoc)
|
|
|
|
if (!contractSnap.exists) throw new APIError(400, 'Invalid contract')
|
|
|
|
const contract = contractSnap.data() as Contract
|
|
|
|
if (
|
|
|
|
contract.mechanism !== 'cpmm-1' ||
|
|
|
|
(contract.outcomeType !== 'BINARY' &&
|
|
|
|
contract.outcomeType !== 'PSEUDO_NUMERIC')
|
|
|
|
)
|
|
|
|
throw new APIError(400, 'Invalid contract')
|
|
|
|
|
|
|
|
const { closeTime } = contract
|
|
|
|
if (closeTime && Date.now() > closeTime)
|
|
|
|
throw new APIError(400, 'Trading is closed')
|
|
|
|
|
|
|
|
if (user.balance < amount) throw new APIError(400, 'Insufficient balance')
|
|
|
|
|
|
|
|
const newLiquidityProvisionDoc = firestore
|
|
|
|
.collection(`contracts/${contractId}/liquidity`)
|
|
|
|
.doc()
|
|
|
|
|
|
|
|
const { newLiquidityProvision, newPool, newP, newTotalLiquidity } =
|
|
|
|
getNewLiquidityProvision(
|
|
|
|
user,
|
|
|
|
amount,
|
|
|
|
contract,
|
|
|
|
newLiquidityProvisionDoc.id
|
2022-07-08 22:08:17 +00:00
|
|
|
)
|
2022-04-21 17:58:12 +00:00
|
|
|
|
2022-07-12 19:36:31 +00:00
|
|
|
if (newP !== undefined && !isFinite(newP)) {
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: 'Liquidity injection rejected due to overflow error.',
|
2022-07-08 22:08:17 +00:00
|
|
|
}
|
2022-07-12 19:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
transaction.update(
|
|
|
|
contractDoc,
|
|
|
|
removeUndefinedProps({
|
|
|
|
pool: newPool,
|
|
|
|
p: newP,
|
|
|
|
totalLiquidity: newTotalLiquidity,
|
2022-04-21 17:58:12 +00:00
|
|
|
})
|
2022-07-12 19:36:31 +00:00
|
|
|
)
|
2022-07-08 22:08:17 +00:00
|
|
|
|
2022-07-12 19:36:31 +00:00
|
|
|
const newBalance = user.balance - amount
|
|
|
|
const newTotalDeposits = user.totalDeposits - amount
|
2022-07-08 22:08:17 +00:00
|
|
|
|
2022-07-12 19:36:31 +00:00
|
|
|
if (!isFinite(newBalance)) {
|
|
|
|
throw new APIError(500, 'Invalid user balance for ' + user.username)
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.update(userDoc, {
|
|
|
|
balance: newBalance,
|
|
|
|
totalDeposits: newTotalDeposits,
|
2022-07-08 22:08:17 +00:00
|
|
|
})
|
2022-07-12 19:36:31 +00:00
|
|
|
|
|
|
|
transaction.create(newLiquidityProvisionDoc, newLiquidityProvision)
|
|
|
|
|
|
|
|
return newLiquidityProvision
|
|
|
|
})
|
2022-07-08 22:08:17 +00:00
|
|
|
})
|
2022-04-21 17:58:12 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|