learn.sol
Week 3 • Defi Integration Challenge

Challenge: DeFi Integration with Anchor CPI

Capstone challenge for this module: integrate an Anchor program with external protocol-like flows using CPI, strict account validation, and realistic test scenarios.

This is your intermediate checkpoint.

You now know enough to design a small protocol integration safely.

Challenge Brief

Build an Anchor program that performs one of these actions through CPI:

  1. Route a token transfer through a vault before settlement
  2. Execute a controlled swap-like flow (simulated or protocol test environment)
  3. Distribute yield/rewards with on-chain accounting

Pick one and implement end-to-end.

Required Capabilities

  • At least one PDA authority signer
  • At least one CPI to token/system/external program
  • State machine with 2+ states
  • 6+ tests including failure paths

Suggested Scope (4 Milestones)

Milestone 1: State and Accounts

Define account model, seed formulas, and authority model.

Milestone 2: Happy Path

Implement main business instruction and make it pass in tests.

Milestone 3: Abuse Tests

Add wrong-authority, wrong-mint, wrong-state, and replay-style tests.

Milestone 4: Client Script

Add a small client flow (prefer @solana/kit for new script code) to execute the path end to end.

Acceptance Criteria

Your challenge is complete when:

  • Program logic enforces all critical rules on-chain
  • CPI accounts are fully validated
  • Tests cover both success and failure behaviors
  • README explains architecture and trust assumptions

Risk Review (Must Answer)

  1. Who can move funds, and how is that authority enforced?
  2. Which accounts are mutable, and why?
  3. What happens if instruction is called twice?
  4. What assumptions do you make about external program behavior?
  5. How will you safely upgrade this logic later?

If you cannot explain your authority model clearly, your design is not ready.

Submission Template (Use This)

  • Problem statement
  • Account model diagram
  • Instruction list and required signers
  • Test matrix (happy + failure)
  • Known limitations and future hardening

What “Good” Looks Like

A strong submission is not the longest one. It is the one with:

  • Clear account constraints
  • Minimal trust assumptions
  • Thoughtful failure testing
  • Explicit upgrade and security notes

When you finish this challenge, you are ready to start building production-style Anchor modules.

Solana Assistant

AI-powered documentation helper

Welcome to Solana Assistant

Ask specific questions about Solana development:

Ask specific questions for better results400px
    Challenge: DeFi Integration with Anchor CPI | learn.sol