Skip to content

Commit 8686b58

Browse files
committed
chore: sync
1 parent b7880ac commit 8686b58

50 files changed

Lines changed: 1296 additions & 2591 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs-v2/src/content/docs/basics/cpi.mdx

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ A CPI is constructed the same way as any other Solana instruction. The caller mu
4848

4949
The System Program's `transfer(){:rs}` instruction requires a `from{:rs}` account that sends the SOL and a `to{:rs}` account that receives it. The `SolTransfer{:rs}` struct declares both accounts plus the System Program itself, since the CPI invokes that program:
5050

51-
<Code code={lib} lang="rust" title="lib.rs" meta="{27-34} collapse={1-25}" />
51+
<Code code={lib} lang="rust" title="lib.rs" meta="collapse={1-26}" />
5252

5353
The tabs below present three approaches to implementing the same CPI, each at a different level of abstraction:
5454

5555
<Tabs groupId="language" items={['CpiContext{:rs}', 'invoke(){:rs}', 'Manual']}>
5656
<Tab>
5757

58-
The first approach uses Anchor's [`CpiContext`](https://docs.rs/anchor-lang/latest/anchor_lang/context/struct.CpiContext.html), which bundles the `program_id{:rs}` and accounts required by the instruction. The `cpi_context{:rs}` is then passed to the `transfer(){:rs}` helper to invoke the instruction:
58+
The first approach uses Anchor's [`CpiContext{:rs}`](https://docs.rs/anchor-lang/latest/anchor_lang/context/struct.CpiContext.html), which bundles the `program_id{:rs}` and accounts required by the instruction. The `cpi_context{:rs}` is then passed to the `transfer(){:rs}` helper to invoke the instruction:
5959

6060
<Code code={lib} lang="rust" title="lib.rs" meta="{15-22} collapse={1-9, 24-34}" />
6161

@@ -201,7 +201,7 @@ The tabs below present two approaches to implementing the same PDA-signed CPI:
201201
<Tabs groupId="language" items={['CpiContext{:rs}', 'invoke_signed(){:rs}']}>
202202
<Tab>
203203

204-
The first approach uses [`CpiContext`](https://docs.rs/anchor-lang/latest/anchor_lang/context/struct.CpiContext.html), then attaches the signer seeds via `with_signer(){:rs}`:
204+
The first approach uses [`CpiContext{:rs}`](https://docs.rs/anchor-lang/latest/anchor_lang/context/struct.CpiContext.html), then attaches the signer seeds via `with_signer(){:rs}`:
205205

206206
<Code code={libPda} lang="rust" title="lib.rs" meta="{15-17,26,28} collapse={1-9, 30-44}" />
207207

@@ -213,31 +213,3 @@ When the CPI executes, the Solana runtime validates that the supplied seeds and
213213
<Tab>
214214

215215
The first approach is a wrapper around `invoke_signed(){:rs}` paired with `system_instruction::transfer(){:rs}`. Calling `invoke_signed(){:rs}` directly produces an equivalent CPI:
216-
217-
```rust showLineNumbers=false {11,16}
218-
use anchor_lang::solana_program::{program::invoke_signed, system_instruction};
219-
220-
pub fn sol_transfer(ctx: Context<SolTransfer>, amount: u64) -> Result<()> {
221-
let from_pubkey = ctx.accounts.pda_account.to_account_info();
222-
let to_pubkey = ctx.accounts.recipient.to_account_info();
223-
let program_id = ctx.accounts.system_program.to_account_info();
224-
225-
let seed = to_pubkey.key();
226-
let bump_seed = ctx.bumps.pda_account;
227-
228-
let signer_seeds: &[&[&[u8]]] = &[&[b"pda", seed.as_ref(), &[bump_seed]]];
229-
230-
let instruction =
231-
&system_instruction::transfer(&from_pubkey.key(), &to_pubkey.key(), amount);
232-
233-
invoke_signed(instruction, &[from_pubkey, to_pubkey, program_id], signer_seeds)?;
234-
Ok(())
235-
}
236-
```
237-
238-
The `signer_seeds{:rs}` are passed into `invoke_signed(){:rs}` as the third argument.
239-
240-
</Tab>
241-
</Tabs>
242-
243-
A reference program containing both approaches is available on [Solana Playground](https://beta.solpg.io/github.com/ZYJLiu/doc-examples/tree/main/cpi-pda).

docs-v2/src/content/docs/basics/idl.mdx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The `instructions` array in the IDL maps directly to the public functions inside
2525

2626
The program below exposes a single `initialize(){:rs}` instruction along with the `Initialize{:rs}` accounts struct it expects:
2727

28-
<Code code={lib} lang="rust" title="lib.rs" meta="{8-12,15-22} collapse={24-27}" />
28+
<Code code={lib} lang="rust" title="lib.rs" meta="{8-12,15-22} collapse={23-27}" />
2929

3030
</Tab>
3131
<Tab>
@@ -53,7 +53,7 @@ The `accounts` array in the IDL maps to every struct annotated with `#[account]{
5353

5454
The program defines a `NewAccount{:rs}` struct with a single `data: u64{:rs}` field:
5555

56-
<Code code={lib} lang="rust" title="lib.rs" meta="{24-27} collapse={5-22}" />
56+
<Code code={lib} lang="rust" title="lib.rs" meta="{24-27} collapse={4-23}" />
5757

5858
</Tab>
5959
<Tab>
@@ -151,23 +151,3 @@ b0 5f 04 76 5b b1 7d e8 a1 93 57 2a d3 5e b1 ae e5 f0 69 e2 09 7e 5c d2 64 56 55
151151
```
152152

153153
The leading 8 bytes become the discriminator:
154-
155-
```text
156-
b0 = 176
157-
5f = 95
158-
04 = 4
159-
76 = 118
160-
5b = 91
161-
b1 = 177
162-
7d = 125
163-
e8 = 232
164-
```
165-
166-
See the [account discriminator source](https://github.com/solana-foundation/anchor/blob/62865c636aecc6974fc9cfebfc6cf08ca4f0bb72/lang/attribute/account/src/lib.rs#L113-L125) for the implementation.
167-
168-
<Callout variant="note">
169-
Two programs that define accounts with the same struct name produce identical discriminators. Anchor avoids confusion at deserialization time by also verifying that the account is owned by the expected program.
170-
</Callout>
171-
172-
</Tab>
173-
</Tabs>

docs-v2/src/content/docs/basics/pda.mdx

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -140,64 +140,3 @@ pub struct InstructionAccounts<'info> {
140140
<Tab>
141141

142142
The `init{:rs}` constraint is commonly used with `seeds{:rs}` and `bump{:rs}` to create a new account whose address is a PDA. Under the hood, `init{:rs}` invokes the System Program to create the account:
143-
144-
```rust showLineNumbers=false {5-7}
145-
#[derive(Accounts)]
146-
pub struct InstructionAccounts<'info> {
147-
#[account(mut)]
148-
pub signer: Signer<'info>,
149-
#[account(
150-
init,
151-
payer = signer,
152-
space = 8 + 1,
153-
seeds = [b"hello_world", signer.key().as_ref()],
154-
bump,
155-
)]
156-
pub pda_account: Account<'info, CustomAccount>,
157-
pub system_program: Program<'info, System>,
158-
}
159-
160-
#[account]
161-
pub struct CustomAccount {
162-
pub bump_seed: u8,
163-
}
164-
```
165-
166-
<Callout variant="note">
167-
The `init{:rs}` constraint must be used with `payer{:rs}` and `space{:rs}`. The `payer{:rs}` specifies the account that will pay for the account creation. The `space{:rs}` specifies the bytes to allocate for the account (including the 8-byte discriminator).
168-
</Callout>
169-
170-
</Tab>
171-
</Tabs>
172-
173-
## PDA seeds in the IDL
174-
175-
PDA seeds defined in the `seeds{:rs}` constraint are included in the program's IDL file. This allows the Anchor client to automatically resolve account addresses using these seeds when constructing instructions.
176-
177-
<Tabs items={['Program', 'IDL', 'Client']}>
178-
<Tab>
179-
180-
The program below defines a `pda_account{:rs}` using a static seed (`b"hello_world"{:rs}`) and the signer's public key as a dynamic seed:
181-
182-
<Code code={lib} lang="rust" title="lib.rs" meta="{18}" />
183-
184-
</Tab>
185-
<Tab>
186-
187-
The IDL records the PDA seeds declared on the account. The static seed `b"hello_world"{:rs}` is converted to byte values, and the dynamic seed is included as a reference to the signer account:
188-
189-
<Code code={idl} lang="json" title="idl.json" meta="{16-17,20-21} collapse={3-8}" />
190-
191-
</Tab>
192-
<Tab>
193-
194-
The Anchor client uses the IDL to automatically resolve the PDA address. The provider wallet acts as the signer, and its public key becomes the dynamic seed for derivation, so the client can call the instruction without explicitly deriving the PDA:
195-
196-
<Code code={client} lang="ts" title="test.ts" meta="{13}" />
197-
198-
When the instruction is invoked, the PDA is printed to program logs:
199-
200-
<Code code={logs} lang="text" meta="{3}" />
201-
202-
</Tab>
203-
</Tabs>

docs-v2/src/content/docs/basics/program-structure.mdx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The following program exposes a single `initialize(){:rs}` instruction that crea
2323

2424
The [`declare_id!(){:rs}`](https://docs.rs/anchor-lang/latest/anchor_lang/macro.declare_id.html) macro embeds the program's on-chain address, known as the program ID, into the compiled binary:
2525

26-
<Code code={lib} lang="rust" title="lib.rs" meta="{3} collapse={5-27}" />
26+
<Code code={lib} lang="rust" title="lib.rs" meta="{3} collapse={4-27}" />
2727

2828
By default, the program ID is the public key of the keypair at `/target/deploy/<program_name>-keypair.json`. To rewrite the `declare_id!(){:rs}` string to match the on-disk keypair, run `$ anchor keys sync{:ansi}`.
2929

@@ -100,9 +100,3 @@ The discriminator is the first 8 bytes of `SHA256("account:<StructName>"){:rs}`,
100100

101101
<Callout variant="note" title="Reserving discriminator space">
102102
Every `init{:rs}` constraint must allocate 8 extra bytes of space for the discriminator in addition to the account's own fields. In the example below, `space = 8 + 8{:rs}` covers 8 bytes for the discriminator plus 8 bytes for the `data: u64{:rs}` field.
103-
104-
```rust showLineNumbers=false
105-
#[account(init, payer = signer, space = 8 + 8)]
106-
pub new_account: Account<'info, NewAccount>,
107-
```
108-
</Callout>

docs-v2/src/content/docs/clients/rust.mdx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,3 @@ The `src/main.rs` file drives the program. Two pieces do the work:
4545
<Code code={main} lang="rust" title="src/main.rs" meta="{13-14,49-75,79}" />
4646

4747
The `Cargo.toml` dependencies:
48-
49-
```toml title="Cargo.toml"
50-
[package]
51-
name = "client-example"
52-
version = "0.1.0"
53-
edition = "2021"
54-
55-
[dependencies]
56-
anchor-client = { version = "1.0.0", features = ["async"] }
57-
anchor-lang = "1.0.0"
58-
anyhow = "1.0.93"
59-
tokio = { version = "1.0", features = ["full"] }
60-
```

docs-v2/src/content/docs/clients/typescript.mdx

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -253,55 +253,3 @@ const account = await program.account.newAccount.fetch(ACCOUNT_ADDRESS)
253253
<Tab>
254254

255255
`program.account.newAccount.fetchMultiple(){:ts}` fetches and deserializes multiple accounts at once, taking an array of addresses:
256-
257-
```ts showLineNumbers=false
258-
const accounts = await program.account.newAccount.fetchMultiple([
259-
ACCOUNT_ADDRESS_ONE,
260-
ACCOUNT_ADDRESS_TWO,
261-
])
262-
```
263-
264-
</Tab>
265-
</Tabs>
266-
267-
## Example
268-
269-
The example below uses `@anchor-lang/core` to interact with a simple Anchor program. The program exposes an `initialize{:rs}` instruction that creates and initializes a counter account, and an `increment{:rs}` instruction that increments the stored value.
270-
271-
<Code code={lib} lang="rust" title="lib.rs" />
272-
273-
A typical folder layout for a TypeScript client that interacts with the program looks like this:
274-
275-
<FileTree>
276-
<Dir name="ts" open={true}>
277-
<Dir name="idl" open={true}>
278-
<File name="example.json" />
279-
<File name="example.ts" />
280-
</Dir>
281-
<File name="example.ts" />
282-
<File name="package.json" />
283-
</Dir>
284-
</FileTree>
285-
286-
The `/idl` directory contains the IDL itself in `example.json` and a generated TypeScript type definition in `example.ts`. Both files are reproduced below for reference:
287-
288-
<Tabs items={['example.json', 'example.ts']}>
289-
<Tab>
290-
291-
<Code code={exampleJson} lang="json" title="example.json" />
292-
293-
</Tab>
294-
<Tab>
295-
296-
<Code code={exampleTs} lang="ts" title="example.ts" />
297-
298-
</Tab>
299-
</Tabs>
300-
301-
<Callout variant="note">
302-
Running `$ anchor build{:ansi}` in an Anchor project regenerates both files automatically: the IDL file at `target/idl/<program-name>.json` (e.g., `target/idl/example.json`) and the TypeScript type definitions at `target/types/<program-name>.ts` (e.g., `target/types/example.ts`).
303-
</Callout>
304-
305-
The script that drives the program follows:
306-
307-
<Code code={exampleClient} lang="ts" title="example.ts" />

docs-v2/src/content/docs/features/declare-program.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,28 @@ The caller program imports the generated `cpi{:rs}`, `accounts{:rs}`, and `progr
9191

9292
`declare_program!(){:rs}` takes the IDL filename (without extension) as its single argument:
9393

94-
<Code code={caller} lang="rust" title="lib.rs" meta="{5} collapse={1-3,7-65}" />
94+
<Code code={caller} lang="rust" title="lib.rs" meta="collapse={1-4,7-65}" />
9595

9696
</Step>
9797
<Step>
9898

9999
Bring the generated modules into scope:
100100

101-
<Code code={caller} lang="rust" title="lib.rs" meta="{6-13} collapse={1-4,15-65}" />
101+
<Code code={caller} lang="rust" title="lib.rs" meta="collapse={1-4,14-65}" />
102102

103103
</Step>
104104
<Step>
105105

106106
Use the imported types in the account validation struct:
107107

108-
<Code code={caller} lang="rust" title="lib.rs" meta="{57-65} collapse={1-55}" />
108+
<Code code={caller} lang="rust" title="lib.rs" meta="collapse={1-56}" />
109109

110110
</Step>
111111
<Step>
112112

113113
Use the `cpi{:rs}` module to invoke the callee's instructions:
114114

115-
<Code code={caller} lang="rust" title="lib.rs" meta="{20-32} collapse={1-19,33-65}" />
115+
<Code code={caller} lang="rust" title="lib.rs" meta="collapse={1-19,33-65}" />
116116

117117
<Code code={caller} lang="rust" title="lib.rs" meta="{34-44} collapse={1-19,45-65}" />
118118

@@ -168,21 +168,21 @@ The client script uses the generated `client{:rs}` module to build instructions
168168

169169
`declare_program!(){:rs}` takes the IDL filename (without extension) as its single argument:
170170

171-
<Code code={client} lang="rust" title="src/main.rs" meta="{13} collapse={1-12,14-82}" />
171+
<Code code={client} lang="rust" title="src/main.rs" meta="collapse={1-12,14-82}" />
172172

173173
</Step>
174174
<Step>
175175

176176
Bring the generated modules into scope:
177177

178-
<Code code={client} lang="rust" title="src/main.rs" meta="{14-18} collapse={1-13,19-82}" />
178+
<Code code={client} lang="rust" title="src/main.rs" meta="collapse={1-13,19-82}" />
179179

180180
</Step>
181181
<Step>
182182

183183
Use the `client{:rs}` module to build the program's instructions:
184184

185-
<Code code={client} lang="rust" title="src/main.rs" meta="{49-58} collapse={1-48,59-82}" />
185+
<Code code={client} lang="rust" title="src/main.rs" meta="collapse={1-48,59-82}" />
186186

187187
<Code code={client} lang="rust" title="src/main.rs" meta="{60-67} collapse={1-48,68-82}" />
188188

@@ -191,14 +191,14 @@ Use the `client{:rs}` module to build the program's instructions:
191191

192192
Add the instructions to a transaction and send it:
193193

194-
<Code code={client} lang="rust" title="src/main.rs" meta="{69-75} collapse={1-68,76-82}" />
194+
<Code code={client} lang="rust" title="src/main.rs" meta="collapse={1-68,76-82}" />
195195

196196
</Step>
197197
<Step>
198198

199199
Use the `accounts{:rs}` module to fetch and deserialize the program's account data:
200200

201-
<Code code={client} lang="rust" title="src/main.rs" meta="{79} collapse={1-78,80-82}" />
201+
<Code code={client} lang="rust" title="src/main.rs" meta="collapse={1-78,80-82}" />
202202

203203
</Step>
204204
</Steps>

docs-v2/src/content/docs/features/errors.mdx

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -116,54 +116,3 @@ pub enum MyError {
116116
### `require!(){:rs}`
117117

118118
The [`require!(){:rs}`](https://docs.rs/anchor-lang/latest/anchor_lang/macro.require.html) macro provides a more concise way to handle error conditions. It combines a condition check with returning an error if the condition is false:
119-
120-
```rust title="lib.rs" {5} showLineNumbers=false
121-
#[program]
122-
mod hello_anchor {
123-
use super::*;
124-
pub fn set_data(ctx: Context<SetData>, data: MyAccount) -> Result<()> {
125-
require!(data.data < 100, MyError::DataTooLarge);
126-
ctx.accounts.my_account.set_inner(data);
127-
Ok(())
128-
}
129-
}
130-
131-
#[error_code]
132-
pub enum MyError {
133-
#[msg("MyAccount may only hold data below 100")]
134-
DataTooLarge
135-
}
136-
```
137-
138-
Anchor provides several `require{:rs}` macros for different validation needs:
139-
140-
- `require!(){:rs}` ensures a condition is true, otherwise returns the given error.
141-
- `require_eq!(){:rs}` ensures two non-pubkey values are equal.
142-
- `require_neq!(){:rs}` ensures two non-pubkey values are not equal.
143-
- `require_keys_eq!(){:rs}` ensures two pubkeys are equal.
144-
- `require_keys_neq!(){:rs}` ensures two pubkeys are not equal.
145-
- `require_gt!(){:rs}` ensures the first non-pubkey value is greater than the second.
146-
- `require_gte!(){:rs}` ensures the first non-pubkey value is greater than or equal to the second.
147-
148-
## Example
149-
150-
The program below validates that an input amount falls within an acceptable range. It defines two custom error variants with messages and uses `require!(){:rs}` to enforce the bounds:
151-
152-
<Tabs items={['Program', 'Client']}>
153-
<Tab>
154-
155-
<Code code={lib} lang="rust" title="lib.rs" meta="{10-11,21-25}" />
156-
157-
</Tab>
158-
<Tab>
159-
160-
<Code code={client} lang="ts" title="test.ts" />
161-
162-
</Tab>
163-
</Tabs>
164-
165-
When a program error occurs, Anchor's TypeScript client SDK returns a detailed [error response](https://github.com/solana-foundation/anchor/blob/62865c636aecc6974fc9cfebfc6cf08ca4f0bb72/ts/packages/anchor/src/error.ts#L51-L71) containing information about the error:
166-
167-
<Code code={errorResponse} lang="shell" title="Error response" />
168-
169-
For a more comprehensive example, see the [errors test program](https://github.com/solana-foundation/anchor/blob/master/tests/errors/programs/errors/src/lib.rs) in the Anchor repository.

0 commit comments

Comments
 (0)