vault.gno
5.36 Kb · 164 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 return shares
106}
107
108// PoolWithdraw burns `shares` from the caller and pays their pro-rata slice of
109// the pool in tUSD from the desk reserve. Returns the tUSD micro paid out.
110func PoolWithdraw(cur realm, shares int64) int64 {
111 lp := cur.Previous().Address()
112 have := poolBals[lp]
113 if shares <= 0 || shares > have {
114 panic("pool: not enough shares")
115 }
116 if left := have - shares; left > 0 && left < minPoolShares {
117 panic("pool: leave at least 0.1 share or withdraw everything")
118 }
119 drip(time.Now().Unix())
120 assets := mulDiv(shares, poolAssets, poolShares)
121 if assets > freeReserve() {
122 panic("pool: the reserve is backing open positions, withdraw less")
123 }
124 poolBals[lp] = have - shares
125 poolShares -= shares
126 poolAssets -= assets
127 if poolShares == 0 {
128 poolAssets, pendingFees = 0, 0 // unreleased fees stay in the reserve
129 }
130 if assets > 0 {
131 if err := usdLedger.Transfer(deskAddr, lp, assets); err != nil {
132 panic(err.Error())
133 }
134 }
135 return assets
136}
137
138// --- reads for the UI ---
139
140// PoolSharePrice is the value of one share in micro-USD, scaled by 1e6 (so a
141// fresh pool reads 1_000000 == $1.000000 per share). Returns 1e6 when empty.
142func PoolSharePrice() int64 {
143 if poolShares == 0 {
144 return microUSD
145 }
146 return mulDiv(assetsNow(), microUSD, poolShares)
147}
148
149// PoolInfo returns pool-wide figures, comma-separated:
150// totalShares,poolAssets,feesAll,deskReserve (all micro, 6 dp).
151func PoolInfo() string {
152 return ufmt.Sprintf("%d,%d,%d,%d", poolShares, assetsNow(), poolFeesAll, usd.BalanceOf(deskAddr))
153}
154
155// PoolOf returns one depositor's position, comma-separated:
156// shares,assets where assets is the current tUSD micro value of those shares.
157func PoolOf(owner address) string {
158 sh := poolBals[owner]
159 assets := int64(0)
160 if sh > 0 && poolShares > 0 {
161 assets = mulDiv(sh, assetsNow(), poolShares)
162 }
163 return ufmt.Sprintf("%d,%d", sh, assets)
164}