Skip to content

Commit

Permalink
fix(utxobystake): Use stake address instead of delegation part (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Feb 8, 2023
1 parent 11b907b commit 7bec205
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/reducers/mod.rs
Expand Up @@ -34,9 +34,9 @@ pub mod tx_count_by_address;
#[cfg(feature = "unstable")]
pub mod tx_count_by_native_token_policy_id;
#[cfg(feature = "unstable")]
pub mod utxos_by_asset;
pub mod utxo_by_stake;
#[cfg(feature = "unstable")]
pub mod utxos_by_stake;
pub mod utxos_by_asset;

#[derive(Deserialize)]
#[serde(tag = "type")]
Expand Down
46 changes: 32 additions & 14 deletions src/reducers/utxo_by_stake.rs
@@ -1,3 +1,4 @@
use pallas::ledger::addresses::{self, Address, StakeAddress};
use pallas::ledger::traverse::MultiEraOutput;
use pallas::ledger::traverse::{MultiEraBlock, MultiEraTx, OutputRef};
use serde::Deserialize;
Expand All @@ -15,6 +16,17 @@ pub struct Reducer {
policy: crosscut::policies::RuntimePolicy,
}

fn any_address_to_stake_bech32(address: Address) -> Option<String> {
match address {
Address::Shelley(s) => match StakeAddress::try_from(s).ok() {
Some(x) => x.to_bech32().ok(),
_ => None,
},
Address::Byron(_) => None,
Address::Stake(_) => None,
}
}

impl Reducer {
fn process_consumed_txo(
&mut self,
Expand All @@ -29,7 +41,8 @@ impl Reducer {
None => return Ok(()),
};

let stake_address = self.get_stake_from_utxo(&utxo);
let address = utxo.address().or_panic()?;
let stake_address = any_address_to_stake_bech32(address);

let stake_address = match stake_address {
Some(x) => x,
Expand Down Expand Up @@ -59,7 +72,8 @@ impl Reducer {
output: &mut super::OutputPort,
) -> Result<(), gasket::error::Error> {
let tx_hash = tx.hash();
let stake_address = self.get_stake_from_utxo(tx_output);
let address = tx_output.address().or_panic()?;
let stake_address = any_address_to_stake_bech32(address);

let stake_address = match stake_address {
Some(x) => x,
Expand All @@ -81,18 +95,6 @@ impl Reducer {
output.send(crdt.into())
}

fn get_stake_from_utxo(&mut self, output: &MultiEraOutput) -> Option<String> {
let stake_address = match output.address().unwrap() {
pallas::ledger::addresses::Address::Shelley(shelley_addr) => {
Some(shelley_addr.delegation().to_bech32().unwrap())
}
pallas::ledger::addresses::Address::Byron(_) => None,
pallas::ledger::addresses::Address::Stake(_) => None,
};

stake_address
}

pub fn reduce_block<'b>(
&mut self,
block: &'b MultiEraBlock<'b>,
Expand Down Expand Up @@ -123,3 +125,19 @@ impl Config {
super::Reducer::UtxoByStake(reducer)
}
}

#[cfg(test)]
mod test {
use super::any_address_to_stake_bech32;
use pallas::ledger::addresses::Address;

#[test]
fn stake_bech32() {
let addr = Address::from_bech32("addr1q86gknmykuldcngv0atyy56ex598p6m8f24nf9nmehmgpgfcmswqs6wnpls37lh7s3du977cxw67a9dpndnmafjs08asyqxe39").unwrap();
let stake_bech32 = any_address_to_stake_bech32(addr).unwrap();
assert_eq!(
stake_bech32,
"stake1uyudc8qgd8fslcgl0mlggk7zl0vr8d0wjksekea75eg8n7cw33m0s"
);
}
}
20 changes: 17 additions & 3 deletions src/reducers/utxos_by_asset.rs
Expand Up @@ -30,7 +30,7 @@ impl Reducer {

fn process_asset(
&mut self,
tx_hash: Hash<32>,
tx_hash: &Hash<32>,
txo_idx: u64,
policy: Hash<28>,
asset: Vec<u8>,
Expand Down Expand Up @@ -62,15 +62,29 @@ impl Reducer {
for (tx_ref, tx_output) in ctx.find_consumed_txos(&tx, &self.policy).or_panic()? {
for asset in tx_output.assets() {
if let Asset::NativeAsset(policy, asset, delta) = asset {
self.process_asset(tx_ref.hash(), tx_ref.index(), policy, asset, -1 * delta as i64, output)?;
self.process_asset(
tx_ref.hash(),
tx_ref.index(),
policy,
asset,
-1 * delta as i64,
output,
)?;
}
}
}

for (idx, txo) in tx.produces() {
for asset in txo.assets() {
if let Asset::NativeAsset(policy, asset, delta) = asset {
self.process_asset(tx.hash(), idx as u64, policy, asset, delta as i64, output)?;
self.process_asset(
&tx.hash(),
idx as u64,
policy,
asset,
delta as i64,
output,
)?;
}
}
}
Expand Down

0 comments on commit 7bec205

Please sign in to comment.