Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/content/docs/basics/cpi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ The `sol_transfer` instruction included in the example code shows a typical
approach for constructing CPIs using the Anchor framework.

This approach involves creating a
[`CpiContext`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/src/context.rs#L171),
[`CpiContext`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/src/context.rs#L171),
which includes the `program_id` and accounts required for the instruction being
called. The `CpiContext` is then passed to an Anchor helper function
([`transfer`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/src/system_program.rs#L298))
([`transfer`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/src/system_program.rs#L298))
to invoke a specific instruction.

```rust
Expand Down Expand Up @@ -472,7 +472,7 @@ The `sol_transfer` instruction included in the example code shows a typical
approach for constructing CPIs using the Anchor framework.

This approach involves creating a
[`CpiContext`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/src/context.rs#L171),
[`CpiContext`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/src/context.rs#L171),
which includes the `program_id` and accounts required for the instruction being
called, followed by a helper function (`transfer`) to invoke a specific
instruction.
Expand Down
7 changes: 4 additions & 3 deletions docs/content/docs/basics/idl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,10 @@ ed = 237

You can find the implementation of the discriminator generation in the Anchor
codebase
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/syn/src/codegen/program/common.rs#L5-L19),
[here](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/codegen/program/common.rs#L5-L19),
for the [`gen_discriminator` method here](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/codegen/program/common.rs#L21-L24),
which is used
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/syn/src/codegen/program/instruction.rs#L27).
[here](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/codegen/program/instruction.rs#L33).

</Tab>
<Tab value="Accounts">
Expand Down Expand Up @@ -479,7 +480,7 @@ e8 = 232

You can find the implementation of the discriminator generation in the Anchor
codebase
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/account/src/lib.rs#L101-L117).
[here](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/account/src/lib.rs#L101-L117).

<Callout type="info">
Note that different programs using identical account names will generate the
Expand Down
34 changes: 17 additions & 17 deletions docs/content/docs/basics/program-structure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ pub struct NewAccount {
## declare_id! macro

The
[`declare_id`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/account/src/lib.rs#L439)
[`declare_id`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/account/src/lib.rs#L537)
macro specifies the on-chain address of the program, known as the program ID.
You can find the implementation of the code generated by the `declare_id!` macro
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/account/src/id.rs#L40-L73).
[here](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/account/src/id.rs#L37-L70).

```rust title="lib.rs"
use anchor_lang::prelude::*;
Expand All @@ -91,14 +91,14 @@ the one generated when you run `anchor build` locally.
## #[program] attribute

The
[`#[program]`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/program/src/lib.rs#L12)
[`#[program]`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/lib.rs#L12)
attribute annotates the module containing all the instruction handlers for your
program. Each public function within this module corresponds to an instruction
that can be invoked.

You can find the implementation of the code generated by the `#[program]`
attribute
[here](https://github.com/coral-xyz/anchor/tree/v0.30.1/lang/syn/src/codegen/program).
[here](https://github.com/coral-xyz/anchor/tree/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/codegen/program).

```rust title="lib.rs"
use anchor_lang::prelude::*;
Expand Down Expand Up @@ -137,11 +137,11 @@ pub struct NewAccount {
Instruction handlers are functions that define the logic executed when an
instruction is invoked. The first parameter of each handler is a `Context<T>`
type, where `T` is a struct implementing the
[`Accounts`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/src/lib.rs#L105)
[`Accounts`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/src/lib.rs#L108)
trait and specifies the accounts the instruction requires.

The
[`Context`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/src/context.rs#L24)
[`Context`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/src/context.rs#L24)
type provides the instruction with access to the following non-argument inputs:

```rust
Expand Down Expand Up @@ -213,16 +213,16 @@ pub struct Initialize<'info> {
## #[derive(Accounts)] macro

The
[`#[derive(Accounts)]`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/derive/accounts/src/lib.rs#L630)
[`#[derive(Accounts)]`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/derive/accounts/src/lib.rs#L631)
macro is applied to a struct to specify the accounts that must be provided when
an instruction is invoked. This macro implements the
[`Accounts`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/src/lib.rs#L105)
[`Accounts`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/src/lib.rs#L108)
trait, which simplifies account validation and serialization and deserialization
of account data.

You can find the implementation of the code generated by the
`#[derive(Accounts)]` macro
[here](https://github.com/coral-xyz/anchor/tree/v0.30.1/lang/syn/src/codegen/accounts).
[here](https://github.com/coral-xyz/anchor/tree/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/codegen/accounts).

```rust
// [!code word:Accounts]
Expand Down Expand Up @@ -268,9 +268,9 @@ Anchor programs in two ways that are generally used together:
`Accounts` trait.

You can find a full list of the constraints
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/syn/src/parser/accounts/constraints.rs)
[here](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/parser/accounts/constraints.rs)
and implementation
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/syn/src/codegen/accounts/constraints.rs).
[here](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/codegen/accounts/constraints.rs).

```rust
#[derive(Accounts)]
Expand All @@ -290,7 +290,7 @@ Anchor programs in two ways that are generally used together:
what the program expects.

You can find the implementation of the account types
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/src/accounts).
[here](https://github.com/coral-xyz/anchor/tree/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/src/accounts).

```rust
#[derive(Accounts)]
Expand Down Expand Up @@ -346,7 +346,7 @@ pub struct NewAccount {
## #[account] attribute

The
[`#[account]`](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/account/src/lib.rs#L66)
[`#[account]`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/account/src/lib.rs#L97)
attribute is applied to structs that define the structure of the data stored in
custom accounts created by your program.

Expand All @@ -362,14 +362,14 @@ This macro implements various traits
[detailed here](https://docs.rs/anchor-lang/latest/anchor_lang/attr.account.html).
The key functionalities of the `#[account]` macro include:

- [Assign Program Owner](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/account/src/lib.rs#L119-L132):
- [Assign Program Owner](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/account/src/lib.rs#L130-L143):
When creating an account, the program owner of the account is automatically
set to the program specified in `declare_id`.
- [Set Discriminator](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/account/src/lib.rs#L101-L117):
- [Set Discriminator](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/account/src/lib.rs#L111-L128):
A unique 8 byte discriminator, specific to the account type, is added as the
first 8 bytes of account data during its initialization. This helps in
differentiating account types and is used for account validation.
- [Data Serialization and Deserialization](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/account/src/lib.rs#L202-L246):
- [Data Serialization and Deserialization](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/account/src/lib.rs#L224-L270):
Account data is automatically serialized and deserialized as the account type.

```rust title="lib.rs"
Expand Down Expand Up @@ -411,7 +411,7 @@ pub struct NewAccount {
An account discriminator in an Anchor program refers to an 8 byte identifier
unique to each account type. You can find the implementation of the account
discriminator
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/account/src/lib.rs#L101-L117).
[here](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/account/src/lib.rs#L111-L128).

The discriminator is the first 8 bytes of the SHA256 hash of the string
`account:<AccountName>`. This discriminator is stored as the first 8 bytes of
Expand Down
2 changes: 1 addition & 1 deletion docs/content/docs/clients/rust.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description:

The [`anchor-client`](https://docs.rs/anchor-client/latest/anchor_client/) crate
is the Rust client library for interacting with Anchor programs. You can find
the source code [here](https://github.com/coral-xyz/anchor/tree/v0.30.1/client).
the source code [here](https://github.com/coral-xyz/anchor/tree/0e5285aecdf410fa0779b7cd09a47f235882c156/client).

## Example

Expand Down
26 changes: 13 additions & 13 deletions docs/content/docs/clients/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description:
---

Anchor provides a Typescript client library
([@coral-xyz/anchor](https://github.com/coral-xyz/anchor/tree/v0.30.1/ts/packages/anchor))
([@coral-xyz/anchor](https://github.com/coral-xyz/anchor/tree/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor))
that simplifies the process of interacting with Solana programs from the client
in JavaScript or TypeScript.

Expand All @@ -20,11 +20,11 @@ in JavaScript or TypeScript.

To interact with an Anchor program using `@coral-xyz/anchor`, you'll need to
create a
[`Program`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/index.ts#L58)
[`Program`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/index.ts#L58)
instance using the program's [IDL file](/docs/basics/idl).

Creating an instance of the `Program` requires the program's IDL and an
[`AnchorProvider`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/provider.ts#L55).
[`AnchorProvider`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/provider.ts#L59).
An `AnchorProvider` is an abstraction that combines two things:

- `Connection` - the connection to a Solana cluster (i.e. localhost, devnet,
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("hello_anchor", () => {
## Invoke Instructions

Once the `Program` is set up using a program's IDL file, you can use the Anchor
[`MethodsBuilder`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/methods.ts#L155)
[`MethodsBuilder`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/methods.ts#L155)
to:

- Build individual instructions
Expand Down Expand Up @@ -216,9 +216,9 @@ Anchor provides multiple methods for building program instructions:
<Tab value=".rpc">

The
[`rpc()`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/methods.ts#L283)
[`rpc()`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/methods.ts#L428)
method
[sends a signed transaction](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/rpc.ts#L29)
[sends a signed transaction](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/rpc.ts#L29)
with the specified instruction and returns a `TransactionSignature`.

When using `.rpc`, the `Wallet` from the `Provider` is automatically included as
Expand Down Expand Up @@ -246,9 +246,9 @@ const transactionSignature = await program.methods
<Tab value=".transaction">

The
[`transaction()`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/methods.ts#L382)
[`transaction()`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/methods.ts#L348)
method
[builds a `Transaction`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/transaction.ts#L18-L26)
[builds a `Transaction`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/transaction.ts#L18-L26)
with the specified instruction without sending the transaction.

```ts
Expand Down Expand Up @@ -277,9 +277,9 @@ const transactionSignature = await connection.sendTransaction(transaction, [
<Tab value=".instruction">

The
[`instruction()`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/methods.ts#L348)
[`instruction()`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/methods.ts#L323)
method
[builds a `TransactionInstruction`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/instruction.ts#L57-L61)
[builds a `TransactionInstruction`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/instruction.ts#L57-L61)
using the specified instruction. This is useful if you want to manually add the
instruction to a transaction and combine it with other instructions.

Expand Down Expand Up @@ -322,7 +322,7 @@ IDL. Anchor provides multiple methods for fetching accounts.
<Tab value="all">

Use
[`all()`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/account.ts#L251)
[`all()`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/account.ts#L251)
to fetch all existing accounts for a specific account type.

```ts
Expand Down Expand Up @@ -357,7 +357,7 @@ const accounts = await program.account.newAccount.all([
<Tab value="fetch">

Use
[`fetch()`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/account.ts#L165)
[`fetch()`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/account.ts#L165)
to fetch the account data for a single account

```ts
Expand All @@ -369,7 +369,7 @@ const account = await program.account.newAccount.fetch(ACCOUNT_ADDRESS);
<Tab value="fetchMultiple">

Use
[`fetchMultiple()`](https://github.com/coral-xyz/anchor/blob/v0.30.1/ts/packages/anchor/src/program/namespace/account.ts#L200)
[`fetchMultiple()`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/ts/packages/anchor/src/program/namespace/account.ts#L200)
to fetch the account data for multiple accounts by passing in an array of
account addresses

Expand Down
Loading