sell bet
This commit is contained in:
parent
191742c88f
commit
b4bbfa6825
|
@ -5,3 +5,4 @@ admin.initializeApp()
|
||||||
export * from './keep-awake'
|
export * from './keep-awake'
|
||||||
export * from './place-bet'
|
export * from './place-bet'
|
||||||
export * from './resolve-market'
|
export * from './resolve-market'
|
||||||
|
export * from './sell-bet'
|
||||||
|
|
150
functions/src/sell-bet.ts
Normal file
150
functions/src/sell-bet.ts
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
import * as admin from 'firebase-admin'
|
||||||
|
import * as functions from 'firebase-functions'
|
||||||
|
|
||||||
|
import { CREATOR_FEE, PLATFORM_FEE } from './resolve-market'
|
||||||
|
import { Bet } from './types/bet'
|
||||||
|
import { Contract } from './types/contract'
|
||||||
|
import { User } from './types/user'
|
||||||
|
|
||||||
|
export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
|
async (
|
||||||
|
data: {
|
||||||
|
contractId: string
|
||||||
|
betId: string
|
||||||
|
},
|
||||||
|
context
|
||||||
|
) => {
|
||||||
|
const userId = context?.auth?.uid
|
||||||
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
||||||
|
|
||||||
|
const { contractId, betId } = 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' }
|
||||||
|
const contract = contractSnap.data() as Contract
|
||||||
|
|
||||||
|
const betDoc = firestore.doc(`contracts/${contractId}/bets/${betId}`)
|
||||||
|
const betSnap = await transaction.get(betDoc)
|
||||||
|
if (!betSnap.exists)
|
||||||
|
return { status: 'error', message: 'Invalid bet' }
|
||||||
|
const bet = betSnap.data() as Bet
|
||||||
|
|
||||||
|
if (bet.isSold)
|
||||||
|
return { status: 'error', message: 'Bet already sold' }
|
||||||
|
|
||||||
|
const newBetDoc = firestore
|
||||||
|
.collection(`contracts/${contractId}/bets`)
|
||||||
|
.doc()
|
||||||
|
|
||||||
|
const { newBet, newPool, newDpmWeights, newBalance, creatorFee } = getSellBetInfo(
|
||||||
|
user,
|
||||||
|
bet,
|
||||||
|
contract,
|
||||||
|
newBetDoc.id
|
||||||
|
)
|
||||||
|
|
||||||
|
const creatorDoc = firestore.doc(`users/${contract.creatorId}`)
|
||||||
|
const creatorSnap = await transaction.get(creatorDoc)
|
||||||
|
if (creatorSnap.exists) {
|
||||||
|
const creator = creatorSnap.data() as User
|
||||||
|
const creatorNewBalance = creator.balance + creatorFee
|
||||||
|
transaction.update(creatorDoc, { balance: creatorNewBalance })
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.update(betDoc, { isSold: true })
|
||||||
|
transaction.create(newBetDoc, newBet)
|
||||||
|
transaction.update(contractDoc, {
|
||||||
|
pool: newPool,
|
||||||
|
dpmWeights: newDpmWeights,
|
||||||
|
})
|
||||||
|
transaction.update(userDoc, { balance: newBalance })
|
||||||
|
|
||||||
|
return { status: 'success' }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
const getSellBetInfo = (
|
||||||
|
user: User,
|
||||||
|
bet: Bet,
|
||||||
|
contract: Contract,
|
||||||
|
newBetId: string
|
||||||
|
) => {
|
||||||
|
const { id: betId, amount, dpmWeight, outcome } = bet
|
||||||
|
|
||||||
|
const { YES: yesPool, NO: noPool } = contract.pool
|
||||||
|
const { YES: yesWeights, NO: noWeights } = contract.dpmWeights
|
||||||
|
|
||||||
|
// average implied probability after selling bet position
|
||||||
|
const p = outcome === 'YES'
|
||||||
|
? (amount +
|
||||||
|
noPool * Math.atan((yesPool - amount) / noPool) -
|
||||||
|
noPool * Math.atan(yesPool / noPool)) /
|
||||||
|
amount
|
||||||
|
|
||||||
|
: yesPool * (Math.atan((amount - noPool) / yesPool) + Math.atan(noPool / yesPool)) /
|
||||||
|
amount
|
||||||
|
|
||||||
|
|
||||||
|
const [sellYesAmount, sellNoAmount] = outcome === 'YES'
|
||||||
|
? [
|
||||||
|
p * amount,
|
||||||
|
p * dpmWeight / yesWeights * noPool,
|
||||||
|
]
|
||||||
|
: [
|
||||||
|
p * dpmWeight / noWeights * yesPool,
|
||||||
|
p * amount,
|
||||||
|
]
|
||||||
|
|
||||||
|
const newPool = { YES: yesPool - sellYesAmount, NO: noPool - sellNoAmount }
|
||||||
|
|
||||||
|
const newDpmWeights =
|
||||||
|
outcome === 'YES'
|
||||||
|
? { YES: yesWeights - dpmWeight, NO: noWeights }
|
||||||
|
: { YES: yesWeights, NO: noWeights - dpmWeight }
|
||||||
|
|
||||||
|
const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2)
|
||||||
|
const probAverage = p
|
||||||
|
const probAfter = newPool.YES ** 2 / (newPool.YES ** 2 + newPool.NO ** 2)
|
||||||
|
|
||||||
|
const keep = 1 - CREATOR_FEE - PLATFORM_FEE
|
||||||
|
|
||||||
|
const [saleAmount, creatorFee] = outcome === 'YES'
|
||||||
|
? [{ YES: sellYesAmount, NO: keep * sellNoAmount }, CREATOR_FEE * sellNoAmount]
|
||||||
|
: [{ YES: keep * sellYesAmount, NO: sellNoAmount }, CREATOR_FEE * sellYesAmount]
|
||||||
|
|
||||||
|
console.log('SELL M$', amount, outcome, 'at', p, 'prob', 'for M$', saleAmount.YES + saleAmount.NO, 'creator fee: M$', creatorFee)
|
||||||
|
|
||||||
|
const newBet: Bet = {
|
||||||
|
id: newBetId,
|
||||||
|
userId: user.id,
|
||||||
|
contractId: contract.id,
|
||||||
|
amount: -amount,
|
||||||
|
dpmWeight: -dpmWeight,
|
||||||
|
outcome,
|
||||||
|
probBefore,
|
||||||
|
probAverage,
|
||||||
|
probAfter,
|
||||||
|
createdTime: Date.now(),
|
||||||
|
sale: {
|
||||||
|
amount: saleAmount,
|
||||||
|
betId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const newBalance = user.balance + sellYesAmount + sellNoAmount
|
||||||
|
|
||||||
|
return { newBet, newPool, newDpmWeights, newBalance, creatorFee }
|
||||||
|
}
|
|
@ -2,11 +2,21 @@ export type Bet = {
|
||||||
id: string
|
id: string
|
||||||
userId: string
|
userId: string
|
||||||
contractId: string
|
contractId: string
|
||||||
amount: number // Amount of bet
|
|
||||||
outcome: 'YES' | 'NO' // Chosen outcome
|
amount: number // bet size; negative if SELL bet
|
||||||
createdTime: number
|
outcome: 'YES' | 'NO'
|
||||||
|
dpmWeight: number // dynamic parimutuel pool weight; negative if SELL bet
|
||||||
|
|
||||||
probBefore: number
|
probBefore: number
|
||||||
probAverage: number
|
probAverage: number
|
||||||
probAfter: number
|
probAfter: number
|
||||||
dpmWeight: number // Dynamic Parimutuel weight
|
|
||||||
|
sale?: {
|
||||||
|
amount: { YES: number, NO: number } // amount user makes from YES and NO pools from sale
|
||||||
|
betId: string // id of bet being sold
|
||||||
|
}
|
||||||
|
|
||||||
|
isSold?: boolean // true if this BUY bet has been sold
|
||||||
|
|
||||||
|
createdTime: number
|
||||||
}
|
}
|
|
@ -17,6 +17,7 @@ import {
|
||||||
resolvedPayout,
|
resolvedPayout,
|
||||||
} from '../lib/calculation/contract'
|
} from '../lib/calculation/contract'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
|
import { cloudFunction } from '../lib/firebase/api-call'
|
||||||
|
|
||||||
export function BetsList(props: { user: User }) {
|
export function BetsList(props: { user: User }) {
|
||||||
const { user } = props
|
const { user } = props
|
||||||
|
@ -240,6 +241,7 @@ export function ContractBetsTable(props: {
|
||||||
<th>Probability</th>
|
<th>Probability</th>
|
||||||
{!isResolved && <th>Est. max payout</th>}
|
{!isResolved && <th>Est. max payout</th>}
|
||||||
<th>{isResolved ? <>Payout</> : <>Current value</>}</th>
|
<th>{isResolved ? <>Payout</> : <>Current value</>}</th>
|
||||||
|
{!isResolved && <th></th>}
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
@ -254,7 +256,7 @@ export function ContractBetsTable(props: {
|
||||||
|
|
||||||
function BetRow(props: { bet: Bet; contract: Contract }) {
|
function BetRow(props: { bet: Bet; contract: Contract }) {
|
||||||
const { bet, contract } = props
|
const { bet, contract } = props
|
||||||
const { amount, outcome, createdTime, probBefore, probAfter, dpmWeight } = bet
|
const { amount, outcome, createdTime, probBefore, probAfter, dpmWeight, sale, isSold } = bet
|
||||||
const { isResolved } = contract
|
const { isResolved } = contract
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -275,10 +277,21 @@ function BetRow(props: { bet: Bet; contract: Contract }) {
|
||||||
: currentValue(contract, bet)
|
: currentValue(contract, bet)
|
||||||
)}
|
)}
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
|
{!isResolved && !sale && !isSold &&
|
||||||
|
<td>
|
||||||
|
<button className='btn' onClick={async e => {
|
||||||
|
e.preventDefault()
|
||||||
|
await sellBet({ contractId: contract.id, betId: bet.id })
|
||||||
|
}}>Sell</button>
|
||||||
|
</td>
|
||||||
|
}
|
||||||
</tr>
|
</tr>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sellBet = cloudFunction('sellBet')
|
||||||
|
|
||||||
function OutcomeLabel(props: { outcome: 'YES' | 'NO' | 'CANCEL' }) {
|
function OutcomeLabel(props: { outcome: 'YES' | 'NO' | 'CANCEL' }) {
|
||||||
const { outcome } = props
|
const { outcome } = props
|
||||||
|
|
||||||
|
|
6
web/lib/firebase/api-call.ts
Normal file
6
web/lib/firebase/api-call.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { getFunctions, httpsCallable } from 'firebase/functions'
|
||||||
|
|
||||||
|
const functions = getFunctions()
|
||||||
|
|
||||||
|
export const cloudFunction = (name: string) => httpsCallable(functions, name)
|
||||||
|
|
|
@ -11,12 +11,22 @@ export type Bet = {
|
||||||
id: string
|
id: string
|
||||||
userId: string
|
userId: string
|
||||||
contractId: string
|
contractId: string
|
||||||
amount: number // Amount of bet
|
|
||||||
outcome: 'YES' | 'NO' // Chosen outcome
|
amount: number // bet size; negative if SELL bet
|
||||||
dpmWeight: number // Dynamic Parimutuel weight
|
outcome: 'YES' | 'NO'
|
||||||
|
dpmWeight: number // dynamic parimutuel pool weight; negative if SELL bet
|
||||||
|
|
||||||
probBefore: number
|
probBefore: number
|
||||||
probAverage: number
|
probAverage: number
|
||||||
probAfter: number
|
probAfter: number
|
||||||
|
|
||||||
|
sale?: {
|
||||||
|
amount: { YES: number, NO: number } // amount user makes from YES and NO pools from sale
|
||||||
|
betId: string // id of bet being sold
|
||||||
|
}
|
||||||
|
|
||||||
|
isSold?: boolean // true if this BUY bet has been sold
|
||||||
|
|
||||||
createdTime: number
|
createdTime: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,11 @@ const formatter = new Intl.NumberFormat('en-US', {
|
||||||
})
|
})
|
||||||
|
|
||||||
export function formatMoney(amount: number) {
|
export function formatMoney(amount: number) {
|
||||||
return 'M$ ' + formatter.format(amount).substring(1)
|
return 'M$ ' + formatter.format(amount).replace('$', '')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatWithCommas(amount: number) {
|
export function formatWithCommas(amount: number) {
|
||||||
return formatter.format(amount).substring(1)
|
return formatter.format(amount).replace('$', '')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatPercent(zeroToOne: number) {
|
export function formatPercent(zeroToOne: number) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user