1

While reviewing the Solana codebase, I came across the invoke_signed function, which is commonly used for cross-program invocations. I'm particularly interested in understanding if it's possible to utilize invoke_signed within a Solana native program, such as the Stake program.

My goal is to explore the feasibility of implementing staking functionality in a Solana native program while using SPL (Solana Program Library) tokens as the staked assets. In essence, I would like to know if it's possible to call SPL token methods via invoke_signed or any other suitable method within a native program. This would involve the ability to add or subtract SPL tokens from a wallet in a staking context.

I would greatly appreciate any insights, recommendations, or references to relevant code examples or documentation that can shed light on this matter. If invoke_signed is not the appropriate approach for this use case, I would welcome suggestions on alternative methods to achieve the desired functionality within a Solana native program.

1

2 Answers 2

2

You can call a CPI to any Solana program, including the Token program which generally manages SPL tokens.

If you look into the source of a CPI call like token::mint_to(cpi_ctx, amount), you'll see that it uses solana_program::program::invoke_signed itself under the hood. Here's how the Anchor source calls the native mint_to ix from the token program:

pub fn mint_to<'info>(
    ctx: CpiContext<'_, '_, '_, 'info, MintTo<'info>>,
    amount: u64,
) -> Result<()> {
    let ix = spl_token::instruction::mint_to(
        &spl_token::ID,
        ctx.accounts.mint.key,
        ctx.accounts.to.key,
        ctx.accounts.authority.key,
        &[],
        amount,
    )?;
    solana_program::program::invoke_signed(
        &ix,
        &[
            ctx.accounts.to.clone(),
            ctx.accounts.mint.clone(),
            ctx.accounts.authority.clone(),
        ],
        ctx.signer_seeds,
    )
    .map_err(Into::into)
}

(https://docs.rs/anchor-spl/latest/src/anchor_spl/token.rs.html#64)

1
  • Thanks, @Whiteseal for your answer. However, I understand the CPI is totally possible in a normal Solana program (Smart contract) but the issue I am facing is using the CPI in native programs, the ones that are shipped with Solana codebase like the Stake and Vote program. When using the CPI there, the invoke function requires the AccountInfo, however in native programs we have the BorrowedAccounts. Sep 22 at 19:13
1

This is possible.

invoke - Used when you want to cpi into another program using your keypair(an onCurve address)

invoke_signed - used when you want to cpi into another program but you want your pda to sign instead.

Here is a link to invoke being used to create a token mint - https://github.com/solana-developers/program-examples/blob/bbe6a8d8a43302c233bf8ae9017c74e9f73dabea/tokens/nft-minter/native/program/src/instructions/create.rs#L60-L74

Here is a link to invoke_signed being used to call mpl to create the metadata account - https://github.com/solana-developers/program-examples/blob/bbe6a8d8a43302c233bf8ae9017c74e9f73dabea/tokens/pda-mint-authority/native/program/src/instructions/create.rs#L91-L119

invoke and invoke signed docs - https://docs.solana.com/developing/programming-model/calling-between-programs

2
  • Thanks, @Jimii for your answer. However, I understand the CPI is totally possible in a normal Solana program (Smart contract) but the issue I am facing is using the CPI in native programs, the ones that are shipped with Solana codebase like the Stake and Vote program. When using the CPI there, the invoke function requires the AccountInfo, however in native programs we have the BorrowedAccounts. Sep 22 at 19:15
  • @SyedGhazanferAnwar, I misunderstood you. In that case, check this out docs.rs/anchor-lang/latest/anchor_lang/accounts/account/…. You will need to implement the functions required by the Account yourself and a wrapper type around the structs you'll be working with. The docs are great to get started. I also haven't worked with stake or vote programs
    – Jimii
    Sep 22 at 19:58

Your Answer

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

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