Skip to content

Instantly share code, notes, and snippets.

@kalloc
Created March 9, 2025 16:46
Show Gist options
  • Save kalloc/4b8a0016d5b2a2ccc67204d274e57f49 to your computer and use it in GitHub Desktop.
Save kalloc/4b8a0016d5b2a2ccc67204d274e57f49 to your computer and use it in GitHub Desktop.
use anchor_lang::prelude::*;
declare_id!("8UgrFcWWrTsme2yQ8xvWGtpKTgNoZmq8Lro9gFyksPJN");
#[program]
pub mod x {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
msg!("Greetings from: {:?}", ctx.program_id);
Ok(())
}
pub fn update(ctx: Context<UpdateCtx>) -> Result<()> {
msg!("{}", ctx.accounts.state.chance);
ctx.accounts.state.chance +=1;
Ok(())
}
}
#[account]
#[derive(InitSpace)]
pub struct PdaState {
pub bump: u8,
pub is_authorized: bool,
pub pda_level: u8,
pub chance: u8,
pub module: bool,
pub bonus: Option<u8>,
pub mint: Pubkey,
#[max_len(5)]
pub races: Vec<u8>,
}
impl PdaState {
pub const SEED: &'static [u8] = b"state";
pub const MAX_SIZE: usize = 32 // admin
+ 8 // freeze until
+ 32 // treasury
+ 32 // bank
+ 32 // reward mint
+ 32 // active season
+ 1 + 50 * 32 // scheduled seasons
+ 1000 // padding
;
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(
init,
seeds = [PdaState::SEED],
payer = payer,
space = 1000,
bump
)]
pub state: Account<'info, PdaState>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct UpdateCtx<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(
mut,
seeds = [PdaState::SEED],
bump
)]
pub state: Account<'info, PdaState>,
pub system_program: Program<'info, System>,
}
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { X } from "../target/types/x";
import { assert } from "chai";
describe("x", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.X as Program<X>;
it("Is initialized!", async () => {
// Add your test here.
const tx = await program.methods.initialize().rpc();
const result= await program.provider.connection.confirmTransaction(tx);
const receipt = await program.provider.connection.getParsedTransaction(tx, "confirmed");
console.log("Your transaction signature", receipt);
});
it("Update twice time", async () => {
// Add your test here.
{
const tx = await program.methods.update().rpc();
const result = await program.provider.connection.confirmTransaction(tx);
const receipt = await program.provider.connection.getParsedTransaction(tx, "confirmed");
console.log("Receipt", receipt);
}
{
const tx = await program.methods.update().rpc();
const result = await program.provider.connection.confirmTransaction(tx);
const receipt = await program.provider.connection.getParsedTransaction(tx, "confirmed");
console.log("Receipt", receipt);
}
const [pda] = anchor.web3.PublicKey.findProgramAddressSync(
[Buffer.from("state")],
program.programId
);
const state = await program.account.pdaState.fetch(pda);
assert.equal(state.chance, 2);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment