package lastcall // The LastCall liquidity pool — the desk's yield product. // // Anyone can deposit tUSD into the pool. Pooled tUSD sits in the desk reserve // next to the admin-seeded float. Trader wins and losses settle against the // reserve and never change the share price; the pool // earns 100% of the desk's trading fees — 0.30% on each spot trade and 0.10% // on each perp open/close — shared pro-rata across depositors. There is no // emitted reward token and no made-up APY: the yield is exactly the fees the // desk actually collects. // // Accounting is share-based, like an ERC-4626 vault. A deposit mints shares at // the current share price (poolAssets / poolShares); accrued fees stream into // poolAssets over a day while shares stay fixed, so each share is worth more over // time and depositing just before a fee lands captures none of it. A withdrawal // burns shares for their pro-rata slice of poolAssets, paid in tUSD from the part // of the reserve that is not locked behind open positions and orders. All amounts are micro-USD (6 dp), like the rest of the // desk. import ( "time" "gno.land/p/nt/ufmt/v0" ) const ( minPoolDeposit = 1_000000 // $1 minimum, blocks dust shares minPoolShares = 100000 // a holding is zero or at least 0.1 share, so a share cannot be inflated feeStreamSec = 86400 ) var ( poolShares int64 // total shares outstanding poolAssets int64 // micro-USD the pool is entitled to in the reserve poolFeesAll int64 // cumulative fees ever routed to the pool (metrics) poolBals = map[address]int64{} // shares held per depositor pendingFees int64 // fees not yet part of poolAssets streamAt int64 // time up to which pendingFees have been released streamEnd int64 // time at which the rest is fully released ) func releasedFees(now int64) int64 { if pendingFees == 0 || now <= streamAt { return 0 } if now >= streamEnd { return pendingFees } return mulDiv(pendingFees, now-streamAt, streamEnd-streamAt) } func drip(now int64) { r := releasedFees(now) pendingFees -= r poolAssets += r streamAt = now } func assetsNow() int64 { return poolAssets + releasedFees(time.Now().Unix()) } // accrueFee routes a desk fee into the pool. Called by spot and perp trading. // If no one has deposited yet (poolShares == 0) the fee simply stays in the // reserve as it did before — there is no one to credit. func accrueFee(fee int64) { if fee <= 0 || poolShares == 0 { return } now := time.Now().Unix() drip(now) pendingFees += fee streamEnd = now + feeStreamSec poolFeesAll += fee } // PoolDeposit pulls `amount` tUSD micro from the caller into the desk reserve // and mints pool shares at the current share price. Returns the shares minted. func PoolDeposit(cur realm, amount int64) int64 { if amount < minPoolDeposit { panic("pool: deposit below minimum ($1)") } lp := cur.Previous().Address() drip(time.Now().Unix()) // A reserve that no longer covers what the pool already claims would hand the // new money to the earlier depositors. if freeReserve() < poolAssets+pendingFees { panic("pool: under water, deposits are paused") } if err := usdLedger.Transfer(lp, deskAddr, amount); err != nil { panic(err.Error()) } var shares int64 if poolShares == 0 || poolAssets == 0 { shares = amount // bootstrap: 1 share == 1 micro-tUSD } else { shares = mulDiv(amount, poolShares, poolAssets) } if shares < minPoolShares { panic("pool: deposit too small, it must buy at least 0.1 share") } poolShares += shares poolAssets += amount poolBals[lp] += shares emitPool("PoolDeposit", lp, amount, shares) return shares } // PoolWithdraw burns `shares` from the caller and pays their pro-rata slice of // the pool in tUSD from the desk reserve. Returns the tUSD micro paid out. func PoolWithdraw(cur realm, shares int64) int64 { lp := cur.Previous().Address() have := poolBals[lp] if shares <= 0 || shares > have { panic("pool: not enough shares") } if left := have - shares; left > 0 && left < minPoolShares { panic("pool: leave at least 0.1 share or withdraw everything") } drip(time.Now().Unix()) assets := mulDiv(shares, poolAssets, poolShares) if assets > freeReserve() { panic("pool: the reserve is backing open positions, withdraw less") } poolBals[lp] = have - shares poolShares -= shares poolAssets -= assets if poolShares == 0 { poolAssets, pendingFees = 0, 0 // unreleased fees stay in the reserve } if assets > 0 { if err := usdLedger.Transfer(deskAddr, lp, assets); err != nil { panic(err.Error()) } } emitPool("PoolWithdraw", lp, assets, shares) return assets } // --- reads for the UI --- // PoolSharePrice is the value of one share in micro-USD, scaled by 1e6 (so a // fresh pool reads 1_000000 == $1.000000 per share). Returns 1e6 when empty. func PoolSharePrice() int64 { if poolShares == 0 { return microUSD } return mulDiv(assetsNow(), microUSD, poolShares) } // PoolInfo returns pool-wide figures, comma-separated: // totalShares,poolAssets,feesAll,deskReserve (all micro, 6 dp). func PoolInfo() string { return ufmt.Sprintf("%d,%d,%d,%d", poolShares, assetsNow(), poolFeesAll, usd.BalanceOf(deskAddr)) } // PoolOf returns one depositor's position, comma-separated: // shares,assets where assets is the current tUSD micro value of those shares. func PoolOf(owner address) string { sh := poolBals[owner] assets := int64(0) if sh > 0 && poolShares > 0 { assets = mulDiv(sh, assetsNow(), poolShares) } return ufmt.Sprintf("%d,%d", sh, assets) }