Crates.io | near-lake-parent-transaction-cache |
lib.rs | near-lake-parent-transaction-cache |
version | 0.8.0-beta.3 |
source | src |
created_at | 2023-06-06 19:29:56.721887 |
updated_at | 2023-11-22 14:36:49.404115 |
description | Ready-to-use context for the Lake Framework in Rust. It provides a cache for keeping the relation between transactions and receipts in cache. |
homepage | |
repository | https://github.com/near/near-lake-framework-rs |
max_upload_size | |
id | 884148 |
size | 85,958 |
Lake Parent Transaction Cache is a ready-to-use context for the Lake Framework in Rust. It provides a cache for keeping the relation between transactions and receipts in cache.
use near_lake_parent_transaction_cache::{ParentTransactionCache, ParentTransactionCacheBuilder};
# use near_lake_framework::LakeBuilder;
# use near_lake_framework::near_lake_primitives::{block::Block, actions::ActionMetaDataExt};
# fn main() {
let parent_transaction_cache_ctx = ParentTransactionCacheBuilder::default()
.build()
.expect("Failed to build the ParentTransactionCache context");
LakeBuilder::default()
.mainnet()
.start_block_height(80504433)
.build()
.expect("Failed to build the Lake Framework")
.run_with_context(handle_block, &parent_transaction_cache_ctx)
.expect("Failed to run the Lake Framework");
# }
async fn handle_block(
mut block: Block,
ctx: &ParentTransactionCache,
) -> anyhow::Result<()> {
for action in block.actions() {
println!(
"Action receipt ID: {:?} | Parent TX hash: {:?}",
action.receipt_id(),
ctx.get_parent_transaction_hash(&action.receipt_id())
);
}
Ok(())
}
To use the Lake Parent Transaction Cache context in your Rust project, follow these steps:
Cargo.toml
file:[dependencies]
near-lake-parent-transaction-cache = "<version>"
use near_lake_parent_transaction_cache::ParentTransactionCache;
use near_lake_primitives::actions::ActionMetaDataExt;
ParentTransactionCache
context:# use near_lake_parent_transaction_cache::ParentTransactionCacheBuilder;
let parent_transaction_cache_ctx = ParentTransactionCacheBuilder::default();
near_lake_framework::LakeBuilder::default()
.mainnet()
.start_block_height(<desired_block_height>)
.build()?
.run_with_context(<your_indexing_function>, &parent_transaction_cache_ctx)?;
Replace <desired_block_height>
with the starting block height you want to use. Replace <you_indexing_function>
with the function you want to use to index the blocks.
We use SizedCache under the hood. So we can configure the cache size by using the cache_size
method:
# use near_lake_parent_transaction_cache::ParentTransactionCacheBuilder;
let parent_transaction_cache_ctx = ParentTransactionCacheBuilder::default()
.cache_size(100_000);
By default the cache size is 100,000.
By default ParentTransactionCache
context will cache the relation between Transaction and Receipt for every Transaction in the block. But you can configure it to watch for specific accounts only:
# use near_lake_parent_transaction_cache::ParentTransactionCacheBuilder;
use near_lake_framework::near_primitives::types::AccountId;
let accounts_to_watch: Vec<AccountId> = vec![
String::from("alice.near").try_into().unwrap(),
String::from("bob.near").try_into().unwrap(),
];
let parent_transaction_cache_ctx = ParentTransactionCacheBuilder::default()
.for_accounts(accounts_to_watch);
for_account
method# use near_lake_parent_transaction_cache::ParentTransactionCacheBuilder;
use near_lake_framework::near_primitives::types::AccountId;
let parent_transaction_cache_ctx = ParentTransactionCacheBuilder::default()
.for_account(String::from("alice.near").try_into().unwrap())
.for_account(String::from("bob.near").try_into().unwrap());