Skip to content

Commit

Permalink
feat: add withdrawal redeemers
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Jul 15, 2024
1 parent e03e374 commit ff3f118
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions pallas-traverse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pallas-codec = { version = "=0.28.0", path = "../pallas-codec" }
hex = "0.4.3"
thiserror = "1.0.31"
paste = "1.0.14"
itertools = "0.13.0"

# TODO: remove once GenesisValue moves into new genesis crate
serde = "1.0.155"
Expand Down
14 changes: 14 additions & 0 deletions pallas-traverse/src/tx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{borrow::Cow, collections::HashSet, ops::Deref};

use itertools::Itertools;
use pallas_codec::{minicbor, utils::KeepRaw};
use pallas_crypto::hash::Hash;
use pallas_primitives::{
Expand Down Expand Up @@ -217,6 +218,19 @@ impl<'b> MultiEraTx<'b> {
raw
}

pub fn withdrawals_sorted_set(&self) -> Vec<(&[u8], u64)> {
match self.withdrawals() {
MultiEraWithdrawals::NotApplicable | MultiEraWithdrawals::Empty => {
std::iter::empty().collect()
}
MultiEraWithdrawals::AlonzoCompatible(x) => x
.iter()
.map(|(k, v)| (k.as_slice(), *v))
.sorted_by_key(|(k, _)| *k)
.collect(),
}
}

/// Return the transaction reference inputs
///
/// NOTE: It is possible for this to return duplicates. See
Expand Down
7 changes: 7 additions & 0 deletions pallas-traverse/src/witnesses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ impl<'b> MultiEraTx<'b> {
})
}

pub fn find_withdrawal_redeemer(&self, withdrawal_order: u32) -> Option<MultiEraRedeemer> {
self.redeemers().into_iter().find(|r| {
r.tag() == pallas_primitives::conway::RedeemerTag::Reward
&& r.index() == withdrawal_order
})
}

pub fn plutus_v2_scripts(&self) -> &[PlutusV2Script] {
match self {
Self::Byron(_) => &[],
Expand Down
18 changes: 13 additions & 5 deletions pallas-utxorpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,19 @@ impl<C: LedgerContext> Mapper<C> {
}
}

pub fn map_withdrawals(&self, x: &(&[u8], u64)) -> u5c::Withdrawal {
pub fn map_withdrawals(
&self,
x: &(&[u8], u64),
tx: &trv::MultiEraTx,
// lexicographical order of the input we're mapping
order: u32,
) -> u5c::Withdrawal {
u5c::Withdrawal {
reward_account: Vec::from(x.0).into(),
coin: x.1,
redeemer: None, // TODO
redeemer: tx
.find_withdrawal_redeemer(order)
.map(|x| self.map_redeemer(&x)),
}
}

Expand Down Expand Up @@ -536,10 +544,10 @@ impl<C: LedgerContext> Mapper<C> {
outputs: tx.outputs().iter().map(|x| self.map_tx_output(x)).collect(),
certificates: tx.certs().iter().map(|x| self.map_cert(x)).collect(),
withdrawals: tx
.withdrawals()
.collect::<Vec<_>>()
.withdrawals_sorted_set()
.iter()
.map(|x| self.map_withdrawals(x))
.enumerate()
.map(|(order, x)| self.map_withdrawals(x, tx, order as u32))
.collect(),
mint: tx
.mints_sorted_set()
Expand Down

0 comments on commit ff3f118

Please sign in to comment.