module Cardano.Chain.UTxO.GenesisUTxO (
  genesisUtxo,
)
where

import Cardano.Chain.Common (Address, Lovelace, makeRedeemAddress)
import Cardano.Chain.Common.NetworkMagic (NetworkMagic, makeNetworkMagic)
import Cardano.Chain.Genesis (unGenesisAvvmBalances, unGenesisNonAvvmBalances)
import qualified Cardano.Chain.Genesis as Genesis
import Cardano.Chain.UTxO.UTxO (UTxO)
import qualified Cardano.Chain.UTxO.UTxO as UTxO
import Cardano.Crypto (fromCompactRedeemVerificationKey)
import Cardano.Prelude
import qualified Data.Map.Strict as M

-- | Create initial 'UTxO' from balances defined in the genesis config
genesisUtxo :: Genesis.Config -> UTxO
genesisUtxo :: Config -> UTxO
genesisUtxo Config
config = [(Address, Lovelace)] -> UTxO
UTxO.fromBalances [(Address, Lovelace)]
balances
  where
    balances :: [(Address, Lovelace)]
    balances :: [(Address, Lovelace)]
balances = [(Address, Lovelace)]
avvmBalances forall a. Semigroup a => a -> a -> a
<> [(Address, Lovelace)]
nonAvvmBalances

    avvmBalances :: [(Address, Lovelace)]
    avvmBalances :: [(Address, Lovelace)]
avvmBalances =
      forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (NetworkMagic -> RedeemVerificationKey -> Address
makeRedeemAddress NetworkMagic
networkMagic forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. CompactRedeemVerificationKey -> RedeemVerificationKey
fromCompactRedeemVerificationKey)
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall k a. Map k a -> [(k, a)]
M.toList (GenesisAvvmBalances -> Map CompactRedeemVerificationKey Lovelace
unGenesisAvvmBalances forall a b. (a -> b) -> a -> b
$ Config -> GenesisAvvmBalances
Genesis.configAvvmDistr Config
config)

    networkMagic :: NetworkMagic
    networkMagic :: NetworkMagic
networkMagic = forall a. AProtocolMagic a -> NetworkMagic
makeNetworkMagic (Config -> ProtocolMagic
Genesis.configProtocolMagic Config
config)

    nonAvvmBalances :: [(Address, Lovelace)]
    nonAvvmBalances :: [(Address, Lovelace)]
nonAvvmBalances =
      forall k a. Map k a -> [(k, a)]
M.toList forall a b. (a -> b) -> a -> b
$ GenesisNonAvvmBalances -> Map Address Lovelace
unGenesisNonAvvmBalances forall a b. (a -> b) -> a -> b
$ Config -> GenesisNonAvvmBalances
Genesis.configNonAvvmBalances Config
config