-
Notifications
You must be signed in to change notification settings - Fork 146
/
mana-supply.ts
58 lines (53 loc) · 1.86 KB
/
mana-supply.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
import { createSupabaseDirectClient } from 'shared/supabase/init'
export const getManaSupply = async () => {
const pg = createSupabaseDirectClient()
const userPortfolio = await pg.one(
`select sum(balance + spice_balance + investment_value) as total_value,
sum(balance) as balance,
sum(spice_balance) as spice_balance,
sum(investment_value) as investment_value,
sum(loan_total) as loan_total
from user_portfolio_history_latest
where
user_id not in ('pyBueUg9y3hrDIUtrus5uAkPHCr1', 'IPTOzEqrpkWmEzh6hwvAyY9PqFb2', 'tRZZ6ihugZQLXPf6aPRneGpWLmz1', 'ttl8PytdL4P9oD0fNneqvwgOZhy1')
and (balance + spice_balance + investment_value) > 0.0
`,
[],
(r: any) => ({
totalValue: Number(r.total_value),
balance: Number(r.balance),
spiceBalance: Number(r.spice_balance),
investmentValue: Number(r.investment_value),
loanTotal: Number(r.loan_total),
})
)
const ammLiquidity = await getAMMLiquidity()
const totalValue = userPortfolio.totalValue + ammLiquidity
const totalUsdValue = totalValue / 100
return {
...userPortfolio,
ammLiquidity,
totalValue,
totalUsdValue,
}
}
const getAMMLiquidity = async () => {
const pg = createSupabaseDirectClient()
const [binaryLiquidity, multiLiquidity] = await Promise.all([
pg.one(
`select sum((data->>'prob')::numeric * (data->'pool'->>'YES')::numeric + (1-(data->>'prob')::numeric) *(data->'pool'->>'NO')::numeric) from contracts
where resolution is null
and mechanism = 'cpmm-1'`,
[],
(r: any) => Number(r.sum)
),
pg.one(
`select sum(prob * pool_yes + (1-prob) * pool_no) from answers
join contracts on contract_id = contracts.id
where resolution is null`,
[],
(r: any) => Number(r.sum)
),
])
return binaryLiquidity + multiLiquidity
}