Deploy your first smart contract

In this short tutorial, we will write and deploy a Hello World contract on Yona

Yona is an SVM L2 and is fully compatible with Solana smart contracts and development tools. Let's write and deploy a Hello World smart contract from scratch.

Setup your local environment

Install Cargo and Rust

First, let's install Cargo (a package manager) and Rust compiler so that we can build and manage dependencies of our smart contracst

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Install Solana CLI

We will use solana command line (CLI) tool to generate key and deploy our smart contract. To install Solana CLI, run:

sh -c "$(curl -sSfL https://release.solana.com/stable/install)"

Depending on your operating system, the Solana CLI may automatically update PATH environment variable or may ask you to add to update the PATH manually.

Please update your PATH environment variable to include the Solana programs:

If you get the above message, simply copy and paste the command recommended by the Solana CLI installer to update your PATH environment variable.

Next restart your terminal to make sure your solana binaries are accessible in all the terminal sessions. To check this, ask Solana CLI to print its version:

solana --version

Now we need to set an RPC URL to point to Yona RPC:

solana config set --url https://devnet-rpc.yona.network/

or

solana config set --url https://testnet.yona.network/

Create a new Yona smart contract project

Yona smart contracts are Rust libraries #

Yona contracts are written in Rust and are libraries that compile to .so format.

First, Initialize a new Rust library named hello_world via the Cargo command line:

cargo init hello_world --lib
cd hello_world

Add the solana-program crate to your new Rust library:

cargo add solana-program

It is highly recommended to keep your solana-program and other Solana Rust dependencies in-line with your installed version of the Solana CLI. For example, if you are running Solana CLI 1.18.14, you can instead run:

cargo add solana-program@"=1.18.14"

Open your Cargo.toml file and add these required Rust library configuration settings, updating your project name as appropriate:

[lib]
name = "hello_world
"crate-type = ["cdylib", "lib"]

As a result, your Cargo.toml file should look like this:

[package]
name = "hello_world"
version = "0.1.0"
edition = "2024"

[lib]
name = "hello_world"
crate-type = ["cdylib", "lib"]


[dependencies]
solana-program = "1.18.14"

Create your first Yona smart contract #

Your Rust based Yona smart contract will live in your src/lib.rs file. There you can import your Rust crates and define your logic. Open your src/lib.rs file in your favorite editor. We recommend VSCode with rust-analyzer extension.

At the top of lib.rs, import the solana-program crate and bring our needed items into the local namespace:

use solana_program::{
    account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey,
};

Every Yona smart contract must define an entrypoint that tells the Yona runtime where to start executing your onchain code. Your program's entrypoint should be a public function:

// declare and export the program's entrypoint
entrypoint!(process_instruction);

// program entrypoint's implementation
pub fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> ProgramResult {
    // log a message to the blockchain
    msg!("Hello, world!");

    // gracefully exit the program
    Ok(())
}

Every smart contract should return the Ok result enum with a value of (). This tells the Yona runtime that your program executed successfully without errors.

The smart contract above will simply log "Hello, world!" to the blockchain cluster, then exit with Ok(()).

Build your Yona smart contract #

In a terminal, you can build your Yona smart contract by running in the root of your project (i.e. the directory with your Cargo.toml file):

cargo build-bpf

Deploy your Yona smart contract #

First, we need to create and fund a Yona account. Run the following comand:

solana-keygen new --outfile deploy_keypair.json

When asked for the passphrase, leave it empty (press enter):

BIP39 Passphrase (empty for none): Wrote new keypair to deploy_keypair.json

As a result, solana-keygen will create a deploy_keypair.json and out the corresponding pubkey and seed phrase:

=====================================================================================
pubkey: AWVhMZ7TFbeYo2B5vifRmacGjVV1dqVEwTtQeEgWh6LQ
=====================================================================================
Save this seed phrase and your BIP39 passphrase to recover your new keypair:
XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX 
=====================================================================================

To fund the account linked to this pubkey, join Yona Discord at https://discord.gg/yonanetwork, go to #devnet-faucet channel, and send 'faucet' command to this channel:

faucet AWVhMZ7TFbeYo2B5vifRmacGjVV1dqVEwTtQeEgWh6LQ

Using the Solana CLI, you can deploy your Yona smart contract:

solana program deploy ./target/deploy/hello_world.so -k deploy_keypair.json

Once your Yona smart contract has been deployed, it will output your smart contract public address (aka its "program id").

outputProgram Id: EFH95fWg49vkFNbAdw9vy75tM7sWZ2hQbTTUmuACGip3

Congratulations! #

You have successfully setup, built, and deployed a Yona smart contract using the Rust language.

Last updated

#26:

Change request updated