{-# LANGUAGE LambdaCase #-}
module Singular.Registry.TxBuilder.Prepare
( PrepareError (..)
, Preparation (..)
, planPreparation
, spendableOuts
, spendableHeld
) where
import Control.Monad (unless, when)
import Data.Foldable (toList)
import Data.List (sortOn)
import Data.Map.Strict qualified as Map
import Data.Ord (Down (..))
import Data.Sequence.Strict qualified as StrictSeq
import Data.Set qualified as Set
import Lens.Micro ((&), (.~), (^.))
import Cardano.Ledger.Address (Addr)
import Cardano.Ledger.Api.Tx (bodyTxL, mkBasicTx, txIdTx)
import Cardano.Ledger.Api.Tx.Body
( feeTxBodyL
, inputsTxBodyL
, mkBasicTxBody
, outputsTxBodyL
, reqSignerHashesTxBodyL
)
import Cardano.Ledger.Api.Tx.Out
( TxOut
, coinTxOutL
, getMinCoinTxOut
, mkBasicTxOut
, referenceScriptTxOutL
, valueTxOutL
)
import Cardano.Ledger.BaseTypes (StrictMaybe (..), TxIx (..))
import Cardano.Ledger.Core (PParams)
import Cardano.Ledger.Mary.Value
( AssetName (..)
, MaryValue (..)
, MultiAsset (..)
, PolicyID (..)
)
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Tx.Balance
( BalanceError (..)
, BalanceResult (..)
, balanceTx
)
import Cardano.Tx.Ledger (ConwayTx)
import Singular.Registry.Ledger (Coin (..), ConwayEra)
import Singular.Registry.TxBuilder.Internal.Identity
( addrKeyHashBytes
, addrWitnessKeyHash
)
sumAssets
:: [TxOut ConwayEra] -> Map.Map PolicyID (Map.Map AssetName Integer)
sumAssets :: [TxOut ConwayEra] -> Map PolicyID (Map AssetName Integer)
sumAssets [TxOut ConwayEra]
outs =
(Map AssetName Integer
-> Map AssetName Integer -> Map AssetName Integer)
-> [Map PolicyID (Map AssetName Integer)]
-> Map PolicyID (Map AssetName Integer)
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith
((Integer -> Integer -> Integer)
-> Map AssetName Integer
-> Map AssetName Integer
-> Map AssetName Integer
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(+))
[Map PolicyID (Map AssetName Integer)
m | BabbageTxOut ConwayEra
o <- [TxOut ConwayEra]
[BabbageTxOut ConwayEra]
outs, let MaryValue Coin
_ (MultiAsset Map PolicyID (Map AssetName Integer)
m) = BabbageTxOut ConwayEra
o BabbageTxOut ConwayEra
-> Getting MaryValue (BabbageTxOut ConwayEra) MaryValue
-> MaryValue
forall s a. s -> Getting a s a -> a
^. Getting MaryValue (BabbageTxOut ConwayEra) MaryValue
(Value ConwayEra -> Const MaryValue (Value ConwayEra))
-> TxOut ConwayEra -> Const MaryValue (TxOut ConwayEra)
forall era. EraTxOut era => Lens' (TxOut era) (Value era)
Lens' (TxOut ConwayEra) (Value ConwayEra)
valueTxOutL]
data PrepareError
= PrepareShortfall
{ PrepareError -> Integer
prepareRequired :: Integer
, PrepareError -> Maybe Integer
prepareFee :: Maybe Integer
, PrepareError -> Integer
prepareRemainderMin :: Integer
, PrepareError -> Integer
prepareHeld :: Integer
, PrepareError -> Integer
prepareLargest :: Integer
}
deriving (Int -> PrepareError -> ShowS
[PrepareError] -> ShowS
PrepareError -> String
(Int -> PrepareError -> ShowS)
-> (PrepareError -> String)
-> ([PrepareError] -> ShowS)
-> Show PrepareError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PrepareError -> ShowS
showsPrec :: Int -> PrepareError -> ShowS
$cshow :: PrepareError -> String
show :: PrepareError -> String
$cshowList :: [PrepareError] -> ShowS
showList :: [PrepareError] -> ShowS
Show, PrepareError -> PrepareError -> Bool
(PrepareError -> PrepareError -> Bool)
-> (PrepareError -> PrepareError -> Bool) -> Eq PrepareError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PrepareError -> PrepareError -> Bool
== :: PrepareError -> PrepareError -> Bool
$c/= :: PrepareError -> PrepareError -> Bool
/= :: PrepareError -> PrepareError -> Bool
Eq)
data Preparation = Preparation
{ Preparation -> [(TxIn, TxOut ConwayEra)]
prepSpent :: [(TxIn, TxOut ConwayEra)]
, Preparation -> ConwayTx
prepTx :: ConwayTx
, Preparation -> TxIn
prepFunding :: TxIn
, Preparation -> Integer
prepFee :: Integer
, Preparation -> Integer
prepFundingCoin :: Integer
, Preparation -> Integer
prepRemainderCoin :: Integer
}
deriving (Int -> Preparation -> ShowS
[Preparation] -> ShowS
Preparation -> String
(Int -> Preparation -> ShowS)
-> (Preparation -> String)
-> ([Preparation] -> ShowS)
-> Show Preparation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Preparation -> ShowS
showsPrec :: Int -> Preparation -> ShowS
$cshow :: Preparation -> String
show :: Preparation -> String
$cshowList :: [Preparation] -> ShowS
showList :: [Preparation] -> ShowS
Show, Preparation -> Preparation -> Bool
(Preparation -> Preparation -> Bool)
-> (Preparation -> Preparation -> Bool) -> Eq Preparation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Preparation -> Preparation -> Bool
== :: Preparation -> Preparation -> Bool
$c/= :: Preparation -> Preparation -> Bool
/= :: Preparation -> Preparation -> Bool
Eq)
spendableOuts
:: [(TxIn, TxOut ConwayEra)] -> [(TxIn, TxOut ConwayEra)]
spendableOuts :: [(TxIn, TxOut ConwayEra)] -> [(TxIn, TxOut ConwayEra)]
spendableOuts [(TxIn, TxOut ConwayEra)]
utxos =
[(TxIn, TxOut ConwayEra)
(TxIn, BabbageTxOut ConwayEra)
u | u :: (TxIn, BabbageTxOut ConwayEra)
u@(TxIn
_, BabbageTxOut ConwayEra
o) <- [(TxIn, TxOut ConwayEra)]
[(TxIn, BabbageTxOut ConwayEra)]
utxos, BabbageTxOut ConwayEra
o BabbageTxOut ConwayEra
-> Getting
(StrictMaybe (Script ConwayEra))
(BabbageTxOut ConwayEra)
(StrictMaybe (Script ConwayEra))
-> StrictMaybe (Script ConwayEra)
forall s a. s -> Getting a s a -> a
^. (StrictMaybe (Script ConwayEra)
-> Const
(StrictMaybe (Script ConwayEra)) (StrictMaybe (Script ConwayEra)))
-> TxOut ConwayEra
-> Const (StrictMaybe (Script ConwayEra)) (TxOut ConwayEra)
Getting
(StrictMaybe (Script ConwayEra))
(BabbageTxOut ConwayEra)
(StrictMaybe (Script ConwayEra))
forall era.
BabbageEraTxOut era =>
Lens' (TxOut era) (StrictMaybe (Script era))
Lens' (TxOut ConwayEra) (StrictMaybe (Script ConwayEra))
referenceScriptTxOutL StrictMaybe (Script ConwayEra)
-> StrictMaybe (Script ConwayEra) -> Bool
forall a. Eq a => a -> a -> Bool
== StrictMaybe (Script ConwayEra)
forall a. StrictMaybe a
SNothing]
spendableHeld :: [(TxIn, TxOut ConwayEra)] -> Integer
spendableHeld :: [(TxIn, TxOut ConwayEra)] -> Integer
spendableHeld [(TxIn, TxOut ConwayEra)]
utxos =
[Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Integer
c | (TxIn
_, BabbageTxOut ConwayEra
o) <- [(TxIn, TxOut ConwayEra)] -> [(TxIn, TxOut ConwayEra)]
spendableOuts [(TxIn, TxOut ConwayEra)]
utxos, let Coin Integer
c = BabbageTxOut ConwayEra
o BabbageTxOut ConwayEra
-> Getting Coin (BabbageTxOut ConwayEra) Coin -> Coin
forall s a. s -> Getting a s a -> a
^. (Coin -> Const Coin Coin)
-> TxOut ConwayEra -> Const Coin (TxOut ConwayEra)
Getting Coin (BabbageTxOut ConwayEra) Coin
forall era. (HasCallStack, EraTxOut era) => Lens' (TxOut era) Coin
Lens' (TxOut ConwayEra) Coin
coinTxOutL]
planPreparation
:: PParams ConwayEra
-> Addr
-> Integer
-> [(TxIn, TxOut ConwayEra)]
-> Either PrepareError Preparation
planPreparation :: PParams ConwayEra
-> Addr
-> Integer
-> [(TxIn, TxOut ConwayEra)]
-> Either PrepareError Preparation
planPreparation PParams ConwayEra
pp Addr
payerAddr Integer
required [(TxIn, TxOut ConwayEra)]
wallet = [[(TxIn, BabbageTxOut ConwayEra)]]
-> Maybe Integer -> Either PrepareError Preparation
go [[(TxIn, BabbageTxOut ConwayEra)]]
prefixes Maybe Integer
forall a. Maybe a
Nothing
where
spendable :: [(TxIn, TxOut ConwayEra)]
spendable = ((TxIn, TxOut ConwayEra) -> Down Coin)
-> [(TxIn, TxOut ConwayEra)] -> [(TxIn, TxOut ConwayEra)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (Coin -> Down Coin
forall a. a -> Down a
Down (Coin -> Down Coin)
-> ((TxIn, TxOut ConwayEra) -> Coin)
-> (TxIn, TxOut ConwayEra)
-> Down Coin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxOut ConwayEra
-> ((Coin -> Const Coin Coin)
-> TxOut ConwayEra -> Const Coin (TxOut ConwayEra))
-> Coin
forall s a. s -> Getting a s a -> a
^. (Coin -> Const Coin Coin)
-> TxOut ConwayEra -> Const Coin (TxOut ConwayEra)
forall era. (HasCallStack, EraTxOut era) => Lens' (TxOut era) Coin
Lens' (TxOut ConwayEra) Coin
coinTxOutL) (TxOut ConwayEra -> Coin)
-> ((TxIn, TxOut ConwayEra) -> TxOut ConwayEra)
-> (TxIn, TxOut ConwayEra)
-> Coin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxIn, TxOut ConwayEra) -> TxOut ConwayEra
forall a b. (a, b) -> b
snd) ([(TxIn, TxOut ConwayEra)] -> [(TxIn, TxOut ConwayEra)]
spendableOuts [(TxIn, TxOut ConwayEra)]
wallet)
prefixes :: [[(TxIn, BabbageTxOut ConwayEra)]]
prefixes = [Int
-> [(TxIn, BabbageTxOut ConwayEra)]
-> [(TxIn, BabbageTxOut ConwayEra)]
forall a. Int -> [a] -> [a]
take Int
k [(TxIn, BabbageTxOut ConwayEra)]
spendable | Int
k <- [Int
1 .. [(TxIn, BabbageTxOut ConwayEra)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(TxIn, BabbageTxOut ConwayEra)]
spendable]]
go :: [[(TxIn, BabbageTxOut ConwayEra)]]
-> Maybe Integer -> Either PrepareError Preparation
go [] Maybe Integer
best = PrepareError -> Either PrepareError Preparation
forall a b. a -> Either a b
Left (Maybe Integer -> PrepareError
shortfall Maybe Integer
best)
go ([(TxIn, BabbageTxOut ConwayEra)]
spent : [[(TxIn, BabbageTxOut ConwayEra)]]
rest) Maybe Integer
best = case [(TxIn, BabbageTxOut ConwayEra)]
-> Either (Maybe Integer) Preparation
attempt [(TxIn, BabbageTxOut ConwayEra)]
spent of
Right Preparation
prep -> Preparation -> Either PrepareError Preparation
forall a b. b -> Either a b
Right Preparation
prep
Left Maybe Integer
fee -> [[(TxIn, BabbageTxOut ConwayEra)]]
-> Maybe Integer -> Either PrepareError Preparation
go [[(TxIn, BabbageTxOut ConwayEra)]]
rest (Maybe Integer -> Maybe Integer -> Maybe Integer
forall {a}. Ord a => Maybe a -> Maybe a -> Maybe a
combineFee Maybe Integer
best Maybe Integer
fee)
combineFee :: Maybe a -> Maybe a -> Maybe a
combineFee Maybe a
Nothing Maybe a
y = Maybe a
y
combineFee Maybe a
x Maybe a
Nothing = Maybe a
x
combineFee (Just a
a) (Just a
b) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> a -> a
forall a. Ord a => a -> a -> a
max a
a a
b)
shortfall :: Maybe Integer -> PrepareError
shortfall Maybe Integer
best =
Integer
-> Maybe Integer -> Integer -> Integer -> Integer -> PrepareError
PrepareShortfall
Integer
required
Maybe Integer
best
Integer
remainderMinAll
([(TxIn, TxOut ConwayEra)] -> Integer
spendableHeld [(TxIn, TxOut ConwayEra)]
wallet)
( case [(TxIn, BabbageTxOut ConwayEra)]
spendable of
[] -> Integer
0
(TxIn
_, BabbageTxOut ConwayEra
o) : [(TxIn, BabbageTxOut ConwayEra)]
_ -> let Coin Integer
c = BabbageTxOut ConwayEra
o BabbageTxOut ConwayEra
-> Getting Coin (BabbageTxOut ConwayEra) Coin -> Coin
forall s a. s -> Getting a s a -> a
^. (Coin -> Const Coin Coin)
-> TxOut ConwayEra -> Const Coin (TxOut ConwayEra)
Getting Coin (BabbageTxOut ConwayEra) Coin
forall era. (HasCallStack, EraTxOut era) => Lens' (TxOut era) Coin
Lens' (TxOut ConwayEra) Coin
coinTxOutL in Integer
c
)
remainderMinAll :: Integer
remainderMinAll =
let tokens :: Map PolicyID (Map AssetName Integer)
tokens = [TxOut ConwayEra] -> Map PolicyID (Map AssetName Integer)
sumAssets (((TxIn, BabbageTxOut ConwayEra) -> BabbageTxOut ConwayEra)
-> [(TxIn, BabbageTxOut ConwayEra)] -> [BabbageTxOut ConwayEra]
forall a b. (a -> b) -> [a] -> [b]
map (TxIn, BabbageTxOut ConwayEra) -> BabbageTxOut ConwayEra
forall a b. (a, b) -> b
snd ([(TxIn, TxOut ConwayEra)] -> [(TxIn, TxOut ConwayEra)]
spendableOuts [(TxIn, TxOut ConwayEra)]
wallet))
Coin Integer
c =
PParams ConwayEra -> TxOut ConwayEra -> Coin
forall era. EraTxOut era => PParams era -> TxOut era -> Coin
getMinCoinTxOut
PParams ConwayEra
pp
(Addr -> Value ConwayEra -> TxOut ConwayEra
forall era.
(EraTxOut era, HasCallStack) =>
Addr -> Value era -> TxOut era
mkBasicTxOut Addr
payerAddr (Coin -> MultiAsset -> MaryValue
MaryValue (Integer -> Coin
Coin Integer
0) (Map PolicyID (Map AssetName Integer) -> MultiAsset
MultiAsset Map PolicyID (Map AssetName Integer)
tokens)))
in Integer
c
attempt :: [(TxIn, BabbageTxOut ConwayEra)]
-> Either (Maybe Integer) Preparation
attempt [(TxIn, BabbageTxOut ConwayEra)]
spent = case PParams ConwayEra
-> [(TxIn, TxOut ConwayEra)]
-> [(TxIn, TxOut ConwayEra)]
-> Addr
-> ConwayTx
-> Either BalanceError BalanceResult
balanceTx PParams ConwayEra
pp [(TxIn, TxOut ConwayEra)]
[(TxIn, BabbageTxOut ConwayEra)]
spent [] Addr
payerAddr ([(TxIn, TxOut ConwayEra)] -> ConwayTx
unsigned [(TxIn, TxOut ConwayEra)]
[(TxIn, BabbageTxOut ConwayEra)]
spent) of
Left BalanceError
err -> Maybe Integer -> Either (Maybe Integer) Preparation
forall a b. a -> Either a b
Left (BalanceError -> Maybe Integer
feeOfError BalanceError
err)
Right BalanceResult
balanced -> [(TxIn, BabbageTxOut ConwayEra)]
-> BalanceResult -> Either (Maybe Integer) Preparation
checkBalanced [(TxIn, BabbageTxOut ConwayEra)]
spent BalanceResult
balanced
unsigned :: [(TxIn, TxOut ConwayEra)] -> ConwayTx
unsigned :: [(TxIn, TxOut ConwayEra)] -> ConwayTx
unsigned [(TxIn, TxOut ConwayEra)]
spent =
TxBody TopTx ConwayEra -> ConwayTx
forall era (l :: TxLevel). EraTx era => TxBody l era -> Tx l era
forall (l :: TxLevel). TxBody l ConwayEra -> Tx l ConwayEra
mkBasicTx (TxBody TopTx ConwayEra -> ConwayTx)
-> TxBody TopTx ConwayEra -> ConwayTx
forall a b. (a -> b) -> a -> b
$
TxBody TopTx ConwayEra
forall era (l :: TxLevel).
(EraTxBody era, Typeable l) =>
TxBody l era
forall (l :: TxLevel). Typeable l => TxBody l ConwayEra
mkBasicTxBody
TxBody TopTx ConwayEra
-> (TxBody TopTx ConwayEra -> TxBody TopTx ConwayEra)
-> TxBody TopTx ConwayEra
forall a b. a -> (a -> b) -> b
& (Set TxIn -> Identity (Set TxIn))
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra)
forall era (l :: TxLevel).
EraTxBody era =>
Lens' (TxBody l era) (Set TxIn)
forall (l :: TxLevel). Lens' (TxBody l ConwayEra) (Set TxIn)
inputsTxBodyL ((Set TxIn -> Identity (Set TxIn))
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra))
-> Set TxIn -> TxBody TopTx ConwayEra -> TxBody TopTx ConwayEra
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [TxIn] -> Set TxIn
forall a. Ord a => [a] -> Set a
Set.fromList (((TxIn, BabbageTxOut ConwayEra) -> TxIn)
-> [(TxIn, BabbageTxOut ConwayEra)] -> [TxIn]
forall a b. (a -> b) -> [a] -> [b]
map (TxIn, BabbageTxOut ConwayEra) -> TxIn
forall a b. (a, b) -> a
fst [(TxIn, TxOut ConwayEra)]
[(TxIn, BabbageTxOut ConwayEra)]
spent)
TxBody TopTx ConwayEra
-> (TxBody TopTx ConwayEra -> TxBody TopTx ConwayEra)
-> TxBody TopTx ConwayEra
forall a b. a -> (a -> b) -> b
& (StrictSeq (TxOut ConwayEra)
-> Identity (StrictSeq (TxOut ConwayEra)))
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra)
(StrictSeq (BabbageTxOut ConwayEra)
-> Identity (StrictSeq (BabbageTxOut ConwayEra)))
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra)
forall era (l :: TxLevel).
EraTxBody era =>
Lens' (TxBody l era) (StrictSeq (TxOut era))
forall (l :: TxLevel).
Lens' (TxBody l ConwayEra) (StrictSeq (TxOut ConwayEra))
outputsTxBodyL
((StrictSeq (BabbageTxOut ConwayEra)
-> Identity (StrictSeq (BabbageTxOut ConwayEra)))
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra))
-> StrictSeq (BabbageTxOut ConwayEra)
-> TxBody TopTx ConwayEra
-> TxBody TopTx ConwayEra
forall s t a b. ASetter s t a b -> b -> s -> t
.~ BabbageTxOut ConwayEra -> StrictSeq (BabbageTxOut ConwayEra)
forall a. a -> StrictSeq a
StrictSeq.singleton
(Addr -> Value ConwayEra -> TxOut ConwayEra
forall era.
(EraTxOut era, HasCallStack) =>
Addr -> Value era -> TxOut era
mkBasicTxOut Addr
payerAddr (Coin -> MultiAsset -> MaryValue
MaryValue (Integer -> Coin
Coin Integer
required) MultiAsset
forall a. Monoid a => a
mempty))
TxBody TopTx ConwayEra
-> (TxBody TopTx ConwayEra -> TxBody TopTx ConwayEra)
-> TxBody TopTx ConwayEra
forall a b. a -> (a -> b) -> b
& (Coin -> Identity Coin)
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra)
forall era. EraTxBody era => Lens' (TxBody TopTx era) Coin
Lens' (TxBody TopTx ConwayEra) Coin
feeTxBodyL ((Coin -> Identity Coin)
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra))
-> Coin -> TxBody TopTx ConwayEra -> TxBody TopTx ConwayEra
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Integer -> Coin
Coin Integer
0
TxBody TopTx ConwayEra
-> (TxBody TopTx ConwayEra -> TxBody TopTx ConwayEra)
-> TxBody TopTx ConwayEra
forall a b. a -> (a -> b) -> b
& (Set (KeyHash Guard) -> Identity (Set (KeyHash Guard)))
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra)
forall era (l :: TxLevel).
(AlonzoEraTxBody era, AtMostEra "Conway" era) =>
Lens' (TxBody l era) (Set (KeyHash Guard))
forall (l :: TxLevel).
AtMostEra "Conway" ConwayEra =>
Lens' (TxBody l ConwayEra) (Set (KeyHash Guard))
Lens' (TxBody TopTx ConwayEra) (Set (KeyHash Guard))
reqSignerHashesTxBodyL
((Set (KeyHash Guard) -> Identity (Set (KeyHash Guard)))
-> TxBody TopTx ConwayEra -> Identity (TxBody TopTx ConwayEra))
-> Set (KeyHash Guard)
-> TxBody TopTx ConwayEra
-> TxBody TopTx ConwayEra
forall s t a b. ASetter s t a b -> b -> s -> t
.~ KeyHash Guard -> Set (KeyHash Guard)
forall a. a -> Set a
Set.singleton (ByteString -> KeyHash Guard
addrWitnessKeyHash (Addr -> ByteString
addrKeyHashBytes Addr
payerAddr))
feeOfError :: BalanceError -> Maybe Integer
feeOfError = \case
InsufficientFee (Coin Integer
f) Coin
_ -> Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
f
BalanceError
FeeNotConverged -> Maybe Integer
forall a. Maybe a
Nothing
CollateralShortfall Coin
_ Coin
_ -> Maybe Integer
forall a. Maybe a
Nothing
checkBalanced :: [(TxIn, BabbageTxOut ConwayEra)]
-> BalanceResult -> Either (Maybe Integer) Preparation
checkBalanced [(TxIn, BabbageTxOut ConwayEra)]
spent (BalanceResult ConwayTx
tx Int
changeIndex) = do
let body :: TxBody TopTx ConwayEra
body = ConwayTx
tx ConwayTx
-> Getting
(TxBody TopTx ConwayEra) ConwayTx (TxBody TopTx ConwayEra)
-> TxBody TopTx ConwayEra
forall s a. s -> Getting a s a -> a
^. Getting (TxBody TopTx ConwayEra) ConwayTx (TxBody TopTx ConwayEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxBody l era)
forall (l :: TxLevel). Lens' (Tx l ConwayEra) (TxBody l ConwayEra)
bodyTxL
outs :: [BabbageTxOut ConwayEra]
outs = StrictSeq (BabbageTxOut ConwayEra) -> [BabbageTxOut ConwayEra]
forall a. StrictSeq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (TxBody TopTx ConwayEra
body TxBody TopTx ConwayEra
-> Getting
(StrictSeq (BabbageTxOut ConwayEra))
(TxBody TopTx ConwayEra)
(StrictSeq (BabbageTxOut ConwayEra))
-> StrictSeq (BabbageTxOut ConwayEra)
forall s a. s -> Getting a s a -> a
^. (StrictSeq (TxOut ConwayEra)
-> Const
(StrictSeq (BabbageTxOut ConwayEra)) (StrictSeq (TxOut ConwayEra)))
-> TxBody TopTx ConwayEra
-> Const
(StrictSeq (BabbageTxOut ConwayEra)) (TxBody TopTx ConwayEra)
Getting
(StrictSeq (BabbageTxOut ConwayEra))
(TxBody TopTx ConwayEra)
(StrictSeq (BabbageTxOut ConwayEra))
forall era (l :: TxLevel).
EraTxBody era =>
Lens' (TxBody l era) (StrictSeq (TxOut era))
forall (l :: TxLevel).
Lens' (TxBody l ConwayEra) (StrictSeq (TxOut ConwayEra))
outputsTxBodyL)
Coin Integer
fee = TxBody TopTx ConwayEra
body TxBody TopTx ConwayEra
-> Getting Coin (TxBody TopTx ConwayEra) Coin -> Coin
forall s a. s -> Getting a s a -> a
^. Getting Coin (TxBody TopTx ConwayEra) Coin
forall era. EraTxBody era => Lens' (TxBody TopTx era) Coin
Lens' (TxBody TopTx ConwayEra) Coin
feeTxBodyL
Bool -> Either (Maybe Integer) () -> Either (Maybe Integer) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
changeIndex Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
1 Bool -> Bool -> Bool
|| [BabbageTxOut ConwayEra] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BabbageTxOut ConwayEra]
outs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
2) (Maybe Integer -> Either (Maybe Integer) ()
forall a b. a -> Either a b
Left Maybe Integer
forall a. Maybe a
Nothing)
case [BabbageTxOut ConwayEra]
outs of
[BabbageTxOut ConwayEra
fundOut, BabbageTxOut ConwayEra
changeOut] -> do
let Coin Integer
fundCoin = BabbageTxOut ConwayEra
fundOut BabbageTxOut ConwayEra
-> Getting Coin (BabbageTxOut ConwayEra) Coin -> Coin
forall s a. s -> Getting a s a -> a
^. (Coin -> Const Coin Coin)
-> TxOut ConwayEra -> Const Coin (TxOut ConwayEra)
Getting Coin (BabbageTxOut ConwayEra) Coin
forall era. (HasCallStack, EraTxOut era) => Lens' (TxOut era) Coin
Lens' (TxOut ConwayEra) Coin
coinTxOutL
MaryValue Coin
_ (MultiAsset Map PolicyID (Map AssetName Integer)
fundMA) = BabbageTxOut ConwayEra
fundOut BabbageTxOut ConwayEra
-> Getting MaryValue (BabbageTxOut ConwayEra) MaryValue
-> MaryValue
forall s a. s -> Getting a s a -> a
^. Getting MaryValue (BabbageTxOut ConwayEra) MaryValue
(Value ConwayEra -> Const MaryValue (Value ConwayEra))
-> TxOut ConwayEra -> Const MaryValue (TxOut ConwayEra)
forall era. EraTxOut era => Lens' (TxOut era) (Value era)
Lens' (TxOut ConwayEra) (Value ConwayEra)
valueTxOutL
Bool -> Either (Maybe Integer) () -> Either (Maybe Integer) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when
( Integer
fundCoin Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
required
Bool -> Bool -> Bool
|| BabbageTxOut ConwayEra
fundOut BabbageTxOut ConwayEra
-> Getting
(StrictMaybe (Script ConwayEra))
(BabbageTxOut ConwayEra)
(StrictMaybe (Script ConwayEra))
-> StrictMaybe (Script ConwayEra)
forall s a. s -> Getting a s a -> a
^. (StrictMaybe (Script ConwayEra)
-> Const
(StrictMaybe (Script ConwayEra)) (StrictMaybe (Script ConwayEra)))
-> TxOut ConwayEra
-> Const (StrictMaybe (Script ConwayEra)) (TxOut ConwayEra)
Getting
(StrictMaybe (Script ConwayEra))
(BabbageTxOut ConwayEra)
(StrictMaybe (Script ConwayEra))
forall era.
BabbageEraTxOut era =>
Lens' (TxOut era) (StrictMaybe (Script era))
Lens' (TxOut ConwayEra) (StrictMaybe (Script ConwayEra))
referenceScriptTxOutL StrictMaybe (Script ConwayEra)
-> StrictMaybe (Script ConwayEra) -> Bool
forall a. Eq a => a -> a -> Bool
/= StrictMaybe (Script ConwayEra)
forall a. StrictMaybe a
SNothing
Bool -> Bool -> Bool
|| Bool -> Bool
not (Map PolicyID (Map AssetName Integer) -> Bool
forall k a. Map k a -> Bool
Map.null Map PolicyID (Map AssetName Integer)
fundMA)
)
(Maybe Integer -> Either (Maybe Integer) ()
forall a b. a -> Either a b
Left Maybe Integer
forall a. Maybe a
Nothing)
let Coin Integer
changeCoin = BabbageTxOut ConwayEra
changeOut BabbageTxOut ConwayEra
-> Getting Coin (BabbageTxOut ConwayEra) Coin -> Coin
forall s a. s -> Getting a s a -> a
^. (Coin -> Const Coin Coin)
-> TxOut ConwayEra -> Const Coin (TxOut ConwayEra)
Getting Coin (BabbageTxOut ConwayEra) Coin
forall era. (HasCallStack, EraTxOut era) => Lens' (TxOut era) Coin
Lens' (TxOut ConwayEra) Coin
coinTxOutL
MaryValue Coin
_ (MultiAsset Map PolicyID (Map AssetName Integer)
changeMA) = BabbageTxOut ConwayEra
changeOut BabbageTxOut ConwayEra
-> Getting MaryValue (BabbageTxOut ConwayEra) MaryValue
-> MaryValue
forall s a. s -> Getting a s a -> a
^. Getting MaryValue (BabbageTxOut ConwayEra) MaryValue
(Value ConwayEra -> Const MaryValue (Value ConwayEra))
-> TxOut ConwayEra -> Const MaryValue (TxOut ConwayEra)
forall era. EraTxOut era => Lens' (TxOut era) (Value era)
Lens' (TxOut ConwayEra) (Value ConwayEra)
valueTxOutL
Coin Integer
changeMin = PParams ConwayEra -> TxOut ConwayEra -> Coin
forall era. EraTxOut era => PParams era -> TxOut era -> Coin
getMinCoinTxOut PParams ConwayEra
pp TxOut ConwayEra
BabbageTxOut ConwayEra
changeOut
Bool -> Either (Maybe Integer) () -> Either (Maybe Integer) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
changeCoin Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
0 Bool -> Bool -> Bool
|| Integer
changeCoin Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
changeMin) (Maybe Integer -> Either (Maybe Integer) ()
forall a b. a -> Either a b
Left (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
fee))
let (Coin Integer
inAda, Map PolicyID (Map AssetName Integer)
inMA) = [BabbageTxOut ConwayEra]
-> (Coin, Map PolicyID (Map AssetName Integer))
sumsOver (((TxIn, BabbageTxOut ConwayEra) -> BabbageTxOut ConwayEra)
-> [(TxIn, BabbageTxOut ConwayEra)] -> [BabbageTxOut ConwayEra]
forall a b. (a -> b) -> [a] -> [b]
map (TxIn, BabbageTxOut ConwayEra) -> BabbageTxOut ConwayEra
forall a b. (a, b) -> b
snd [(TxIn, BabbageTxOut ConwayEra)]
spent)
Bool -> Either (Maybe Integer) () -> Either (Maybe Integer) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Integer
inAda Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
required Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
fee Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
changeCoin Bool -> Bool -> Bool
&& Map PolicyID (Map AssetName Integer)
inMA Map PolicyID (Map AssetName Integer)
-> Map PolicyID (Map AssetName Integer) -> Bool
forall a. Eq a => a -> a -> Bool
== Map PolicyID (Map AssetName Integer)
changeMA) (Either (Maybe Integer) () -> Either (Maybe Integer) ())
-> Either (Maybe Integer) () -> Either (Maybe Integer) ()
forall a b. (a -> b) -> a -> b
$
Maybe Integer -> Either (Maybe Integer) ()
forall a b. a -> Either a b
Left Maybe Integer
forall a. Maybe a
Nothing
Preparation -> Either (Maybe Integer) Preparation
forall a b. b -> Either a b
Right
Preparation
{ prepSpent :: [(TxIn, TxOut ConwayEra)]
prepSpent = [(TxIn, TxOut ConwayEra)]
[(TxIn, BabbageTxOut ConwayEra)]
spent
, prepTx :: ConwayTx
prepTx = ConwayTx
tx
, prepFunding :: TxIn
prepFunding = TxId -> TxIx -> TxIn
TxIn (ConwayTx -> TxId
forall era (l :: TxLevel). EraTx era => Tx l era -> TxId
txIdTx ConwayTx
tx) (Word16 -> TxIx
TxIx Word16
0)
, prepFee :: Integer
prepFee = Integer
fee
, prepFundingCoin :: Integer
prepFundingCoin = Integer
required
, prepRemainderCoin :: Integer
prepRemainderCoin = Integer
changeCoin
}
[BabbageTxOut ConwayEra]
_ -> Maybe Integer -> Either (Maybe Integer) Preparation
forall a b. a -> Either a b
Left Maybe Integer
forall a. Maybe a
Nothing
sumsOver :: [BabbageTxOut ConwayEra]
-> (Coin, Map PolicyID (Map AssetName Integer))
sumsOver [BabbageTxOut ConwayEra]
outs =
( Integer -> Coin
Coin ([Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Integer
c | BabbageTxOut ConwayEra
o <- [BabbageTxOut ConwayEra]
outs, let Coin Integer
c = BabbageTxOut ConwayEra
o BabbageTxOut ConwayEra
-> Getting Coin (BabbageTxOut ConwayEra) Coin -> Coin
forall s a. s -> Getting a s a -> a
^. (Coin -> Const Coin Coin)
-> TxOut ConwayEra -> Const Coin (TxOut ConwayEra)
Getting Coin (BabbageTxOut ConwayEra) Coin
forall era. (HasCallStack, EraTxOut era) => Lens' (TxOut era) Coin
Lens' (TxOut ConwayEra) Coin
coinTxOutL])
, [TxOut ConwayEra] -> Map PolicyID (Map AssetName Integer)
sumAssets [TxOut ConwayEra]
[BabbageTxOut ConwayEra]
outs
)