Skip to content

Pool Reaping Transition

{-# OPTIONS --safe #-}

open import Ledger.Conway.Specification.Abstract
open import Ledger.Conway.Specification.Transaction

module Ledger.Conway.Specification.PoolReap
  (txs : _) (open TransactionStructure txs)
  (abs : AbstractFunctions txs)
  where
open import Ledger.Prelude
open import Ledger.Conway.Specification.Utxo txs abs
open import Ledger.Conway.Specification.Certs govStructure
record PoolReapState : Type where
  inductive
  constructor ⟦_,_,_,_⟧ᵖ
  field
    utxoSt     : UTxOState   -- utxo state
    acnt       : Acnt        -- accounting
    dState     : DState      -- delegation state
    pState     : PState      -- pool state
instance
  unquoteDecl HasCast-PoolReapState = derive-HasCast
                [ (quote PoolReapState , HasCast-PoolReapState) ]

private variable
  e lastEpoch : Epoch
  poolReapState : PoolReapState

data
  _⊢_⇀⦇_,POOLREAP⦈_ :   PoolReapState  Epoch  PoolReapState  Type where
  POOLREAP : let
    open PoolReapState poolReapState
    open StakePoolParams
    open UTxOState
    open PState
    open DState
    open Acnt
    open PParams

    retired    = pState .retiring ⁻¹ e
    rewardAcnts : DepositPurpose  Credential
    rewardAcnts =
      mapKeys PoolDeposit $
      mapValues rewardAccount $
      pState .pools  retired

    rewardAcnts' : Credential  Coin
    rewardAcnts' = aggregateBy (rewardAcnts ˢ) (utxoSt .deposits)

    refunds : Credential  Coin
    refunds = rewardAcnts'  dom (dState .rewards)

    mRefunds = rewardAcnts'  dom (dState .rewards) 

    unclaimed  = getCoin mRefunds

    retiredDeposits :  DepositPurpose
    retiredDeposits = mapˢ PoolDeposit retired

    utxoSt' = record utxoSt { deposits = utxoSt .deposits  retiredDeposits  }

    acnt' = record acnt { treasury = acnt .treasury + unclaimed }

    dState' =
      $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#683}{\htmlId{1979}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{1986}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#4274}{\htmlId{1987}{\htmlClass{Field}{\text{voteDelegs}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#683}{\htmlId{2006}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{2013}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#4304}{\htmlId{2014}{\htmlClass{Field}{\text{stakeDelegs}}}}\, \,\href{Axiom.Set.Map.html#17848}{\htmlId{2026}{\htmlClass{Function Operator}{\text{∣\^{}}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1254}{\htmlId{2029}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#17848}{\htmlId{2037}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#683}{\htmlId{2047}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{2054}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#4335}{\htmlId{2055}{\htmlClass{Field}{\text{rewards}}}}\, \,\href{Axiom.Set.Map.Dec.html#2149}{\htmlId{2063}{\htmlClass{Function Operator}{\text{∪⁺}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1552}{\htmlId{2066}{\htmlClass{Bound}{\text{refunds}}}}\,
      \end{pmatrix}$

    pState' =
      $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#732}{\htmlId{2105}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{2112}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#4398}{\htmlId{2113}{\htmlClass{Field}{\text{pools}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2119}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1254}{\htmlId{2121}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2129}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#732}{\htmlId{2139}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{2146}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#4420}{\htmlId{2147}{\htmlClass{Field}{\text{fPools}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2154}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1254}{\htmlId{2156}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2164}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#732}{\htmlId{2174}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{2181}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#4442}{\htmlId{2182}{\htmlClass{Field}{\text{retiring}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2191}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1254}{\htmlId{2193}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2201}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \end{pmatrix}$

    in
    ────────────────────────────────
    _  $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#597}{\htmlId{2266}{\htmlClass{Function}{\text{utxoSt}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#640}{\htmlId{2275}{\htmlClass{Function}{\text{acnt}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#683}{\htmlId{2282}{\htmlClass{Function}{\text{dState}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#732}{\htmlId{2291}{\htmlClass{Function}{\text{pState}}}}\, \end{pmatrix}$ ⇀⦇ e ,POOLREAP⦈ $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#1813}{\htmlId{2318}{\htmlClass{Bound}{\text{utxoSt'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#1894}{\htmlId{2328}{\htmlClass{Bound}{\text{acnt'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#1961}{\htmlId{2336}{\htmlClass{Bound}{\text{dState'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#2087}{\htmlId{2346}{\htmlClass{Bound}{\text{pState'}}}}\, \end{pmatrix}$