4

Anchor 0.30.0 added the declare_program! macro which generates rust bindings from an anchor idl. I was able to find examples on how to use this for CPIs, but not for how to do this for client-side rust similar to how it works with typescript. If I have an instruction called MyInstruction as part of my_program, I can do:

use anchor_lang::prelude::*;
declare_program!(my_program);
use my_program::client::accounts::MyInstruction as MyInstructionAccs;
use my_program::client::args::MyInstruction as MyInstructionArgs;

let accs = MyInstructionAccs {
    acc1: someAccount,
};
let args = MyInstructionArgs { 
    arg1: someArg,
};

Is there anything else Anchor can do for me from here to actually build & send this transaction and invoke MyInstruction in my_program?

Edit: I'm not asking how to generate a client program for Rust in general, as there's already a number of tools for this. Judging from this issue the idea behind declare_program! was to remove the need for external crates to do this, which is why I'm asking.

1 Answer 1

4

I think you might be looking for the Anchor Client library:
https://docs.rs/anchor-client/latest/anchor_client/

It allows you, through the program call loaded as a Rust program dependency, to directly access the Account state and invoke instructions.

For example, to get account data for the Marinade staking program, I did the following:
https://github.com/ochaloup/marinade-anchor-client/blob/main/src/main.rs

For transaction invocation, you can use request(), passing in the accounts and the instruction data. This isn't a full example, but you can check the direction we take here:
https://github.com/marinade-finance/marinade-common-rs-cli/blob/main/libs/marinade-client-rs/src/marinade/instructions.rs#L23

The Program ID is generated into the Rust code after the macros are expanded (anchor expand) and then used directly within the client-side program with use marinade_finance::ID;.

3
  • This is a helpful answer in general, but I won't accept it because I was asking for how to do this with declare_program!. This might've not been 100% clear from my question before I edited it though, so I'll still upvote this :)
    – McBain
    Commented May 28 at 15:47
  • Oh, I see the point now, I have not got the point sorry. While not having the 0.30.0 experience 🤷‍♂️
    – chalda
    Commented May 28 at 17:41
  • 1
    Upon further consideration I realize I misunderstood what declare_program! was for and your answer is the best way to do things so I accepted it. This raised other problems: solana.stackexchange.com/questions/14296/… , but that's a separate story. Thank you for your answer!
    – McBain
    Commented May 28 at 20:04

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.