Skip to content

An example of cross-program invocations (CPI) on Solana with a ‘puppet’ and ‘puppet master’ setup.

Notifications You must be signed in to change notification settings

glebson1988/sol-puppet

Repository files navigation

Solana Puppet CPI Example

This project demonstrates Cross-Program Invocation (CPI) in Solana using the Anchor framework. The example consists of two programs:

  1. sol-puppet: A simple program that manages data and has an authority for updates.
  2. sol-puppet-master: A master program that interacts with the sol-puppet program via CPI.

Structure

Programs

  • sol-puppet

    • Handles a mutable puppet account with a data field and an authority field.
    • Exposes two instructions:
      1. initialize: Initializes the puppet account with an authority.
      2. set_data: Updates the data field of the puppet account (requires the correct authority).
  • sol-puppet-master

    • Calls the set_data instruction in the sol-puppet program using CPI.
    • Exposes one instruction:
      1. pull_strings: Proxies the set_data call with proper account validation.

Code Overview

Sol-Puppet

Instructions

  1. 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).
  2. set_data
    • Updates the data field of the puppet account.
    • Requires:
      • authority to sign the transaction.

Accounts

  • puppet: Holds the data and authority.
  • user: The account initializing the puppet.

Sol-Puppet-Master

Instruction

  1. pull_strings
    • Calls the set_data instruction in sol-puppet via CPI.
    • Requires:
      • puppet account to be mutable.
      • authority account for signing.

Testing

The tests verify CPI functionality between the two programs. Key steps:

  1. Initialize puppet account:

    • Call the initialize method in sol-puppet.
    • Set the authority for the account.
  2. Perform CPI with pull_strings:

    • Use sol-puppet-master to call the set_data instruction in sol-puppet.
    • Ensure that the puppet account's data is updated correctly.

Run Tests

Ensure you have the Solana toolchain and Anchor framework installed. Then, execute:

anchor test

About

An example of cross-program invocations (CPI) on Solana with a ‘puppet’ and ‘puppet master’ setup.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published