Skip to main content

Getting started in 5 minutes

This guide provides a general overview of running programs on the Vara Network. It guides you through how to write a program, compile it to Wasm and deploy it to the network.

important

Want to take your blockchain development skills to the next level? Join Gear Academy's free courses. Start from scratch with our Beginner Course or explore the implementation of programs using Gear technologies with the Intermediate Course. More courses are being developed.

Don't miss this opportunity to become a pro Vara blockchain developer. Enroll now in Gear Academy's courses!

Prerequisites

  1. Linux users should generally install GCC and Clang according to their distribution’s documentation.

    For example, on Ubuntu use:

    sudo apt install -y build-essential clang cmake curl

    On macOS, you can get a compiler toolset by running:

    xcode-select --install
  2. Make sure you have installed all the tools required to build a program in Rust. Rustup will be used to get Rust compiler ready:

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  3. A Wasm compiler is necessary for compiling a Rust program to Wasm, add it to the toolchain.

    rustup target add wasm32-unknown-unknown

Note: If you use Windows, download and install Build Tools for Visual Studio.

Creating your first Vara program

To get started, install the sails-cli tool using the following command:

cargo install sails-cli

After installation, you can create a new Vara project named vara-app by running:

cargo sails new-program vara-app

Your vara-app directory tree should look like this:

vara-app

├── app
│ └── src
│ └── lib.rs

├── client
│ └── ...

├── src
│ └── lib.rs

├── tests
│ └── gtest.rs

├── build.rs

├── Cargo.toml

└── README.md

In Cargo.toml, the essential libraries required for building your first project have been included:

vara-app/Cargo.toml
[workspace]

members = ["client"]


[package]
name = "vara-app"
version = "0.1.0"
edition = "2021"

[dependencies]
vara-app-app = { path = "app" }

[build-dependencies]
vara-app-app = { path = "app" }
sails-rs = { version = "0.5.0", features = ["wasm-builder"] }
sails-idl-gen = "0.5.0"

[dev-dependencies]
vara-app = { path = ".", features = ["wasm-binary"] }
vara-app-client = { path = "client" }
sails-rs = { version = "0.5.0", features = ["gtest"] }
tokio = { version = "1.39", features = ["rt", "macros"] }

[features]
wasm-binary = []

Let's move on to the main code:

This Rust code defines a simple program for the Vara Network, now updated with a new structure. The program consists of a VaraAppProgram struct with a method to instantiate it and another method to return a VaraAppService struct. The VaraAppService struct has a service method that returns the string "Hello from VaraApp!". This example demonstrates the basic structure and functionality of a Vara program using the sails_rs library.

vara-app/app/src/lib.rs
#![no_std]

use sails_rs::prelude::*;

struct VaraAppService(());

#[sails_rs::service]
impl VaraAppService {
pub fn new() -> Self {
Self(())
}

// Service's method (command)
pub fn do_something(&mut self) -> String {
"Hello from VaraApp!".to_string()
}

// Service's query
pub fn get_something(&self) -> String {
"Hello from VaraApp!".to_string()
}
}

pub struct VaraAppProgram(());

#[sails_rs::program]
impl VaraAppProgram {
// Program's constructor
pub fn new() -> Self {
Self(())
}

// Exposed service
pub fn vara_app(&self) -> VaraAppService {
VaraAppService::new()
}
}

Build your program with a single command:

cargo build --release

After a successful build, you can execute your tests to verify that everything is functioning correctly:

cargo test --release

If everything has been executed successfully, your working directory should now contain a target directory structured as follows:

vara-app
├── ...
├── target
├── ...
└── wasm32-unknown-unknown
└── release
├── vara_app.wasm <---- this is our built .wasm file
├── vara_app.opt.wasm <---- this is optimized .wasm file
└── vara_app.idl <---- this is our application interface .idl file
  • vara_app.wasm is the output Wasm binary built from source files
  • vara_app.opt.wasm is the optimized Wasm aimed to be uploaded to the blockchain
    (Optimization include reducing the file size and improving performance)
  • vara_app.idl is the Interface Definition Language (IDL) file that describes the application's interface, defining the structure and methods callable on the vara_app program. It’s essential for interacting with the application on the blockchain, specifying available functions and their inputs and outputs. This file is crucial for developers and clients to ensure they use the correct method signatures and data types when interacting with the deployed program.

Deploy your program to the Testnet

Gear provides an application for developers (Gear Idea) that implements all of the possibilities of interaction with programs in Vara networks (mainnet and testnet), available at idea.gear-tech.io.

Create account

  1. Download the Polkadot extension for your browser via https://polkadot.js.org/extension/. This extension manages accounts and allows the signing of transactions with those accounts. It is a secure tool that allows injecting your accounts into any Substrate-based dapp. It does not perform wallet functions, e.g send funds.

  2. Once downloaded, click + button to create a new account:

    Add account

  3. Make sure you save your 12-word mnemonic seed securely.

    Create account

  4. Select the network that will be used for this account - choose "Allow to use on any chain". Provide any name to this account and password and click "Add the account with the generated seed" to complete account registration.

    Finalizing account

  5. Go to idea.gear-tech.io. You will be prompted to grant access to your account for the application, click "Yes, allow this application access".

    Allow access

  6. Make sure you are connected to the Vara Network Testnet. The network name is at the bottom left corner of the page.

    Network name

  7. You may switch the network by clicking on the network name.

    Switch network

  8. Click the Connect button on the top-right to select an account that will be connected to Vara.

Connect account

  1. In accordance with the Actor model, programs are uploaded to a network via messages. Vara node charges a gas fee during message processing. Your account balance needs to have enough funds to upload a program to the TestNet. Click the following button to get the test balance:

    Get balance

    A notification about successful balance replenishment will appear after passing captcha at the bottom of the window. You can also see the current account balance next to the account name in the upper right corner.

    Transfer balance

Upload program

  1. When your account balance is sufficient, click the Upload program button to open a popup window for uploading your new program.

    Upload program button

  2. In the popup, click the Select file button and navigate to the .opt.wasm file we have pointed to above

    Upload new program

  3. After uploading the .opt.wasm file, you need to upload the IDL file. Click the Select file button and navigate to the .idl file referenced earlier.

    Upload idl button

  4. Specify the program name, click the Calculate Gas button to set the gas limit automatically, and then click the Submit button.

    Upload program form

  5. Sign the gear.uploadProgram transaction to Vara. It is recommended to set the checkbox Remember my password for the next 15 minutes for your convenience.

    Sign transaction

  6. Once your program is uploaded, go to the Programs section, find your program, and select it.

    Recently uploaded programs

  7. Click on your program to see more information, such as Metadata/Sails, Messages, Events, and the Vouchers pane.

    Main page program

Send message to a program

  1. Send your newly uploaded program a message to see how it responds! Click the Send message button.

  2. Click the Calculate Gas button to set the gas limit automatically, then click the Send Message button.

    Send form

  3. Sign the gear.sendMessage transaction as it is shown in step 5 of the section Upload Program.

  4. After your message has been successfully processed, you will see corresponding notifications:

    Log

    You have just sent a DoSomething command to your program!

  5. Go back to your program by either clicking the Cancel button or using the browser's back button.

  6. In the message pane, you can view the corresponding response.

    Log

  7. Click on the message response to see more information. The expected "Hello from VaraApp!" response can be seen in the Payload.

    Log


Further reading

For more info about writing programs for Vara and the specifics behind the program implementation, refer to this article.