3

Could someone explain the difference and if there are there any additional benefits to using CpiContext::new_with_signer vs invoke_signed when making a CPI in an Anchor program?

1 Answer 1

4

invoke_signed is not part of Anchor, but rather part of Solana SDK itself. It is used to make CPI calls.

new_with_signer() creates and returns an Anchor CPI context. This context gathers all the necessary info to make a CPI call but it does not actually make the call itself.

That part is done by the CPI methods that Anchor generates, e.g. my_program::cpi::my_instruction(). This method receives the context and will actually call invoked_signed.

You can see this in the macro's implementation.

1
  • 3
    This is correct. It might be worth noting that the anchor expand command helps visualize this concept by showing that invoke_signed is indeed called eventually
    – zfedoran
    Commented Aug 5, 2022 at 0: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.