caelum0x b03c1bcea8 feat(model-vault): revenue/dividend pool — stake model tokens, earn pro-rata fees (P2)
Iteration 4 of the AI-model-tokens plan: model-token holders can now stake their tokens
and earn a pro-rata share of the vault's revenue (in uclaw) — the "dividend-bearing"
property of a tokenized AI model.

Synthetix-style reward-index accrual (O(1) per stake/unstake/claim, no unbounded staker
iteration):
- Global Dividend { reward_per_token_stored: Uint256 (1e18 SCALE), total_staked }; per
  staker Stake { staked, reward_index_snapshot, pending } in STAKES Map<&Addr,Stake>.
- On revenue: delta = amount * SCALE / total_staked. On any stake/unstake/claim: settle
  first (pending += staked*(global-snapshot)/SCALE), then advance the snapshot — late
  stakers never retroactively earn.
- src/rewards.rs: pure reward_index_delta + earned_since_snapshot, 9 unit tests
  (settlement formula, two-staker split, late-staker exclusion, Uint256 precision at
  large values, dust floor-rounding, zero guards).

New Execute msgs (exactly-one-coin validation, checked math, BankMsg::Send payouts):
- Stake / Unstake { amount } — model tokens escrowed outside the curve.
- ClaimRewards — pays settled pending in reserve_denom.
- DistributeRevenue — anyone funds the pool; errors NoStakers when total_staked == 0 so
  funds are never stranded.

Swap fee (Config.fee_bps, default 30 bps) skimmed from every Buy (off the reserve input,
before the curve) and Sell (off the gross reserve output) and routed into the dividend
pool via the same reward-index bump — stakers earn from the model's usage. When
total_staked == 0 the fee falls back into the curve reserve (documented, never stranded);
curve invariant k = reserve*inventory still holds on the post-fee amounts.

New queries StakeInfo (live-computed claimable) and PoolInfo. INVARIANT (state.rs/README):
reward_per_token_stored monotonic; contract reserve balance covers pool.reserve +
settleable rewards; contract model balance covers pool.inventory + total_staked.

Verified: cargo build, cargo test (15 unit + 20 integration = 35, 0 failures),
cargo build --target wasm32-unknown-unknown --release (model_vault.wasm ~270KB),
clippy + fmt clean. Adversarial verify pass confirmed settle-ordering, pro-rata splits,
stranded-funds guards, no balance double-count, and funds conservation. Existing curve
tests preserved via fee_bps=0 seed helpers. Live deploy still uses cosmwasm/optimizer.
2026-06-01 19:02:36 +03:00
..
2026-05-15 19:18:10 +03:00
2026-05-15 19:18:10 +03:00