share redemption
This commit is contained in:
parent
90888a2cc6
commit
3145966a3c
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,87 +34,92 @@ 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
|
||||||
const userDoc = firestore.doc(`users/${userId}`)
|
.runTransaction(async (transaction) => {
|
||||||
const userSnap = await transaction.get(userDoc)
|
const userDoc = firestore.doc(`users/${userId}`)
|
||||||
if (!userSnap.exists)
|
const userSnap = await transaction.get(userDoc)
|
||||||
return { status: 'error', message: 'User not found' }
|
if (!userSnap.exists)
|
||||||
const user = userSnap.data() as User
|
return { status: 'error', message: 'User not found' }
|
||||||
|
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)
|
if (!contractSnap.exists)
|
||||||
return { status: 'error', message: 'Invalid contract' }
|
|
||||||
const contract = contractSnap.data() as Contract
|
|
||||||
|
|
||||||
const { closeTime, outcomeType, mechanism } = contract
|
|
||||||
if (closeTime && Date.now() > closeTime)
|
|
||||||
return { status: 'error', message: 'Trading is closed' }
|
|
||||||
|
|
||||||
const yourBetsSnap = await transaction.get(
|
|
||||||
contractDoc.collection('bets').where('userId', '==', userId)
|
|
||||||
)
|
|
||||||
const yourBets = yourBetsSnap.docs.map((doc) => doc.data() as Bet)
|
|
||||||
|
|
||||||
const loanAmount = getLoanAmount(yourBets, amount)
|
|
||||||
if (user.balance < amount - loanAmount)
|
|
||||||
return { status: 'error', message: 'Insufficient balance' }
|
|
||||||
|
|
||||||
if (outcomeType === 'FREE_RESPONSE') {
|
|
||||||
const answerSnap = await transaction.get(
|
|
||||||
contractDoc.collection('answers').doc(outcome)
|
|
||||||
)
|
|
||||||
if (!answerSnap.exists)
|
|
||||||
return { status: 'error', message: 'Invalid contract' }
|
return { status: 'error', message: 'Invalid contract' }
|
||||||
}
|
const contract = contractSnap.data() as Contract
|
||||||
|
|
||||||
const newBetDoc = firestore
|
const { closeTime, outcomeType, mechanism } = contract
|
||||||
.collection(`contracts/${contractId}/bets`)
|
if (closeTime && Date.now() > closeTime)
|
||||||
.doc()
|
return { status: 'error', message: 'Trading is closed' }
|
||||||
|
|
||||||
const { newBet, newPool, newTotalShares, newTotalBets, newBalance } =
|
const yourBetsSnap = await transaction.get(
|
||||||
outcomeType === 'BINARY'
|
contractDoc.collection('bets').where('userId', '==', userId)
|
||||||
? mechanism === 'dpm-2'
|
)
|
||||||
? getNewBinaryDpmBetInfo(
|
const yourBets = yourBetsSnap.docs.map((doc) => doc.data() as Bet)
|
||||||
|
|
||||||
|
const loanAmount = getLoanAmount(yourBets, amount)
|
||||||
|
if (user.balance < amount - loanAmount)
|
||||||
|
return { status: 'error', message: 'Insufficient balance' }
|
||||||
|
|
||||||
|
if (outcomeType === 'FREE_RESPONSE') {
|
||||||
|
const answerSnap = await transaction.get(
|
||||||
|
contractDoc.collection('answers').doc(outcome)
|
||||||
|
)
|
||||||
|
if (!answerSnap.exists)
|
||||||
|
return { status: 'error', message: 'Invalid contract' }
|
||||||
|
}
|
||||||
|
|
||||||
|
const newBetDoc = firestore
|
||||||
|
.collection(`contracts/${contractId}/bets`)
|
||||||
|
.doc()
|
||||||
|
|
||||||
|
const { newBet, newPool, newTotalShares, newTotalBets, newBalance } =
|
||||||
|
outcomeType === 'BINARY'
|
||||||
|
? mechanism === 'dpm-2'
|
||||||
|
? getNewBinaryDpmBetInfo(
|
||||||
|
user,
|
||||||
|
outcome as 'YES' | 'NO',
|
||||||
|
amount,
|
||||||
|
contract,
|
||||||
|
loanAmount,
|
||||||
|
newBetDoc.id
|
||||||
|
)
|
||||||
|
: (getNewBinaryCpmmBetInfo(
|
||||||
|
user,
|
||||||
|
outcome as 'YES' | 'NO',
|
||||||
|
amount,
|
||||||
|
contract,
|
||||||
|
loanAmount,
|
||||||
|
newBetDoc.id
|
||||||
|
) as any)
|
||||||
|
: getNewMultiBetInfo(
|
||||||
user,
|
user,
|
||||||
outcome as 'YES' | 'NO',
|
outcome,
|
||||||
amount,
|
amount,
|
||||||
contract,
|
contract as any,
|
||||||
loanAmount,
|
loanAmount,
|
||||||
newBetDoc.id
|
newBetDoc.id
|
||||||
)
|
)
|
||||||
: (getNewBinaryCpmmBetInfo(
|
|
||||||
user,
|
|
||||||
outcome as 'YES' | 'NO',
|
|
||||||
amount,
|
|
||||||
contract,
|
|
||||||
loanAmount,
|
|
||||||
newBetDoc.id
|
|
||||||
) as any)
|
|
||||||
: getNewMultiBetInfo(
|
|
||||||
user,
|
|
||||||
outcome,
|
|
||||||
amount,
|
|
||||||
contract as any,
|
|
||||||
loanAmount,
|
|
||||||
newBetDoc.id
|
|
||||||
)
|
|
||||||
|
|
||||||
transaction.create(newBetDoc, newBet)
|
transaction.create(newBetDoc, newBet)
|
||||||
|
|
||||||
transaction.update(
|
transaction.update(
|
||||||
contractDoc,
|
contractDoc,
|
||||||
removeUndefinedProps({
|
removeUndefinedProps({
|
||||||
pool: newPool,
|
pool: newPool,
|
||||||
totalShares: newTotalShares,
|
totalShares: newTotalShares,
|
||||||
totalBets: newTotalBets,
|
totalBets: newTotalBets,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
transaction.update(userDoc, { balance: newBalance })
|
transaction.update(userDoc, { balance: newBalance })
|
||||||
|
|
||||||
return { status: 'success', betId: newBetDoc.id }
|
return { status: 'success', betId: newBetDoc.id }
|
||||||
})
|
})
|
||||||
|
.then(async (result) => {
|
||||||
|
await redeemShares(userId, contractId)
|
||||||
|
return result
|
||||||
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
81
functions/src/redeem-shares.ts
Normal file
81
functions/src/redeem-shares.ts
Normal 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()
|
Loading…
Reference in New Issue
Block a user