diff --git a/Cargo.lock b/Cargo.lock index 3ee4faea..dec518d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1089,8 +1089,8 @@ checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" [[package]] name = "pallas" -version = "0.14.0" -source = "git+https://github.com/txpipe/pallas.git#c647e515106bf3cbe37a2066d012a399beed6af1" +version = "0.15.0" +source = "git+https://github.com/txpipe/pallas.git#323402eb54996756c61b5cc1c5805ddaadc79e75" dependencies = [ "pallas-addresses", "pallas-codec", @@ -1103,8 +1103,8 @@ dependencies = [ [[package]] name = "pallas-addresses" -version = "0.14.0" -source = "git+https://github.com/txpipe/pallas.git#c647e515106bf3cbe37a2066d012a399beed6af1" +version = "0.15.0" +source = "git+https://github.com/txpipe/pallas.git#323402eb54996756c61b5cc1c5805ddaadc79e75" dependencies = [ "base58", "bech32 0.9.1", @@ -1116,8 +1116,8 @@ dependencies = [ [[package]] name = "pallas-codec" -version = "0.14.0" -source = "git+https://github.com/txpipe/pallas.git#c647e515106bf3cbe37a2066d012a399beed6af1" +version = "0.15.0" +source = "git+https://github.com/txpipe/pallas.git#323402eb54996756c61b5cc1c5805ddaadc79e75" dependencies = [ "hex", "minicbor 0.18.0", @@ -1126,8 +1126,8 @@ dependencies = [ [[package]] name = "pallas-crypto" -version = "0.14.0" -source = "git+https://github.com/txpipe/pallas.git#c647e515106bf3cbe37a2066d012a399beed6af1" +version = "0.15.0" +source = "git+https://github.com/txpipe/pallas.git#323402eb54996756c61b5cc1c5805ddaadc79e75" dependencies = [ "cryptoxide", "hex", @@ -1139,8 +1139,8 @@ dependencies = [ [[package]] name = "pallas-miniprotocols" -version = "0.14.0" -source = "git+https://github.com/txpipe/pallas.git#c647e515106bf3cbe37a2066d012a399beed6af1" +version = "0.15.0" +source = "git+https://github.com/txpipe/pallas.git#323402eb54996756c61b5cc1c5805ddaadc79e75" dependencies = [ "hex", "itertools", @@ -1152,8 +1152,8 @@ dependencies = [ [[package]] name = "pallas-multiplexer" -version = "0.14.0" -source = "git+https://github.com/txpipe/pallas.git#c647e515106bf3cbe37a2066d012a399beed6af1" +version = "0.15.0" +source = "git+https://github.com/txpipe/pallas.git#323402eb54996756c61b5cc1c5805ddaadc79e75" dependencies = [ "byteorder", "hex", @@ -1165,8 +1165,8 @@ dependencies = [ [[package]] name = "pallas-primitives" -version = "0.14.0" -source = "git+https://github.com/txpipe/pallas.git#c647e515106bf3cbe37a2066d012a399beed6af1" +version = "0.15.0" +source = "git+https://github.com/txpipe/pallas.git#323402eb54996756c61b5cc1c5805ddaadc79e75" dependencies = [ "base58", "bech32 0.9.1", @@ -1180,8 +1180,8 @@ dependencies = [ [[package]] name = "pallas-traverse" -version = "0.14.0" -source = "git+https://github.com/txpipe/pallas.git#c647e515106bf3cbe37a2066d012a399beed6af1" +version = "0.15.0" +source = "git+https://github.com/txpipe/pallas.git#323402eb54996756c61b5cc1c5805ddaadc79e75" dependencies = [ "hex", "pallas-addresses", diff --git a/src/reducers/address_by_stake.rs b/src/reducers/address_by_stake.rs new file mode 100644 index 00000000..4b4211ef --- /dev/null +++ b/src/reducers/address_by_stake.rs @@ -0,0 +1,58 @@ +use pallas::ledger::addresses::Address::Shelley; +use pallas::ledger::addresses::{Error, StakeAddress}; +use pallas::ledger::traverse::MultiEraBlock; +use serde::Deserialize; + +use crate::{model, prelude::*}; + +#[derive(Deserialize)] +pub struct Config { + pub key_prefix: Option, +} + +pub struct Reducer { + config: Config, +} + +impl Reducer { + pub fn reduce_block<'b>( + &mut self, + block: &'b MultiEraBlock<'b>, + output: &mut super::OutputPort, + ) -> Result<(), gasket::error::Error> { + for tx in block.txs().into_iter() { + for (_, out) in tx.produces().into_iter() { + let address = out.address().or_panic()?; + match address { + Shelley(shelly_address) => { + let stake_address_result: Result = + shelly_address.clone().try_into(); + if stake_address_result.is_ok() { + let stake_address = + stake_address_result.unwrap().to_bech32().or_panic()?; + let payment_address = shelly_address.to_bech32().or_panic()?; + + let crdt = model::CRDTCommand::set_add( + self.config.key_prefix.as_deref(), + &stake_address, + payment_address, + ); + output.send(crdt.into()); + } + } + _ => (), + }; + } + } + + Ok(()) + } +} + +impl Config { + pub fn plugin(self) -> super::Reducer { + let reducer = Reducer { config: self }; + + super::Reducer::AddressByStake(reducer) + } +} diff --git a/src/reducers/mod.rs b/src/reducers/mod.rs index 2cac97bf..7d583030 100644 --- a/src/reducers/mod.rs +++ b/src/reducers/mod.rs @@ -35,6 +35,8 @@ pub mod tx_count_by_address; pub mod tx_count_by_native_token_policy_id; #[cfg(feature = "unstable")] pub mod utxo_by_nft; +#[cfg(feature = "unstable")] +pub mod address_by_stake; #[derive(Deserialize)] #[serde(tag = "type")] @@ -63,6 +65,8 @@ pub enum Config { AssetHoldersByAsset(asset_holders_by_asset_id::Config), #[cfg(feature = "unstable")] UtxoByNft(utxo_by_nft::Config), + #[cfg(feature = "unstable")] + AddressByStake(address_by_stake::Config) } impl Config { @@ -96,6 +100,8 @@ impl Config { Config::AssetHoldersByAsset(c) => c.plugin(chain, policy), #[cfg(feature = "unstable")] Config::UtxoByNft(c) => c.plugin(policy), + #[cfg(feature = "unstable")] + Config::AddressByStake(c) => c.plugin(), } } } @@ -170,6 +176,8 @@ pub enum Reducer { AssetHoldersByAssetId(asset_holders_by_asset_id::Reducer), #[cfg(feature = "unstable")] UtxoByNft(utxo_by_nft::Reducer), + #[cfg(feature = "unstable")] + AddressByStake(address_by_stake::Reducer), } impl Reducer { @@ -204,6 +212,8 @@ impl Reducer { Reducer::AssetHoldersByAssetId(x) => x.reduce_block(block, ctx, output), #[cfg(feature = "unstable")] Reducer::UtxoByNft(x) => x.reduce_block(block, ctx, output), + #[cfg(feature = "unstable")] + Reducer::AddressByStake(x) => x.reduce_block(block, output), } } }