Skip to content

Commit

Permalink
Merge pull request #1 from Confucian-e/program/counter
Browse files Browse the repository at this point in the history
feat: add program counter
  • Loading branch information
Confucian-e authored Apr 25, 2024
2 parents e4cee02 + 8886257 commit 815c8d4
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ resolution = true
skip-lint = false

[programs.localnet]
counter = "DsPBXEEbHUt3Vdyx2qdesFLmDRTRndwA2v6qfdjAxL1T"
solana_program_practice = "9u81MKiqMR7oRyM5M9v666eKoWqEMRiR9bF5amjmyEug"

[registry]
Expand Down
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions programs/counter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "counter"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "counter"

[features]
default = []
cpi = ["no-entrypoint"]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = "0.30.0"
2 changes: 2 additions & 0 deletions programs/counter/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
48 changes: 48 additions & 0 deletions programs/counter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use anchor_lang::prelude::*;

declare_id!("DsPBXEEbHUt3Vdyx2qdesFLmDRTRndwA2v6qfdjAxL1T");

#[program]
pub mod counter {
use super::*;

pub fn initialize(ctx: Context<Initialize>, start: u64) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count = start;

Ok(())
}

pub fn increment(ctx: Context<Increment>) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count += 1;

Ok(())
}
}

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(
init,
seeds = [b"counter"],
bump,
payer = signer,
space = 8 + std::mem::size_of::<Counter>()
)]
pub counter: Account<'info, Counter>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Increment<'info> {
#[account(mut)]
pub counter: Account<'info, Counter>,
}

#[account]
pub struct Counter {
pub count: u64,
}
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
anchor-client = "0.30.0"
solana-program-practice = { version = "0.1.0", path = "../programs/solana-program-practice" }
counter = { version = "0.1.0", path = "../programs/counter" }
3 changes: 3 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#[cfg(test)]
mod test_initialize;

#[cfg(test)]
mod test_counter;
73 changes: 73 additions & 0 deletions tests/src/test_counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use anchor_client::{
solana_sdk::{
commitment_config::CommitmentConfig, pubkey::Pubkey, signature::read_keypair_file,
system_program,
},
Client, Cluster,
};

use counter;

#[test]
fn test_initialize() {
let anchor_wallet = std::env::var("ANCHOR_WALLET").unwrap();
let payer = read_keypair_file(&anchor_wallet).unwrap();

let client = Client::new_with_options(Cluster::Localnet, &payer, CommitmentConfig::confirmed());
let program = client.program(counter::ID).unwrap();

let seeds = &[b"counter".as_ref()];
let (counter, _) = Pubkey::find_program_address(seeds, &counter::ID);

let start = 1;
let _tx = program
.request()
.accounts(counter::accounts::Initialize {
counter,
signer: program.payer(),
system_program: system_program::ID,
})
.args(counter::instruction::Initialize { start })
.signer(&payer)
.send()
.unwrap();

let counter_account: counter::Counter = program.account(counter).unwrap();
assert_eq!(counter_account.count, start);
}

#[test]
fn test_increment() {
let anchor_wallet = std::env::var("ANCHOR_WALLET").unwrap();
let payer = read_keypair_file(&anchor_wallet).unwrap();

let client = Client::new_with_options(Cluster::Localnet, &payer, CommitmentConfig::confirmed());
let program = client.program(counter::ID).unwrap();

let seeds = &[b"counter".as_ref()];
let (counter, _) = Pubkey::find_program_address(seeds, &counter::ID);

let start = 1;
let _tx = program
.request()
.accounts(counter::accounts::Initialize {
counter,
signer: program.payer(),
system_program: system_program::ID,
})
.args(counter::instruction::Initialize { start })
.signer(&payer)
.send()
.unwrap();

let _tx = program
.request()
.accounts(counter::accounts::Increment { counter })
.args(counter::instruction::Increment {})
.signer(&payer)
.send()
.unwrap();

let counter_account: counter::Counter = program.account(counter).unwrap();
assert_eq!(counter_account.count, start + 1);
}

0 comments on commit 815c8d4

Please sign in to comment.