Skip to content

Commit

Permalink
revise block api
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroShkvorets committed Mar 20, 2024
1 parent 2e31daf commit 395e20d
Showing 4 changed files with 178 additions and 88 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"core",
"substreams-antelope",
@@ -12,7 +13,7 @@ opt-level = 's'
strip = "debuginfo"

[workspace.package]
version = "0.3.4"
version = "0.4.0"
edition = "2021"
description = "Substreams development kit for Antelope chains, contains Firehose Block model and helpers."
authors = ["Fred <fred@pinax.network>", "Denis <denis@pinax.network>", "Yaro <yaro@pinax.network>"]
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -15,31 +15,23 @@

- [Substreams documentation](https://substreams.streamingfast.io)

## 🛠 Feature Roadmap

- [x] [Antelope blocks](https://github.com/pinax-network/firehose-antelope/blob/develop/proto/sf/antelope/type/v1/type.proto)
- [x] Block helper methods
- [x] all_transaction_traces
- [x] executed_transaction_traces
- [x] transaction_traces_count
- [x] executed_input_action_count
- [x] executed_total_action_count
- [x] actions()

## Install

```
$ cargo add substreams-antelope
```

## Quickstart
## Usage

Refer to [Docs.rs](https://docs.rs/substreams-antelope/latest/substreams_antelope/struct.Block.html#implementations) for helper methods on `Block` that extract action and transaction iterators from the Antelope block.

**Cargo.toml**

```toml
[dependencies]
substreams = "0.5"
substreams-antelope = "0.2"
substreams-antelope = "0.4"
```

**src/lib.rs**
@@ -53,7 +45,7 @@ use substreams_antelope::{Block, ActionTraces};
fn map_action_traces(block: Block) -> Result<ActionTraces, Error> {
let mut action_traces = vec![];

for trx in block.executed_transaction_traces() {
for trx in block.transaction_traces() {
for trace in trx.action_traces {
action_traces.push(trace);
}
102 changes: 102 additions & 0 deletions abigen-tests/tests/abigen_test.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@ mod tests {

use substreams_antelope::pb;
use substreams_antelope_abigen::Abigen;
use pb::TransactionStatus::TransactionstatusExecuted;

mod actions {
use substreams_antelope_abigen::decoder::decode;
use substreams_antelope_abigen::types::*;
@@ -22,6 +24,20 @@ mod tests {
Ok(decode::<Self>(&trace.action.as_ref().unwrap().json_data)?)
}
}

#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Transfer2 {
pub from: Name,
pub to: Name,
pub quantity: Asset,
}
impl substreams_antelope::Action for Transfer2 {
const NAME: &'static str = "transfer2";
fn decode(trace: &substreams_antelope::pb::ActionTrace) -> Result<Self, substreams_antelope::Error> {
Ok(decode::<Self>(&trace.action.as_ref().unwrap().json_data)?)
}
}
}

fn create_test_block(
@@ -52,7 +68,12 @@ mod tests {
fn create_transfer_trace() -> pb::TransactionTrace {
pb::TransactionTrace {
id: String::from("trx1"),
receipt: Some(pb::TransactionReceiptHeader {
status: TransactionstatusExecuted as i32,
..Default::default()
}),
action_traces: vec![
// transfer action
pb::ActionTrace {
action: Some(pb::Action {
account: "tokencontract".into(),
@@ -63,6 +84,7 @@ mod tests {
receiver: "tokencontract".into(),
..Default::default()
},
// notification #1
pb::ActionTrace {
action: Some(pb::Action {
account: "tokencontract".into(),
@@ -73,6 +95,7 @@ mod tests {
receiver: "acc1".into(),
..Default::default()
},
// notification #2
pb::ActionTrace {
action: Some(pb::Action {
account: "tokencontract".into(),
@@ -83,6 +106,39 @@ mod tests {
receiver: "acc2".into(),
..Default::default()
},
// action from another token contract
pb::ActionTrace {
action: Some(pb::Action {
account: "othercontract".into(),
name: "transfer".into(),
json_data: r#"{"from": "acc1", "to": "acc2", "quantity": "1.0000 EOS", "memo": "hello"}"#.into(),
..Default::default()
}),
receiver: "othercontract".into(),
..Default::default()
},
// transfer2 action without memo
pb::ActionTrace {
action: Some(pb::Action {
account: "tokencontract".into(),
name: "transfer2".into(),
json_data: r#"{"from": "acc1", "to": "acc2", "quantity": "1.0000 EOS"}"#.into(),
..Default::default()
}),
receiver: "tokencontract".into(),
..Default::default()
},
// transfer2 notification without memo
pb::ActionTrace {
action: Some(pb::Action {
account: "tokencontract".into(),
name: "transfer2".into(),
json_data: r#"{"from": "acc1", "to": "acc2", "quantity": "1.0000 EOS"}"#.into(),
..Default::default()
}),
receiver: "acc2".into(),
..Default::default()
},
],
..Default::default()
}
@@ -138,6 +194,32 @@ mod tests {
&transfer_trace.action_traces[0]
),]
);
pretty_assertions::assert_eq!(
actions.len(),
1
);
}

#[test]
fn test_actions_2() {
let transfer_trace = create_transfer_trace();
let block = create_test_block(false, vec![transfer_trace.clone()], vec![], 2, 0, 5, 0, 7, 0);
let actions: Vec<_> = block.actions::<actions::Transfer2>(&["tokencontract"]).collect();
pretty_assertions::assert_eq!(
actions,
vec![(
actions::Transfer2 {
from: "acc1".into(),
to: "acc2".into(),
quantity: "1.0000 EOS".into(),
},
&transfer_trace.action_traces[4]
),]
);
pretty_assertions::assert_eq!(
actions.len(),
1
);
}

#[test]
@@ -169,4 +251,24 @@ mod tests {
]
);
}

#[test]
fn test_notifications_2() {
let transfer_trace = create_transfer_trace();
let block = create_test_block(false, vec![transfer_trace.clone()], vec![], 2, 0, 5, 0, 7, 0);
let actions: Vec<_> = block.notifications::<actions::Transfer2>(&["tokencontract"]).collect();
pretty_assertions::assert_eq!(
actions,
vec![
(
actions::Transfer2 {
from: "acc1".into(),
to: "acc2".into(),
quantity: "1.0000 EOS".into(),
},
&transfer_trace.action_traces[5]
),
]
);
}
}
Loading

0 comments on commit 395e20d

Please sign in to comment.