{-# 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#570}{\htmlId{1836}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{1843}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3811}{\htmlId{1844}{\htmlClass{Field}{\text{voteDelegs}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#570}{\htmlId{1863}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{1870}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3841}{\htmlId{1871}{\htmlClass{Field}{\text{stakeDelegs}}}}\, \,\href{Axiom.Set.Map.html#17848}{\htmlId{1883}{\htmlClass{Function Operator}{\text{∣\^{}}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1111}{\htmlId{1886}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#17848}{\htmlId{1894}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#570}{\htmlId{1904}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{1911}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3872}{\htmlId{1912}{\htmlClass{Field}{\text{rewards}}}}\, \,\href{Axiom.Set.Map.Dec.html#2149}{\htmlId{1920}{\htmlClass{Function Operator}{\text{∪⁺}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1409}{\htmlId{1923}{\htmlClass{Bound}{\text{refunds}}}}\,
      \end{pmatrix}$

    pState' =
      $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#619}{\htmlId{1962}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{1969}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3935}{\htmlId{1970}{\htmlClass{Field}{\text{pools}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{1976}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1111}{\htmlId{1978}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{1986}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#619}{\htmlId{1996}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{2003}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3957}{\htmlId{2004}{\htmlClass{Field}{\text{fPools}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2011}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1111}{\htmlId{2013}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2021}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \\ \,\href{Ledger.Conway.Specification.PoolReap.html#619}{\htmlId{2031}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{2038}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Specification.Certs.html#3979}{\htmlId{2039}{\htmlClass{Field}{\text{retiring}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2048}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Specification.PoolReap.html#1111}{\htmlId{2050}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#13604}{\htmlId{2058}{\htmlClass{Function Operator}{\text{ᶜ}}}}\,
      \end{pmatrix}$

    in
    ────────────────────────────────
    _  $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#484}{\htmlId{2123}{\htmlClass{Function}{\text{utxoSt}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#527}{\htmlId{2132}{\htmlClass{Function}{\text{acnt}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#570}{\htmlId{2139}{\htmlClass{Function}{\text{dState}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#619}{\htmlId{2148}{\htmlClass{Function}{\text{pState}}}}\, \end{pmatrix}$ ⇀⦇ e ,POOLREAP⦈ $\begin{pmatrix} \,\href{Ledger.Conway.Specification.PoolReap.html#1670}{\htmlId{2175}{\htmlClass{Bound}{\text{utxoSt'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#1751}{\htmlId{2185}{\htmlClass{Bound}{\text{acnt'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#1818}{\htmlId{2193}{\htmlClass{Bound}{\text{dState'}}}}\, \\ \,\href{Ledger.Conway.Specification.PoolReap.html#1944}{\htmlId{2203}{\htmlClass{Bound}{\text{pState'}}}}\, \end{pmatrix}$