# Flare for Rust Devs

> Learn how to interact with Flare using alloy-rs.

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.

Source: https://dev.flare.network/network/guides/flare-for-rust-developers

This guide is for developers who want to interact with Flare using Rust. In this guide, using Rust, you will learn how to:

-   Query a contract on Flare using [alloy-rs](https://github.com/alloy-rs), an async library for interacting with Ethereum-like chains.
-   Compile a Solidity contract using the CLI interface of [solc](https://github.com/argotorg/solidity), the Solidity compiler.
-   Deploy your compiled contract on Flare.

tip

All examples in this guide are available at [developer-hub/examples](https://github.com/flare-foundation/developer-hub/tree/main/examples).

## Getting started[​](#getting-started "Direct link to Getting started")

Install the Solidity compiler by following the instructions in the [Solidity documentation](https://docs.soliditylang.org/en/latest/installing-solidity.html#linux-packages). The main commands are provided here:

```
brew tap ethereum/ethereumbrew install solidity
```

```
sudo add-apt-repository -y ppa:ethereum/ethereumsudo apt updatesudo apt install solc
```

Install the following dependencies:

```
cargo add alloy eyre tokio --features alloy/full,tokio/rt,tokio/rt-multi-thread,tokio/macros
```

### Usage[​](#usage "Direct link to Usage")

You need to connect to testnet or mainnet via an RPC, any RPC listed on the [Network Configuration](/network/overview#configuration) page will work. For this guide, you can use the Public RPC.

src/bin/chain\_id.rs

```
use alloy::providers::{Provider, ProviderBuilder};use eyre::Result;#[tokio::main]async fn main() -> Result<()> {    let provider =        ProviderBuilder::new().connect_http("https://coston2-api.flare.network/ext/C/rpc".parse()?);    let chain_id = provider.get_chain_id().await?;    println!("Chain ID: {chain_id}"); // Chain ID: 114    Ok(())}
```

src/bin/chain\_id.rs

```
use alloy::providers::{Provider, ProviderBuilder};use eyre::Result;#[tokio::main]async fn main() -> Result<()> {    let provider =        ProviderBuilder::new().connect_http("https://flare-api.flare.network/ext/C/rpc".parse()?);    let chain_id = provider.get_chain_id().await?;    println!("Chain ID: {chain_id}"); // Chain ID: 14    Ok(())}
```

```
cargo run --bin chain_id
```

## Querying a contract[​](#querying-a-contract "Direct link to Querying a contract")

To query a contract, two pieces of information are required:

-   Contract address
-   Contract ABI (Application Binary Interface)

For this example, you can use the `FlareContractRegistry` contract which has the same address `0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019` across both testnet and mainnet.

### Fetch ABI[​](#fetch-abi "Direct link to Fetch ABI")

To fetch a contract's ABI, copy the FlareContractRegistry ABI from the appropriate Routescan endpoint and paste it into `FlareContractRegistry.json` next to your `Cargo.toml`. Pick the URL that matches the chain you'll deploy to:

-   **Flare Testnet Coston2 (chain id 114):** [routescan URL](https://api.routescan.io/v2/network/testnet/evm/114/etherscan/api?module=contract&action=getabi&address=0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019&format=raw)
-   **Flare Mainnet (chain id 14):** [routescan URL](https://api.routescan.io/v2/network/mainnet/evm/14/etherscan/api?module=contract&action=getabi&address=0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019&format=raw)

### Make query[​](#make-query "Direct link to Make query")

You can now query the `FlareContractRegistry` contract to get the addresses of other Flare contracts.

For example, querying it for the address of the `WNat` contract:

src/bin/make\_query.rs

```
use alloy::{primitives::address, providers::ProviderBuilder, sol};use eyre::Result;sol!(    #[sol(rpc)]    FlareContractRegistry,    "FlareContractRegistry.json");#[tokio::main]async fn main() -> Result<()> {    let provider =        ProviderBuilder::new().connect_http("https://coston2-api.flare.network/ext/C/rpc".parse()?);    let registry = FlareContractRegistry::new(        address!("aD67FE66660Fb8dFE9d6b1b4240d8650e30F6019"),        provider,    );    let address = registry        .getContractAddressByName("WNat".to_string())        .call()        .await?;    println!("WNat address: {address}"); // WNat address: 0xC67DCE33D7A8efA5FfEB961899C73fe01bCe9273    Ok(())}
```

src/bin/make\_query.rs

```
use alloy::{primitives::address, providers::ProviderBuilder, sol};use eyre::Result;sol!(    #[sol(rpc)]    FlareContractRegistry,    "FlareContractRegistry.json");#[tokio::main]async fn main() -> Result<()> {    let provider =        ProviderBuilder::new().connect_http("https://flare-api.flare.network/ext/C/rpc".parse()?);    let registry = FlareContractRegistry::new(        address!("aD67FE66660Fb8dFE9d6b1b4240d8650e30F6019"),        provider,    );    let address = registry        .getContractAddressByName("WNat".to_string())        .call()        .await?;    println!("WNat address: {address}"); // WNat address: 0x1D80c49BbBCd1C0911346656B529DF9E5c2F783d    Ok(())}
```

```
cargo run --bin make_query
```

## Compiling a contract[​](#compiling-a-contract "Direct link to Compiling a contract")

For this example, you can use the `FtsoV2FeedConsumer` contract to query the FTSOv2 feeds. Copy the `FtsoV2FeedConsumer` sample contract code given below, and save the `.sol` file in the same folder as your `Cargo.toml`.

<details>
<summary>FtsoV2FeedConsumer sample contract</summary>

`FtsoV2FeedConsumer` sample contract

Note that the contract interface dependencies have been inlined to avoid any import issues.

FtsoV2FeedConsumer.sol

```
// SPDX-License-Identifier: MITpragma solidity >=0.8.0 <0.9.0;interface IFlareContractRegistry {    function getContractAddressByName(        string calldata _name    ) external view returns (address);}/** * THIS IS A TEST INTERFACE. * Functions are payable in the production interface. */interface TestFtsoV2Interface {    function getFeedsById(        bytes21[] calldata _feedIds    )        external        view        returns (            uint256[] memory _values,            int8[] memory _decimals,            uint64 _timestamp        );}/** * THIS IS AN EXAMPLE CONTRACT. * DO NOT USE THIS CODE IN PRODUCTION. */contract FtsoV2FeedConsumer {    IFlareContractRegistry internal contractRegistry;    TestFtsoV2Interface internal ftsoV2;    // Feed IDs, see https://dev.flare.network/ftso/feeds for full list    bytes21[] public feedIds = [        bytes21(0x01464c522f55534400000000000000000000000000), // FLR/USD        bytes21(0x014254432f55534400000000000000000000000000), // BTC/USD        bytes21(0x014554482f55534400000000000000000000000000) // ETH/USD    ];    /**     * Constructor initializes the FTSOv2 contract.     * The contract registry is used to fetch the FtsoV2 contract address.     */    constructor() {        contractRegistry = IFlareContractRegistry(            0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019        );        ftsoV2 = TestFtsoV2Interface(            contractRegistry.getContractAddressByName("FtsoV2")        );    }    /**     * Get the current value of the feeds.     */    function getFtsoV2CurrentFeedValues()        external        view        returns (            uint256[] memory _feedValues,            int8[] memory _decimals,            uint64 _timestamp        )    {        /* Your custom feed consumption logic. In this example the values are just returned. */        return ftsoV2.getFeedsById(feedIds);    }}
```

</details>

### Compile with solc[​](#compile-with-solc "Direct link to Compile with solc")

To compile the contract using the Solidity CLI compiler, create a file named `config.json`:

config.json

```
{  "language": "Solidity",  "sources": {    "FtsoV2FeedConsumer.sol": {      "urls": [        "./FtsoV2FeedConsumer.sol"      ]    }  },  "settings": {    "outputSelection": {      "FtsoV2FeedConsumer.sol": {        "FtsoV2FeedConsumer": [          "abi",          "metadata",          "evm.bytecode",          "evm.bytecode.sourceMap"        ]      }    },    "optimizer": {      "enabled": true,      "runs": 200    },    "evmVersion": "cancun"  }}
```

```
solc --standard-json config.json  > FtsoV2FeedConsumer.json
```

This will generate a `FtsoV2FeedConsumer.json` file with the contract's ABI and bytecode.

### Modify format[​](#modify-format "Direct link to Modify format")

Things get a bit annoying here, as the alloy-rs `solc!()` macro expects a specific format for the JSON, which is not the same as the output from the Solidity compiler. To fix this, in the generated `FtsoV2FeedConsumer.json`, remove the top-level JSON fields, after you are done, the JSON should look like:

FtsoV2FeedConsumer.json

```
{  "abi": [    ...  ],  "evm": {    ...  }  "metadata": "..."}
```

## Create account[​](#create-account "Direct link to Create account")

Before deploying a contract, you need to have an account with some testnet or mainnet gas tokens. You can create a new Flare account using `create_account.rs`:

src/bin/create\_account.rs

```
use alloy::signers::local::LocalSigner;use eyre::Result;#[tokio::main]async fn main() -> Result<()> {    let signer = LocalSigner::random();    println!(        "Account: {}, Private key: {}",        signer.address(),        signer.as_nonzero_scalar()    );    Ok(())}
```

```
cargo run --bin create_account
```

This will output a new private key and an account pair.

danger

-   Never share your private keys.
-   Never put your private keys in source code.
-   Never commit private keys to a Git repository.

You can save the account and private key into environment variables `ACCOUNT` and `ACCOUNT_PRIVATE_KEY` respectively.

note

You can also import the raw hex private key to MetaMask and any other wallet - the private key can be shared between your Rust code and any number of wallets.

-   For testnet, you can get free testnet C2FLR on the [Coston2 Faucet](https://faucet.flare.network/coston2).
-   For mainnet you will need FLR.

## Deploying with alloy-rs[​](#deploying-with-alloy-rs "Direct link to Deploying with alloy-rs")

With the account ready, you can now deploy the contract. In a `deploy_contract.rs` file, you can define the following code to deploy the contract:

src/bin/deploy\_contract.rs

```
// THIS IS EXAMPLE CODE. DO NOT USE THIS CODE IN PRODUCTION.use alloy::{    network::EthereumWallet, providers::ProviderBuilder, signers::local::PrivateKeySigner, sol,};use eyre::Result;sol!(    #[sol(rpc)]    FtsoV2FeedConsumer,    "FtsoV2FeedConsumer.json");#[tokio::main]async fn main() -> Result<()> {    let private_key = std::env::var("ACCOUNT_PRIVATE_KEY")?;    let signer: PrivateKeySigner = private_key.parse().unwrap();    let wallet = EthereumWallet::from(signer.clone());    let provider = ProviderBuilder::new()        .wallet(wallet)        .connect_http("https://coston2-api.flare.network/ext/C/rpc".parse()?);    let contract = FtsoV2FeedConsumer::deploy(&provider).await?;    println!("Deployed contract at address: {}", contract.address());    Ok(())}
```

src/bin/deploy\_contract.rs

```
// THIS IS EXAMPLE CODE. DO NOT USE THIS CODE IN PRODUCTION.use alloy::{    network::EthereumWallet, providers::ProviderBuilder, signers::local::PrivateKeySigner, sol,};use eyre::Result;sol!(    #[sol(rpc)]    FtsoV2FeedConsumer,    "FtsoV2FeedConsumer.json");#[tokio::main]async fn main() -> Result<()> {    let private_key = std::env::var("ACCOUNT_PRIVATE_KEY")?;    let signer: PrivateKeySigner = private_key.parse().unwrap();    let wallet = EthereumWallet::from(signer.clone());    let provider = ProviderBuilder::new()        .wallet(wallet)        .connect_http("https://flare-api.flare.network/ext/C/rpc".parse()?);    let contract = FtsoV2FeedConsumer::deploy(&provider).await?;    println!("Deployed contract at address: {}", contract.address());    Ok(())}
```

You can now run the `deploy_contract.rs` script to deploy the contract. The contract address will be printed once the deployment is successful. You can check the contract address on a Flare Blockchain explorer, linked on the [Network Configuration](/network/overview#configuration) page.

```
cargo run --bin deploy_contract
```

Congratulations! You have now successfully deployed a contract on Flare using 🦀.

What's next?

Learn how to interact with Flare's enshrined oracle [FTSOv2 using Rust](/ftso/guides/read-feeds-offchain).
