Sol.College

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 directorySource files
web26
anchor13
hivefi-web-demo11
(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.

maffmoff/HiveFi · anchor/programs/dhf/src/lib.rs L1–24 @ 5a13b34 · MIT · original
use 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>,
  1. 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.
  2. 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.
  3. 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.
  4. Line 22 pub authority: Signer<'info>,: Requires this account to have signed the transaction.
maffmoff/HiveFi · anchor/programs/dhf/src/vault/mint.rs L1–24 @ 5a13b34 · MIT · original
use 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(())}
  1. Line 2 use anchor_spl::token::{self, MintTo, TokenAccount, Token, Mint};: Mints new tokens. The mint authority must sign.
  2. Line 11 pub mint_authority: Signer<'info>,: Requires this account to have signed the transaction.
  3. Line 21 let cpi_ctx = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts);: Makes a cross-program invocation into another program.
maffmoff/HiveFi · anchor/programs/dhf/src/vault/burn.rs L1–24 @ 5a13b34 · MIT · original
use 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(())}
  1. Line 11 pub mint_authority: Signer<'info>,: Requires this account to have signed the transaction.
  2. Line 21 let cpi_ctx = CpiContext::new(ctx.accounts.token_program.to_account_info(), cpi_accounts);: Makes a cross-program invocation into another program.
maffmoff/HiveFi · anchor/programs/dhf/src/vault/deposit.rs L1–24 @ 5a13b34 · MIT · original
use 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(())}
  1. Line 2 use anchor_spl::token::{self, MintTo, TokenAccount, Token};: Mints new tokens. The mint authority must sign.
  2. 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.

DirectoryCommandRuns
.npm run devnx serve web
.npm run buildnx build web
hivefi-web-demonpm run devnext dev
hivefi-web-demonpm run startnext start
hivefi-web-demonpm run buildnext build
hivefi-web-demonpm run lintnext lint
anchoranchor buildcompile programs and generate IDLs
anchoranchor testlocal validator + test suite
anchoranchor run test../node_modules/.bin/nx run anchor:jest

What you could build from it