In this part of Module 3, you'll delve into the practical aspects of coding a flip program that leverages Cross-Program Invocation (CPI) techniques in Solana using Solidity. You will start with creating an interface in Solidity and then move on to writing tests for the program using `TypeScript`, followed by building, deploying, and testing the flip program.
illustration
b
Build, Deploy, and Test CPI Flip Program

In this lesson, you will:

  • Set up your program for the Solana Devnet.
  • Generate a new wallet keypair and airdrop Solana Devnet tokens.
  • Build and deploy your CPI Flip program to the Devnet.

This lesson guides you through the steps to build and deploy your CPI Flip program to the Solana Devnet. It includes configuring your network cluster for the devnet, generating a new wallet keypair, and completing the build and deployment processes.

To begin, navigate to your anchor.toml file and switch the cluster to devnet. Then, in your project's root directory, execute the following command to build the program:

anchor build

This command generates a target folder in the project root and compiles the Solana program, enabling client-side interaction through RPC methods.

First, check your configuration:

solana config get

You should see an output similar to this:

Config File: ~/.config/solana/cli/config.yml
RPC URL: https://api.devnet.solana.com
WebSocket URL: wss://api.devnet.solana.com/ (computed)
Keypair Path: ~/.config/solana/id.json
Commitment: confirmed

If needed, set your cluster to devnet:

solana config set --url https://api.devnet.solana.com

Then, generate a new keypair for your wallet:

solana-keygen new

Verify your wallet address and balance:

solana address
solana balance

If you need Devnet SOL, airdrop tokens:

solana airdrop 4

To deploy, start a devnet cluster:

solana-test-validator

Then deploy your program:

anchor deploy

You should see a confirmation like this:

Deploying cluster: https://api.devnet.solana.com
Upgrade authority: ~/.config/solana/id.json
Deploying program "hand"...
Program path: ~/composability/cpi/target/deploy/hand.so...
Program Id: 7zJ7E8uZccmCH89eykH8yZsB6G685nbz7sSq8N9sZTtq

Deploying program "lever"...
Program path: ~/composability/cpi/target/deploy/lever.so...
Program Id: J5J3mD4ABcRtB7YcgrJBgugiQAP3DfcZi5bSAdiMAZWh

Deploy success

After deploying, update the program IDs in both your Solidity file and anchor.toml to match the newly deployed programs. The updated anchor.toml should look like this:

[features]
seeds = false
skip-lint = false

[programs.devnet]
hand = "7zJ7E8uZccmCH89eykH8yZsB6G685nbz7sSq8N9sZTtq"
lever="J5J3mD4ABcRtB7YcgrJBgugiQAP3DfcZi5bSAdiMAZWh"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "Devnet"
wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

Rebuild the program:

anchor build

Ensure all dependencies are installed:

yarn install
npm install

Finally, run tests:

anchor test

You should see results confirming the successful execution of your tests, indicating the program's correct functioning after CPI calls to toggle the power state.

cross-program-invocation
Your transaction signature 5LixPfBtppvK6CGUgxKaY4obZfksEGoLXaKJMcvKgQVqcAir91oc1NHc8SCJ9MTbt4U9jvBHgQJPEQxwgkbqSXQv
State: true
    ✔ Initialize the lever! (1307ms)
Your transaction signature 5HkGWLAriP5XRJ9MTCpnMP8ZzUx2JMXsDk2B8TNYzuXPm4Wng1BwfaDMsUwiXqHjSVA683DA9YWuWVUKNvN4hyDV
Your transaction signature 4v1Zxyc5czpgkJ1SYYcwVi1C7K9icehPYkzDLUTyu9QfenaYqRnnkzVfX5Hyw6LXvA5kcdZEaiDsVx6o1hyNJx6x
State: false
    ✔ Pull the lever! (1528ms)
Your transaction signature 4qWHg6hrdWcXmGoS5zD4RGHdPczcMNYzKZnDTef8yyN7RNJj5Spj1ULG2ii6wBgf4pSczZNd3bApxedDQCKhSFhW
State: true
    ✔ Pull it again! (694ms)

  3 passing (4s)

This completes the lesson on building, deploying, and testing the CPI Flip program in Solana using Solidity and Solang. In the next lesson, we'll delve into further aspects of Solana's development.