module Cooked.InitialDistribution
( InitialDistribution (..),
distributionFromList,
toInitDistWithMinAda,
unsafeToInitDistWithMinAda,
)
where
import Control.Monad
import Cooked.MockChain.GenerateTx
import Cooked.MockChain.MinAda
import Cooked.Skeleton
import Cooked.Wallet
import Data.Default
import Data.List
import Plutus.Script.Utils.Value qualified as Script
import PlutusLedgerApi.V3 qualified as Api
data InitialDistribution where
InitialDistribution ::
{InitialDistribution -> [TxSkelOut]
unInitialDistribution :: [TxSkelOut]} ->
InitialDistribution
instance Default InitialDistribution where
def :: InitialDistribution
def = [(Wallet, [Value])] -> InitialDistribution
distributionFromList ([(Wallet, [Value])] -> InitialDistribution)
-> (Value -> [(Wallet, [Value])]) -> Value -> InitialDistribution
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Wallet] -> [[Value]] -> [(Wallet, [Value])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Wallet]
knownWallets ([[Value]] -> [(Wallet, [Value])])
-> (Value -> [[Value]]) -> Value -> [(Wallet, [Value])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Value] -> [[Value]]
forall a. a -> [a]
repeat ([Value] -> [[Value]]) -> (Value -> [Value]) -> Value -> [[Value]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Value -> [Value]
forall a. Int -> a -> [a]
replicate Int
5 (Value -> InitialDistribution) -> Value -> InitialDistribution
forall a b. (a -> b) -> a -> b
$ Integer -> Value
Script.ada Integer
100
instance Semigroup InitialDistribution where
InitialDistribution
i <> :: InitialDistribution -> InitialDistribution -> InitialDistribution
<> InitialDistribution
j = [TxSkelOut] -> InitialDistribution
InitialDistribution (InitialDistribution -> [TxSkelOut]
unInitialDistribution InitialDistribution
i [TxSkelOut] -> [TxSkelOut] -> [TxSkelOut]
forall a. Semigroup a => a -> a -> a
<> InitialDistribution -> [TxSkelOut]
unInitialDistribution InitialDistribution
j)
instance Monoid InitialDistribution where
mempty :: InitialDistribution
mempty = [TxSkelOut] -> InitialDistribution
InitialDistribution [TxSkelOut]
forall a. Monoid a => a
mempty
toInitDistWithMinAda :: InitialDistribution -> Either GenerateTxError InitialDistribution
toInitDistWithMinAda :: InitialDistribution -> Either GenerateTxError InitialDistribution
toInitDistWithMinAda (InitialDistribution [TxSkelOut]
initDist) =
[TxSkelOut] -> InitialDistribution
InitialDistribution ([TxSkelOut] -> InitialDistribution)
-> Either GenerateTxError [TxSkelOut]
-> Either GenerateTxError InitialDistribution
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [TxSkelOut]
-> (TxSkelOut -> Either GenerateTxError TxSkelOut)
-> Either GenerateTxError [TxSkelOut]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [TxSkelOut]
initDist (Params -> TxSkelOut -> Either GenerateTxError TxSkelOut
toTxSkelOutWithMinAda Params
forall a. Default a => a
def)
unsafeToInitDistWithMinAda :: InitialDistribution -> InitialDistribution
unsafeToInitDistWithMinAda :: InitialDistribution -> InitialDistribution
unsafeToInitDistWithMinAda InitialDistribution
initDist = case InitialDistribution -> Either GenerateTxError InitialDistribution
toInitDistWithMinAda InitialDistribution
initDist of
Left GenerateTxError
err -> [Char] -> InitialDistribution
forall a. HasCallStack => [Char] -> a
error ([Char] -> InitialDistribution) -> [Char] -> InitialDistribution
forall a b. (a -> b) -> a -> b
$ GenerateTxError -> [Char]
forall a. Show a => a -> [Char]
show GenerateTxError
err
Right InitialDistribution
initDist' -> InitialDistribution
initDist'
distributionFromList :: [(Wallet, [Api.Value])] -> InitialDistribution
distributionFromList :: [(Wallet, [Value])] -> InitialDistribution
distributionFromList = [TxSkelOut] -> InitialDistribution
InitialDistribution ([TxSkelOut] -> InitialDistribution)
-> ([(Wallet, [Value])] -> [TxSkelOut])
-> [(Wallet, [Value])]
-> InitialDistribution
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([TxSkelOut] -> (Wallet, [Value]) -> [TxSkelOut])
-> [TxSkelOut] -> [(Wallet, [Value])] -> [TxSkelOut]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\[TxSkelOut]
x (Wallet
user, [Value]
values) -> [TxSkelOut]
x [TxSkelOut] -> [TxSkelOut] -> [TxSkelOut]
forall a. Semigroup a => a -> a -> a
<> (Value -> TxSkelOut) -> [Value] -> [TxSkelOut]
forall a b. (a -> b) -> [a] -> [b]
map (Wallet -> Value -> TxSkelOut
forall a. ToPubKeyHash a => a -> Value -> TxSkelOut
paysPK Wallet
user) [Value]
values) []