Skip to content

Instantly share code, notes, and snippets.

@zappycode
Created January 16, 2024 14:19
Show Gist options
  • Save zappycode/eb98e02e3a20ca20dac95364395c61b1 to your computer and use it in GitHub Desktop.
Save zappycode/eb98e02e3a20ca20dac95364395c61b1 to your computer and use it in GitHub Desktop.
Color Program
use anchor_lang::prelude::*;
// This is your program's public key and it will update
// automatically when you build the project.
declare_id!("ApNvFquHyG4kQTnAb3VAscw7URn3JDF6LYacsSzdxJqm");
#[program]
pub mod rgb_color_setter {
// Imports
use anchor_lang::solana_program::{
native_token::sol_to_lamports, program::invoke, system_instruction::transfer,
};
// Using Import Inheritance
use super::*;
// NOTE Initialize will exexute only once for the first time
// After deploying, you then call this function
// Could be called by someone else, but very unlikely
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
ctx.accounts.config.fee_recipient = ctx.accounts.fee_recipient.key();
ctx.accounts.color.red = 255;
ctx.accounts.color.green = 0;
ctx.accounts.color.blue = 0;
// You could do something like this as long as you declare the variable down at the bottom as a let or a struct
// It's preffered to put it in the Intialize truct as a program
ctx.accounts.color.mynumber = 5;
// String could be a different size so that's why it's not a great way to store data
Ok(())
}
pub fn set_color(ctx: Context<SetColor>, red: u8, green: u8, blue: u8) -> Result<()> {
// Transfer Funds from User to Recipient Account
// For cross program interaction
match invoke(
&transfer(
ctx.accounts.user.key,
&ctx.accounts.config.fee_recipient,
sol_to_lamports(0.001),
),
// Safety check
&[
ctx.accounts.user.to_account_info(),
ctx.accounts.fee_recipient.to_account_info(),
],
) {
Ok(()) => {
ctx.accounts.color.red = red;
ctx.accounts.color.green = green;
ctx.accounts.color.blue = blue;
}
Err(err) => {
return Err(err.into());
}
}
Ok(())
}
}
// --------------------------------------------------------------------------------------------
// This is what is passed into the intialize function
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(
init,
seeds = [CONFIG_PDA_SEED.as_ref()],
bump,
payer = admin,
space = 8 + Config::LEN,
)]
pub config: Account<'info, Config>,
#[account(
init,
seeds = [COLOR_PDA_SEED.as_ref()],
bump,
payer = admin,
space = 8 + Color::LEN,
)]
pub color: Account<'info, Color>,
#[account(mut)]
pub admin: Signer<'info>,
pub fee_recipient: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct SetColor<'info> {
#[account(
seeds = [CONFIG_PDA_SEED.as_ref()],
bump,
)]
pub config: Account<'info, Config>,
#[account(
mut,
seeds = [COLOR_PDA_SEED.as_ref()],
bump,
)]
pub color: Account<'info, Color>,
#[account(mut)]
pub user: Signer<'info>,
#[account(mut, constraint = config.fee_recipient == fee_recipient.key(),)]
pub fee_recipient: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
// ------------------------------------------------------------------------------
// State
#[account]
#[derive(Default, Debug)]
pub struct Color {
pub red: u8,
pub green: u8,
pub blue: u8,
pub mynumber: u8,
}
// Implimination, showing the length. You are allocating the memory of the stuff above. Look below at Config for the same thing
impl Color {
pub const LEN: usize = 1 + 1 + 1 + 1;
}
// This is only for const
// let mynumber: u8
#[account]
#[derive(Default, Debug)]
pub struct Config {
pub fee_recipient: Pubkey,
}
impl Config {
pub const LEN: usize = 32;
}
//Accounts Seeds
pub const CONFIG_PDA_SEED: &[u8] = b"rgb-color-config-account-9000";
pub const COLOR_PDA_SEED: &[u8] = b"rgb-color-account-9000";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment