Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e11565
commit bbb8433
Showing
1 changed file
with
68 additions
and
97 deletions.
There are no files selected for viewing
165 changes: 68 additions & 97 deletions
165
tests/pda-derivation/programs/pda-derivation/src/lib.rs
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,105 +1,76 @@ | ||
//! The typescript example serves to show how one would setup an Anchor | ||
//! workspace with TypeScript tests and migrations. | ||
import * as anchor from "@project-serum/anchor"; | ||
import BN from "bn.js"; | ||
import { Keypair } from "@solana/web3.js"; | ||
import { findProgramAddressSync } from "@project-serum/anchor/dist/cjs/utils/pubkey"; | ||
import { Program } from "@project-serum/anchor"; | ||
import { PdaDerivation } from "../target/types/pda_derivation"; | ||
import { expect } from "chai"; | ||
const encode = anchor.utils.bytes.utf8.encode; | ||
|
||
use anchor_lang::prelude::*; | ||
describe("typescript", () => { | ||
// Configure the client to use the local cluster. | ||
anchor.setProvider(anchor.AnchorProvider.local("http://127.0.0.1:8899")); | ||
|
||
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); | ||
const program = anchor.workspace.PdaDerivation as Program<PdaDerivation>; | ||
console.log(program, program.programId.toBase58()); | ||
const base = Keypair.generate(); | ||
const dataKey = Keypair.generate(); | ||
const data = new BN(1); | ||
const seedA = 4; | ||
|
||
pub const MY_SEED: [u8; 2] = *b"hi"; | ||
pub const MY_SEED_STR: &str = "hi"; | ||
pub const MY_SEED_U8: u8 = 1; | ||
pub const MY_SEED_U32: u32 = 2; | ||
pub const MY_SEED_U64: u64 = 3; | ||
it("Inits the base account", async () => { | ||
await program.methods | ||
.initBase(data, dataKey.publicKey) | ||
.accounts({ | ||
base: base.publicKey, | ||
}) | ||
.signers([base]) | ||
.rpc(); | ||
}); | ||
|
||
#[program] | ||
pub mod pda_derivation { | ||
use super::*; | ||
it("Inits the derived accounts", async () => { | ||
const MY_SEED = "hi"; | ||
const MY_SEED_STR = "hi"; | ||
const MY_SEED_U8 = 1; | ||
const MY_SEED_U32 = 2; | ||
const MY_SEED_U64 = 3; | ||
const expectedPDAKey = findProgramAddressSync( | ||
[ | ||
Buffer.from([seedA]), | ||
encode("another-seed"), | ||
encode("test"), | ||
base.publicKey.toBuffer(), | ||
base.publicKey.toBuffer(), | ||
encode(MY_SEED), | ||
encode(MY_SEED_STR), | ||
Buffer.from([MY_SEED_U8]), | ||
new anchor.BN(MY_SEED_U32).toArrayLike(Buffer, "le", 4), | ||
new anchor.BN(MY_SEED_U64).toArrayLike(Buffer, "le", 8), | ||
new anchor.BN(data).toArrayLike(Buffer, "le", 8), | ||
dataKey.publicKey.toBuffer(), | ||
], | ||
program.programId | ||
)[0]; | ||
|
||
pub fn init_base(ctx: Context<InitBase>, data: u64, data_key: Pubkey) -> Result<()> { | ||
let base = &mut ctx.accounts.base; | ||
base.base_data = data; | ||
base.base_data_key = data_key; | ||
Ok(()) | ||
} | ||
await program.methods | ||
.initBase(data, dataKey.publicKey) | ||
.accounts({ | ||
base: base.publicKey, | ||
}) | ||
.signers([base]) | ||
.rpc(); | ||
const tx = program.methods.initMyAccount(seedA).accounts({ | ||
base: base.publicKey, | ||
base2: base.publicKey, | ||
}); | ||
|
||
pub fn init_my_account(ctx: Context<InitMyAccount>, _seed_a: u8) -> Result<()> { | ||
ctx.accounts.account.data = 1337; | ||
Ok(()) | ||
} | ||
} | ||
const keys = await tx.pubkeys(); | ||
expect(keys.account.equals(expectedPDAKey)).is.true; | ||
|
||
#[derive(Accounts)] | ||
pub struct InitBase<'info> { | ||
#[account( | ||
init, | ||
payer = payer, | ||
space = 8+8+32, | ||
)] | ||
base: Account<'info, BaseAccount>, | ||
#[account(mut)] | ||
payer: Signer<'info>, | ||
system_program: Program<'info, System>, | ||
} | ||
await tx.rpc(); | ||
|
||
#[derive(Accounts)] | ||
#[instruction(seed_a: u8)] | ||
pub struct InitMyAccount<'info> { | ||
base: Account<'info, BaseAccount>, | ||
base2: AccountInfo<'info>, | ||
#[account( | ||
init, | ||
payer = payer, | ||
space = 8+8, | ||
seeds = [ | ||
&seed_a.to_le_bytes(), | ||
"another-seed".as_bytes(), | ||
b"test".as_ref(), | ||
base.key().as_ref(), | ||
base2.key.as_ref(), | ||
MY_SEED.as_ref(), | ||
MY_SEED_STR.as_bytes(), | ||
MY_SEED_U8.to_le_bytes().as_ref(), | ||
&MY_SEED_U32.to_le_bytes(), | ||
&MY_SEED_U64.to_le_bytes(), | ||
base.base_data.to_le_bytes().as_ref(), | ||
base.base_data_key.as_ref(), | ||
], | ||
bump, | ||
)] | ||
account: Account<'info, MyAccount>, | ||
nested: Nested<'info>, | ||
#[account(mut)] | ||
payer: Signer<'info>, | ||
system_program: Program<'info, System>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
#[instruction(seed_a: u8)] | ||
pub struct Nested<'info> { | ||
#[account( | ||
seeds = [ | ||
&seed_a.to_le_bytes(), | ||
"nested-seed".as_bytes(), | ||
b"test".as_ref(), | ||
MY_SEED.as_ref(), | ||
MY_SEED_STR.as_bytes(), | ||
MY_SEED_U8.to_le_bytes().as_ref(), | ||
&MY_SEED_U32.to_le_bytes(), | ||
&MY_SEED_U64.to_le_bytes(), | ||
], | ||
bump, | ||
)] | ||
/// CHECK: Not needed | ||
account_nested: AccountInfo<'info>, | ||
} | ||
|
||
#[account] | ||
pub struct MyAccount { | ||
data: u64, | ||
} | ||
|
||
#[account] | ||
pub struct BaseAccount { | ||
base_data: u64, | ||
base_data_key: Pubkey, | ||
} | ||
const actualData = (await program.account.myAccount.fetch(expectedPDAKey)) | ||
.data; | ||
expect(actualData.toNumber()).is.equal(1337); | ||
}); | ||
}); |