Skip to content

Repository Containing all code samples written while learning at the Turbin3 Solana Builders Cohort

Notifications You must be signed in to change notification settings

brianobot/TURBIN3-Q1-25

Repository files navigation

Brian's Turbin3 Q1 2025 Codebase 🦇

This is a comprehensive reference to the codes i wrote and edited while learning in the Solana Turbin3 Program.

🗂️ Content/Folders

  • airdrop: Contain typescript code written and edited for the pre-requisite task
  • escrow: Contains Anchor program for an Escrow Smart Contract
  • marketplace: Contains Anchor program for NFT Marketplace Smart Contract
  • pre_req_task_rust: Contains rust codes written and edited for the second pre-requisite task
  • solana-starter: Contains SPL relateds edited during the first class
  • vault: Contains Anchor program code for a vault program
  • Capstone_Letter_of_Intent(LOI).pdf: Capstone Letter of Intent for the Turbin3 2025 Q1 Builders Cohort

⚙️ Setup

🔨 Built with:

  • 🦀 Rust: A general-purpose programming language emphasizing performance, type safety, and concurrency
  • typescript-logoTypescript: A free and open-source high-level programming language developed by Microsoft that adds static typing with optional type annotations to JavaScript
  • ⚓️ Anchor: A tool that simplifies the process of building Solana programs and emphasis safety.
  • metaplexMetaplex: Metaplex is a decentralized protocol that allows users to create, sell, and manage digital assets on the Solana blockchain. It's one of the most widely used blockchain protocols for NFTs.
  • Grit: Motivation from the Most High and Personal Grit

Anchor Stuffs

  • you can initialize an anchor project to use rust for testing with anchor init --test-template rust <project_name>
  • anchor deploy and anchor test would use the cluster speocief in the Anchor.toml file
  • Programs are updated by deploying the program to the same address
  • you can close a program to reclaim the SOL allocated to the program
    • once a program is closed, the program ID can not be used to deploy a new program

## Other Random Stuffs

  • Everything above u32, can not be represented correctly in JS number and therefore we must use BN to represent Big Numbers
    import { BN } from "bn.js";
    
    let bigNumber = new BN(1);

Anchor CPIs and Errors

  • anchor cpi feature generates CPI helper functions for invoking instructions on existign anchor program
  • if you do not have access rto CPI helper function, you can still use invoke and invoke_signed directly
  • error_code attribute macro is used to create Custom Anchor Errors

CPIContext are used to create CPI Context which are similar to COntext,

Fields on the CPIContext include;

  • accounts: List of accounts needed for the CPI Call, Type must implements the ToAccountMetas adn ToAccountInfos<'info> traits, which are added by #[derive(Accounts)] attribute macro
  • remaining_Accounts
  • program
  • signer_seeds

To create a new one that passes the current transaction signature to the transaction that would be perform by the cpi use this

CPIContext::new(cpi_program, cpi_accounts);

You use CPIContext::new_with_signer to construct new instance when signing on behalf of PDA for the CPI

CPIContext::new_with_signer(cpi_program, cpi_accounts, seeds);

When calling another anchor program with a published crate, anchor can generate instruction builders and CPI helper functions for you, simple add the program as a dependency to your Cargo.toml file and specify the ```cpi`` feature

[dependencies]
callee = { path = "../callee", features = ["cpi"] }

by doing so, you have enable cpu feature and your program gains access to the callee::cpi module. the cpi modules turns callee's instruction handlers into Rust functions, these functions takes CPIContext and any extra data needed for the instruction,

Errors

Ultimately all programs return the same error type ProgramError, however you can use AnchorError as an abstraction on top of the ProgramError,

fields on the AnchorError

pub struct AnchorError {
  pub error_name: String,
  pub error_code_number: u32,
  pub error_msg: String,
  pub error_origin: Option<ErrorOrigin>,
  pub compared_values: Option<ComparedValues>,
}

you can add custom errors like so

#[error_code]
pub enum MyError {
    #[msg("MyAccount may only hold data below 100")]
    DataTooLarge
}

you can use the require! macro to simplify returning errors

🤝 Contribution

While using this material as a reference material for your studies or research, if you do see the need to contribute to the content of this repository, please you are welcome to do so, Fork the base repository which is the actively maintained version of this repository and then create a pull request.

👨🏽‍🔧 Maintainer

About

Repository Containing all code samples written while learning at the Turbin3 Solana Builders Cohort

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published