Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Update recipes.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jozanza authored Jan 29, 2024
1 parent bdbb2cb commit 6a07a17
Showing 1 changed file with 3 additions and 22 deletions.
25 changes: 3 additions & 22 deletions docs/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ let instruction = Instruction {
data
};
let tx = Transaction::new_with_payer(&[instruction], None);
let tx_hash = hashsolana::sign_and_send_transaction(&tx);
let tx_hash = solana::sign_and_send_transaction(&tx);
```

### Fetch an Account
Expand All @@ -164,30 +164,11 @@ if !res.is_fetched() {

### Deserialize Account Data

#### Anchor

```rust
use turbo::solana;
use solana::{anchor, rpc::AccountInfo};
use my_anchor_program::MyAnchorAccount;

let account: AccountInfo = ...;
match anchor::try_from_slice::<MyAnchorAccount>(&account.data) {
Ok(data) => {
// Do stuff with deserialized data.
}
Err(err) => {
// Uh oh deserialization error.
}
}
```

?> If the Anchor account has extra space allocated, follow the instructions in the next section.

#### Borsh

Use this approach if one of the following is true:

- You are interacting with an Anchor program
- You are interacting with a non-Anchor program that borsh-encodes account data
- You are deserializing an account that has extra space allocated

Expand All @@ -199,7 +180,7 @@ use my_anchor_program::MyAnchorAccount;
let account: AccountInfo = ...;
// You will need to skip the first 8 bytes when decoding an Anchor struct.
// Those bytes are reserved for a "discriminator" – basically, the struct's IDL schema identifier.
match MyAnchorAccount::deserialize(&account.data[8..]) {
match MyAnchorAccount::deserialize(&mut account.data.get(8..).unwrap_or(&[])) {
Ok(data) => {
// Do stuff with deserialized data.
}
Expand Down

0 comments on commit 6a07a17

Please sign in to comment.