Getting Started with Tangle Blueprints
Welcome to the Tangle Blueprint tutorial! This guide will walk you through creating a simple Hello World
Blueprint for Tangle. By the end of this tutorial, you'll have a basic understanding of how to create, build, and deploy a Tangle Blueprint.
What are Tangle Blueprints?
Tangle Blueprints are specifications for Actively Validated Services (AVS) on the Tangle Network. An AVS is an off-chain service that runs arbitrary computations for a user-specified period of time. Blueprints provide a useful abstraction, allowing developers to create reusable service infrastructures similar to smart contracts.
Prerequisites
Before you begin, ensure you have the following installed:
- Rust (opens in a new tab)
- Forge (opens in a new tab)
- Tangle (opens in a new tab)
cargo-tangle
CLI tool
Creating my First Blueprint
See the CLI Quickstart for instructions on creating a blueprint.
Key Files
src/lib.rs
This file contains the core logic of your Blueprint, including job definitions, report structures, and event listeners. Here's an example of a simple "Hello World" job:
-
Jobs: Define the main computational tasks of your Blueprint. For more details on creating jobs, see our Blueprint Job Documentation.
-
Reports: Define offchain reporting logic for the custom slashing functionality of your Blueprint. Learn more about reports in our Blueprint Report Documentation.
-
Event Listeners: Set up listeners to react to on-chain events, offchain events, cron-job like listeners, and even custom event triggers. For a deep dive into event listeners, check out our Event Listener Documentation.
Here's a basic example incorporating these elements:
use gadget_sdk as sdk;
use sdk::job;
use std::convert::Infallible;
/// Returns "Hello World!" if who is None, otherwise returns "Hello, <who>!"
#[job(id = 0, params(who), result(), verifier(evm = "HelloBlueprint"))]
pub fn say_hello(who: Option<String>) -> Result<String, Infallible> {
match who {
Some(who) => Ok(format!("Hello, {}!", who)),
None => Ok("Hello World!".to_string()),
}
}
This job takes an optional who
parameter and returns a greeting.
src/main.rs
This file serves as the entry point for your Actively Validated Service (AVS) node. It sets up the runtime environment, initializes the necessary components, and starts the event watcher. Here's a breakdown of its key responsibilities:
- Environment Setup: It loads the configuration, initializes the logger, and sets up error handling.
- Client Initialization: It creates a connection to the Tangle Network using the provided RPC endpoint.
- Registration: It checks if the node should run registration (implementation pending).
- Event Handler Creation: It instantiates the event handler for the
say_hello
job. - Event Watcher: It starts the Substrate event watcher, which listens for relevant events on the Tangle Network.
use color_eyre::Result;
use gadget_sdk as sdk;
use hello_world_blueprint as blueprint;
use sdk::{
config::ContextConfig, events_watcher::substrate::SubstrateEventWatcher,
events_watcher::tangle::TangleEventsWatcher, tangle_subxt::,
};
use structopt::StructOpt;
#[tokio::main]
async fn main() -> Result<()> {
init_logger();
color_eyre::install()?;
// Initialize the environment
let config = ContextConfig::from_args();
let env = sdk::config::load(config)?;
let signer = env.first_sr25519_signer()?;
let client = subxt::OnlineClient::from_url(&env.rpc_endpoint).await?;
if env.should_run_registration() {
todo!();
}
let service_id = env.service_id.expect("should exist");
// Create the event handler from the job
let say_hello_job = blueprint::SayHelloEventHandler { service_id, signer };
tracing::info!("Starting the event watcher ...");
SubstrateEventWatcher::run(
&TangleEventsWatcher {
span: env.span.clone(),
},
client,
vec![Box::new(say_hello_job)],
).await?;
Ok(())
}
fn init_logger() {
let env_filter = tracing_subscriber::EnvFilter::from_default_env();
tracing_subscriber::fmt()
.compact()
.with_target(true)
.with_env_filter(env_filter)
.init();
}
This sets up the AVS node, initializes the environment, and starts the event watcher.
Building Your Project
To build your project, run:
cargo build
This command compiles your Rust code and checks for any errors.
Deploying Your Blueprint
Next Steps
Congratulations! You've created, built, and deployed your first Tangle Blueprint. Here are some suggestions for what to do next:
-
Explore more complex job implementations in your
lib.rs
file. Learn more about Jobs and the Job Macro system. -
Implement reporting mechanisms using the Reports Macro to ensure the integrity of your AVS.
-
Dive deeper into Event Listeners to understand how to trigger your jobs and reports based on various events.
-
Learn about Context and Context Extensions to manage dependencies and state in your Blueprint.
-
If you're interested in building for EigenLayer, check out our guide on Building an EigenLayer AVS.
-
Implement tests for your Blueprint using
tokio::test
. Learn more about Testing Blueprints. -
Explore the Tangle network's features and how they interact with your Blueprint. Understand EVM and Native Addresses and EVM to Substrate transfers.
-
Familiarize yourself with EVM Precompiles available on Tangle Network.
-
Learn about Deploying Contracts with Hardhat for more advanced contract deployment scenarios.
For more advanced topics and in-depth information, check out our other documentation pages and the Rust async book (opens in a new tab).
If you encounter any issues or have questions, please don't hesitate to open an issue on our GitHub repository (opens in a new tab). We're here to help you succeed in building with Tangle Blueprints!
For more advanced topics and in-depth information, check out our other documentation pages and the Rust async book (opens in a new tab).
Feedback and Support
If you encounter any issues or have questions, please don't hesitate to open an issue on our GitHub repository (opens in a new tab). We're here to help you succeed in building with Tangle Blueprints!