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
Install Solana CLI
We will use solana
command line (CLI) tool to generate key and deploy our smart contract. To install Solana CLI, run:
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.
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:
Now we need to set an RPC URL to point to Yona RPC:
or
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:
Add the solana-program
crate to your new Rust library:
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:
Open your Cargo.toml
file and add these required Rust library configuration settings, updating your project name as appropriate:
As a result, your Cargo.toml
file should look like this:
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:
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:
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):
Deploy your Yona smart contract #
First, we need to create and fund a Yona account. Run the following comand:
When asked for the passphrase, leave it empty (press enter):
As a result, solana-keygen
will create a deploy_keypair.json
and out the corresponding pubkey and seed phrase:
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:
Using the Solana CLI, you can deploy your Yona smart contract:
Once your Yona smart contract has been deployed, it will output your smart contract public address (aka its "program id").
Congratulations! #
You have successfully setup, built, and deployed a Yona smart contract using the Rust language.
Last updated