This project demonstrates Cross-Program Invocation (CPI) in Solana using the Anchor framework. The example consists of two programs:
sol-puppet
: A simple program that manages data and has an authority for updates.sol-puppet-master
: A master program that interacts with thesol-puppet
program via CPI.
-
sol-puppet
- Handles a mutable
puppet
account with adata
field and anauthority
field. - Exposes two instructions:
initialize
: Initializes thepuppet
account with an authority.set_data
: Updates thedata
field of thepuppet
account (requires the correct authority).
- Handles a mutable
-
sol-puppet-master
- Calls the
set_data
instruction in thesol-puppet
program using CPI. - Exposes one instruction:
pull_strings
: Proxies theset_data
call with proper account validation.
- Calls the
initialize
- Initializes a new
puppet
account. - Requires:
- A payer to fund the account.
- A public key for the
authority
.
- Space allocation:
8 + 8 + 32
bytes (account discriminator, data, and authority).
- Initializes a new
set_data
- Updates the
data
field of thepuppet
account. - Requires:
authority
to sign the transaction.
- Updates the
puppet
: Holds the data and authority.user
: The account initializing thepuppet
.
pull_strings
- Calls the
set_data
instruction insol-puppet
via CPI. - Requires:
puppet
account to be mutable.authority
account for signing.
- Calls the
The tests verify CPI functionality between the two programs. Key steps:
-
Initialize
puppet
account:- Call the
initialize
method insol-puppet
. - Set the
authority
for the account.
- Call the
-
Perform CPI with
pull_strings
:- Use
sol-puppet-master
to call theset_data
instruction insol-puppet
. - Ensure that the
puppet
account'sdata
is updated correctly.
- Use
Ensure you have the Solana toolchain and Anchor framework installed. Then, execute:
anchor test