Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init vault upgrade #62

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions programs/tokenized_vault/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub const VAULT_SEED: &str = "vault";
pub const SHARES_SEED: &str = "shares";
pub const SHARES_ACCOUNT_SEED: &str = "shares_account";
pub const UNDERLYING_SEED: &str = "underlying";
pub const ROLES_SEED: &str = "roles";
pub const CONFIG_SEED: &str = "config";
pub const STRATEGY_DATA_SEED: &str = "strategy_data";
Expand Down
12 changes: 7 additions & 5 deletions programs/tokenized_vault/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use access_control::state::UserRole;
use access_control::{
constants::USER_ROLE_SEED,
program::AccessControl,
Expand All @@ -10,8 +9,7 @@ use anchor_spl::{
token_interface::{Mint, TokenAccount, TokenInterface}
};

use crate::constants::{SHARES_SEED, UNDERLYING_SEED, ONE_SHARE_TOKEN, USER_DATA_SEED};

use crate::constants::{SHARES_SEED, ONE_SHARE_TOKEN, USER_DATA_SEED};
use crate::events::VaultDepositEvent;
use crate::state::{UserData, Vault};
use crate::utils::{accountant, token, vault};
Expand All @@ -35,8 +33,12 @@ pub struct Deposit<'info> {
#[account(mut)]
pub user_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), vault.key().as_ref()], bump)]
pub vault_token_account: InterfaceAccount<'info, TokenAccount>,
#[account(
mut,
associated_token::mint = underlying_mint,
associated_token::authority = vault,
)]
pub vault_token_account: Box<InterfaceAccount<'info, TokenAccount>>,

#[account(mut, seeds = [SHARES_SEED.as_bytes(), vault.key().as_ref()], bump)]
pub shares_mint: InterfaceAccount<'info, Mint>,
Expand Down
21 changes: 14 additions & 7 deletions programs/tokenized_vault/src/instructions/direct_deposit.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use access_control::{
constants::USER_ROLE_SEED,
program::AccessControl,
state::{Role, UserRole}
state::Role
};
use anchor_lang::prelude::*;
use anchor_spl::{
associated_token::AssociatedToken,
token::Token,
token_interface::{Mint, TokenAccount, TokenInterface},
};
use strategy::program::Strategy;

use crate::constants::{SHARES_SEED, STRATEGY_DATA_SEED, UNDERLYING_SEED, ONE_SHARE_TOKEN, USER_DATA_SEED};

use strategy::{
constants::UNDERLYING_SEED,
program::Strategy
};
use crate::constants::{SHARES_SEED, STRATEGY_DATA_SEED, ONE_SHARE_TOKEN, USER_DATA_SEED};
use crate::events::{VaultDepositEvent, UpdatedCurrentDebtForStrategyEvent};
use crate::state::{UserData, Vault, StrategyData};
use crate::utils::{accountant, strategy as strategy_utils, token, vault};
Expand All @@ -35,8 +37,12 @@ pub struct DirectDeposit<'info> {
#[account(mut)]
pub user_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), vault.key().as_ref()], bump)]
pub vault_token_account: InterfaceAccount<'info, TokenAccount>,
#[account(
mut,
associated_token::mint = underlying_mint,
associated_token::authority = vault,
)]
pub vault_token_account: Box<InterfaceAccount<'info, TokenAccount>>,

#[account(mut, seeds = [SHARES_SEED.as_bytes(), vault.key().as_ref()], bump)]
pub shares_mint: InterfaceAccount<'info, Mint>,
Expand Down Expand Up @@ -98,6 +104,7 @@ pub struct DirectDeposit<'info> {
#[account(mut)]
pub user: Signer<'info>,

pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,
pub shares_token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
Expand Down
121 changes: 110 additions & 11 deletions programs/tokenized_vault/src/instructions/init_vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ use access_control::{
program::AccessControl,
state::{UserRole, Role}
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
use anchor_spl::{
associated_token::{AssociatedToken, get_associated_token_address},
metadata::{
create_metadata_accounts_v3,
mpl_token_metadata::types::DataV2,
CreateMetadataAccountsV3,
Metadata,
},
token::Token,
token_interface::{Mint, TokenAccount, TokenInterface}
};

use crate::constants::{CONFIG_SEED, VAULT_SEED, UNDERLYING_SEED};
use crate::state::{Vault, Config, VaultConfig};
use crate::constants::{CONFIG_SEED, SHARES_SEED, VAULT_SEED};
use crate::events::{TokenData, TokenMetaData, VaultInitEvent};
use crate::state::{Config, SharesConfig, Vault, VaultConfig};

#[derive(Accounts)]
pub struct InitVault<'info> {
Expand All @@ -25,18 +36,31 @@ pub struct InitVault<'info> {

#[account(
init,
seeds = [UNDERLYING_SEED.as_bytes(), vault.key().as_ref()],
bump,
payer = signer,
token::mint = underlying_mint,
token::authority = vault,
associated_token::mint = underlying_mint,
associated_token::authority = vault,
)]
pub underlying_token_account: Box<InterfaceAccount<'info, TokenAccount>>,

#[account(
init,
seeds = [SHARES_SEED.as_bytes(), vault.key().as_ref()],
bump,
payer = signer,
mint::decimals = 9,
mint::authority = shares_mint,
mint::token_program = shares_token_program,
)]
pub shares_mint: Box<InterfaceAccount<'info, Mint>>,

/// CHECK: We initialize this metadata account via the Metaplex Metadata Program, so we don't have to check it here
#[account(mut)]
pub metadata: UncheckedAccount<'info>,

#[account()]
pub underlying_mint: Box<InterfaceAccount<'info, Mint>>,

#[account(seeds = [CONFIG_SEED.as_bytes()], bump)]
#[account(mut, seeds = [CONFIG_SEED.as_bytes()], bump)]
pub config: Box<Account<'info, Config>>,

#[account(
Expand All @@ -48,26 +72,101 @@ pub struct InitVault<'info> {
bump,
seeds::program = access_control.key()
)]
pub roles: Account<'info, UserRole>,
pub roles: Box<Account<'info, UserRole>>,

#[account(mut, constraint = roles.check_role()?)]
pub signer: Signer<'info>,

pub associated_token_program: Program<'info, AssociatedToken>,
pub access_control: Program<'info, AccessControl>,
pub metadata_program: Program<'info, Metadata>,
pub token_program: Interface<'info, TokenInterface>,
pub shares_token_program: Program<'info, Token>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,
}

pub fn handle_init_vault(ctx: Context<InitVault>, config: Box<VaultConfig>) -> Result<()> {
pub fn handle_init_vault(ctx: Context<InitVault>, config: Box<VaultConfig>, shares_config: Box<SharesConfig>) -> Result<()> {
ctx.accounts.vault.load_init()?.init(
ctx.accounts.config.next_vault_index,
ctx.bumps.vault,
ctx.bumps.shares_mint,
ctx.accounts.vault.key(),
ctx.accounts.underlying_mint.as_ref(),
ctx.accounts.underlying_token_account.key(),
config.as_ref()
)
)?;

ctx.accounts.config.next_vault_index += 1;

let token_data: DataV2 = DataV2 {
name: shares_config.name.clone(),
symbol: shares_config.symbol.clone(),
uri: shares_config.uri,
seller_fee_basis_points: 0,
creators: None,
collection: None,
uses: None,
};

let vault_key = ctx.accounts.vault.key();
let seeds = &[SHARES_SEED.as_bytes(), vault_key.as_ref(), &[ctx.bumps.shares_mint]];
let signer = [&seeds[..]];

create_metadata_accounts_v3(
CpiContext::new_with_signer(
ctx.accounts.metadata_program.to_account_info(),
CreateMetadataAccountsV3 {
payer: ctx.accounts.signer.to_account_info(),
update_authority: ctx.accounts.shares_mint.to_account_info(),
mint: ctx.accounts.shares_mint.to_account_info(),
metadata: ctx.accounts.metadata.to_account_info(),
mint_authority: ctx.accounts.shares_mint.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
rent: ctx.accounts.rent.to_account_info(),
},
&signer
),
token_data,
false,
true,
None,
)?;

let underlying_token = TokenData{
mint: ctx.accounts.underlying_mint.key(),
account: ctx.accounts.underlying_token_account.key(),
decimals: ctx.accounts.underlying_mint.decimals,
metadata: TokenMetaData {
name: "".to_string(),
symbol: "".to_string()
}
};

let share_token = TokenData{
mint: ctx.accounts.shares_mint.key(),
account: get_associated_token_address(&ctx.accounts.vault.key(), &ctx.accounts.shares_mint.key()),
decimals: ctx.accounts.shares_mint.decimals,
metadata: TokenMetaData {
name: shares_config.name,
symbol: shares_config.symbol,
}
};

emit!(VaultInitEvent {
vault_key: ctx.accounts.vault.key(),
underlying_token,
share_token,
deposit_limit: config.deposit_limit,
min_user_deposit: config.min_user_deposit,
accountant: config.accountant,
profit_max_unlock_time: config.profit_max_unlock_time,
kyc_verified_only: config.kyc_verified_only,
direct_deposit_enabled: config.direct_deposit_enabled,
whitelisted_only: config.whitelisted_only,
});

Ok(())
}


4 changes: 2 additions & 2 deletions programs/tokenized_vault/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod deposit;
pub mod direct_deposit;
pub mod initialize;
pub mod init_vault;
pub mod init_vault_shares;
// pub mod init_vault_shares;
pub mod process_report;
pub mod remove_strategy;
pub mod revoke_whitelisting;
Expand All @@ -20,7 +20,7 @@ pub use deposit::*;
pub use direct_deposit::*;
pub use initialize::*;
pub use init_vault::*;
pub use init_vault_shares::*;
// pub use init_vault_shares::*;
pub use process_report::*;
pub use remove_strategy::*;
pub use revoke_whitelisting::*;
Expand Down
12 changes: 10 additions & 2 deletions programs/tokenized_vault/src/instructions/process_report.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anchor_lang::prelude::*;
use anchor_spl::{
associated_token::AssociatedToken,
token::Token,
token_interface::{ Mint, TokenAccount},
};
Expand All @@ -9,7 +10,7 @@ use access_control::{
state::{UserRole, Role}
};

use crate::constants::{ MAX_BPS_EXTENDED, SHARES_ACCOUNT_SEED, SHARES_SEED, STRATEGY_DATA_SEED, ONE_SHARE_TOKEN};
use crate::constants::{ MAX_BPS_EXTENDED, SHARES_SEED, STRATEGY_DATA_SEED, ONE_SHARE_TOKEN};
use crate::events::StrategyReportedEvent;
use crate::state::{Vault, StrategyData};
use crate::utils::{accountant, strategy, token};
Expand Down Expand Up @@ -37,7 +38,12 @@ pub struct ProcessReport<'info> {
#[account(mut, seeds = [SHARES_SEED.as_bytes(), vault.key().as_ref()], bump)]
pub shares_mint: Box<InterfaceAccount<'info, Mint>>,

#[account(mut, seeds = [SHARES_ACCOUNT_SEED.as_bytes(), vault.key().as_ref()], bump)]
#[account(
init_if_needed,
payer = signer,
associated_token::mint = shares_mint,
associated_token::authority = vault,
)]
pub vault_shares_token_account: Box<InterfaceAccount<'info, TokenAccount>>,

/// CHECK:
Expand Down Expand Up @@ -65,6 +71,8 @@ pub struct ProcessReport<'info> {
#[account(mut, constraint = roles.check_role()?)]
pub signer: Signer<'info>,

pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,
pub access_control: Program<'info, AccessControl>,
pub token_program: Program<'info, Token>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ pub fn handle_remove_strategy(ctx: Context<RemoveStrategy>, strategy: Pubkey, fo
timestamp: Clock::get()?.unix_timestamp,
});


vault.strategies_amount -= 1;

Ok(())
Expand Down
15 changes: 11 additions & 4 deletions programs/tokenized_vault/src/instructions/update_debt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ use access_control::{
state::{UserRole, Role}
};

use strategy::program::Strategy;
use strategy::{
constants::UNDERLYING_SEED,
program::Strategy
};

use crate::events::UpdatedCurrentDebtForStrategyEvent;
use crate::state::{StrategyData, Vault};
use crate::errors::ErrorCode;
use crate::utils::strategy as strategy_utils;
use crate::constants::{STRATEGY_DATA_SEED, UNDERLYING_SEED};
use crate::constants::STRATEGY_DATA_SEED;

#[derive(Accounts)]
#[instruction(new_debt: u64)]
Expand All @@ -24,8 +27,12 @@ pub struct UpdateStrategyDebt<'info> {
)]
pub vault: AccountLoader<'info, Vault>,

#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), vault.key().as_ref()], bump)]
pub vault_token_account: InterfaceAccount<'info, TokenAccount>,
#[account(
mut,
associated_token::mint = underlying_mint,
associated_token::authority = vault,
)]
pub vault_token_account: Box<InterfaceAccount<'info, TokenAccount>>,

#[account(mut, address = vault.load()?.underlying_mint)]
pub underlying_mint: InterfaceAccount<'info, Mint>,
Expand Down
18 changes: 8 additions & 10 deletions programs/tokenized_vault/src/instructions/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ use anchor_spl::{

use strategy::program::Strategy;

use crate::constants::{MAX_BPS, ONE_SHARE_TOKEN, SHARES_SEED, USER_DATA_SEED};
use crate::errors::ErrorCode;
use crate::events::VaultWithdrawlEvent;
use crate::state::{StrategyData, UserData, Vault};
use crate::utils::{accountant, strategy as strategy_utils, token, unchecked::*};
use crate::errors::ErrorCode;
use crate::constants::{
UNDERLYING_SEED,
USER_DATA_SEED,
SHARES_SEED,
MAX_BPS,
ONE_SHARE_TOKEN
};

#[derive(Accounts)]
pub struct Withdraw<'info> {
Expand All @@ -26,8 +20,12 @@ pub struct Withdraw<'info> {
#[account(mut)]
pub user_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), vault.key().as_ref()], bump)]
pub vault_token_account: InterfaceAccount<'info, TokenAccount>,
#[account(
mut,
associated_token::mint = underlying_mint,
associated_token::authority = vault,
)]
pub vault_token_account: Box<InterfaceAccount<'info, TokenAccount>>,

/// CHECK:
#[account(mut, address = vault.load()?.accountant)]
Expand Down
Loading
Loading