Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

vault.gno

3.93 Kb · 116 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 raise
 15// poolAssets while shares stay fixed, so each share is worth more over time. A
 16// withdrawal burns shares for their pro-rata slice of poolAssets, paid in tUSD
 17// from the desk reserve. All amounts are micro-USD (6 dp), like the rest of the
 18// desk.
 19
 20import "gno.land/p/nt/ufmt/v0"
 21
 22const minPoolDeposit = 1_000000 // $1 minimum, blocks dust shares
 23
 24var (
 25	poolShares  int64                 // total shares outstanding
 26	poolAssets  int64                 // micro-USD the pool is entitled to in the reserve
 27	poolFeesAll int64                 // cumulative fees ever routed to the pool (metrics)
 28	poolBals    = map[address]int64{} // shares held per depositor
 29)
 30
 31// accrueFee routes a desk fee into the pool. Called by spot and perp trading.
 32// If no one has deposited yet (poolShares == 0) the fee simply stays in the
 33// reserve as it did before — there is no one to credit.
 34func accrueFee(fee int64) {
 35	if fee <= 0 || poolShares == 0 {
 36		return
 37	}
 38	poolAssets += fee
 39	poolFeesAll += fee
 40}
 41
 42// PoolDeposit pulls `amount` tUSD micro from the caller into the desk reserve
 43// and mints pool shares at the current share price. Returns the shares minted.
 44func PoolDeposit(cur realm, amount int64) int64 {
 45	if amount < minPoolDeposit {
 46		panic("pool: deposit below minimum ($1)")
 47	}
 48	lp := cur.Previous().Address()
 49	if err := usdLedger.Transfer(lp, deskAddr, amount); err != nil {
 50		panic(err.Error())
 51	}
 52	var shares int64
 53	if poolShares == 0 || poolAssets == 0 {
 54		shares = amount // bootstrap: 1 share == 1 micro-tUSD
 55	} else {
 56		shares = mulDiv(amount, poolShares, poolAssets)
 57	}
 58	if shares <= 0 {
 59		panic("pool: deposit too small for a share")
 60	}
 61	poolShares += shares
 62	poolAssets += amount
 63	poolBals[lp] += shares
 64	return shares
 65}
 66
 67// PoolWithdraw burns `shares` from the caller and pays their pro-rata slice of
 68// the pool in tUSD from the desk reserve. Returns the tUSD micro paid out.
 69func PoolWithdraw(cur realm, shares int64) int64 {
 70	lp := cur.Previous().Address()
 71	have := poolBals[lp]
 72	if shares <= 0 || shares > have {
 73		panic("pool: not enough shares")
 74	}
 75	assets := mulDiv(shares, poolAssets, poolShares)
 76	poolBals[lp] = have - shares
 77	poolShares -= shares
 78	poolAssets -= assets
 79	if poolShares == 0 {
 80		poolAssets = 0 // avoid dust stranding the last share price
 81	}
 82	if assets > 0 {
 83		if err := usdLedger.Transfer(deskAddr, lp, assets); err != nil {
 84			panic(err.Error())
 85		}
 86	}
 87	return assets
 88}
 89
 90// --- reads for the UI ---
 91
 92// PoolSharePrice is the value of one share in micro-USD, scaled by 1e6 (so a
 93// fresh pool reads 1_000000 == $1.000000 per share). Returns 1e6 when empty.
 94func PoolSharePrice() int64 {
 95	if poolShares == 0 {
 96		return microUSD
 97	}
 98	return mulDiv(poolAssets, microUSD, poolShares)
 99}
100
101// PoolInfo returns pool-wide figures, comma-separated:
102// totalShares,poolAssets,feesAll,deskReserve  (all micro, 6 dp).
103func PoolInfo() string {
104	return ufmt.Sprintf("%d,%d,%d,%d", poolShares, poolAssets, poolFeesAll, usd.BalanceOf(deskAddr))
105}
106
107// PoolOf returns one depositor's position, comma-separated:
108// shares,assets  where assets is the current tUSD micro value of those shares.
109func PoolOf(owner address) string {
110	sh := poolBals[owner]
111	assets := int64(0)
112	if sh > 0 && poolShares > 0 {
113		assets = mulDiv(sh, poolAssets, poolShares)
114	}
115	return ufmt.Sprintf("%d,%d", sh, assets)
116}