Transitioning to Solana: Understanding Creating CPIs for Smart Contracts

5raQ...PSvC
21 Oct 2024
52


I'm writing a smart contract in Solana, but I have some questions. You can always find answers to these questions in the documentation, but it's not that easy. As someone transitioning from an EVM, it was hard for me to immediately grasp the concept, Solana \ is something completely different. For more information about switching from EVM to Solana, see the official documentation. This note will delve into the concept of CPI.

You can learn more about topic by this posts


Vlog link



Do not fail to comment on this post after reading it, my friend. I truly appreciate it when there is a response! ❤️

Recommendations

You can follow the following tips for working with a PDA::

  • Always use a PDA when you need to store user data, rather than storing it in software storage .
  • Check the specified PDA address by re-creating it using find_program_address. If your program needs to use as little gas as possible, you can save find_program_address on the client side and only make sure that the account has not yet been created or initialized.
  • Use all associated PDA accounts in the start file to search for program _address and use enumeration to create the start file to avoid conflicts between the start files.




🤓 Determining the CPI

A program-to-program call (CPI) is a direct call to one program from another. Just as any client can call any program using JSON RPC, any program can directly call any other program. The only requirement for calling another program's instruction from your program is to compose the instruction correctly . You can perform CPI for embedded programs, other programs that you have created, and third-party programs.


How to create a CPI

CPIs are created using the invoke or invoke_signed function from solana_program package. You use invoke, in effect, to pass the original signature of the transaction passed to your program. You use invoke_signed to force your program to "sign" its PDAs.

To create a CPI that requires a PDA as a signer, use the function \ ** * invoke_signed*** and specify the necessary initial values to get the required PDA of the calling program.
the invoke_signed function expects 3 parameters 👉:

  • instructions — instructions for calling .
  • account _infos — accounts required to complete the instruction, where one of them is the device required for signing .
  • subscribers_seeds — source data for receiving the PDA.

 

Code Links

Here are some links to the code and examples you can look at.


These are instructions for performing cpi 

1) description of performing transfers with signature requests
2) description of making transfers via PDA (using a lease)

Examples in SolPg (remix IDE anlog)

1) SolPg examples 1
2) SolPg examples 2

Examples of programs on Solana (you need to select the ones that are needed to solve the problem on Solana)


Participate and engage with my community.


Get fast shipping, movies & more with Amazon Prime

Start free trial

Enjoy this blog? Subscribe to Alxy

0 Comments