Sol.College

Full lesson

snap-solve

A full-stack urban-reporting dApp built on Solana and Next.js that enables users to create and sign incentivized petitions for local community issues.

What it does

From the project README:

Welcome to the SnapSolve project! SnapSolve revolutionizes urban reporting by incentivizing petitions, empowering citizens to report issues such as infrastructure damage, while voters prioritize them for swift action. This decentralized platform rewards both reporters and voters, promoting faster responses and greater transparency. Get rewarded by creating or signing petitions—it's that simple!
Original
Eddy3129/snap-solve
SuperStack fork
Blockchains/snap-solve @ 0231acf
Stars
1
Last push (original)
2024-10-10
Languages
TypeScript, Rust, CSS, JavaScript
Detected stack
AnchorNext.jsweb3.js
Categories
GamingConsumer & Social
Licence
MIT
Homepage
https://snap-solve.vercel.app

Architecture

32 source files. By extension: .tsx ×21, .ts ×8, .js ×2, .rs ×1.

Top-level directorySource files
components14
app7
(root)4
lib4
petition_program3

On-chain programs: petition_program/programs/petition_program

Dapp: deploying

This fork is queued for its own live dapp on Sol.College, with a Devnet/Mainnet switch. It appears here, at /dapps/snap-solve/, 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.

Eddy3129/snap-solve · petition_program/programs/petition_program/src/lib.rs L121–144 @ 0231acf · MIT · original
        }            Ok(())    }    } #[derive(Accounts)]pub struct CreatePetition<'info> {    #[account(        init_if_needed,         payer = creator,        space = Petition::LEN,        seeds = [b"petition", creator.key().as_ref()],        bump,    )]    pub petition: Account<'info, Petition>,    #[account(mut)]    pub creator: Signer<'info>,    pub system_program: Program<'info, System>,} #[derive(Accounts)]pub struct VotePetition<'info> {
  1. Line 131 init_if_needed,: Creates the account only if it doesn't exist. Watch for re-initialisation bugs.
  2. Line 134 seeds = [b"petition", creator.key().as_ref()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match.
  3. Line 135 bump,: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
  4. Line 139 pub creator: Signer<'info>,: Requires this account to have signed the transaction.
Eddy3129/snap-solve · components/petition/create-post.tsx L109–132 @ 0231acf · MIT · original
     if (!anchorWallet) {      alert('Wallet not connected');      return;    }     try {      // Initialize Anchor provider      const provider = new AnchorProvider(connection, anchorWallet, {        preflightCommitment: 'processed',      });      setProvider(provider); // Sets the default provider for Anchor       // Instantiate Program with correct program ID      const programId = new PublicKey("xnvDsEDqnUh22BRpicSSSJHKjKtBJpPa1j1esc8tpww");      const program = new Program(idl as Idl, provider);       // Derive the PDA for the petition account using the creator's public key      const [petitionPDA, bump] = await PublicKey.findProgramAddressSync(        [Buffer.from("petition"), publicKey.toBuffer()], // Use creator's public key for PDA        program.programId      );       const targetVotesBN = new anchor.BN(targetVotes);
  1. Line 127 const [petitionPDA, bump] = await PublicKey.findProgramAddressSync(: Derives a PDA from seeds and the program ID and returns the address with its canonical bump.
  2. Line 127 const [petitionPDA, bump] = await PublicKey.findProgramAddressSync(: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.

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 devnext dev
.npm run startnext start
.npm run buildnext build
.npm run lintnext lint
petition_programnpm run lintprettier */*.js "*/**/*{.js,.ts}" --check
petition_programanchor buildcompile programs and generate IDLs
petition_programanchor testlocal validator + test suite
petition_programanchor run testyarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts

What you could build from it