Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamSspirit committed Oct 24, 2024
1 parent 48b4ca5 commit 6aa0b88
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 104 deletions.
68 changes: 22 additions & 46 deletions basics/counter/poseidon/tests/bankrun.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import * as anchor from "@coral-xyz/anchor";
import { Keypair, PublicKey } from "@solana/web3.js";
import { BankrunProvider } from "anchor-bankrun";
import { assert } from "chai";
import { startAnchor } from "solana-bankrun";
import type { CounterProgramPoseidon } from "../target/types/counter_program_poseidon";

const IDL = require("../target/idl/counter_program.json");
import * as anchor from '@coral-xyz/anchor';
import { Keypair, PublicKey } from '@solana/web3.js';
import { BankrunProvider } from 'anchor-bankrun';
import { assert } from 'chai';
import { startAnchor } from 'solana-bankrun';
import type { CounterProgramPoseidon } from '../target/types/counter_program_poseidon';

const IDL = require('../target/idl/counter_program.json');
const PROGRAM_ID = new PublicKey(IDL.address);

describe("counter_program", async () => {
describe('counter_program', async () => {
// Configure the client to use the anchor-bankrun
const context = await startAnchor(
"",
[{ name: "counter_program", programId: PROGRAM_ID }],
[]
);
const context = await startAnchor('', [{ name: 'counter_program', programId: PROGRAM_ID }], []);

const provider = new BankrunProvider(context);

Expand All @@ -25,7 +21,7 @@ describe("counter_program", async () => {
// Generate a new keypair for the counter account
const counterKeypair = new Keypair();

it("Initialize Counter", async () => {
it('Initialize Counter', async () => {
await program.methods
.initializeCounter()
.accounts({
Expand All @@ -35,44 +31,24 @@ describe("counter_program", async () => {
})
.rpc();

const currentCount = await program.account.counterState.fetch(
counterKeypair.publicKey
);
const currentCount = await program.account.counterState.fetch(counterKeypair.publicKey);

assert(
currentCount.count.toNumber() === 0,
"Expected initialized count to be 0"
);
assert(currentCount.count.toNumber() === 0, 'Expected initialized count to be 0');
});

it("Increment Counter", async () => {
await program.methods
.incrementCounter()
.accounts({ counter: counterKeypair.publicKey })
.rpc();
it('Increment Counter', async () => {
await program.methods.incrementCounter().accounts({ counter: counterKeypair.publicKey }).rpc();

const currentCount = await program.account.counterState.fetch(
counterKeypair.publicKey
);
const currentCount = await program.account.counterState.fetch(counterKeypair.publicKey);

assert(currentCount.count.toNumber() === 1, "Expected count to be 1");
assert(currentCount.count.toNumber() === 1, 'Expected count to be 1');
});

it("Decrement Counter", async () => {
await program.methods
.decrementCounter()
.accounts({ counter: counterKeypair.publicKey })
.rpc();
it('Decrement Counter', async () => {
await program.methods.decrementCounter().accounts({ counter: counterKeypair.publicKey }).rpc();

const currentCount = await program.account.counterState.fetch(
counterKeypair.publicKey
);
const currentCount = await program.account.counterState.fetch(counterKeypair.publicKey);

assert(
currentCount.count.toNumber() === 0,
"Expected count to be 0 after decrement"
);
assert(currentCount.count.toNumber() === 0, 'Expected count to be 0 after decrement');
});


});
});
53 changes: 20 additions & 33 deletions basics/counter/poseidon/tests/counter-program.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { CounterProgramPoseidon } from "../target/types/counter_program_poseidon";
import { Keypair } from "@solana/web3.js";
import { assert } from "chai";
import * as anchor from '@coral-xyz/anchor';
import { Program } from '@coral-xyz/anchor';
import { Keypair } from '@solana/web3.js';
import { assert } from 'chai';
import { CounterProgramPoseidon } from '../target/types/counter_program_poseidon';

describe("counter", () => {
describe('counter', () => {
// Configure the client to use the local cluster.
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
Expand All @@ -14,7 +14,7 @@ describe("counter", () => {
// Generate a new keypair for the counter account
const counterKeypair = new Keypair();

it("Initializes the Counter", async () => {
it('Initializes the Counter', async () => {
// Initialize the counter state
await program.methods
.initializeCounter()
Expand All @@ -26,54 +26,41 @@ describe("counter", () => {
.rpc();

// Fetch the current state of the counter
const currentCount = await program.account.counterState.fetch(
counterKeypair.publicKey
);
const currentCount = await program.account.counterState.fetch(counterKeypair.publicKey);

// Assert that the counter was initialized to 0
assert(
currentCount.count.toNumber() === 0,
"Expected initialized count to be 0"
);
// Assert that the counter was initialized to 0
assert(currentCount.count.toNumber() === 0, 'Expected initialized count to be 0');
});

it("Increments the Counter", async () => {
it('Increments the Counter', async () => {
// Call the increment method
await program.methods
.incrementCounter()
.accounts({
counter: counterKeypair.publicKey
counter: counterKeypair.publicKey,
})
.rpc();

// Fetch the updated counter state
const currentCount = await program.account.counterState.fetch(
counterKeypair.publicKey
);
const currentCount = await program.account.counterState.fetch(counterKeypair.publicKey);

// // Assert that the counter was incremented to 1
assert(currentCount.count.toNumber() === 1, "Expected count to be 1");
});
// // Assert that the counter was incremented to 1
assert(currentCount.count.toNumber() === 1, 'Expected count to be 1');
});

it("Decrements the Counter", async () => {
it('Decrements the Counter', async () => {
// Decrement the counter
await program.methods
.decrementCounter()
.accounts({
counter: counterKeypair.publicKey
counter: counterKeypair.publicKey,
})
.rpc();

// Fetch the updated counter state
const currentCount = await program.account.counterState.fetch(
counterKeypair.publicKey
);
const currentCount = await program.account.counterState.fetch(counterKeypair.publicKey);

// Assert that the counter was decremented to 1
assert(
currentCount.count.toNumber() === 1,
"Expected count to be 1 after decrement"
);
assert(currentCount.count.toNumber() === 1, 'Expected count to be 1 after decrement');
});

});
45 changes: 20 additions & 25 deletions basics/counter/poseidon/ts-programs/src/counterProgramposeidon.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import { Account, Pubkey, Result, i64, u8, Signer } from "@solanaturbine/poseidon";
import { Account, Pubkey, Result, Signer, i64, u8 } from '@solanaturbine/poseidon';

export default class CounterProgramPoseidon {
static PROGRAM_ID = new Pubkey("D5GsmVgazEnZ657NZJS4L6RcmnM8FkhR2qxyxcB4Whb3");

initializeCounter(counter: CounterState, user: Signer): Result {
counter.derive(["count"])
.init();
// Set the initial value to the `count` field of the account
counter.count = new i64(0);
}

incrementCounter(counter: CounterState): Result {
counter.derive(["count"]);
counter.count = counter.count.add(1);
}

decrementCounter(counter: CounterState): Result {
counter.derive(["count"]);
counter.count = counter.count.sub(1);
}
static PROGRAM_ID = new Pubkey('D5GsmVgazEnZ657NZJS4L6RcmnM8FkhR2qxyxcB4Whb3');

initializeCounter(counter: CounterState, user: Signer): Result {
counter.derive(['count']).init();
// Set the initial value to the `count` field of the account
counter.count = new i64(0);
}

incrementCounter(counter: CounterState): Result {
counter.derive(['count']);
counter.count = counter.count.add(1);
}

decrementCounter(counter: CounterState): Result {
counter.derive(['count']);
counter.count = counter.count.sub(1);
}
}

export interface CounterState extends Account {
count: i64; // This field store the counter result
bump: u8; // bump is for PDA (program derieved account, a special type of account which controlled by program on Solana)
count: i64; // This field store the counter result
bump: u8; // bump is for PDA (program derieved account, a special type of account which controlled by program on Solana)
}




0 comments on commit 6aa0b88

Please sign in to comment.