Skip to content

Commit

Permalink
feat(interop): support standalone utxo mapper for u5c (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Jan 17, 2025
1 parent 022b521 commit b5bcb9e
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions pallas-utxorpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<C: LedgerContext> Mapper<C> {
.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)))
})
}

Expand Down Expand Up @@ -136,7 +136,11 @@ impl<C: LedgerContext> Mapper<C> {
}
}

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(),
Expand All @@ -146,9 +150,8 @@ impl<C: LedgerContext> Mapper<C> {
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() {
Expand All @@ -158,7 +161,28 @@ impl<C: LedgerContext> Mapper<C> {
}
}

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(),
Expand All @@ -172,25 +196,7 @@ impl<C: LedgerContext> Mapper<C> {
.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)),
}
}

Expand Down Expand Up @@ -493,7 +499,7 @@ impl<C: LedgerContext> Mapper<C> {
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()
Expand Down Expand Up @@ -546,7 +552,9 @@ impl<C: LedgerContext> Mapper<C> {
.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(),
Expand Down

0 comments on commit b5bcb9e

Please sign in to comment.