Created
November 1, 2021 01:36
-
-
Save farzaa/2e9d23f9bc7abe2345e9d8ff0e53df64 to your computer and use it in GitHub Desktop.
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
use anchor_lang::prelude::*; | |
declare_id!("FRt1SH6KPxYgnTt8C2ZsGJfFGjfkaV5ufvFjWzdQDumK"); | |
#[program] | |
mod mysolanaapp { | |
use super::*; | |
pub fn create(ctx: Context<Create>) -> ProgramResult { | |
let base_account = &mut ctx.accounts.base_account; | |
base_account.count = 0; | |
Ok(()) | |
} | |
pub fn increment(ctx: Context<Increment>) -> ProgramResult { | |
let base_account = &mut ctx.accounts.base_account; | |
base_account.count += 1; | |
let cs = CustomStruct { | |
num: 0, | |
name: "farza".to_string(), | |
}; | |
base_account.data_list.push(cs); | |
Ok(()) | |
} | |
} | |
// Transaction instructions | |
#[derive(Accounts)] | |
pub struct Create<'info> { | |
#[account(init, payer = user, space = 320)] | |
pub base_account: Account<'info, BaseAccount>, | |
#[account(mut)] | |
pub user: Signer<'info>, | |
pub system_program: Program <'info, System>, | |
} | |
// Transaction instructions | |
#[derive(Accounts)] | |
pub struct Increment<'info> { | |
#[account(mut)] | |
pub base_account: Account<'info, BaseAccount>, | |
} | |
#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)] | |
pub struct CustomStruct { | |
pub num: u64, | |
pub name: String, | |
} | |
#[account] | |
pub struct BaseAccount { | |
pub count: u64, | |
pub data_list: Vec<CustomStruct>, | |
} |
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
const assert = require("assert"); | |
const anchor = require("@project-serum/anchor"); | |
const { SystemProgram } = anchor.web3; | |
describe("mysolanaapp", () => { | |
/* create and set a Provider */ | |
const provider = anchor.Provider.env(); | |
console.log("p", provider) | |
anchor.setProvider(provider); | |
const program = anchor.workspace.Mysolanaapp; | |
console.log("p", program) | |
it("Creates a counter)", async () => { | |
/* Call the create function via RPC */ | |
const baseAccount = anchor.web3.Keypair.generate(); | |
await program.rpc.create({ | |
accounts: { | |
baseAccount: baseAccount.publicKey, | |
user: provider.wallet.publicKey, | |
systemProgram: SystemProgram.programId, | |
}, | |
signers: [baseAccount], | |
}); | |
/* Fetch the account and check the value of count */ | |
const account = await program.account.baseAccount.fetch(baseAccount.publicKey); | |
console.log('Count 0: ', account.count.toString()) | |
assert.ok(account.count.toString() == 0); | |
_baseAccount = baseAccount; | |
}); | |
it("Increments the counter", async () => { | |
const baseAccount = _baseAccount; | |
await program.rpc.increment({ | |
accounts: { | |
baseAccount: baseAccount.publicKey, | |
}, | |
}); | |
const account = await program.account.baseAccount.fetch(baseAccount.publicKey); | |
console.log('Count 1: ', account.count.toString()) | |
console.log('all data in list ', account.dataList) | |
assert.ok(account.count.toString() == 1); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment