/
add-house-subsidy.ts
79 lines (66 loc) · 2.16 KB
/
add-house-subsidy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as admin from 'firebase-admin'
import { FieldValue } from 'firebase-admin/firestore'
import { CPMMContract, CPMMMultiContract } from 'common/contract'
import { getNewLiquidityProvision } from 'common/add-liquidity'
import { APIError } from 'common/api/utils'
import { runTxnFromBank } from 'shared/txn/run-txn'
const firestore = admin.firestore()
export const addHouseSubsidy = (contractId: string, amount: number) => {
return firestore.runTransaction(async (transaction) => {
const contractDoc = firestore.doc(`contracts/${contractId}`)
const snap = await transaction.get(contractDoc)
const contract = snap.data() as CPMMContract | CPMMMultiContract
const { newTotalLiquidity, newSubsidyPool } = getNewLiquidityProvision(
amount,
contract
)
const { status, message, txn } = await runTxnFromBank(transaction, {
fromType: 'BANK',
amount,
toId: contractId,
toType: 'CONTRACT',
category: 'ADD_SUBSIDY',
token: 'M$',
})
if (status === 'error') {
throw new APIError(500, message ?? 'Unknown error')
}
transaction.update(contractDoc, {
subsidyPool: newSubsidyPool,
totalLiquidity: newTotalLiquidity,
} as Partial<CPMMContract>)
return txn
})
}
export const addHouseSubsidyToAnswer = (
contractId: string,
answerId: string,
amount: number
) => {
return firestore.runTransaction(async (transaction) => {
const contractDoc = firestore.doc(`contracts/${contractId}`)
const { status, message, txn } = await runTxnFromBank(transaction, {
fromType: 'BANK',
amount,
toId: contractId,
toType: 'CONTRACT',
category: 'ADD_SUBSIDY',
token: 'M$',
data: { answerId },
})
if (status === 'error') {
throw new APIError(500, message ?? 'Unknown error')
}
transaction.update(contractDoc, {
totalLiquidity: FieldValue.increment(amount),
})
const answerDoc = firestore.doc(
`contracts/${contractId}/answersCpmm/${answerId}`
)
transaction.update(answerDoc, {
totalLiquidity: FieldValue.increment(amount),
subsidyPool: FieldValue.increment(amount),
})
return txn
})
}