Interact with Rootstock using Rust
Rust is a fast and memory-efficient language, it can power performance-critical services, run on embedded devices, and easily integrate with other languages.
This tutorial helps to get started on Rootstock using Rust by performing basic operations such as sending transactions and calling contracts using Alloy crate, similiar to ethers.
Introduction to Alloy
Alloy connects applications to blockchains, it serves as an entry point to connect with evm compatible blockchains. It is a rewrite of ethers-rs library from the ground up, with new features, high performance, etc. An example is Foundry, a tool written in Rust which uses Alloy as a dependency to connect with blockchains.
For more information, see Alloy Examples to help you get started.
Prerequisites
- Rust
- Install the latest version of Rust. If you already have Rust installed, make sure to use the latest version or update using the
rustuptoolchain.
- Install the latest version of Rust. If you already have Rust installed, make sure to use the latest version or update using the
Getting Started
Create a Rust project
Run the command below using cargo to create a starter project.
cargo new rootstock-rs
Next step is to update
cargo.tomlfile with dependencies explained in next section.
Setup Alloy
To install Alloy run the following command below in the root directory of the project:
cd rootstock-rs
cargo add alloy --git https://github.com/alloy-rs/alloy
Find more about Alloy setup using meta crate
Note: All the dependencies required are mentioned in the
.tomlfile below. Copy and paste into thecargo.tomlfile.
[package]
name = "rootstock-alloy"
version = "0.1.0"
edition = "2021"
[dependencies]
alloy = { git = "https://github.com/alloy-rs/alloy", version = "0.1.3", default-features = true, features = ["providers", "rpc-client", "transport-http", "sol-types", "json", "contract", "rpc-types", "rpc-types-eth", "network", "signers", "signer-local"] }
eyre = "0.6.12"
futures-util = "0.3.30"
tokio = { version = "1", features = ["full"] }
The types and import statements in Alloy dependencies are expected to change. If you face any type related errors while running the given examples in this tutorial, its recommended to check the Alloy repo and documentation.