Sol.College

Full lesson

greedypig-solana

What it does

From the project README:

This is an example of an on-chain CRUD dapp. This example is a journal dapp where you can create, read, update, and delete journal entries on the solana blockchain and interact with the solana program via a UI.
Original
cjustinobi/greedypig-solana
SuperStack fork
Blockchains/greedypig-solana @ c155714
Stars
1
Last push (original)
2024-10-09
Languages
TypeScript, Rust, JavaScript, CSS
Detected stack
AnchorNext.jsweb3.js
Categories
Other
Licence
MIT
Homepage
https://greedypig-solana.vercel.app

Architecture

53 source files. By extension: .tsx ×35, .ts ×12, .js ×4, .rs ×2.

Top-level directorySource files
web44
anchor7
(root)2

On-chain programs: anchor/programs/journal

Dapp: deploying

This fork is queued for its own live dapp on Sol.College, with a Devnet/Mainnet switch. It appears here, at /dapps/greedypig-solana/, once it is deployed and checked on devnet. Until then, run it locally with the commands below.

Key code, explained

Excerpts chosen automatically where the code uses the most Solana concepts, pinned to the fork's commit.

cjustinobi/greedypig-solana · anchor/programs/journal/src/lib.rs L241–264 @ c155714 · MIT · original
 #[derive(Accounts)]#[instruction(title: String, winning_score: u64, staking_amount: u64, apparatus: String, mode: String, max_players: u8)]pub struct CreateGame<'info> {    #[account(        init,        seeds = [title.as_bytes(), owner.key().as_ref()],         bump,         payer = owner,        space = GameAccountState::space(&title, &apparatus, &mode, max_players)      )]    pub game_account: Account<'info, GameAccountState>,    #[account(mut)]    pub owner: Signer<'info>,    pub system_program: Program<'info, System>,} #[derive(Accounts)]pub struct JoinGame<'info> {    #[account(mut)]    pub game_account: Account<'info, GameAccountState>,    pub player: Signer<'info>,
  1. Line 243 #[instruction(title: String, winning_score: u64, staking_amount: u64, apparatus: String, m: Touches staking state: the amount locked and when it was locked.
  2. Line 246 init,: Creates and pays for a new account. Space and payer must be specified.
  3. Line 247 seeds = [title.as_bytes(), owner.key().as_ref()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match.
  4. Line 248 bump,: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
  5. Line 256 pub owner: Signer<'info>,: Requires this account to have signed the transaction.
cjustinobi/greedypig-solana · anchor/programs/journal/src/lib copy.rs L49–72 @ c155714 · MIT · original
        Ok(())    }} #[account]pub struct JournalEntryState {    pub owner: Pubkey,    pub title: String,    pub message: String,} #[derive(Accounts)]#[instruction(title: String, message: String)]pub struct CreateEntry<'info> {    #[account(        init,        seeds = [title.as_bytes(), owner.key().as_ref()],         bump,         payer = owner,         space = 8 + 32 + 4 + title.len() + 4 + message.len()    )]    pub game_entry: Account<'info, JournalEntryState>,    #[account(mut)]    pub owner: Signer<'info>,
  1. Line 64 init,: Creates and pays for a new account. Space and payer must be specified.
  2. Line 65 seeds = [title.as_bytes(), owner.key().as_ref()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match.
  3. Line 66 bump,: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
  4. Line 72 pub owner: Signer<'info>,: Requires this account to have signed the transaction.

How to run, build and test

These commands come from the repo's own manifests (package.json scripts, Anchor.toml, Cargo, go.mod, Makefile). Check the README for prerequisites and environment variables.

DirectoryCommandRuns
.npm run devnx serve web
.npm run buildnx build web
anchoranchor buildcompile programs and generate IDLs
anchoranchor testlocal validator + test suite
anchoranchor run test../node_modules/.bin/nx run anchor:jest

What you could build from it