Skip to content

Commit 30da422

Browse files
committed
counter: add anchor example implementation
1 parent 540c9af commit 30da422

File tree

12 files changed

+191
-0
lines changed

12 files changed

+191
-0
lines changed

basics/counter/anchor/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
.anchor
3+
.DS_Store
4+
target
5+
**/*.rs.bk
6+
node_modules
7+
test-ledger
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
.anchor
3+
.DS_Store
4+
target
5+
node_modules
6+
dist
7+
build
8+
test-ledger

basics/counter/anchor/Anchor.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[features]
2+
seeds = false
3+
skip-lint = false
4+
[programs.localnet]
5+
counter_anchor = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
6+
7+
[registry]
8+
url = "https://api.apr.dev"
9+
10+
[provider]
11+
cluster = "localnet"
12+
wallet = "/Users/noahgundotra/.config/solana/id.json"
13+
14+
[scripts]
15+
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

basics/counter/anchor/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[workspace]
2+
members = [
3+
"programs/*"
4+
]
5+
6+
[profile.release]
7+
overflow-checks = true
8+
lto = "fat"
9+
codegen-units = 1
10+
[profile.release.build-override]
11+
opt-level = 3
12+
incremental = false
13+
codegen-units = 1

basics/counter/anchor/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Anchor Counter
2+
3+
Anchor enforces init constraints that enforces good programming paradigms.
4+
5+
This means this program has an additional initialization instruction for `Counter`s that the Solana native program does not.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Migrations are an early feature. Currently, they're nothing more than this
2+
// single deploy script that's invoked from the CLI, injecting a provider
3+
// configured from the workspace's Anchor.toml.
4+
5+
const anchor = require("@project-serum/anchor");
6+
7+
module.exports = async function (provider) {
8+
// Configure client to use the provider.
9+
anchor.setProvider(provider);
10+
11+
// Add your deploy script here.
12+
};

basics/counter/anchor/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"scripts": {
3+
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
4+
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
5+
},
6+
"dependencies": {
7+
"@project-serum/anchor": "^0.25.0"
8+
},
9+
"devDependencies": {
10+
"chai": "^4.3.4",
11+
"mocha": "^9.0.3",
12+
"ts-mocha": "^10.0.0",
13+
"@types/bn.js": "^5.1.0",
14+
"@types/chai": "^4.3.0",
15+
"@types/mocha": "^9.0.0",
16+
"typescript": "^4.3.5",
17+
"prettier": "^2.6.2"
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "counter_anchor"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "counter_anchor"
10+
11+
[features]
12+
no-entrypoint = []
13+
no-idl = []
14+
no-log-ix-name = []
15+
cpi = ["no-entrypoint"]
16+
default = []
17+
18+
[dependencies]
19+
anchor-lang = { version = "0.25.0", features=["init-if-needed"] }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.bpfel-unknown-unknown.dependencies.std]
2+
features = []
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use anchor_lang::prelude::*;
2+
3+
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
4+
5+
#[program]
6+
pub mod counter_anchor {
7+
use super::*;
8+
9+
pub fn initialize_counter(ctx: Context<InitializeCounter>) -> Result<()> {
10+
Ok(())
11+
}
12+
13+
pub fn increment(ctx: Context<Increment>) -> Result<()> {
14+
ctx.accounts.counter.count += 1;
15+
Ok(())
16+
}
17+
}
18+
19+
#[account]
20+
pub struct Counter {
21+
count: u64,
22+
}
23+
24+
#[derive(Accounts)]
25+
pub struct InitializeCounter<'info> {
26+
#[account(init, space=8+8, payer=payer)]
27+
pub counter: Account<'info, Counter>,
28+
#[account(mut)]
29+
pub payer: Signer<'info>,
30+
pub system_program: Program<'info, System>,
31+
}
32+
33+
#[derive(Accounts)]
34+
pub struct Increment<'info> {
35+
#[account(mut)]
36+
pub counter: Account<'info, Counter>,
37+
}

0 commit comments

Comments
 (0)