Sol.College

Full lesson

astherus-sol-treasury

What it does

From the project README:

solana config set -url devnet solana config set --url https://api.devnet.solana.com
Original
astherus-contract/astherus-sol-treasury
SuperStack fork
Blockchains/astherus-sol-treasury @ 9f6193f
Stars
0
Last push (original)
2024-10-04
Languages
TypeScript, Rust
Detected stack
Anchor
Categories
Other
Licence
Apache-2.0

Architecture

25 source files. By extension: .ts ×13, .rs ×12.

Top-level directorySource files
programs12
tests7
app5
migrations1

On-chain programs: programs/cloud-sol-treasury

Dapp: deploying

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

astherus-contract/astherus-sol-treasury · programs/cloud-sol-treasury/src/token.rs L73–96 @ 9f6193f · Apache-2.0 · original
    let bank = &mut ctx.accounts.bank.load_mut()?;    let current_timestamp = Clock::get()?.unix_timestamp as u32;    let index_vec = index_str.split(",").map(|x| x.parse().unwrap()).collect();    bank.remove_claim_history_item(index_vec, current_timestamp);    Ok(())} #[derive(Accounts)]pub struct AddToken<'info> {    #[account(mut)]    pub signer: Signer<'info>,    #[account(constraint = admin.load() ?.authority == * signer.key, has_one = price_feed_program)]    pub admin: AccountLoader<'info, Admin>,    #[account(init, payer = signer, space = 8 + std::mem::size_of::< Bank > ())]    pub bank: AccountLoader<'info, Bank>,    /// CHECK:    #[account(seeds = [constants::TOKEN_VAULT_AUTHORITY.as_bytes(), bank.key().as_ref()], bump)]    pub token_vault_authority: UncheckedAccount<'info>,    #[account(init, payer = signer, associated_token::mint = token_mint, associated_token::authority = token_vault_authority,)]    pub token_vault: Account<'info, TokenAccount>,    /// CHECK: We're reading data from this chainlink feed account    pub price_feed: UncheckedAccount<'info>,    /// CHECK: This is the Chainlink program library    pub price_feed_program: UncheckedAccount<'info>,
  1. Line 74 let current_timestamp = Clock::get()?.unix_timestamp as u32;: Reads on-chain time from the Clock sysvar.
  2. Line 83 pub signer: Signer<'info>,: Requires this account to have signed the transaction.
  3. Line 84 #[account(constraint = admin.load() ?.authority == * signer.key, has_one = price_feed_prog: Checks that a stored pubkey field matches the passed account.
  4. Line 86 #[account(init, payer = signer, space = 8 + std::mem::size_of::< Bank > ())]: Creates and pays for a new account. Space and payer must be specified.
  5. Line 89 #[account(seeds = [constants::TOKEN_VAULT_AUTHORITY.as_bytes(), bank.key().as_ref()], bump: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match.
  6. Line 89 #[account(seeds = [constants::TOKEN_VAULT_AUTHORITY.as_bytes(), bank.key().as_ref()], bump: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
astherus-contract/astherus-sol-treasury · programs/cloud-sol-treasury/src/sol.rs L61–84 @ 9f6193f · Apache-2.0 · original
} pub fn remove_sol_claim_history(ctx: Context<RemoveSolClaimHistory>, index_str: String) -> Result<()> {    let sol_vault = &mut ctx.accounts.sol_vault.load_mut()?;    let current_timestamp = Clock::get()?.unix_timestamp as u32;    let index_vec = index_str.split(",").map(|x| x.parse().unwrap()).collect();    sol_vault.remove_claim_history_item(index_vec, current_timestamp);    Ok(())} #[derive(Accounts)]pub struct AddSol<'info> {    #[account(mut)]    pub signer: Signer<'info>,    #[account(mut, constraint = admin.load() ?.authority == * signer.key, has_one = price_feed_program)]    pub admin: AccountLoader<'info, Admin>,    #[account(init, payer = signer, space = 8 + std::mem::size_of::< SolVault > (), seeds = [constants::SOL_VAULT.as_bytes(), admin.key().as_ref()], bump)]    pub sol_vault: AccountLoader<'info, SolVault>,    /// CHECK: We're reading data from this chainlink feed account    pub price_feed: UncheckedAccount<'info>,    /// CHECK: This is the Chainlink program library    pub price_feed_program: UncheckedAccount<'info>,    pub system_program: Program<'info, System>,}
  1. Line 65 let current_timestamp = Clock::get()?.unix_timestamp as u32;: Reads on-chain time from the Clock sysvar.
  2. Line 74 pub signer: Signer<'info>,: Requires this account to have signed the transaction.
  3. Line 75 #[account(mut, constraint = admin.load() ?.authority == * signer.key, has_one = price_feed: Checks that a stored pubkey field matches the passed account.
  4. Line 77 #[account(init, payer = signer, space = 8 + std::mem::size_of::< SolVault > (), seeds = [c: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match.
  5. Line 77 #[account(init, payer = signer, space = 8 + std::mem::size_of::< SolVault > (), seeds = [c: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
  6. Line 77 #[account(init, payer = signer, space = 8 + std::mem::size_of::< SolVault > (), seeds = [c: Creates and pays for a new account. Space and payer must be specified.
astherus-contract/astherus-sol-treasury · programs/cloud-sol-treasury/src/deposit.rs L85–108 @ 9f6193f · Apache-2.0 · original
     msg!("DepositTokenEvent:tokenMint={},bank={},from={},to={},signer={},amount={}",        bank.token_mint.to_string(),        ctx.accounts.bank.key().to_string(),        ctx.accounts.depositor.key().to_string(),        ctx.accounts.token_vault.key().to_string(),        ctx.accounts.signer.key().to_string(),        amount);     Ok(())} #[derive(Accounts)]pub struct DepositSol<'info> {    #[account(mut)]    pub signer: Signer<'info>,    #[account()]    pub admin: AccountLoader<'info, Admin>,    #[account(mut, has_one = admin, seeds = [constants::SOL_VAULT.as_bytes(), admin.key().as_ref()], bump = admin.load() ?.sol_vault_bump)]    pub sol_vault: AccountLoader<'info, SolVault>,    pub system_program: Program<'info, System>,} #[derive(Accounts)]
  1. Line 86 msg!("DepositTokenEvent:tokenMint={},bank={},from={},to={},signer={},amount={}",: Logs to the program log, which is visible in explorers and the test output.
  2. Line 100 pub signer: Signer<'info>,: Requires this account to have signed the transaction.
  3. Line 103 #[account(mut, has_one = admin, seeds = [constants::SOL_VAULT.as_bytes(), admin.key().as_r: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match.
  4. Line 103 #[account(mut, has_one = admin, seeds = [constants::SOL_VAULT.as_bytes(), admin.key().as_r: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve.
  5. Line 103 #[account(mut, has_one = admin, seeds = [constants::SOL_VAULT.as_bytes(), admin.key().as_r: Checks that a stored pubkey field matches the passed account.
astherus-contract/astherus-sol-treasury · programs/cloud-sol-treasury/src/withdraw.rs L325–348 @ 9f6193f · Apache-2.0 · original
     let cpi = CpiContext::new_with_signer(        token_program.to_account_info(),        cpi_accounts,        signer_seeds,    );     anchor_spl::token::transfer(cpi, amount)?;     admin.claim_per_hour_cursor = cursor;    admin.claim_per_hour_value = per_hour_value;     emit!(WithdrawTokenEvent{     token_mint: bank.token_mint,     bank: bank_pubkey,     from: token_vault.key(),     to:receiver.key(),     signer: signer.key(),     amount: amount,     idempotent:idempotent,    });     msg!("WithdrawTokenEvent:tokenMint={},bank={},from={},to={},signer={},amount={},idempotent={},deadLine={}",        bank.token_mint.to_string(),
  1. Line 326 let cpi = CpiContext::new_with_signer(: Makes a CPI signed by a PDA. The signer seeds prove the program controls that address.
  2. Line 332 anchor_spl::token::transfer(cpi, amount)?;: An SPL token transfer through the Token Program.
  3. Line 337 emit!(WithdrawTokenEvent{: Emits an Anchor event that clients can parse from the logs.
  4. Line 347 msg!("WithdrawTokenEvent:tokenMint={},bank={},from={},to={},signer={},amount={},idempotent: Logs to the program log, which is visible in explorers and the test output.

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 startts-node app/main_localnet.ts
.npm run lintprettier */*.js "*/**/*{.js,.ts}" --check
.anchor buildcompile programs and generate IDLs
.anchor testlocal validator + test suite
.anchor run testyarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts
.cargo buildRust workspace build
.cargo testRust tests

What you could build from it

Used in lessons