-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LEP-001 Introduce Lake Context (#68)
* feat: Implement LEP-001 (Context) * clean up Lake impl to reuse code * Update docstrings * refactor: Resolves #42 replace anyhow with LakeError enum (thiserror) (#70) * fix: typo in README
- Loading branch information
Showing
11 changed files
with
222 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! This example show how to use a context with Lake Framework. | ||
//! It is going to follow the NEAR Social contract and the block height along | ||
//! with a number of calls to the contract. | ||
use std::io::Write; | ||
|
||
use near_lake_framework::near_lake_primitives; | ||
// We need to import this trait to use the `as_function_call` method. | ||
use near_lake_primitives::actions::ActionMetaDataExt; | ||
|
||
const CONTRACT_ID: &str = "social.near"; | ||
|
||
#[derive(Clone)] | ||
struct FileContext { | ||
path: std::path::PathBuf, | ||
} | ||
|
||
impl FileContext { | ||
fn new(path: impl Into<std::path::PathBuf>) -> Self { | ||
Self { path: path.into() } | ||
} | ||
|
||
// append to the file | ||
pub fn write(&self, value: &str) -> anyhow::Result<()> { | ||
let mut file = std::fs::OpenOptions::new() | ||
.create(true) | ||
.append(true) | ||
.open(&self.path)?; | ||
file.write_all(value.as_bytes())?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() -> anyhow::Result<()> { | ||
println!("Starting..."); | ||
// Create the context | ||
let context = FileContext::new("./output.txt"); | ||
// Lake Framework start boilerplate | ||
near_lake_framework::LakeBuilder::default() | ||
.mainnet() | ||
.start_block_height(88444526) | ||
.build()? | ||
// developer-defined async function that handles each block | ||
.run_with_context(print_function_calls_to_my_account, &context)?; | ||
Ok(()) | ||
} | ||
|
||
async fn print_function_calls_to_my_account( | ||
mut block: near_lake_primitives::block::Block, | ||
ctx: &FileContext, | ||
) -> anyhow::Result<()> { | ||
let block_height = block.block_height(); | ||
let actions: Vec<&near_lake_primitives::actions::FunctionCall> = block | ||
.actions() | ||
.filter(|action| action.receiver_id().as_str() == CONTRACT_ID) | ||
.filter_map(|action| action.as_function_call()) | ||
.collect(); | ||
|
||
if !actions.is_empty() { | ||
// Here's the usage of the context. | ||
ctx.write( | ||
format!( | ||
"Block #{} - {} calls to {}\n", | ||
block_height, | ||
actions.len(), | ||
CONTRACT_ID | ||
) | ||
.as_str(), | ||
)?; | ||
println!("Block #{:?}\n{:#?}", block_height, actions); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.