Skip to content

Commit

Permalink
fix: update blueprint examples (#628)
Browse files Browse the repository at this point in the history
* fix(examples)!: updating and improving blueprint examples wip

* fix!: blueprint examples update wip

* fix: blueprint examples building

* fix!: more example tests wip

* fix: rustdoc-types version bump

* fix: clippy

* chore: toolchain bump

* fix: example cleanup for docs

---------

Co-authored-by: drewstone <[email protected]>
  • Loading branch information
Tjemmmic and drewstone authored Feb 1, 2025
1 parent c7d4fac commit 6ab37d0
Show file tree
Hide file tree
Showing 29 changed files with 400 additions and 192 deletions.
19 changes: 17 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ members = [
]
exclude = [
"blueprints/incredible-squaring-symbiotic",
"blueprints/examples"
]

[workspace.package]
Expand Down Expand Up @@ -38,6 +37,7 @@ broken_intra_doc_links = "deny"
blueprint-sdk = { version = "0.1.0", path = "./crates/sdk", default-features = false }

# Blueprint Examples
blueprint-examples = { version = "0.1.0", path = "./blueprints/examples", default-features = false }
incredible-squaring-blueprint = { version = "0.1.1", path = "./blueprints/incredible-squaring", default-features = false }
incredible-squaring-blueprint-eigenlayer = { version = "0.1.1", path = "./blueprints/incredible-squaring-eigenlayer", default-features = false }

Expand Down Expand Up @@ -211,7 +211,7 @@ itertools = { version = "0.13.0", default-features = false }
paste = { version = "1.0.15", default-features = false }
proc-macro2 = { version = "1.0", default-features = false }
quote = { version = "1.0", default-features = false }
rustdoc-types = { version = "0.33.0", default-features = false }
rustdoc-types = { version = "0.35.0", default-features = false }
syn = { version = "2.0.75", default-features = false }
trybuild = { version = "1.0", default-features = false }
typed-builder = { version = "0.19", default-features = false }
Expand Down
30 changes: 4 additions & 26 deletions blueprints/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,20 @@ repository.workspace = true
publish = false

[dependencies]
async-trait = { workspace = true }
blueprint-sdk = { workspace = true, features = ["std"] }
eigensdk = { workspace = true }
blueprint-sdk = { workspace = true, features = ["std", "testing", "evm", "tangle", "eigenlayer", "macros", "cronjob"] }
color-eyre = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tempfile = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tokio-util = { workspace = true }
tracing = { workspace = true }
uuid = { workspace = true }

alloy-primitives = { workspace = true }
alloy-json-abi = { workspace = true }
alloy-sol-types = { workspace = true }
alloy-rpc-client = { workspace = true }
alloy-rpc-types = { workspace = true }
alloy-rpc-types-eth = { workspace = true }
alloy-provider = { workspace = true }
alloy-pubsub = { workspace = true }
alloy-signer = { workspace = true }
alloy-signer-local = { workspace = true }
alloy-network = { workspace = true }
alloy-node-bindings = { workspace = true }
alloy-contract = { workspace = true }
alloy-consensus = { workspace = true }
alloy-transport = { workspace = true }
alloy-transport-http = { workspace = true }

[dev-dependencies]
gadget-testing-utils = { workspace = true }
blueprint-sdk = { workspace = true, features = ["std", "testing", "evm", "tangle", "eigenlayer", "macros", "cronjob"] }

[build-dependencies]
blueprint-metadata = { workspace = true }
blueprint-build-utils = { workspace = true }
blueprint-sdk = { workspace = true, features = ["build"] }


[features]
default = ["std"]
Expand Down
4 changes: 1 addition & 3 deletions blueprints/examples/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
fn main() {
println!("cargo:rerun-if-changed=src/lib.rs");
println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=contracts/src/*");
blueprint_build_utils::build_contracts(vec!["contracts"]);
blueprint_metadata::generate_json();
blueprint_sdk::build::utils::build_contracts(vec!["contracts"]);
}
83 changes: 61 additions & 22 deletions blueprints/examples/src/eigen_context.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use alloy_primitives::U256;
use alloy_primitives::{address, Address, Bytes};
use blueprint_sdk::alloy::primitives::{address, Bytes, U256};
use blueprint_sdk::alloy::rpc::types::Log;
use blueprint_sdk::alloy::sol;
use blueprint_sdk::config::GadgetConfiguration;
use blueprint_sdk::contexts::eigenlayer::EigenlayerContext;
use blueprint_sdk::event_listeners::core::InitializableEventHandler;
use blueprint_sdk::event_listeners::evm::EvmContractEventListener;
use blueprint_sdk::macros::contexts::EigenlayerContext;
use blueprint_sdk::macros::load_abi;
use blueprint_sdk::std::{env, Zero};
use blueprint_sdk::utils::evm::get_provider_http;
use blueprint_sdk::{job, Error};
use color_eyre::eyre::eyre;
use gadget_sdk::event_listener::evm::contracts::EvmContractEventListener;
use gadget_sdk::event_utils::InitializableEventHandler;
use gadget_sdk::subxt_core::ext::sp_runtime::traits::Zero;
use gadget_sdk::utils::evm::get_provider_http;
use gadget_sdk::{config::StdGadgetConfiguration, contexts::EigenlayerContext, job, load_abi};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::ops::Deref;

alloy_sol_types::sol!(
sol!(
#[allow(missing_docs)]
#[sol(rpc)]
#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -24,14 +26,17 @@ load_abi!(
"contracts/out/ExampleTaskManager.sol/ExampleTaskManager.json"
);

type ProcessorError =
blueprint_sdk::event_listeners::core::Error<blueprint_sdk::event_listeners::evm::error::Error>;

#[derive(Clone, EigenlayerContext)]
pub struct ExampleEigenContext {
#[config]
pub std_config: StdGadgetConfiguration,
pub std_config: GadgetConfiguration,
}

pub async fn constructor(
env: StdGadgetConfiguration,
env: GadgetConfiguration,
) -> color_eyre::Result<impl InitializableEventHandler> {
let example_address = env::var("EXAMPLE_TASK_MANAGER_ADDRESS")
.map(|addr| addr.parse().expect("Invalid EXAMPLE_TASK_MANAGER_ADDRESS"))
Expand Down Expand Up @@ -63,19 +68,25 @@ pub async fn constructor(
pub async fn handle_job(
ctx: ExampleEigenContext,
event: ExampleTaskManager::NewTaskCreated,
log: alloy_rpc_types::Log,
) -> Result<u32, Box<dyn std::error::Error>> {
log: Log,
) -> Result<u32, Error> {
// Example address, quorum number, and index
let operator_addr = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8");
let quorum_number: u8 = 0;
let index: U256 = U256::from(0);

// Get an Operator's ID as FixedBytes from its Address.
let operator_id = ctx.get_operator_id(operator_addr).await?;
let operator_id = ctx
.eigenlayer_client()
.await?
.get_operator_id(operator_addr)
.await?;
println!("Operator ID from Address: {:?}", operator_id);

// Get an Operator's latest stake update.
let latest_stake_update = ctx
.eigenlayer_client()
.await?
.get_latest_stake_update(operator_id, quorum_number)
.await?;
println!("Latest Stake Update: \n\tStake: {:?},\n\tUpdate Block Number: {:?},\n\tNext Update Block Number: {:?}",
Expand All @@ -87,12 +98,16 @@ pub async fn handle_job(

// Get Operator stake in Quorums at a given block.
let stake_in_quorums_at_block = ctx
.eigenlayer_client()
.await?
.get_operator_stake_in_quorums_at_block(block_number, Bytes::from(vec![0]))
.await?;
assert!(!stake_in_quorums_at_block.is_empty());

// Get an Operator's stake in Quorums at the current block.
let stake_in_quorums_at_current_block = ctx
.eigenlayer_client()
.await?
.get_operator_stake_in_quorums_at_current_block(operator_id)
.await?;
println!(
Expand All @@ -102,12 +117,18 @@ pub async fn handle_job(
assert!(!stake_in_quorums_at_current_block.is_empty());

// Get an Operator by ID.
let operator_by_id = ctx.get_operator_by_id(*operator_id).await?;
let operator_by_id = ctx
.eigenlayer_client()
.await?
.get_operator_by_id(*operator_id)
.await?;
println!("Operator by ID: {:?}", operator_by_id);
assert_eq!(operator_by_id, operator_addr);

// Get an Operator stake history.
let stake_history = ctx
.eigenlayer_client()
.await?
.get_operator_stake_history(operator_id, quorum_number)
.await?;
println!("Stake History for {operator_id} in Quorum {quorum_number}:");
Expand All @@ -121,20 +142,28 @@ pub async fn handle_job(

// Get an Operator stake update at a given index.
let stake_update_at_index = ctx
.eigenlayer_client()
.await?
.get_operator_stake_update_at_index(quorum_number, operator_id, index)
.await?;
println!("Stake Update at Index {index}: \n\tStake: {:?}\n\tUpdate Block Number: {:?}\n\tNext Update Block Number: {:?}", stake_update_at_index.stake, stake_update_at_index.updateBlockNumber, stake_update_at_index.nextUpdateBlockNumber);
assert!(stake_update_at_index.nextUpdateBlockNumber.is_zero());

// Get an Operator's stake at a given block number.
let stake_at_block_number = ctx
.eigenlayer_client()
.await?
.get_operator_stake_at_block_number(operator_id, quorum_number, block_number)
.await?;
println!("Stake at Block Number: {:?}", stake_at_block_number);
assert!(!stake_at_block_number.is_zero());

// Get an Operator's details.
let operator = ctx.get_operator_details(operator_addr).await?;
let operator = ctx
.eigenlayer_client()
.await?
.get_operator_details(operator_addr)
.await?;
println!("Operator Details: \n\tAddress: {:?},\n\tEarnings receiver address: {:?},\n\tDelegation approver address: {:?},\n\tMetadata URL: {:?},\n\tStaker Opt Out Window Blocks: {:?}",
operator.address,
operator.earnings_receiver_address,
Expand All @@ -145,11 +174,15 @@ pub async fn handle_job(

// Get an Operator's latest stake update.
let latest_stake_update = ctx
.eigenlayer_client()
.await?
.get_latest_stake_update(operator_id, quorum_number)
.await?;
let block_number = latest_stake_update.updateBlockNumber - 1;
// Get the total stake at a given block number from a given index.
let total_stake_at_block_number_from_index = ctx
.eigenlayer_client()
.await?
.get_total_stake_at_block_number_from_index(quorum_number, block_number, index)
.await?;
println!(
Expand All @@ -159,7 +192,11 @@ pub async fn handle_job(
assert!(total_stake_at_block_number_from_index.is_zero());

// Get the total stake history length of a given quorum.
let total_stake_history_length = ctx.get_total_stake_history_length(quorum_number).await?;
let total_stake_history_length = ctx
.eigenlayer_client()
.await?
.get_total_stake_history_length(quorum_number)
.await?;
println!(
"Total Stake History Length: {:?}",
total_stake_history_length
Expand All @@ -168,6 +205,8 @@ pub async fn handle_job(

// Provides the public keys of existing registered operators within the provided block range.
let existing_registered_operator_pub_keys = ctx
.eigenlayer_client()
.await?
.query_existing_registered_operator_pub_keys(0, block_number as u64)
.await?;
println!(
Expand All @@ -183,7 +222,7 @@ pub async fn handle_job(
}

pub async fn handle_events(
event: (ExampleTaskManager::NewTaskCreated, alloy_rpc_types::Log),
) -> Result<(ExampleTaskManager::NewTaskCreated, alloy_rpc_types::Log), gadget_sdk::Error> {
Ok(event)
event: (ExampleTaskManager::NewTaskCreated, Log),
) -> Result<Option<(ExampleTaskManager::NewTaskCreated, Log)>, ProcessorError> {
Ok(Some(event))
}
15 changes: 8 additions & 7 deletions blueprints/examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use alloy_primitives::Address;
use blueprint_examples::{eigen_context, periodic_web_poller, raw_tangle_events, services_context};
use gadget_sdk::info;
use gadget_sdk::runners::eigenlayer::EigenlayerBLSConfig;
use gadget_sdk::runners::{tangle::TangleConfig, BlueprintRunner};
use std::env;
use blueprint_sdk::alloy::primitives::Address;
use blueprint_sdk::logging::info;
use blueprint_sdk::runners::core::runner::BlueprintRunner;
use blueprint_sdk::runners::eigenlayer::bls::EigenlayerBLSConfig;
use blueprint_sdk::runners::tangle::tangle::TangleConfig;
use blueprint_sdk::std::env;

#[gadget_sdk::main(env)]
#[blueprint_sdk::main(env)]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
info!("~~~ Executing Blueprint Examples ~~~");

Expand All @@ -19,7 +20,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
info!("Running Tangle examples");
BlueprintRunner::new(TangleConfig::default(), env.clone())
.job(raw_tangle_events::constructor(env.clone()).await?)
.job(periodic_web_poller::constructor())
.job(periodic_web_poller::constructor("1/2 * * * * *"))
.job(services_context::constructor(env.clone()).await?)
.run()
.await?;
Expand Down
Loading

0 comments on commit 6ab37d0

Please sign in to comment.