diff --git a/pallas-utxorpc/src/lib.rs b/pallas-utxorpc/src/lib.rs index 7b1c2ce0..b25084e0 100644 --- a/pallas-utxorpc/src/lib.rs +++ b/pallas-utxorpc/src/lib.rs @@ -88,7 +88,7 @@ impl Mapper { .and_then(|x| x.get(&as_txref)) .and_then(|(era, cbor)| { let o = trv::MultiEraOutput::decode(*era, cbor.as_slice()).ok()?; - Some(self.map_tx_output(&o, tx)) + Some(self.map_tx_output(&o, Some(tx))) }) } @@ -136,7 +136,11 @@ impl Mapper { } } - pub fn map_tx_datum(&self, x: &trv::MultiEraOutput, tx: &trv::MultiEraTx) -> u5c::Datum { + pub fn map_tx_datum( + &self, + x: &trv::MultiEraOutput, + tx: Option<&trv::MultiEraTx>, + ) -> u5c::Datum { u5c::Datum { hash: match x.datum() { Some(babbage::PseudoDatumOption::Data(x)) => x.original_hash().to_vec().into(), @@ -146,9 +150,8 @@ impl Mapper { payload: match x.datum() { Some(babbage::PseudoDatumOption::Data(x)) => self.map_plutus_datum(&x.0).into(), Some(babbage::PseudoDatumOption::Hash(x)) => tx - .find_plutus_data(&x) - .map(|d| self.map_plutus_datum(d)) - .into(), + .and_then(|tx| tx.find_plutus_data(&x)) + .map(|d| self.map_plutus_datum(d)), _ => None, }, original_cbor: match x.datum() { @@ -158,7 +161,28 @@ impl Mapper { } } - pub fn map_tx_output(&self, x: &trv::MultiEraOutput, tx: &trv::MultiEraTx) -> u5c::TxOutput { + pub fn map_any_script(&self, x: &conway::MintedScriptRef) -> u5c::Script { + match x { + conway::PseudoScript::NativeScript(x) => u5c::Script { + script: u5c::script::Script::Native(Self::map_native_script(&x)).into(), + }, + conway::PseudoScript::PlutusV1Script(x) => u5c::Script { + script: u5c::script::Script::PlutusV1(x.0.to_vec().into()).into(), + }, + conway::PseudoScript::PlutusV2Script(x) => u5c::Script { + script: u5c::script::Script::PlutusV2(x.0.to_vec().into()).into(), + }, + conway::PseudoScript::PlutusV3Script(x) => u5c::Script { + script: u5c::script::Script::PlutusV3(x.0.to_vec().into()).into(), + }, + } + } + + pub fn map_tx_output( + &self, + x: &trv::MultiEraOutput, + tx: Option<&trv::MultiEraTx>, + ) -> u5c::TxOutput { u5c::TxOutput { address: x.address().map(|a| a.to_vec()).unwrap_or_default().into(), coin: x.value().coin(), @@ -172,25 +196,7 @@ impl Mapper { .map(|x| self.map_policy_assets(x)) .collect(), datum: self.map_tx_datum(x, tx).into(), - script: match x.script_ref() { - Some(conway::PseudoScript::NativeScript(x)) => u5c::Script { - script: u5c::script::Script::Native(Self::map_native_script(&x)).into(), /* */ - } - .into(), - Some(conway::PseudoScript::PlutusV1Script(x)) => u5c::Script { - script: u5c::script::Script::PlutusV1(x.0.to_vec().into()).into(), - } - .into(), - Some(conway::PseudoScript::PlutusV2Script(x)) => u5c::Script { - script: u5c::script::Script::PlutusV2(x.0.to_vec().into()).into(), - } - .into(), - Some(conway::PseudoScript::PlutusV3Script(x)) => u5c::Script { - script: u5c::script::Script::PlutusV3(x.0.to_vec().into()).into(), - } - .into(), - None => None, - }, + script: x.script_ref().map(|x| self.map_any_script(&x)), } } @@ -493,7 +499,7 @@ impl Mapper { outputs: tx .outputs() .iter() - .map(|x| self.map_tx_output(x, tx)) + .map(|x| self.map_tx_output(x, Some(tx))) .collect(), certificates: tx .certs() @@ -546,7 +552,9 @@ impl Mapper { .iter() .map(|x| self.map_tx_collateral(x, &resolved, tx)) .collect(), - collateral_return: tx.collateral_return().map(|x| self.map_tx_output(&x, tx)), + collateral_return: tx + .collateral_return() + .map(|x| self.map_tx_output(&x, Some(tx))), total_collateral: tx.total_collateral().unwrap_or_default(), } .into(),