2022-05-22 08:36:05 +00:00
|
|
|
import { partition, sumBy } from 'lodash'
|
2022-03-29 19:56:56 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
|
2022-06-01 02:42:35 +00:00
|
|
|
import { BinaryContract } from '../../common/contract'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { getCpmmSellBetInfo } from '../../common/sell-bet'
|
|
|
|
import { addObjects, removeUndefinedProps } from '../../common/util/object'
|
2022-03-29 19:56:56 +00:00
|
|
|
import { getValues } from './utils'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Bet } from '../../common/bet'
|
2022-03-29 19:56:56 +00:00
|
|
|
|
|
|
|
export const sellShares = functions.runWith({ minInstances: 1 }).https.onCall(
|
|
|
|
async (
|
|
|
|
data: {
|
|
|
|
contractId: string
|
|
|
|
shares: number
|
|
|
|
outcome: 'YES' | 'NO'
|
|
|
|
},
|
|
|
|
context
|
|
|
|
) => {
|
|
|
|
const userId = context?.auth?.uid
|
|
|
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
|
|
|
|
|
|
|
const { contractId, shares, outcome } = data
|
|
|
|
|
|
|
|
// Run as transaction to prevent race conditions.
|
|
|
|
return await firestore.runTransaction(async (transaction) => {
|
|
|
|
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 contractDoc = firestore.doc(`contracts/${contractId}`)
|
|
|
|
const contractSnap = await transaction.get(contractDoc)
|
|
|
|
if (!contractSnap.exists)
|
|
|
|
return { status: 'error', message: 'Invalid contract' }
|
2022-06-01 02:42:35 +00:00
|
|
|
const contract = contractSnap.data() as BinaryContract
|
2022-03-29 19:56:56 +00:00
|
|
|
const { closeTime, mechanism, collectedFees, volume } = contract
|
|
|
|
|
|
|
|
if (mechanism !== 'cpmm-1')
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: 'Sell shares only works with mechanism cpmm-1',
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closeTime && Date.now() > closeTime)
|
|
|
|
return { status: 'error', message: 'Trading is closed' }
|
|
|
|
|
|
|
|
const userBets = await getValues<Bet>(
|
|
|
|
contractDoc.collection('bets').where('userId', '==', userId)
|
|
|
|
)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const prevLoanAmount = sumBy(userBets, (bet) => bet.loanAmount ?? 0)
|
2022-03-31 05:54:37 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const [yesBets, noBets] = partition(
|
2022-03-29 19:56:56 +00:00
|
|
|
userBets ?? [],
|
|
|
|
(bet) => bet.outcome === 'YES'
|
|
|
|
)
|
|
|
|
const [yesShares, noShares] = [
|
2022-05-22 08:36:05 +00:00
|
|
|
sumBy(yesBets, (bet) => bet.shares),
|
|
|
|
sumBy(noBets, (bet) => bet.shares),
|
2022-03-29 19:56:56 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
const maxShares = outcome === 'YES' ? yesShares : noShares
|
|
|
|
if (shares > maxShares + 0.000000000001) {
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: `You can only sell ${maxShares} shares`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const newBetDoc = firestore
|
|
|
|
.collection(`contracts/${contractId}/bets`)
|
|
|
|
.doc()
|
|
|
|
|
|
|
|
const { newBet, newPool, newP, newBalance, fees } = getCpmmSellBetInfo(
|
|
|
|
user,
|
|
|
|
shares,
|
|
|
|
outcome,
|
|
|
|
contract,
|
2022-03-31 05:54:37 +00:00
|
|
|
prevLoanAmount,
|
2022-03-29 19:56:56 +00:00
|
|
|
newBetDoc.id
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!isFinite(newP)) {
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: 'Trade rejected due to overflow error.',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isFinite(newBalance)) {
|
|
|
|
throw new Error('Invalid user balance for ' + user.username)
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.update(userDoc, { balance: newBalance })
|
|
|
|
transaction.create(newBetDoc, newBet)
|
|
|
|
transaction.update(
|
|
|
|
contractDoc,
|
|
|
|
removeUndefinedProps({
|
|
|
|
pool: newPool,
|
|
|
|
p: newP,
|
2022-05-27 21:02:02 +00:00
|
|
|
collectedFees: addObjects(fees, collectedFees),
|
2022-03-29 19:56:56 +00:00
|
|
|
volume: volume + Math.abs(newBet.amount),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
return { status: 'success' }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|