-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Confucian-e/program/counter
feat: add program counter
- Loading branch information
Showing
8 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
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" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains 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
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, | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
#[cfg(test)] | ||
mod test_initialize; | ||
|
||
#[cfg(test)] | ||
mod test_counter; |
This file contains 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
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); | ||
} |