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 raise // poolAssets while shares stay fixed, so each share is worth more over time. A // withdrawal burns shares for their pro-rata slice of poolAssets, paid in tUSD // from the desk reserve. All amounts are micro-USD (6 dp), like the rest of the // desk. import "gno.land/p/nt/ufmt/v0" const minPoolDeposit = 1_000000 // $1 minimum, blocks dust shares 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 ) // 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 } poolAssets += fee 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() 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 <= 0 { panic("pool: deposit too small for a share") } poolShares += shares poolAssets += amount poolBals[lp] += 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") } assets := mulDiv(shares, poolAssets, poolShares) poolBals[lp] = have - shares poolShares -= shares poolAssets -= assets if poolShares == 0 { poolAssets = 0 // avoid dust stranding the last share price } if assets > 0 { if err := usdLedger.Transfer(deskAddr, lp, assets); err != nil { panic(err.Error()) } } 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(poolAssets, 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, poolAssets, 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, poolAssets, poolShares) } return ufmt.Sprintf("%d,%d", sh, assets) }