diff --git a/CHANGELOG.md b/CHANGELOG.md index 18fb284..819bcc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,21 +4,25 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +- The return of store type StoreSetSum which allows both summing and setting a value in a store. This is useful for storing aggregated values in a store. Note: to read deltas from this store, you will need to use the Deltas type. The delta values will come prefixed with "set:" or "sum:" depending on the operation, followed by the string representation of the value. It is up to the user to decode the values as necessary. + ## 0.5.18 - Accept all characters within index keys except `-`,`'`, `"`, `&&`, `||`, `)`, `(` and spaces of all kind. ## 0.5.17 -- Removed a store type added in 0.5.15 and 0.5.16 which added a store type which has been cancelled due to a design issue. +- Removed StoreSetSum. (Reverted changes from 0.5.16 and 0.5.15) ## 0.5.16 -- reverted +- Add StoreSetSum to the WRITABLE_STORE array ## 0.5.15 -- reverted +- Add new store type StoreSetSum which allows both summing and setting a value in a store. This is useful for storing aggregated values in a store. ## 0.5.14 diff --git a/substreams-macro/src/handler.rs b/substreams-macro/src/handler.rs index 6dcd299..6a5def2 100644 --- a/substreams-macro/src/handler.rs +++ b/substreams-macro/src/handler.rs @@ -149,7 +149,7 @@ pub fn main(item: TokenStream, module_type: ModuleType) -> TokenStream { } } -const WRITABLE_STORE: [&'static str; 27] = [ +const WRITABLE_STORE: [&'static str; 31] = [ "StoreSetRaw", "StoreSetString", "StoreSetBigInt", @@ -177,6 +177,10 @@ const WRITABLE_STORE: [&'static str; 27] = [ "StoreMinFloat64", "StoreMinBigDecimal", "StoreAppend", + "StoreSetSumInt64", + "StoreSetSumFloat64", + "StoreSetSumBigInt", + "StoreSetSumBigDecimal", ]; const READABLE_STORE: [&'static str; 8] = [ diff --git a/substreams/src/externs.rs b/substreams/src/externs.rs index 057d73d..b4e9a4a 100644 --- a/substreams/src/externs.rs +++ b/substreams/src/externs.rs @@ -104,5 +104,33 @@ pub mod state { value_ptr: *const u8, value_len: u32, ); + pub fn set_sum_bigint( + ord: i64, + key_ptr: *const u8, + key_len: u32, + value_ptr: *const u8, + value_len: u32, + ); + pub fn set_sum_bigdecimal( + ord: i64, + key_ptr: *const u8, + key_len: u32, + value_ptr: *const u8, + value_len: u32, + ); + pub fn set_sum_int64( + ord: i64, + key_ptr: *const u8, + key_len: u32, + value_ptr: *const u8, + value_len: u32, + ); + pub fn set_sum_float64( + ord: i64, + key_ptr: *const u8, + key_len: u32, + value_ptr: *const u8, + value_len: u32, + ); } } diff --git a/substreams/src/state.rs b/substreams/src/state.rs index 12a4368..82625f7 100644 --- a/substreams/src/state.rs +++ b/substreams/src/state.rs @@ -423,3 +423,187 @@ where } } } + +#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] +pub fn set_sum_bigint(ord: i64, key: K, value: V) + where + K: AsRef, + V: AsRef, String: From +{ + #[cfg(target_arch = "wasm32")] + { + let key = key.as_ref(); + let data: String = value.into(); + + unsafe { + externs::state::set_sum_bigint( + ord, + key.as_ptr(), + key.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } +} + +#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] +pub fn set_sum_bigdecimal(ord: i64, key: K, value: V) + where + K: AsRef, + V: AsRef, String: From +{ + #[cfg(target_arch = "wasm32")] + { + let key = key.as_ref(); + let data: String = value.into(); + + unsafe { + externs::state::set_sum_bigdecimal( + ord, + key.as_ptr(), + key.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } +} + +#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] +pub fn set_sum_int64(ord: i64, key: K, value: V) + where + K: AsRef, + V: AsRef, String: From +{ + #[cfg(target_arch = "wasm32")] + { + let key = key.as_ref(); + let data: String = value.into(); + + unsafe { + externs::state::set_sum_int64( + ord, + key.as_ptr(), + key.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } +} + +#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] +pub fn set_sum_float64(ord: i64, key: K, value: V) + where + K: AsRef, + V: AsRef, String: From +{ + #[cfg(target_arch = "wasm32")] + { + let key = key.as_ref(); + let data: String = value.into(); + + unsafe { + externs::state::set_sum_float64( + ord, + key.as_ptr(), + key.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } +} + +#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] +pub fn set_sum_bigint(ord: i64, key: K, value: V) + where + K: AsRef, + V: AsRef, String: From +{ + #[cfg(target_arch = "wasm32")] + { + let key = key.as_ref(); + let data: String = value.into(); + + unsafe { + externs::state::set_sum_bigint( + ord, + key.as_ptr(), + key.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } +} + +#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] +pub fn set_sum_bigdecimal(ord: i64, key: K, value: V) + where + K: AsRef, + V: AsRef, String: From +{ + #[cfg(target_arch = "wasm32")] + { + let key = key.as_ref(); + let data: String = value.into(); + + unsafe { + externs::state::set_sum_bigdecimal( + ord, + key.as_ptr(), + key.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } +} + +#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] +pub fn set_sum_int64(ord: i64, key: K, value: V) + where + K: AsRef, + V: AsRef, String: From +{ + #[cfg(target_arch = "wasm32")] + { + let key = key.as_ref(); + let data: String = value.into(); + + unsafe { + externs::state::set_sum_int64( + ord, + key.as_ptr(), + key.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } +} + +#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] +pub fn set_sum_float64(ord: i64, key: K, value: V) + where + K: AsRef, + V: AsRef, String: From +{ + #[cfg(target_arch = "wasm32")] + { + let key = key.as_ref(); + let data: String = value.into(); + + unsafe { + externs::state::set_sum_float64( + ord, + key.as_ptr(), + key.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } +} \ No newline at end of file diff --git a/substreams/src/store.rs b/substreams/src/store.rs index 89252c5..1feaf0b 100644 --- a/substreams/src/store.rs +++ b/substreams/src/store.rs @@ -772,6 +772,85 @@ where } } +// -------------------- StoreSetSum -------------------- // +pub trait StoreSetSum { + fn new() -> Self; + fn set>(&self, ord: u64, key: K, value: T); + fn sum>(&self, ord: u64, key: K, value: T); +} + +pub struct StoreSetSumInt64 {} + +impl StoreSetSum for StoreSetSumInt64 { + fn new() -> Self { + StoreSetSumInt64 {} + } + + fn set>(&self, ord: u64, key: K, value: i64) { + let v = format!("set:{}", value.to_string()); + state::set_sum_int64(ord as i64, key, v); + } + + fn sum>(&self, ord: u64, key: K, value: i64) { + let v = format!("sum:{}", value.to_string()); + state::set_sum_int64(ord as i64, key, v); + } +} + +pub struct StoreSetSumFloat64 {} + +impl StoreSetSum for StoreSetSumFloat64 { + fn new() -> Self { + StoreSetSumFloat64 {} + } + + fn set>(&self, ord: u64, key: K, value: f64) { + let v = format!("set:{}", value.to_string()); + state::set_sum_float64(ord as i64, key, v); + } + + fn sum>(&self, ord: u64, key: K, value: f64) { + let v = format!("sum:{}", value.to_string()); + state::set_sum_float64(ord as i64, key, v); + } +} + +pub struct StoreSetSumBigInt {} + +impl StoreSetSum for StoreSetSumBigInt { + fn new() -> Self { + StoreSetSumBigInt {} + } + + fn set>(&self, ord: u64, key: K, value: BigInt) { + let v = format!("set:{}", value.to_string()); + state::set_sum_bigint(ord as i64, key, v); + } + + fn sum>(&self, ord: u64, key: K, value: BigInt) { + let v = format!("sum:{}", value.to_string()); + state::set_sum_bigint(ord as i64, key, v); + } +} + +pub struct StoreSetSumBigDecimal {} + +impl StoreSetSum for StoreSetSumBigDecimal { + fn new() -> Self { + StoreSetSumBigDecimal {} + } + + fn set>(&self, ord: u64, key: K, value: BigDecimal) { + let v = format!("set:{}", value.to_string()); + state::set_sum_bigdecimal(ord as i64, key, v); + } + + fn sum>(&self, ord: u64, key: K, value: BigDecimal) { + let v = format!("sum:{}", value.to_string()); + state::set_sum_bigdecimal(ord as i64, key, v); + } +} + // -------------------- StoreGet -------------------- // /// StoreGet is a trait which is implemented on any type of typed StoreGet pub trait StoreGet {