Developers
Hello World Blueprint

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:

To install the Tangle CLI, run:

curl --proto '=https' --tlsv1.2 -LsSf https://github.com/tangle-network/gadget/releases/download/cargo-tangle/v0.1.1-beta.7/cargo-tangle-installer.sh | sh

Alternatively, install from source:

cargo install cargo-tangle --git https://github.com/tangle-network/gadget --force

Creating a New Project

  1. Open your terminal and run:

    cargo tangle gadget create --name hello-world-blueprint
  2. Follow the prompts to set up your project.

Project Structure

After creation, your project will have the following structure:

hello-world-blueprint/
├── src/
│ ├── lib.rs
│ └── main.rs
├── Cargo.toml
├── README.md
└── <other files>

Let's look at the 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:

  1. Jobs: Define the main computational tasks of your Blueprint. For more details on creating jobs, see our Blueprint Job Tutorial.

  2. Reports: Structure the output of your jobs. Learn more about reports in our Blueprint Report Documentation.

  3. Event Listeners: Set up listeners to react to on-chain events. For a deep dive into event listeners, check out our Event Listener Guide.

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:

  1. Environment Setup: It loads the configuration, initializes the logger, and sets up error handling.
  2. Client Initialization: It creates a connection to the Tangle Network using the provided RPC endpoint.
  3. Registration: It checks if the node should run registration (implementation pending).
  4. Event Handler Creation: It instantiates the event handler for the say_hello job.
  5. 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:
```bash
cargo build

This command compiles your Rust code and checks for any errors.

Deploying Your Blueprint

To deploy your Blueprint to the Tangle network, use:

cargo tangle gadget deploy

This command will package your Blueprint and deploy it to the Tangle network test environment.

Next Steps

Congratulations! You've created, built, and deployed your first Tangle Blueprint. Here are some suggestions for what to do next:

  1. Explore more complex job implementations in your lib.rs file. Learn more about Jobs and the Job Macro system.

  2. Implement reporting mechanisms using the Reports Macro to ensure the integrity of your AVS.

  3. Dive deeper into Event Listeners to understand how to trigger your jobs and reports based on various events.

  4. Learn about Context and Context Extensions to manage dependencies and state in your Blueprint.

  5. Implement Custom Registration Hooks to handle operator registration logic.

  6. Explore Request Hooks to manage incoming requests to your AVS.

  7. If you're interested in building for EigenLayer, check out our guide on Building an EigenLayer AVS.

  8. Implement tests for your Blueprint using tokio::test. Learn more about Testing Blueprints.

  9. Explore the Tangle network's features and how they interact with your Blueprint. Understand EVM and Native Addresses and EVM to Substrate transfers.

  10. Familiarize yourself with EVM Precompiles available on Tangle Network.

  11. 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!