manifold/web/lib/service/create-contract.ts
mantikoros f48ae0170b
Sell bets (#12)
* sell bet

* dev mode

* single-pot no-refund payoff; bet selling

* Increase default fetch size 25 -> 99

* Fix about page numbering

* Don't flash no markets when loading on tag page.

* Change Title to use body font

* Make a bunch of predictions at once (#9)

* Set up a page to make bulk predictions

* Integrate preview into the same card

* List created predictions

* Make changes per James's comments

* Increase the starting balance (#11)

* Remove references to paying for our Mantic Dollars

* Update simulator to use new calculations

* Change simulator random to be evenly random again

* Sell bet UI

* Migrate contracts and bets script

* Add comment to script

* bets => trades; exclude sold bets

* change sale formula

* Change current value to uncapped sell value.

* Disable sell button while selling

* Update some 'bet' to 'trade'

Co-authored-by: Austin Chen <akrolsmir@gmail.com>
Co-authored-by: jahooma <jahooma@gmail.com>
2021-12-24 15:06:01 -06:00

63 lines
1.5 KiB
TypeScript

import {
Contract,
getContractFromSlug,
pushNewContract,
} from '../firebase/contracts'
import { User } from '../firebase/users'
import { randomString } from '../util/random-string'
import { slugify } from '../util/slugify'
// consider moving to cloud function for security
export async function createContract(
question: string,
description: string,
initialProb: number,
creator: User
) {
const proposedSlug = slugify(question).substring(0, 35)
const preexistingContract = await getContractFromSlug(proposedSlug)
const slug = preexistingContract
? proposedSlug + '-' + randomString()
: proposedSlug
const { startYes, startNo } = calcStartPool(initialProb)
const contract: Omit<Contract, 'id'> = {
slug,
outcomeType: 'BINARY',
creatorId: creator.id,
creatorName: creator.name,
creatorUsername: creator.username,
question: question.trim(),
description: description.trim(),
startPool: { YES: startYes, NO: startNo },
pool: { YES: startYes, NO: startNo },
totalShares: { YES: 0, NO: 0 },
isResolved: false,
// TODO: Set create time to Firestore timestamp
createdTime: Date.now(),
lastUpdatedTime: Date.now(),
}
return await pushNewContract(contract)
}
export function calcStartPool(initialProbInt: number, initialCapital = 200) {
const p = initialProbInt / 100.0
const startYes =
p === 0.5
? p * initialCapital
: -(initialCapital * (-p + Math.sqrt((-1 + p) * -p))) / (-1 + 2 * p)
const startNo = initialCapital - startYes
return { startYes, startNo }
}