Full lesson
solanacontract
solana contract
What it does
From the project README:
Since this program is extended from the original Escrow Program, I assumed you have gone through the original blog post at least once.
- Original
- T-emporal/solanacontract
- SuperStack fork
- Blockchains/solanacontract @
65c4c72 - Stars
- 0
- Last push (original)
- 2024-10-07
- Languages
- Rust, Makefile, TypeScript, LLVM, DTrace, C
- Detected stack
- Anchor
- Categories
- Other
- Licence
- MIT
Architecture
11 source files. By extension: .rs ×9, .ts ×2.
| Top-level directory | Source files |
|---|---|
programs | 9 |
migrations | 1 |
tests | 1 |
On-chain programs: programs/anchor-escrow
Dapp: deploying
This fork is queued for its own live dapp on Sol.College, with a Devnet/Mainnet switch. It appears here, at /dapps/solanacontract/, 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.
programs/anchor-escrow/src/contexts/exchange.rs L25–48 @ 65c4c72 · MIT · original associated_token::authority = taker )] pub taker_ata_a: Box<Account<'info, TokenAccount>>, #[account( mut, associated_token::mint = mint_b, associated_token::authority = taker )] pub taker_ata_b: Box<Account<'info, TokenAccount>>, #[account( init_if_needed, payer = taker, associated_token::mint = mint_b, associated_token::authority = initializer )] pub initializer_ata_b: Box<Account<'info, TokenAccount>>, #[account( mut, has_one = mint_b, constraint = taker_ata_b.amount >= amount_to_exchange, close = initializer, seeds=[b"state", escrow.seed.to_le_bytes().as_ref()], bump = escrow.bump, )]- Line 35
init_if_needed,: Creates the account only if it doesn't exist. Watch for re-initialisation bugs. - Line 43
has_one = mint_b,: Checks that a stored pubkey field matches the passed account. - Line 45
close = initializer,: Closes the account at the end of the instruction and refunds its lamports. - Line 46
seeds=[b"state", escrow.seed.to_le_bytes().as_ref()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match. - Line 47
bump = escrow.bump,: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
programs/anchor-escrow/src/lib.rs L133–156 @ 65c4c72 · MIT · original #[account(mut)] pub pt_mint: Account<'info, Mint>, #[account(mut)] pub yt_mint: Account<'info, Mint>, #[account(mut)] pub pt_token_account: Account<'info, TokenAccount>, #[account(mut)] pub yt_token_account: Account<'info, TokenAccount>, pub authority: Signer<'info>, pub token_program: Program<'info, Token>,} #[derive(Accounts)]pub struct CreateVault<'info> { #[account(mut)] pub user: Signer<'info>, #[account( init, payer = user, space = Vault::LEN, seeds = [b"vault", user.key().as_ref()], bump )] pub vault: Account<'info, Vault>,- Line 141
pub authority: Signer<'info>,: Requires this account to have signed the transaction. - Line 150
init,: Creates and pays for a new account. Space and payer must be specified. - Line 153
seeds = [b"vault", user.key().as_ref()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match. - Line 154
bump: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
programs/anchor-escrow/src/contexts/cancel.rs L13–36 @ 65c4c72 · MIT · original pub initializer: Signer<'info>, pub mint_a: Account<'info, Mint>, #[account( mut, associated_token::mint = mint_a, associated_token::authority = initializer )] pub initializer_ata_a: Account<'info, TokenAccount>, #[account( mut, has_one = initializer, has_one = mint_a, seeds=[b"state", escrow.seed.to_le_bytes().as_ref()], bump = escrow.bump, )] pub escrow: Account<'info, Escrow>, #[account( mut, associated_token::mint = mint_a, associated_token::authority = escrow )] pub vault: Account<'info, TokenAccount>, pub associated_token_program: Program<'info, AssociatedToken>, pub token_program: Program<'info, Token>,- Line 13
pub initializer: Signer<'info>,: Requires this account to have signed the transaction. - Line 23
has_one = initializer,: Checks that a stored pubkey field matches the passed account. - Line 25
seeds=[b"state", escrow.seed.to_le_bytes().as_ref()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match. - Line 26
bump = escrow.bump,: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
programs/anchor-escrow/src/contexts/initialize.rs L13–36 @ 65c4c72 · MIT · original pub initializer: Signer<'info>, pub mint_a: Account<'info, Mint>, pub mint_b: Account<'info, Mint>, #[account( mut, constraint = initializer_ata_a.amount >= initializer_amount, associated_token::mint = mint_a, associated_token::authority = initializer )] pub initializer_ata_a: Account<'info, TokenAccount>, #[account( init_if_needed, payer = initializer, space = Escrow::INIT_SPACE, seeds = [b"state".as_ref(), &seed.to_le_bytes()], bump )] pub escrow: Account<'info, Escrow>, #[account( init_if_needed, payer = initializer, associated_token::mint = mint_a, associated_token::authority = escrow )]- Line 13
pub initializer: Signer<'info>,: Requires this account to have signed the transaction. - Line 24
init_if_needed,: Creates the account only if it doesn't exist. Watch for re-initialisation bugs. - Line 27
seeds = [b"state".as_ref(), &seed.to_le_bytes()],: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match. - Line 28
bump: 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.
| Directory | Command | Runs |
|---|---|---|
. | npm run build | anchor build |
. | npm run test | anchor test --skip-build --skip-deploy |
. | npm run deploy | anchor deploy |
. | anchor build | compile programs and generate IDLs |
. | anchor test | local validator + test suite |
. | anchor run test | yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts |
. | cargo build | Rust workspace build |
. | cargo test | Rust tests |
What you could build from it
- Fork the SuperStack copy and extend it within the Other gaps listed on the idea page.
- Learn the pattern first: Program Derived Addresses (PDAs)
- Learn the pattern first: Cross-Program Invocations (CPIs)