Full lesson
HiveFi
- Original
- maffmoff/HiveFi
- SuperStack fork
- Blockchains/HiveFi @
5a13b34 - Stars
- 0
- Last push (original)
- 2024-10-08
- Languages
- TypeScript, Rust, Shell, JavaScript, CSS
- Detected stack
- AnchorNext.jsweb3.js
- Categories
- Other
- Licence
- MIT
Architecture
52 source files. By extension: .tsx ×29, .ts ×11, .rs ×7, .js ×5.
| Top-level directory | Source files |
|---|---|
web | 26 |
anchor | 13 |
hivefi-web-demo | 11 |
(root) | 2 |
On-chain programs: anchor/programs/dhf
Dapp: deploying
This fork is queued for its own live dapp on Sol.College, with a Devnet/Mainnet switch. It appears here, at /dapps/hivefi/, 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.
anchor/programs/dhf/src/lib.rs L1–24 @ 5a13b34 · MIT · originaluse crate::types::Vault; // types.rsからVaultをインポート #[program]pub mod strategy_vault { use super::*; pub fn create_vault(ctx: Context<CreateVault>, bump: u8) -> Result<()> { let vault = &mut ctx.accounts.vault; vault.mint = ctx.accounts.mint.key(); vault.authority = ctx.accounts.authority.key(); vault.bump = bump; Ok(()) }} #[derive(Accounts)]pub struct CreateVault<'info> { #[account(init, payer = authority, space = 8 + 32 + 32 + 1, seeds = [b"vault", mint.key().as_ref()], bump)] pub vault: Account<'info, Vault>, // Vault型を使用 pub mint: Account<'info, Mint>, #[account(mut)] pub authority: Signer<'info>, pub system_program: Program<'info, System>, pub rent: Sysvar<'info, Rent>,- Line 7
pub fn create_vault(ctx: Context<CreateVault>, bump: u8) -> Result<()> {: Uses the bump seed, the extra byte that pushes the derived address off the ed25519 curve. - Line 18
#[account(init, payer = authority, space = 8 + 32 + 32 + 1, seeds = [b"vault", mint.key().: Declares the PDA seeds. Anchor re-derives the address and rejects the account if it doesn't match. - Line 18
#[account(init, payer = authority, space = 8 + 32 + 32 + 1, seeds = [b"vault", mint.key().: Creates and pays for a new account. Space and payer must be specified. - Line 22
pub authority: Signer<'info>,: Requires this account to have signed the transaction.
anchor/programs/dhf/src/vault/mint.rs L1–24 @ 5a13b34 · MIT · originaluse anchor_lang::prelude::*;use anchor_spl::token::{self, MintTo, TokenAccount, Token, Mint}; #[derive(Accounts)]pub struct MintTokens<'info> { #[account(mut)] pub mint: Account<'info, Mint>, #[account(mut)] pub user_token_account: Account<'info, TokenAccount>, #[account(mut)] pub mint_authority: Signer<'info>, pub token_program: Program<'info, Token>,} pub fn mint(ctx: Context<MintTokens>, amount: u64) -> Result<()> { let cpi_accounts = MintTo { mint: ctx.accounts.mint.to_account_info(), to: ctx.accounts.user_token_account.to_account_info(), authority: ctx.accounts.mint_authority.to_account_info(), }; let cpi_ctx = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts); token::mint_to(cpi_ctx, amount)?; Ok(())}- Line 2
use anchor_spl::token::{self, MintTo, TokenAccount, Token, Mint};: Mints new tokens. The mint authority must sign. - Line 11
pub mint_authority: Signer<'info>,: Requires this account to have signed the transaction. - Line 21
let cpi_ctx = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts);: Makes a cross-program invocation into another program.
anchor/programs/dhf/src/vault/burn.rs L1–24 @ 5a13b34 · MIT · originaluse anchor_lang::prelude::*;use anchor_spl::token::{self, Burn, TokenAccount, Token, Mint}; #[derive(Accounts)]pub struct BurnTokens<'info> { #[account(mut)] pub mint: Account<'info, Mint>, #[account(mut)] pub user_token_account: Account<'info, TokenAccount>, #[account(mut)] pub mint_authority: Signer<'info>, pub token_program: Program<'info, Token>,} pub fn burn(ctx: Context<BurnTokens>, amount: u64) -> Result<()> { let cpi_accounts = Burn { mint: ctx.accounts.mint.to_account_info(), from: ctx.accounts.user_token_account.to_account_info(), authority: ctx.accounts.mint_authority.to_account_info(), }; let cpi_ctx = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts); token::burn(cpi_ctx, amount)?; Ok(())}- Line 11
pub mint_authority: Signer<'info>,: Requires this account to have signed the transaction. - Line 21
let cpi_ctx = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts);: Makes a cross-program invocation into another program.
anchor/programs/dhf/src/vault/deposit.rs L1–24 @ 5a13b34 · MIT · originaluse anchor_lang::prelude::*;use anchor_spl::token::{self, MintTo, TokenAccount, Token};use crate::types::Vault; #[derive(Accounts)]pub struct Deposit<'info> { #[account(mut)] pub user_token_account: Account<'info, TokenAccount>, #[account(mut)] pub vault: Account<'info, Vault>, pub mint_authority: AccountInfo<'info>, pub token_program: Program<'info, Token>,} pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> { let cpi_accounts = MintTo { mint: ctx.accounts.vault.mint.to_account_info(), to: ctx.accounts.user_token_account.to_account_info(), authority: ctx.accounts.mint_authority.to_account_info(), }; let cpi_ctx = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts); token::mint_to(cpi_ctx, amount)?; Ok(())}- Line 2
use anchor_spl::token::{self, MintTo, TokenAccount, Token};: Mints new tokens. The mint authority must sign. - Line 21
let cpi_ctx = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts);: Makes a cross-program invocation into another program.
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 dev | nx serve web |
. | npm run build | nx build web |
hivefi-web-demo | npm run dev | next dev |
hivefi-web-demo | npm run start | next start |
hivefi-web-demo | npm run build | next build |
hivefi-web-demo | npm run lint | next lint |
anchor | anchor build | compile programs and generate IDLs |
anchor | anchor test | local validator + test suite |
anchor | anchor run test | ../node_modules/.bin/nx run anchor:jest |
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)