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
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.
-
3This 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– zfedoranCommented Aug 5, 2022 at 0:04