Last active
November 15, 2024 04:40
-
-
Save aeither/837af6b62a16374cc8b167c68ea7cf73 to your computer and use it in GitHub Desktop.
Solana Anchor Lang Cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------------------------- // | |
// Use Anchor Prelude | |
use anchor_lang::prelude::*; | |
// - Import mods | |
pub mod constant; | |
pub mod states; | |
use crate::{constant::*, states::*}; | |
// Declare id | |
declare_id!("11111111111111111111111111111111"); | |
pub rent: Sysvar<'info, Rent>, | |
// Create Program | |
#[program] | |
mod my_program { | |
use super::*; | |
pub fn init(ctx: Context<MyContext>) -> Result<()> { | |
Ok(()) | |
} | |
// Create fn | |
pub fn name(ctx: Context<MyContext>, idx: u8, description: String) -> Result<()> { | |
// - where <update_account> is a #[derive(Accounts)] > #[account(mut)] | |
let update_account = &mut ctx.accounts.update_account; | |
// ReQuire | |
require!(!update_account.marked, MyError::CustomErrorName); | |
// - Fill contents with argument | |
update_account.authority = ctx.accounts.authority.key(); | |
update_account.idx = idx; | |
update_account.description = description; | |
// - Increase or decrease count .checked_add/.checked_sub | |
user_profile.total_likes = user_profile.total_likes | |
.checked_sub(1) | |
.unwrap(); | |
Ok(()) | |
} | |
// - Pass to #[instruction(bump: u8)] | |
pub fn pass_intruction(ctx: Context<MyContext>, _bump: u8) -> Result<()> { | |
Ok(()) | |
} | |
} | |
// Create Context | |
#[derive(Accounts)] | |
#[instruction(bump: u8)] | |
pub struct MyContext<'info> { | |
// Init Account | |
#[account(init, payer = authority, space = 8)] | |
pub new_account: Account<'info, NewAccount>, | |
// Mut Account | |
#[account(mut)] | |
pub update_account: Account<'info, NewAccount>, | |
// - Init with seeds and bump to create PDAs | |
#[account( | |
init, | |
payer = authority, | |
space = 8 + std::mem::size_of::<UserProfile>(), | |
seeds = [b"user_account", authority.key().as_ref(), &[user_profile.last_index as u8].as_ref()], | |
bump = bump, | |
)] | |
pub user_profile: Box<Account<'info, UserProfile>>, | |
// Signer | |
#[account(mut)] | |
pub authority: Signer<'info>, | |
// System Program | |
pub system_program: Program<'info, System>, | |
} | |
// Create Account | |
#[account] | |
#[derive(Default)] | |
pub struct MyAccount { | |
field: u64, | |
} | |
// - Constant for seeds | |
#[constant] | |
pub const USER_ACCOUNT: &[u8] = b"user_account"; | |
// Create Custom Error | |
#[error_code] | |
pub enum MyError { | |
#[msg("Custom Error Message")] | |
CustomErrorName, | |
} | |
// ---------------------------------------------------------- // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment