vault.gno
5.45 Kb · 166 lines
1package lastcall
2
3// The LastCall liquidity pool — the desk's yield product.
4//
5// Anyone can deposit tUSD into the pool. Pooled tUSD sits in the desk reserve
6// next to the admin-seeded float. Trader wins and losses settle against the
7// reserve and never change the share price; the pool
8// earns 100% of the desk's trading fees — 0.30% on each spot trade and 0.10%
9// on each perp open/close — shared pro-rata across depositors. There is no
10// emitted reward token and no made-up APY: the yield is exactly the fees the
11// desk actually collects.
12//
13// Accounting is share-based, like an ERC-4626 vault. A deposit mints shares at
14// the current share price (poolAssets / poolShares); accrued fees stream into
15// poolAssets over a day while shares stay fixed, so each share is worth more over
16// time and depositing just before a fee lands captures none of it. A withdrawal
17// burns shares for their pro-rata slice of poolAssets, paid in tUSD from the part
18// of the reserve that is not locked behind open positions and orders. All amounts are micro-USD (6 dp), like the rest of the
19// desk.
20
21import (
22 "time"
23
24 "gno.land/p/nt/ufmt/v0"
25)
26
27const (
28 minPoolDeposit = 1_000000 // $1 minimum, blocks dust shares
29 minPoolShares = 100000 // a holding is zero or at least 0.1 share, so a share cannot be inflated
30 feeStreamSec = 86400
31)
32
33var (
34 poolShares int64 // total shares outstanding
35 poolAssets int64 // micro-USD the pool is entitled to in the reserve
36 poolFeesAll int64 // cumulative fees ever routed to the pool (metrics)
37 poolBals = map[address]int64{} // shares held per depositor
38
39 pendingFees int64 // fees not yet part of poolAssets
40 streamAt int64 // time up to which pendingFees have been released
41 streamEnd int64 // time at which the rest is fully released
42)
43
44func releasedFees(now int64) int64 {
45 if pendingFees == 0 || now <= streamAt {
46 return 0
47 }
48 if now >= streamEnd {
49 return pendingFees
50 }
51 return mulDiv(pendingFees, now-streamAt, streamEnd-streamAt)
52}
53
54func drip(now int64) {
55 r := releasedFees(now)
56 pendingFees -= r
57 poolAssets += r
58 streamAt = now
59}
60
61func assetsNow() int64 { return poolAssets + releasedFees(time.Now().Unix()) }
62
63// accrueFee routes a desk fee into the pool. Called by spot and perp trading.
64// If no one has deposited yet (poolShares == 0) the fee simply stays in the
65// reserve as it did before — there is no one to credit.
66func accrueFee(fee int64) {
67 if fee <= 0 || poolShares == 0 {
68 return
69 }
70 now := time.Now().Unix()
71 drip(now)
72 pendingFees += fee
73 streamEnd = now + feeStreamSec
74 poolFeesAll += fee
75}
76
77// PoolDeposit pulls `amount` tUSD micro from the caller into the desk reserve
78// and mints pool shares at the current share price. Returns the shares minted.
79func PoolDeposit(cur realm, amount int64) int64 {
80 if amount < minPoolDeposit {
81 panic("pool: deposit below minimum ($1)")
82 }
83 lp := cur.Previous().Address()
84 drip(time.Now().Unix())
85 // A reserve that no longer covers what the pool already claims would hand the
86 // new money to the earlier depositors.
87 if freeReserve() < poolAssets+pendingFees {
88 panic("pool: under water, deposits are paused")
89 }
90 if err := usdLedger.Transfer(lp, deskAddr, amount); err != nil {
91 panic(err.Error())
92 }
93 var shares int64
94 if poolShares == 0 || poolAssets == 0 {
95 shares = amount // bootstrap: 1 share == 1 micro-tUSD
96 } else {
97 shares = mulDiv(amount, poolShares, poolAssets)
98 }
99 if shares < minPoolShares {
100 panic("pool: deposit too small, it must buy at least 0.1 share")
101 }
102 poolShares += shares
103 poolAssets += amount
104 poolBals[lp] += shares
105 emitPool("PoolDeposit", lp, amount, shares)
106 return shares
107}
108
109// PoolWithdraw burns `shares` from the caller and pays their pro-rata slice of
110// the pool in tUSD from the desk reserve. Returns the tUSD micro paid out.
111func PoolWithdraw(cur realm, shares int64) int64 {
112 lp := cur.Previous().Address()
113 have := poolBals[lp]
114 if shares <= 0 || shares > have {
115 panic("pool: not enough shares")
116 }
117 if left := have - shares; left > 0 && left < minPoolShares {
118 panic("pool: leave at least 0.1 share or withdraw everything")
119 }
120 drip(time.Now().Unix())
121 assets := mulDiv(shares, poolAssets, poolShares)
122 if assets > freeReserve() {
123 panic("pool: the reserve is backing open positions, withdraw less")
124 }
125 poolBals[lp] = have - shares
126 poolShares -= shares
127 poolAssets -= assets
128 if poolShares == 0 {
129 poolAssets, pendingFees = 0, 0 // unreleased fees stay in the reserve
130 }
131 if assets > 0 {
132 if err := usdLedger.Transfer(deskAddr, lp, assets); err != nil {
133 panic(err.Error())
134 }
135 }
136 emitPool("PoolWithdraw", lp, assets, shares)
137 return assets
138}
139
140// --- reads for the UI ---
141
142// PoolSharePrice is the value of one share in micro-USD, scaled by 1e6 (so a
143// fresh pool reads 1_000000 == $1.000000 per share). Returns 1e6 when empty.
144func PoolSharePrice() int64 {
145 if poolShares == 0 {
146 return microUSD
147 }
148 return mulDiv(assetsNow(), microUSD, poolShares)
149}
150
151// PoolInfo returns pool-wide figures, comma-separated:
152// totalShares,poolAssets,feesAll,deskReserve (all micro, 6 dp).
153func PoolInfo() string {
154 return ufmt.Sprintf("%d,%d,%d,%d", poolShares, assetsNow(), poolFeesAll, usd.BalanceOf(deskAddr))
155}
156
157// PoolOf returns one depositor's position, comma-separated:
158// shares,assets where assets is the current tUSD micro value of those shares.
159func PoolOf(owner address) string {
160 sh := poolBals[owner]
161 assets := int64(0)
162 if sh > 0 && poolShares > 0 {
163 assets = mulDiv(sh, assetsNow(), poolShares)
164 }
165 return ufmt.Sprintf("%d,%d", sh, assets)
166}