Since 2009, we have been utilizing our extensive expertise in blockchain technologies to help businesses, both large and small, maximize their efficiency.
Explore More
With more than 400+ experts, Oodles comprises a fantastic resource of business knowledge that spans multiple industries. Whatever the circumstances, we keep to our obligations.
Explore More
At Oodles, we help our clients work with a human understanding but at superhuman speed something that others can't. They thus advance and maintain their lead
31st March 2024
16 min read
Senior Associate Consultant L1 - Development
The Anchor framework is a Rust-based framework for building Solana programs using Solana blockchain development. It simplifies the process of developing Solana smart contracts by providing a higher-level abstraction over Solana's low-level programming model. Anchor abstracts away much of the complexity involved in writing Solana programs, making it easier for developers to focus on application logic rather than low-level details.
Go here to install Rust.
To set up Solana, visit the Solana website to download and install the software. Once installed, open your terminal and run the command `solana-keygen new` to generate a key pair. This will create a keypair at the default location.
Go here to install Yarn.
Anchor version manager is a tool for using multiple versions of the anchor-cli. It will require the same dependencies as building from source. It is recommended you uninstall the NPM package if you have it installed.
Install avm using Cargo. Note this will replace your anchor binary if you had one installed.
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
On Linux systems, you may need to install additional dependencies if the cargo install fails. E.g. on Ubuntu:
Install the latest version of the CLI using AVM, and then set it to be the version to use.
avm install latest
avm use latest
Verify the installation.
anchor --version
1.) Initialize a new project, simply run:
anchor init
2.) Let's begin by opening lib.rs in the programs folder, we can proceed with developing our program.
3.) Below is the program code
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Token, TokenAccount, Transfer as SplTransfer};
use solana_program::system_instruction;
declare_id!("MyUniqueProgramId");
#[program]
pub mod my_program {
use super::*;
// Function for transferring lamports
pub fn transfer_lamports(ctx: Context, amount: u64) -> Result<()> {
let sender_account = &ctx.accounts.sender;
let recipient_account = &ctx.accounts.recipient;
// Create the transfer instruction
let transfer_instruction =
system_instruction::transfer(sender_account.key, recipient_account.key, amount);
// Invoke the transfer instruction
anchor_lang::solana_program::program::invoke_signed(
&transfer_instruction,
&[
sender_account.to_account_info(),
recipient_account.clone(),
ctx.accounts.system_program.to_account_info(),
],
&[],
)?;
Ok(())
}
// Function for transferring SPL tokens
pub fn transfer_spl_tokens(ctx: Context, amount: u64) -> Result<()> {
let destination_account = &ctx.accounts.destination_token_account;
let source_account = &ctx.accounts.source_token_account;
let token_program = &ctx.accounts.spl_token_program;
let authority = &ctx.accounts.authority;
// Transfer tokens from source to destination
let cpi_accounts = SplTransfer {
from: source_account.to_account_info().clone(),
to: destination_account.to_account_info().clone(),
authority: authority.to_account_info().clone(),
};
let cpi_program = token_program.to_account_info();
// Invoke SPL token transfer
token::transfer(CpiContext::new(cpi_program, cpi_accounts), amount)?;
Ok(())
}
}
// Accounts for transferring lamports
#[derive(Accounts)]
pub struct LamportTransfer<'info> {
#[account(mut)]
pub sender: Signer<'info>,
#[account(mut)]
pub recipient: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
// Accounts for transferring SPL tokens
#[derive(Accounts)]
pub struct SplTokenTransfer<'info> {
pub authority: Signer<'info>,
#[account(mut)]
pub source_token_account: Account<'info, TokenAccount>,
#[account(mut)]
pub destination_token_account: Account<'info, TokenAccount>,
pub spl_token_program: Program<'info, Token>,
}
4.)Let's break down the code and explain the program:
To get started with Solana blockchain development or a project development on Solana, connect with our blockchain developers,
Ashish Gushain
Ashish Gushain is a highly experienced backend developer with over 1+ years of industry expertise in Solidity and Smart contract development. He possesses in-depth knowledge of web3, blockchain architecture, and practical experience in working with various tools and frameworks such as Nodejs, Hardhat, Remix, React, and databases such as MongoDB and Postgres. He is highly skilled in decentralized finance (DeFi) and specializes in implementing complex financial protocols on the blockchain. He has a passion for reading and constantly stays updated with the latest trends in the blockchain and cryptocurrency space.
Senior Associate Consultant L1 - Development
By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.
We would love to
hear from you!
Innovate with confidence!