Skip to content

Commit 3ed97da

Browse files
authored
Merge pull request #6 from ngundotra/counter
Add Counter Program example implementations
2 parents f6714dc + af0a009 commit 3ed97da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1671
-0
lines changed

basics/counter/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Counter
2+
3+
This example program allows anyone to create a counter and increment it.
4+
5+
Any counter can be incremented by any key.
6+
7+
## Note: Seahorse
8+
9+
Seahorse currently does not allow the program to initialize anchor
10+
accounts unless they are PDAs.
11+
12+
Seahorse example only allows users to increment the counter that corresponds to their public key.
13+
14+

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

basics/counter/anchor/.prettierignore

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 = "0.25.0"
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as anchor from "@project-serum/anchor";
2+
import { Program } from "@project-serum/anchor";
3+
import {
4+
Keypair
5+
} from '@solana/web3.js'
6+
import { assert } from "chai";
7+
import { CounterAnchor } from "../target/types/counter_anchor";
8+
9+
describe("counter_anchor", () => {
10+
// Configure the client to use the local cluster.
11+
anchor.setProvider(anchor.AnchorProvider.env());
12+
13+
const program = anchor.workspace.CounterAnchor as Program<CounterAnchor>;
14+
15+
it("Test increment", async () => {
16+
const counterKeypair = Keypair.generate();
17+
const counter = counterKeypair.publicKey;
18+
19+
// Initialize counter
20+
await program.methods
21+
.initializeCounter()
22+
.accounts({ counter, payer: program.provider.publicKey })
23+
.signers([counterKeypair])
24+
.rpc({ skipPreflight: true, commitment: "confirmed" });
25+
let currentCount = (await program.account.counter.fetch(counter, "confirmed")).count.toNumber();
26+
assert(currentCount === 0, "Expected initialized count to be 0");
27+
28+
// Increment counter
29+
await program.methods
30+
.increment()
31+
.accounts({ counter })
32+
.rpc({ skipPreflight: true, commitment: "confirmed" });
33+
currentCount = (await program.account.counter.fetch(counter, "confirmed")).count.toNumber();
34+
assert(currentCount === 1, "Expected count to be 1");
35+
36+
// Increment counter
37+
await program.methods
38+
.increment()
39+
.accounts({ counter })
40+
.rpc({ skipPreflight: true, commitment: "confirmed" });
41+
currentCount = (await program.account.counter.fetch(counter, "confirmed")).count.toNumber();
42+
assert(currentCount === 2, "Expected count to be 2");
43+
});
44+
});

basics/counter/anchor/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"types": ["mocha", "chai"],
4+
"typeRoots": ["./node_modules/@types"],
5+
"lib": ["es2015"],
6+
"module": "commonjs",
7+
"target": "es6",
8+
"esModuleInterop": true
9+
}
10+
}

basics/counter/mpl-stack/.solitarc.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @ts-check
2+
const path = require('path');
3+
const programDir = path.join(__dirname);
4+
const idlDir = path.join(__dirname, 'idl');
5+
const sdkDir = path.join(__dirname, 'ts', 'generated');
6+
const binaryInstallDir = path.join(__dirname, 'target', 'solita');
7+
8+
module.exports = {
9+
idlGenerator: 'shank',
10+
programName: 'counter_mpl_stack',
11+
programId: 'Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS',
12+
idlDir,
13+
sdkDir,
14+
binaryInstallDir,
15+
programDir,
16+
};

basics/counter/mpl-stack/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "counter-mpl-stack"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib", "lib"]
8+
9+
[features]
10+
no-entrypoint = []
11+
cpi = ["no-entrypoint"]
12+
default = []
13+
14+
[dependencies]
15+
borsh = "0.9"
16+
shank = "0.0.8"
17+
solana-program = "1.10.38"

basics/counter/mpl-stack/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Counter: MPL Stack
2+
3+
This example program is written using Solana native using MPL stack.
4+
5+
6+
## Setup
7+
8+
1. Build the program with `cargo build-bpf`
9+
2. Compile the idl with `shank build`
10+
3. Build the typescript SDK with `yarn solita`
11+
- Temporarily, we have to modify line 58 in ts/generated/accounts/Counter.ts
12+
to `const accountInfo = await connection.getAccountInfo(address, { commitment: "confirmed" });` in order to allow the tests to pass. In the future versions of Solita, this will be fixed.
13+
4. Run tests with `yarn test`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "0.1.0",
3+
"name": "counter_mpl_stack",
4+
"instructions": [
5+
{
6+
"name": "Increment",
7+
"accounts": [
8+
{
9+
"name": "counter",
10+
"isMut": true,
11+
"isSigner": false,
12+
"desc": "Counter account to increment"
13+
}
14+
],
15+
"args": [],
16+
"discriminant": {
17+
"type": "u8",
18+
"value": 0
19+
}
20+
}
21+
],
22+
"accounts": [
23+
{
24+
"name": "Counter",
25+
"type": {
26+
"kind": "struct",
27+
"fields": [
28+
{
29+
"name": "count",
30+
"type": "u64"
31+
}
32+
]
33+
}
34+
}
35+
],
36+
"metadata": {
37+
"origin": "shank",
38+
"address": "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS",
39+
"binaryVersion": "0.0.8",
40+
"libVersion": "0.0.8"
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": "0.1.0",
3+
"name": "counter_solana_native",
4+
"instructions": [
5+
{
6+
"name": "Increment",
7+
"accounts": [],
8+
"args": [],
9+
"discriminant": {
10+
"type": "u8",
11+
"value": 0
12+
}
13+
}
14+
],
15+
"accounts": [
16+
{
17+
"name": "Counter",
18+
"type": {
19+
"kind": "struct",
20+
"fields": [
21+
{
22+
"name": "count",
23+
"type": "u64"
24+
}
25+
]
26+
}
27+
}
28+
],
29+
"metadata": {
30+
"origin": "shank",
31+
"address": "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
32+
}
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
preset: 'ts-jest/presets/default',
3+
testEnvironment: 'node',
4+
testTimeout: 100000,
5+
resolver: "ts-jest-resolver",
6+
};

basics/counter/mpl-stack/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "counter-mpl-stack",
3+
"version": "0.1.0",
4+
"description": "Counter program written using MPL tooling",
5+
"main": "index.js",
6+
"author": "ngundotra",
7+
"license": "Apache-2.0",
8+
"private": false,
9+
"scripts": {
10+
"start-validator": "solana-test-validator --reset --quiet --bpf-program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS ./target/deploy/counter_solana_native.so",
11+
"run-tests": "jest tests --detectOpenHandles",
12+
"test": "start-server-and-test start-validator http://localhost:8899/health run-tests"
13+
},
14+
"devDependencies": {
15+
"@types/bn.js": "^5.1.1",
16+
"@types/jest": "^29.0.0",
17+
"chai": "^4.3.6",
18+
"jest": "^29.0.2",
19+
"start-server-and-test": "^1.14.0",
20+
"ts-jest": "^28.0.8",
21+
"ts-jest-resolver": "^2.0.0",
22+
"ts-node": "^10.9.1",
23+
"typescript": "^4.8.2"
24+
},
25+
"dependencies": {
26+
"@metaplex-foundation/beet": "^0.6.1",
27+
"@metaplex-foundation/solita": "^0.15.2",
28+
"@solana/web3.js": "^1.56.2"
29+
}
30+
}

0 commit comments

Comments
 (0)