Cross-Program Invocation (CPI) is a mechanism in Solana that allows one program to call functions on another program, enabling complex applications like swap programs to interact with multiple programs. In this tutorial, we build a 'puppet' program that stores data and a 'puppet master' program that modifies the puppet's data through CPI, demonstrating how programs can communicate and share state across different program boundaries on the Solana blockchain.
Cross-Program Invocations in Solana: Building a Puppet Program
Added:hello guys welcome back to salon development series and in this video we'll be building a puppet program so this is mainly to understand cross program invocations which will be really necessary when building complicated programs popping a program can be like a really great example of this so if you want to swap program like you will need cross program locations and yeah that's the core concept so yeah without any further ado let's get into it [Music] to begin with let's fork anchor because there are like already the test written for us for the puppet program so yeah let's just do that that get clone https slash github.com slash project serum slash anchor and hit enter and we have successfully cloned anchor not folk sorry i misspelled it before we have cloned anchor and in this we have the test written for us so this is example four yeah example four so to begin with let's first delete the existing programs so that we can type it ourselves and learn from it yup okay as we have cloned this now we'll just go to the examples page example uh so now let's just open anchor and install the dependencies so type cd anchor examples basic examples tutorial basics three okay so now let's install the dependencies type yarn and it will install the stuff it will take some time one eternity later cool we have now successfully installed the dependencies next we can create lib.rs so let's first create lib.rs for for source sorry for puppet so now we write the puppet program and next we write the puppet master program which will be setting the data for the puppet cool next okay so now let's start by importing some stuff which we had got okay so let me just paste it here cool we have now done the stuff uh now we have imported the stuff which we need right now so cool next we'll start by creating the initialize function here let me create that initialize context initialize the result will be and then okay okay cool next we'll create the account so this will store the data so let's start by creating that pub struck data we have that next the data will be iq64 perfect now let's create let's create the okay now let's specify which accounts we need while initializing the stuff so initialize info okay cool so now let's do this account so this will be our puppet account so let's first initialize this payer will be the user and space plus it yeah so next puppet puppet will be account account then cool next we will have so now let's just write down the system program and the pair so pay your account pair will be the signer the person who signs this thing and next we will have the system program yes cool yeah we have now wrote down this next we'll just add it here context yeah we have this now okay next like we have we have it initialized after this we will create a function to set the data so this will be the function which the puppet master will be calling to set the data so we'll have we'll create this part okay so next we just create the contact for this let's remove this this two and we'll only have puppet perfect cool yes yes cool next we are gonna put it here context context contact set data okay and next we will pass a argument here and this will be the data argument so yeah we have that this is the data argument so now we'll have the puppet program here puppet is equal to which will be mutable so let's import it here puppet next we will have data so yeah here we go we have initialized the stuff so we have our puppet program ready okay next we will create the puppet program so here we go we create lib.rs here lib.rs we have created it next wheel copy this stuff for fact we have this and next here we are gonna name it puppet master this will be the program which will be changing the data of the puppet so yeah let's just initialize the stuff sorry like we don't need initialize like we can yeah as this is just a program we are not creating a separate account so we don't need initialize so we just create we'll just put the set data function here so instead of set data let's set that we can just call it set so as we are not creating account like initialize not needed we'll just remove initialize and like just keep set next we are gonna import some stuff here okay wait a minute okay now we are gonna import the set data so we will be importing like importing this data and this here so let's start this by this puppet quote unquote colon colon cpi and cpi accounts dot dot set data so we have imported this next we are gonna import the program so so this is the set data we are importing set data next we are going to import the program so the program will be puppet program and after this we are going to import the data for the data account so yeah let's just import that uh we have it here cool so we have imported the stuff next we are gonna pass the okay let's create this now accounts and construct constructs a data set data after this account account mutable puppet data perfect and then we'll import puppet and then puppet data and next we are going to import the puppet program so let's add that puppet program so here we go we have imported this to perfect okay so as we have done this next we are gonna put info here so this is gonna solve most of the issue okay so next we'll pass the argument so here the argument will be data data will be u64 and next we will do the cpi so yeah let me just get the program here cpi program is equal to contact accounts program dot account info account info yeah next we are going to write the cpi context so the cpi context will be the set data part so yeah let's just do that so set data over the packet and here we are gonna set the data so puppet colon and this is the puppet program which we'll be mentioning here so yeah puppet program is mentioned so the account which is storing the data data account so here we are gonna pass that and close this bracket and next we are gonna sign the uh the cpi so let's just do that sorry this should be cpi account sorry sorry sorry this is cpi accounts so cpi accounts okay so just type let cpi underscore contacts okay so now let's just enter the cpi context here so cpi underscore ctx is equal to cpi context new then the cpi program and after that we are going to mention the cpi accounts finally we are going to do the cpi the cross program invocation and set the data so now let's start that create puppet dot dot cpi set data let us just put it down open the bracket and here first we mentioned the context and after that we are going to mention the data so yeah this is this simple and yeah we have this ready so we have now successfully wrote a cpi program next we are going to compile this let's just do that anchor test sorry we are going to test this the next morning we have successfully wrote our cpi program and it has been tested so we are good to go so that's a wrap for this video and if you liked it just hit that small like button and subscribe to my channel and currently watching the series [Music]
Up Next

Understanding Self-Sovereign Identity: A Technical Introduction
@ssiambassador1718
22.8K views•2019-06-12

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Operational Security Essentials: A Guide for Hacktivists (OPSEC)
@hitbsecconf
157.4K views•2012-11-26

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto



















![Fundermentals: Accounts, Instructions and Transactions [Solana Basics Day2] - Feb 7th '26](https://i.ytimg.com/vi_webp/fXKyOrudpgA/maxresdefault.webp)


![Advanced CPI calls in Anchor [Solana Tutorial] - May 9th '23](https://i.ytimg.com/vi_webp/AKOWg65QWbQ/maxresdefault.webp)







![Batching Token Instructions [Solana Tutorial] - May 17th '26](https://i.ytimg.com/vi/QhAA3sMcCos/maxresdefault.jpg)







