Skip to content

Commit 1f38abb

Browse files
committed
update anchor basics examples
1 parent 80ac28d commit 1f38abb

File tree

23 files changed

+318
-61
lines changed

23 files changed

+318
-61
lines changed

.github/workflows/anchor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
"basics/pda-rent-payer/anchor"
3131
"basics/processing-instructions/anchor"
3232
"basics/program-derived-addresses/anchor"
33+
"basics/realloc/anchor"
3334
"basics/rent/anchor"
3435
"basics/repository-layout/anchor"
3536
"basics/transfer-sol/anchor"

basics/account-data/anchor/tests/test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as anchor from "@coral-xyz/anchor"
22
import { AnchorProgramExample } from "../target/types/anchor_program_example"
3-
import { Keypair, SystemProgram } from "@solana/web3.js"
3+
import { Keypair } from "@solana/web3.js"
44

55
describe("Account Data!", () => {
66
const provider = anchor.AnchorProvider.env()
@@ -34,7 +34,6 @@ describe("Account Data!", () => {
3434
.accounts({
3535
addressInfo: addressInfoAccount.publicKey,
3636
payer: payer.publicKey,
37-
systemProgram: SystemProgram.programId,
3837
})
3938
.signers([addressInfoAccount])
4039
.rpc()

basics/checking-accounts/anchor/programs/anchor-program-example/src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ declare_id!("ECWPhR3rJbaPfyNFgphnjxSEexbTArc7vxD8fnW6tgKw");
88
pub mod anchor_program_example {
99
use super::*;
1010

11-
#[allow(clippy::result_large_err)]
1211
pub fn check_accounts(_ctx: Context<CheckingAccounts>) -> Result<()> {
1312
Ok(())
1413
}
1514
}
1615

16+
// Account validation in Anchor is done using the types and constraints specified in the #[derive(Accounts)] structs
17+
// This is a simple example and does not include all possible constraints and types
1718
#[derive(Accounts)]
1819
pub struct CheckingAccounts<'info> {
19-
payer: Signer<'info>,
20-
#[account(mut)]
21-
/// CHECK: This account's data is empty
22-
account_to_create: AccountInfo<'info>,
20+
payer: Signer<'info>, // checks account is signer
21+
22+
/// CHECK: No checks performed, example of an unchecked account
2323
#[account(mut)]
24-
/// CHECK: This account's data is empty
25-
account_to_change: AccountInfo<'info>,
26-
system_program: Program<'info, System>,
24+
account_to_create: UncheckedAccount<'info>,
25+
/// CHECK: Perform owner check using constraint
26+
#[account(
27+
mut,
28+
owner = id()
29+
)]
30+
account_to_change: UncheckedAccount<'info>,
31+
system_program: Program<'info, System>, // checks account is executable, and is the system program
2732
}
Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
1-
import * as anchor from "@coral-xyz/anchor";
2-
import { AnchorProgramExample } from "../target/types/anchor_program_example";
1+
import * as anchor from "@coral-xyz/anchor"
2+
import { AnchorProgramExample } from "../target/types/anchor_program_example"
3+
import {
4+
Keypair,
5+
SystemProgram,
6+
Transaction,
7+
sendAndConfirmTransaction,
8+
} from "@solana/web3.js"
39

410
describe("Anchor example", () => {
5-
const provider = anchor.AnchorProvider.env();
6-
anchor.setProvider(provider);
11+
const provider = anchor.AnchorProvider.env()
12+
anchor.setProvider(provider)
713
const program = anchor.workspace
8-
.AnchorProgramExample as anchor.Program<AnchorProgramExample>;
9-
const payer = provider.wallet as anchor.Wallet;
14+
.AnchorProgramExample as anchor.Program<AnchorProgramExample>
15+
const wallet = provider.wallet as anchor.Wallet
1016

1117
// We'll create this ahead of time.
1218
// Our program will try to modify it.
13-
const accountToChange = anchor.web3.Keypair.generate();
19+
const accountToChange = new Keypair()
1420
// Our program will create this.
15-
const accountToCreate = anchor.web3.Keypair.generate();
21+
const accountToCreate = new Keypair()
1622

1723
it("Create an account owned by our program", async () => {
18-
let ix = anchor.web3.SystemProgram.createAccount({
24+
let instruction = SystemProgram.createAccount({
1925
fromPubkey: provider.wallet.publicKey,
2026
newAccountPubkey: accountToChange.publicKey,
2127
lamports: await provider.connection.getMinimumBalanceForRentExemption(0),
2228
space: 0,
2329
programId: program.programId, // Our program
24-
});
30+
})
2531

26-
await anchor.web3.sendAndConfirmTransaction(
27-
provider.connection,
28-
new anchor.web3.Transaction().add(ix),
29-
[payer.payer, accountToChange]
30-
);
31-
});
32+
const transaction = new Transaction().add(instruction)
33+
34+
await sendAndConfirmTransaction(provider.connection, transaction, [
35+
wallet.payer,
36+
accountToChange,
37+
])
38+
})
3239

3340
it("Check accounts", async () => {
3441
await program.methods
3542
.checkAccounts()
3643
.accounts({
37-
payer: provider.wallet.publicKey,
44+
payer: wallet.publicKey,
3845
accountToCreate: accountToCreate.publicKey,
3946
accountToChange: accountToChange.publicKey,
40-
systemProgram: anchor.web3.SystemProgram.programId,
4147
})
42-
.signers([payer.payer])
43-
.rpc();
44-
});
45-
});
48+
.rpc()
49+
})
50+
})

basics/close-account/anchor/programs/close-account/src/instructions/create_user.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ pub struct CreateUserContext<'info> {
2121
}
2222

2323
pub fn create_user(ctx: Context<CreateUserContext>, name: String) -> Result<()> {
24-
let user_account = &mut ctx.accounts.user_account;
25-
26-
user_account.bump = *ctx.bumps.get("user_account").unwrap();
27-
user_account.user = ctx.accounts.user.key();
28-
user_account.name = name;
29-
24+
*ctx.accounts.user_account = UserState {
25+
bump: *ctx.bumps.get("user_account").unwrap(),
26+
user: ctx.accounts.user.key(),
27+
name,
28+
};
3029
Ok(())
3130
}

basics/create-account/anchor/tests/test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ describe("Create a system account", () => {
2020
.accounts({
2121
payer: wallet.publicKey,
2222
newAccount: newKeypair.publicKey,
23-
systemProgram: SystemProgram.programId,
2423
})
2524
.signers([newKeypair])
2625
.rpc()
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
import * as anchor from "@coral-xyz/anchor";
2-
import { Hand } from "../target/types/hand";
3-
import { Lever } from "../target/types/lever";
1+
import * as anchor from "@coral-xyz/anchor"
2+
import { Hand } from "../target/types/hand"
3+
import { Lever } from "../target/types/lever"
4+
import { Keypair } from "@solana/web3.js"
45

56
describe("CPI Example", () => {
6-
const provider = anchor.AnchorProvider.env();
7-
anchor.setProvider(provider);
8-
const hand = anchor.workspace.Hand as anchor.Program<Hand>;
9-
const lever = anchor.workspace.Lever as anchor.Program<Lever>;
7+
const provider = anchor.AnchorProvider.env()
8+
anchor.setProvider(provider)
9+
const hand = anchor.workspace.Hand as anchor.Program<Hand>
10+
const lever = anchor.workspace.Lever as anchor.Program<Lever>
1011

11-
const powerAccount = anchor.web3.Keypair.generate();
12+
// Generate a new keypair for the power account
13+
const powerAccount = new Keypair()
1214

1315
it("Initialize the lever!", async () => {
1416
await lever.methods
1517
.initialize()
1618
.accounts({
1719
power: powerAccount.publicKey,
1820
user: provider.wallet.publicKey,
19-
systemProgram: anchor.web3.SystemProgram.programId,
2021
})
2122
.signers([powerAccount])
22-
.rpc();
23-
});
23+
.rpc()
24+
})
2425

2526
it("Pull the lever!", async () => {
2627
await hand.methods
@@ -29,8 +30,8 @@ describe("CPI Example", () => {
2930
power: powerAccount.publicKey,
3031
leverProgram: lever.programId,
3132
})
32-
.rpc();
33-
});
33+
.rpc()
34+
})
3435

3536
it("Pull it again!", async () => {
3637
await hand.methods
@@ -39,6 +40,6 @@ describe("CPI Example", () => {
3940
power: powerAccount.publicKey,
4041
leverProgram: lever.programId,
4142
})
42-
.rpc();
43-
});
44-
});
43+
.rpc()
44+
})
45+
})

basics/pda-rent-payer/anchor/programs/anchor-program-example/src/instructions/create_new_account.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn create_new_account(ctx: Context<CreateNewAccount>) -> Result<()> {
2424
// The minimum lamports for rent exemption
2525
let lamports = (Rent::get()?).minimum_balance(0);
2626

27-
// Create the new account using the PDA signer seeds
27+
// Create the new account, transferring lamports from the rent vault to the new account
2828
create_account(
2929
CpiContext::new(
3030
ctx.accounts.system_program.to_account_info(),

basics/pda-rent-payer/anchor/programs/anchor-program-example/src/instructions/init_rent_vault.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub struct InitRentVault<'info> {
1717
system_program: Program<'info, System>,
1818
}
1919

20+
// When lamports are transferred to a new address (without and existing account),
21+
// An account owned by the system program is created by default
2022
pub fn init_rent_vault(ctx: Context<InitRentVault>, fund_lamports: u64) -> Result<()> {
2123
transfer(
2224
CpiContext::new(

basics/pda-rent-payer/anchor/tests/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as anchor from "@coral-xyz/anchor"
22
import { PdaRentPayer } from "../target/types/pda_rent_payer"
3-
import { LAMPORTS_PER_SOL, Keypair } from "@solana/web3.js"
3+
import { LAMPORTS_PER_SOL, PublicKey, Keypair } from "@solana/web3.js"
44
import { assert } from "chai"
55

66
describe("PDA Rent-Payer", () => {
@@ -11,7 +11,7 @@ describe("PDA Rent-Payer", () => {
1111
const program = anchor.workspace.PdaRentPayer as anchor.Program<PdaRentPayer>
1212

1313
// PDA for the Rent Vault
14-
const [rentVaultPDA] = anchor.web3.PublicKey.findProgramAddressSync(
14+
const [rentVaultPDA] = PublicKey.findProgramAddressSync(
1515
[Buffer.from("rent_vault")],
1616
program.programId
1717
)

0 commit comments

Comments
 (0)