1313module Cardano.Rpc.Server.Internal.UtxoRpc.Type
1414 ( utxoRpcPParamsToProtocolParams
1515 , utxoToUtxoRpcAnyUtxoData
16+ , anyUtxoDataUtxoRpcToUtxo
1617 , txOutToUtxoRpcTxOutput
1718 , utxoRpcTxOutputToTxOut
1819 , protocolParamsToUtxoRpcPParams
@@ -37,6 +38,7 @@ import Cardano.Api.Value
3738import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as UtxoRpc
3839import Cardano.Rpc.Server.Internal.Orphans ()
3940
41+ import Cardano.Binary qualified as CBOR
4042import Cardano.Ledger.Api qualified as L
4143import Cardano.Ledger.BaseTypes (WithOrigin (.. ))
4244import Cardano.Ledger.BaseTypes qualified as L
@@ -430,13 +432,52 @@ scriptDataToUtxoRpcPlutusData = \case
430432utxoToUtxoRpcAnyUtxoData :: forall era . IsEra era => UTxO era -> [Proto UtxoRpc. AnyUtxoData ]
431433utxoToUtxoRpcAnyUtxoData utxo =
432434 toList utxo <&> \ (txIn, txOut) -> do
435+ let era = useEra @ era
436+ txOutCbor =
437+ obtainCommonConstraints era $
438+ CBOR. serialize' $
439+ toShelleyTxOut (convert era) txOut
433440 defMessage
434- & # nativeBytes .~ " " -- TODO where to get that from? run cbor serialisation of utxos list?
441+ & # nativeBytes .~ txOutCbor
435442 & # txoRef .~ inject txIn
436443 & # cardano .~ txOutToUtxoRpcTxOutput txOut
437444
445+ anyUtxoDataUtxoRpcToUtxo
446+ :: forall era m
447+ . HasCallStack
448+ => MonadThrow m
449+ => Era era
450+ -> [Proto UtxoRpc. AnyUtxoData ]
451+ -> m (UTxO era )
452+ anyUtxoDataUtxoRpcToUtxo era = fmap fromList . foldM f mempty
453+ where
454+ f
455+ :: [(TxIn , TxOut CtxUTxO era )]
456+ -> Proto UtxoRpc. AnyUtxoData
457+ -> m [(TxIn , TxOut CtxUTxO era )]
458+ f acc e = do
459+ txOut <- obtainCommonConstraints era $ utxoRpcTxOutputToTxOut $ e ^. # cardano
460+ txIn <- txoRefUtxoRpcToTxIn $ e ^. # txoRef
461+ pure $ (txIn, txOut) : acc
462+
463+ txoRefUtxoRpcToTxIn
464+ :: forall m
465+ . HasCallStack
466+ => MonadThrow m
467+ => Proto UtxoRpc. TxoRef
468+ -> m TxIn
469+ txoRefUtxoRpcToTxIn txoRef = do
470+ txId' <-
471+ liftEitherError $
472+ deserialiseFromRawBytes asType $
473+ txoRef ^. # hash
474+ pure $ TxIn txId' (TxIx . fromIntegral $ txoRef ^. # index)
475+
438476txOutToUtxoRpcTxOutput
439- :: forall era . IsEra era => TxOut CtxUTxO era -> Proto UtxoRpc. TxOutput
477+ :: forall era
478+ . IsEra era
479+ => TxOut CtxUTxO era
480+ -> Proto UtxoRpc. TxOutput
440481txOutToUtxoRpcTxOutput (TxOut addressInEra txOutValue datum script) = do
441482 let multiAsset =
442483 fromList $
@@ -487,7 +528,8 @@ utxoRpcTxOutputToTxOut txOutput = do
487528 addrUtf8 <- liftEitherError $ T. decodeUtf8' (txOutput ^. # address)
488529 address <-
489530 maybe (throwM . stringException $ " Cannot decode address: " <> T. unpack addrUtf8) pure $
490- deserialiseAddress (AsAddress AsShelleyAddr ) addrUtf8
531+ obtainCommonConstraints era $
532+ deserialiseAddress asType addrUtf8
491533 datum <-
492534 case txOutput ^. # maybe'datum of
493535 Just datumRpc ->
@@ -515,7 +557,7 @@ utxoRpcTxOutputToTxOut txOutput = do
515557 pure (AssetId pId assetName, outCoin <> mintCoin)
516558 pure $
517559 TxOut
518- ( AddressInEra ( ShelleyAddressInEra (convert era)) address)
560+ address
519561 ( obtainCommonConstraints era $
520562 TxOutValueShelleyBased (convert era) (toMaryValue $ coinValue <> multiAssetValue)
521563 )
0 commit comments