Skip to content

Commit

Permalink
Fixed all doctests, remove all ignores, move docs in substreams-macro…
Browse files Browse the repository at this point in the history
… directly to `substreams::handlers`
  • Loading branch information
maoueh committed Jun 3, 2022
1 parent b98ca77 commit d615bad
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-unknown-unknown"
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 0 additions & 61 deletions rust/substreams-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,11 @@ mod errors;
mod handler;
mod store;

/// Marks function to setup substream map handler wasm boilerplate
///
/// ## Usage
///
///
/// ```rust,ignore
/// #[substreams::handlers::map]
/// fn map_handler(blk: eth::Block) -> Result<eth::TransactionTrace, substreams::error::Error> {
/// Ok(blk.transactions_traces[0])
/// }
/// ```
///
/// Equivalent code not using `#[substream::handlers::map]`
///
/// ```rust,ignore
/// #[no_mangle]
/// pub extern "C" fn map_handler(blk_ptr: *mut u8, blk_len: usize) {
/// substreams::register_panic_hook();
/// let func = || -> Result<eth::TransactionTrace, substreams::error::Error> {
/// let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len).unwrap();
/// {
/// Ok(blk.transactions_traces[0])
/// }
/// };
/// let result = func();
/// if result.is_err() {
/// panic!(result.err().unwrap())
/// }
/// substreams::output(substreams::proto::encode(result).unwrap());
/// }
/// ```
#[proc_macro_attribute]
pub fn map(args: TokenStream, item: TokenStream) -> TokenStream {
return handler::main(args, item, config::ModuleType::Map);
}

/// Marks function to setup substream store handler wasm boilerplate
/// ## Usage
///
///
/// ```rust,ignore
/// use substreams::{log, store};
///
/// #[substreams::handlers::store]
/// fn build_nft_state(transfers: erc721::Transfers, s: store::StoreAddInt64, pairs: store::Reader, tokens: store::Reader) {
/// log::info!("Length {}", transfers.len());
/// }
/// ```
///
/// Equivalent code not using `#[substream::handlers::store]`
///
/// ```rust,ignore
/// use substreams::{log, store};
///
/// #[no_mangle]
/// pub extern "C" fn build_nft_state(transfers_ptr: *mut u8, transfers_len: usize, pairs_idx: u32, tokens_idx: u32) {
/// substreams::register_panic_hook();
/// let transfers: erc721::Transfers = substreams::proto::decode_ptr(transfers_ptr, transfers_len).unwrap();
/// let pairs: store::Reader = store::Reader::new(pairs_idx);
/// let tokens: store::Reader = store::Reader::new(tokens_idx);
/// let s: store::StoreAddInt64 = store::StoreAddInt64::new();
/// {
/// log::info!("Length {}", transfers.len());
/// }
/// }
/// ```
#[proc_macro_attribute]
pub fn store(args: TokenStream, item: TokenStream) -> TokenStream {
return handler::main(args, item, config::ModuleType::Store);
Expand Down
83 changes: 82 additions & 1 deletion rust/substreams/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,85 @@
//! This create exports useful macros that you can utilize to develop
//! Substreams handlers. Theses macros' goal is to significantly reduce boilerplate
//! code and ensure that your handler are more readeable
pub use substreams_macro::{map, store};

/// Marks function to setup substream map handler WASM boilerplate
///
/// ## Usage
///
///
/// ```rust
/// # mod eth { pub type Block = (); }
/// # mod proto { pub type Custom = (); }
///
/// #[substreams::handlers::map]
/// fn map_handler(blk: eth::Block) -> Result<proto::Custom, substreams::errors::Error> {
/// unimplemented!("do something");
/// }
/// ```
///
/// Equivalent code not using `#[substream::handlers::map]`
///
/// ```rust
/// # mod eth { pub type Block = (); }
/// # mod proto {
/// # #[derive(Debug)]
/// # pub struct Custom(u8);
/// # impl prost::Message for Custom {
/// # fn encode_raw<B: prost::bytes::BufMut>(&self, _: &mut B) where Self: Sized { todo!() }
/// # fn merge_field<B: prost::bytes::Buf>(&mut self, _: u32, _: prost::encoding::WireType, _: &mut B, _: prost::encoding::DecodeContext) -> Result<(), prost::DecodeError> where Self: Sized { todo!() }
/// # fn encoded_len(&self) -> usize { todo!() }
/// # fn clear(&mut self) { todo!() }
/// # }
/// # }
///
/// #[no_mangle]
/// pub extern "C" fn map_handler(blk_ptr: *mut u8, blk_len: usize) {
/// substreams::register_panic_hook();
/// let func = || -> Result<proto::Custom, substreams::errors::Error> {
/// let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len).unwrap();
/// {
/// unimplemented!("do something");
/// }
/// };
/// let result = func();
/// if result.is_err() {
/// panic!(result.err().unwrap())
/// }
/// substreams::output(substreams::proto::encode(&result.unwrap()).unwrap());
/// }
/// ```
pub use substreams_macro::map;

/// Marks function to setup substream store handler WASM boilerplate
/// ## Usage
///
///
/// ```rust
/// use substreams::{log, store};
/// # mod proto { pub type Custom = (); }
///
/// #[substreams::handlers::store]
/// fn build_nft_state(data: proto::Custom, s: store::StoreAddInt64, pairs: store::StoreGet, tokens: store::StoreGet) {
/// unimplemented!("do something");
/// }
/// ```
///
/// Equivalent code not using `#[substream::handlers::store]`
///
/// ```rust
/// use substreams::{log, store};
/// # mod proto { pub type Custom = (); }
///
/// #[no_mangle]
/// pub extern "C" fn build_nft_state(data_ptr: *mut u8, data_len: usize, pairs_idx: u32, tokens_idx: u32) {
/// substreams::register_panic_hook();
/// let data: proto::Custom = substreams::proto::decode_ptr(data_ptr, data_len).unwrap();
/// let pairs: store::StoreGet = store::StoreGet::new(pairs_idx);
/// let tokens: store::StoreGet = store::StoreGet::new(tokens_idx);
/// let s: store::StoreAddInt64 = store::StoreAddInt64::new();
/// {
/// unimplemented!("do something");
/// }
/// }
/// ```
pub use substreams_macro::store;
23 changes: 12 additions & 11 deletions rust/substreams/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@
//!
//! ```no_run
//! use substreams::{errors::Error, store};
//! # mod eth { pub type Block = (); }
//! # mod proto { pub type Custom = (); }
//!
//! // map handler that takes a source as input
//! /// Map handler that takes a source as input
//! #[substreams::handlers::map]
//! fn map_transfers(blk: eth::Block) -> Result<proto::Custom, Error> {
//! // do something
//! unimplemented!("do something");
//! }
//!
//! // map handler that takes a source, and a store in get mode as inputs
//! /// Map handler that takes a source, and a store in get mode as inputs
//! #[substreams::handlers::map]
//! fn map_ownerships(blk: eth::Block, store: store::StoreGet) -> Result<proto::Custom, Error> {
//! // do something
//! unimplemented!("do something");
//! }
//!
//! // map handler that takes a source, another map, and a store in get mode as inputs
//! /// Map handler that takes a source, another map, and a store in get mode as inputs
//! #[substreams::handlers::map]
//! fn map_mints(blk: eth::Block, mints: proto::Custom, store: store::StoreGet) -> Result<proto::Custom, Error> {
//! // do something
//! unimplemented!("do something");
//! }
//! //!
//! // map handler that takes a source, another map, and a store in delta mode as inputs
//!
//! /// Map handler that takes a source, another map, and a store in delta mode as inputs
//! #[substreams::handlers::map]
//! fn map_db(blk: eth::Block, mints: proto::Custom, store_deltas: store::Deltas) -> Result<proto::Custom, Error> {
//! // do something
//! unimplemented!("do something");
//! }
//! ```
//!
Expand All @@ -49,6 +51,7 @@
//!
//! ```no_run
//! use substreams::store;
//! # mod proto { pub type Custom = (); }
//!
//! #[substreams::handlers::store]
//! fn store_transfers(objects: proto::Custom, output: store::StoreAddInt64) {
Expand All @@ -65,8 +68,6 @@
//! // to something
//! }
//!```
//!
//!
pub mod errors;
mod externs;
pub mod handlers;
Expand Down
2 changes: 0 additions & 2 deletions rust/substreams/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
//! is used across Substreams
//!



use prost::{DecodeError, EncodeError};

/// Given an array of bytes, it will decode data in a Protobuf Message
Expand Down
16 changes: 0 additions & 16 deletions rust/substreams/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub type Deltas = Vec<pb::substreams::StoreDelta>;
#[derive(StoreWriter)]
pub struct StoreSet {}
impl StoreSet {

/// Set a given key to a given value, if the key existed before, it will be replaced.
pub fn set(&self, ord: u64, key: String, value: &Vec<u8>) {
state::set(ord as i64, key, value);
Expand All @@ -37,7 +36,6 @@ impl StoreSet {
#[derive(StoreWriter)]
pub struct StoreSetIfNotExists {}
impl StoreSetIfNotExists {

/// Set a given key to a given value, if the key existed before, it will be ignored and not set.
pub fn set_if_not_exists(&self, ord: u64, key: String, value: &Vec<u8>) {
state::set_if_not_exists(ord as i64, key, value);
Expand All @@ -56,7 +54,6 @@ impl StoreSetIfNotExists {
#[derive(StoreWriter)]
pub struct StoreAddInt64 {}
impl StoreAddInt64 {

/// Will add the value to the already present value at the key (or default to
/// zero if the key was not set)
pub fn add(&self, ord: u64, key: String, value: i64) {
Expand All @@ -77,7 +74,6 @@ impl StoreAddInt64 {
#[derive(StoreWriter)]
pub struct StoreAddFloat64 {}
impl StoreAddFloat64 {

/// Will add the value to the already present value at the key (or default to
/// zero if the key was not set)
pub fn add(&self, ord: u64, key: String, value: f64) {
Expand All @@ -98,7 +94,6 @@ impl StoreAddFloat64 {
#[derive(StoreWriter)]
pub struct StoreAddBigFloat {}
impl StoreAddBigFloat {

/// Will add the value to the already present value at the key (or default to
/// zero if the key was not set)
pub fn add(&self, ord: u64, key: String, value: &BigDecimal) {
Expand All @@ -119,7 +114,6 @@ impl StoreAddBigFloat {
#[derive(StoreWriter)]
pub struct StoreAddBigInt {}
impl StoreAddBigInt {

/// Will add the value to the already present value of the keys (or default to
/// zero if the key was not set)
pub fn add(&self, ord: u64, key: String, value: &BigInt) {
Expand All @@ -140,7 +134,6 @@ impl StoreAddBigInt {
#[derive(StoreWriter)]
pub struct StoreMaxInt64 {}
impl StoreMaxInt64 {

/// max will set the provided key in the store only if the value received in
/// parameter is bigger than the one already present in the store, with
/// a default of the zero value when the key is absent.
Expand All @@ -154,7 +147,6 @@ impl StoreMaxInt64 {
#[derive(StoreWriter)]
pub struct StoreMaxBigInt {}
impl StoreMaxBigInt {

/// Will set the provided key in the store only if the value received in
/// parameter is bigger than the one already present in the store, with
/// a default of the zero value when the key is absent.
Expand All @@ -168,7 +160,6 @@ impl StoreMaxBigInt {
#[derive(StoreWriter)]
pub struct StoreMaxFloat64 {}
impl StoreMaxFloat64 {

/// Will set the provided key in the store only if the value received in
/// parameter is bigger than the one already present in the store, with
/// a default of the zero value when the key is absent.
Expand All @@ -182,7 +173,6 @@ impl StoreMaxFloat64 {
#[derive(StoreWriter)]
pub struct StoreMaxBigFloat {}
impl StoreMaxBigFloat {

/// Will set the provided key in the store only if the value received in
/// parameter is bigger than the one already present in the store, with
/// a default of the zero value when the key is absent.
Expand All @@ -196,8 +186,6 @@ impl StoreMaxBigFloat {
#[derive(StoreWriter)]
pub struct StoreMinInt64 {}
impl StoreMinInt64 {


/// Will set the provided key in the store only if the value received in
/// parameter is smaller than the one already present in the store, with
/// a default of the zero value when the key is absent.
Expand All @@ -211,7 +199,6 @@ impl StoreMinInt64 {
#[derive(StoreWriter)]
pub struct StoreMinBigInt {}
impl StoreMinBigInt {

/// Will set the provided key in the store only if the value received in
/// parameter is smaller than the one already present in the store, with
/// a default of the zero value when the key is absent.
Expand All @@ -225,7 +212,6 @@ impl StoreMinBigInt {
#[derive(StoreWriter)]
pub struct StoreMinFloat64 {}
impl StoreMinFloat64 {

/// Will set the provided key in the store only if the value received in
/// parameter is smaller than the one already present in the store, with
/// a default of the zero value when the key is absent.
Expand All @@ -239,7 +225,6 @@ impl StoreMinFloat64 {
#[derive(StoreWriter)]
pub struct StoreMinBigFloat {}
impl StoreMinBigFloat {

/// Will set the provided key in the store only if the value received in
/// parameter is smaller than the one already present in the store, with
/// a default of the zero value when the key is absent.
Expand All @@ -254,7 +239,6 @@ pub struct StoreGet {
}

impl StoreGet {

/// Return a StoreGet object with a store index set
pub fn new(idx: u32) -> StoreGet {
StoreGet { idx }
Expand Down

0 comments on commit d615bad

Please sign in to comment.