@@ -43,7 +43,7 @@ import Data.Either (fromRight)
4343import Data.Either.Combinators (mapLeft , maybeToRight )
4444import Data.Hex (hex )
4545import Data.Kind (Type )
46- import Data.List (nub , sort )
46+ import Data.List (nub , sort , sortOn )
4747import Data.Map (Map )
4848import Data.Map qualified as Map
4949import Data.Maybe (fromMaybe )
@@ -97,7 +97,9 @@ import Plutus.V1.Ledger.Api (
9797 TxInfo (TxInfo ),
9898 )
9999import Plutus.V1.Ledger.Api qualified as Plutus
100+ import PlutusTx.AssocMap qualified as AMap
100101import PlutusTx.Builtins (fromBuiltin )
102+ import PlutusTx.Prelude qualified as PPrelude
101103import Prelude
102104
103105-- | Getting information of the latest block
@@ -192,8 +194,11 @@ queryTxOuts ::
192194queryTxOuts txIds = do
193195 res <- queryChainIndex @ w $ TxsFromTxIds txIds
194196 return $ case res of
195- TxIdsResponse chainTxs -> Right $ foldMap (fmap fst . txOutRefMap) chainTxs
197+ TxIdsResponse chainTxs -> Right $ foldMap (fmap (sortTxOut . fst ) . txOutRefMap) chainTxs
196198 _ -> Left " Wrong PAB response"
199+ where
200+ sortTxOut :: TxOut -> TxOut
201+ sortTxOut txOut = txOut {txOutValue = sortValue $ txOutValue txOut}
197202
198203-- There is no match txOutRefs request, and we don't want a separate PAB query per input.
199204-- So, for efficiency, we're going to query the transactions for all inputs combined,
@@ -220,23 +225,30 @@ buildTxInfo ::
220225 Tx ->
221226 Eff effs (Either Text TxInfo )
222227buildTxInfo pabConf tx = do
223- let txOutRefs = txInRef <$> Set. toList (txInputs tx)
228+ let txOutRefs = txInRef <$> Set. toList (txInputs tx) -- This will already be in order, for Sets listify acsending
224229 eTxInInfos <- getTxInInfos @ w txOutRefs
225230 return $
226231 (`second` eTxInInfos) $ \ txInInfos ->
227232 TxInfo
228233 { txInfoInputs = txInInfos
229234 , txInfoOutputs = txOutputs tx
230- , txInfoFee = txFee tx
231- , txInfoMint = txMint tx
235+ , txInfoFee = sortValue $ txFee tx
236+ , txInfoMint = sortValue $ txMint tx
232237 , txInfoDCert = [] -- We don't support staking or stake redeeming at this time
233238 , txInfoWdrl = []
234239 , txInfoValidRange = slotRangeToPOSIXTimeRange (pcSlotConfig pabConf) $ txValidRange tx
235- , txInfoSignatories = Ledger. pubKeyHash <$> Map. keys (txSignatures tx)
236- , txInfoData = Map. toList $ txData tx
240+ , txInfoSignatories = sort $ Ledger. pubKeyHash <$> Map. keys (txSignatures tx)
241+ , txInfoData = sortOn fst $ Map. toList $ txData tx
237242 , txInfoId = Ledger. txId tx
238243 }
239244
245+ -- | Sorts the internal maps
246+ sortValue :: Value -> Value
247+ sortValue (Value. Value m) = Value. Value $ sortMap $ sortMap PPrelude. <$> m
248+ where
249+ sortMap :: forall (k :: Type ) (v :: Type ). Ord k => AMap. Map k v -> AMap. Map k v
250+ sortMap = AMap. fromList . sortOn fst . AMap. toList
251+
240252-- | Build a tx body and write it to disk
241253buildTx ::
242254 forall (w :: Type ) (effs :: [Type -> Type ]).
@@ -476,19 +488,19 @@ unsafeSerialiseAddress network address =
476488 Right a -> a
477489 Left _ -> error " Couldn't create address"
478490
479- -- calculateExBudget :: Script -> [BuiltinData] -> Either Text ExBudget
480- -- calculateExBudget script builtinData = do
481- -- -- TODO, pull this from the protocol, they're the same for now but may not always be
482- -- modelParams <- maybeToRight "Cost model params invalid." Plutus.defaultCostModelParams
483- -- let serialisedScript = ShortByteString.toShort $ LazyByteString.toStrict $ Codec.serialise script
484- -- pData = map Plutus.builtinDataToData builtinData
485- -- mapLeft showText $
486- -- snd $
487- -- Plutus.evaluateScriptCounting Plutus.Verbose modelParams serialisedScript pData
488-
489491calculateExBudget :: Script -> [BuiltinData ] -> Either Text ExBudget
490492calculateExBudget script builtinData = do
491- mapLeft showText $ fst <$> Scripts. evaluateScript (Scripts. applyArguments script $ Plutus. builtinDataToData <$> builtinData)
493+ -- TODO, pull this from the protocol, they're the same for now but may not always be
494+ modelParams <- maybeToRight " Cost model params invalid." Plutus. defaultCostModelParams
495+ let serialisedScript = ShortByteString. toShort $ LazyByteString. toStrict $ Codec. serialise script
496+ pData = map Plutus. builtinDataToData builtinData
497+ mapLeft showText $
498+ snd $
499+ Plutus. evaluateScriptCounting Plutus. Verbose modelParams serialisedScript pData
500+
501+ -- calculateExBudget :: Script -> [BuiltinData] -> Either Text ExBudget
502+ -- calculateExBudget script builtinData = do
503+ -- mapLeft showText $ fst <$> Scripts.evaluateScript (Scripts.applyArguments script $ Plutus.builtinDataToData <$> builtinData)
492504
493505exBudgetToCliArg :: ExBudget -> Text
494506exBudgetToCliArg (ExBudget (ExCPU steps) (ExMemory memory)) =
0 commit comments