0

If I want to create an spl token without anchor in rust, I can do this like described here. How do I do it using the anchor_client crate?

1 Answer 1

2

I believe, or as I understand, there is no reason to construct the instructions via anchor_client. The Anchor client generates the client abstractions for the Anchor program so that instruction-building methods do not need to be written by hand. When this builder is used, the result is an Instruction object that can be included in a Transaction.

Anchor implements some of the SPL programs (written in "native" Solana Rust) to be easily usable in CPI and for example for decoding data. The package is available at https://github.com/coral-xyz/anchor/tree/v0.30.0/spl.

One can use the Anchor RequestBuilder to pass in the created Instruction objects, and Anchor may help in sending multiple instructions within one Transaction.

The program.request() call generates the builder, and instruction(ix) is used to put any Instruction into the builder.

You can use the program abstraction to deserialize the account data.

# Cargo.toml
[dependencies]
anchor-client = "0.29.0"
anchor-spl = "0.29.0"
# main.rs
let spl_program = anchor_client.program(anchor_spl::token::spl_token::id())?;
let token_info: anchor_spl::token::TokenAccount = spl_program.account(random_token_pubkey)?;

I did a small test on this at: https://github.com/ochaloup/marinade-anchor-client/blob/main/src/main.rs#L42

1
  • 1
    This part was just what I was looking for let _ = request_builder.instruction(init_mint);, didn't know you could just append instructions to the request builder like that. Thanks!
    – McBain
    Commented May 31 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.