{-# 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


  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#558}{\htmlId{1824}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{1831}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3777}{\htmlId{1832}{\htmlClass{Field}{\text{voteDelegs}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#558}{\htmlId{1851}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{1858}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3807}{\htmlId{1859}{\htmlClass{Field}{\text{stakeDelegs}}}}\, \,\href{Axiom.Set.Map.html#14509}{\htmlId{1871}{\htmlClass{Function Operator}{\text{∣\^{}}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1099}{\htmlId{1874}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#14509}{\htmlId{1882}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#558}{\htmlId{1892}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{1899}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3838}{\htmlId{1900}{\htmlClass{Field}{\text{rewards}}}}\, \,\href{Axiom.Set.Map.Dec.html#2046}{\htmlId{1908}{\htmlClass{Function Operator}{\text{∪⁺}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1397}{\htmlId{1911}{\htmlClass{Bound}{\text{refunds}}}}\,
      \end{pmatrix}$

    pState' = $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#607}{\htmlId{1944}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{1951}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3901}{\htmlId{1952}{\htmlClass{Field}{\text{pools}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{1958}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1099}{\htmlId{1960}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{1968}{\htmlClass{Function Operator}{\text{ᶜ}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#607}{\htmlId{1972}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{1979}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3923}{\htmlId{1980}{\htmlClass{Field}{\text{retiring}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{1989}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1099}{\htmlId{1991}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{1999}{\htmlClass{Function Operator}{\text{ᶜ}}}}\, \end{pmatrix}$

    in
    ────────────────────────────────
    _  $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#472}{\htmlId{2058}{\htmlClass{Function}{\text{utxoSt}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#515}{\htmlId{2067}{\htmlClass{Function}{\text{acnt}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#558}{\htmlId{2074}{\htmlClass{Function}{\text{dState}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#607}{\htmlId{2083}{\htmlClass{Function}{\text{pState}}}}\, \end{pmatrix}$ ⇀⦇ e ,POOLREAP⦈ $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#1658}{\htmlId{2110}{\htmlClass{Bound}{\text{utxoSt'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#1739}{\htmlId{2120}{\htmlClass{Bound}{\text{acnt'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#1806}{\htmlId{2128}{\htmlClass{Bound}{\text{dState'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#1932}{\htmlId{2138}{\htmlClass{Bound}{\text{pState'}}}}\, \end{pmatrix}$