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 directory | Source files |
|---|---|
programs | 12 |
tests | 7 |
app | 5 |
migrations | 1 |
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.
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>,- Line 74
let current_timestamp = Clock::get()?.unix_timestamp as u32;: Reads on-chain time from the Clock sysvar. - Line 83
pub signer: Signer<'info>,: Requires this account to have signed the transaction. - Line 84
#[account(constraint = admin.load() ?.authority == * signer.key, has_one = price_feed_prog: Checks that a stored pubkey field matches the passed account. - 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. - 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. - 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.
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>,}- Line 65
let current_timestamp = Clock::get()?.unix_timestamp as u32;: Reads on-chain time from the Clock sysvar. - Line 74
pub signer: Signer<'info>,: Requires this account to have signed the transaction. - Line 75
#[account(mut, constraint = admin.load() ?.authority == * signer.key, has_one = price_feed: Checks that a stored pubkey field matches the passed account. - 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. - 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. - 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.
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)]- Line 86
msg!("DepositTokenEvent:tokenMint={},bank={},from={},to={},signer={},amount={}",: Logs to the program log, which is visible in explorers and the test output. - Line 100
pub signer: Signer<'info>,: Requires this account to have signed the transaction. - 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. - 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. - 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.
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(),- Line 326
let cpi = CpiContext::new_with_signer(: Makes a CPI signed by a PDA. The signer seeds prove the program controls that address. - Line 332
anchor_spl::token::transfer(cpi, amount)?;: An SPL token transfer through the Token Program. - Line 337
emit!(WithdrawTokenEvent{: Emits an Anchor event that clients can parse from the logs. - 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.
| Directory | Command | Runs |
|---|---|---|
. | npm run start | ts-node app/main_localnet.ts |
. | npm run lint | prettier */*.js "*/**/*{.js,.ts}" --check |
. | 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)