8 golden rules to remember when searching for the perfect job

The road to landing your dream job can be a bumpy one. Luckily there are experts like Jeff Lovejoy, SumUp’s Future Talent Programme Manager, who are here to help guide you and make the process a…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Create a Solana Client with Rust

Solana is a decentralized blockchain that allows you to run decentralized apps.

I have been exploring this world a little in the last few months and would like to share some nice stuff about the Rust API for Solana. It has been being a good starting point for me.

Navigate to the folder containing the project and open your favorite IDE / Editor. I’m using Visual Studio Code.

Create a lib.rs file.

Add the following dependencies to the .toml file.

Create an RPC client on main.rs, that will be injected in the functions on lib.rs file. This way we centralize the configuration.

Also for simplicity, I’m injecting the RPC client instead of creating a struct with the client, for example.

Imports to be included on lib.rs file:

I’ll create a method to return a new keypair, only to reduce the dependencies of the rust SDK inside of the main file and better centralize the calls to the Solana network.

Function that returns newly created keypair

In this example, for every execution, we will be creating new keypairs for both sender and receiver.

The following snippet shows the code to request an airdrop.

The methods receive the amount in lamports, which represents 0.000000001 SOL. Therefore, it’s necessary to calculate the value in lamports prior to sending it to the RPC client methods.

Requesting an airdrop on Solana devnet

We can also check the balance using an account’s public key. The balance is returned in lamports, so we can format it by dividing by our created constant.

Function that checks the balance for a public key

And now the transfer between our two newly created accounts.

Transferring funds between accounts
Creation of keypairs and output of both keypair’s public keys
Output of public keys on console
Example of an account balance on Solana Explorer

We are almost finishing it. Here goes the snippet of the full execution in the main.rs file:

Run cargo run to execute it. The output should be as follows:

Console output

Here you can see the transfer transaction:

Solana Explorer

And that is all, folks. Hope it may have been useful for other that, like myself, are learning Rust and Solana.

See you in the next one!

Add a comment

Related posts:

Saudade da cidade

Fragmentos de saudade da cidade viva (sem o peso da pandemia). Texto publicado em Março de 2018 e reformulado em 2021. Recife é onde eu nasci, me criei, estudei, é a cidade que eu amo. Nesse momento…

I explained Ethereum to my brother

My brother is a smart boy. He’s interested in things like parties, cars, girls, etc. See, there’s nothing connected with software but I really wanted to share with him what I learned about…

Demand and Supply

In the world before the fear we walked like lions. In a pride. Not any more. If you listen carefully you can hear the beating heart of loneliness.