Skip to main content

Create a Block

The simplest block you can create is an empty block. You will only need connect your client to a node and then call the Client.block(&self) function.

The following code example will:

  1. Create a Client which will connect to the Shimmer Testnet.
  2. Build and post an empty block.

Code Example

Dotenv

This example uses dotenv, which is not safe to use in production environments.

// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! This example sends a block with no payload.
//! Run: `cargo run --example block_no_payload --release -- [NODE URL]`.

use iota_client::{Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args().nth(1).unwrap_or_else(|| {
// This example uses dotenv, which is not safe for use in production.
dotenv::dotenv().ok();
std::env::var("NODE_URL").unwrap()
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;

// Create and send the block.
let block = client.block().finish().await?;

println!("{block:#?}");

println!(
"Block with no payload sent: {}/block/{}",
std::env::var("EXPLORER_URL").unwrap(),
block.id()
);

Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example simple_block --release

Expected Output

Block {
protocol_version: 2,
parents: Parents(
BoxedSlicePrefix([
BlockId(0x2f68b25006f0e1b55ada3e9278e69796380971e97587c233d138c593139d255d),
BlockId(0x76d25ac0073d14381d9a38c61ecb9665241d771bd89564e467c7fdf98e770ddd),
BlockId(0xd072addb86a5a59a69b17d0e8fc3368771dc5ea7f7c2257045e508ae37f27a46),
BlockId(0xfea2e90318a1b234569a3d698e97906d2c6a0084eb571971bf9ce9c4d31ee6c9),
]),
),
payload: OptionalPayload(
None,
),
nonce: 4420,
}
Block with no payload sent: https://explorer.shimmer.network/testnet/block/0x9f794dde42c7be9cd209cc992f69e1f8b7b40578303825049079d8ac128aa20f