Skip to content

Commit

Permalink
add token 2022 support
Browse files Browse the repository at this point in the history
  • Loading branch information
vito-kovalione committed Dec 4, 2024
1 parent f67a853 commit d6f88ec
Show file tree
Hide file tree
Showing 21 changed files with 1,575 additions and 116 deletions.
10 changes: 5 additions & 5 deletions programs/strategy/src/instructions/deploy_funds.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anchor_lang::prelude::*;
use anchor_spl::{
token::Token,
token_interface::TokenAccount,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::utils::unchecked_strategy::UncheckedStrategy;
use crate::error::ErrorCode;
Expand All @@ -17,10 +14,13 @@ pub struct DeployFunds<'info> {
#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), strategy.key().as_ref()], bump)]
pub underlying_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, constraint = underlying_mint.key() == strategy.underlying_mint())]
pub underlying_mint: InterfaceAccount<'info, Mint>,

#[account(mut, constraint = signer.key() == strategy.manager() @ErrorCode::AccessDenied)]
pub signer: Signer<'info>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
}

pub fn handle_deploy_funds<'info>(ctx: Context<'_, '_, '_, 'info, DeployFunds<'info>>, amount: u64) -> Result<()> {
Expand Down
11 changes: 6 additions & 5 deletions programs/strategy/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anchor_lang::prelude::*;
use anchor_spl::{
token::Token,
token_interface::TokenAccount,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::error::ErrorCode;
use crate::utils::token::transfer;
Expand All @@ -21,10 +18,13 @@ pub struct Deposit<'info> {
#[account(mut)]
pub vault_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, constraint = underlying_mint.key() == strategy.underlying_mint())]
pub underlying_mint: InterfaceAccount<'info, Mint>,

#[account(constraint = signer.key() == strategy.vault() @ErrorCode::AccessDenied)]
pub signer: Signer<'info>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
}

pub fn handle_deposit<'info>(
Expand All @@ -47,6 +47,7 @@ pub fn handle_deposit<'info>(
ctx.accounts.vault_token_account.to_account_info(),
ctx.accounts.underlying_token_account.to_account_info(),
ctx.accounts.signer.to_account_info(),
&ctx.accounts.underlying_mint,
amount
)
}
10 changes: 5 additions & 5 deletions programs/strategy/src/instructions/free_funds.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anchor_lang::prelude::*;
use anchor_spl::{
token::Token,
token_interface::TokenAccount,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::utils::unchecked_strategy::UncheckedStrategy;
use crate::error::ErrorCode;
Expand All @@ -17,10 +14,13 @@ pub struct FreeFunds<'info> {
#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), strategy.key().as_ref()], bump)]
pub underlying_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, constraint = underlying_mint.key() == strategy.underlying_mint())]
pub underlying_mint: InterfaceAccount<'info, Mint>,

#[account(mut, constraint = signer.key() == strategy.manager() @ErrorCode::AccessDenied)]
pub signer: Signer<'info>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
}

// difference between freed and actual is the loss or gain
Expand Down
9 changes: 3 additions & 6 deletions programs/strategy/src/instructions/init_strategy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use anchor_lang::prelude::*;
use anchor_lang::Discriminator;
use anchor_spl::{
token::{ Token, TokenAccount},
token_interface::Mint,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
use access_control::{
constants::USER_ROLE_SEED,
program::AccessControl,
Expand Down Expand Up @@ -40,7 +37,7 @@ pub struct InitStrategy<'info> {
token::mint = underlying_mint,
token::authority = strategy,
)]
pub token_account: Box<Account<'info, TokenAccount>>,
pub token_account: Box<InterfaceAccount<'info, TokenAccount>>,

#[account(seeds = [CONFIG_SEED.as_bytes()], bump)]
pub config: Account<'info, Config>,
Expand All @@ -66,7 +63,7 @@ pub struct InitStrategy<'info> {
#[account(mut, constraint = roles.check_role()?)]
pub signer: Signer<'info>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,
pub access_control: Program<'info, AccessControl>,
Expand Down
10 changes: 5 additions & 5 deletions programs/strategy/src/instructions/report.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anchor_lang::prelude::*;
use anchor_spl::{
token::Token,
token_interface::TokenAccount,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::utils::unchecked_strategy::UncheckedStrategy;
use crate::error::ErrorCode;
Expand All @@ -17,10 +14,13 @@ pub struct Report<'info> {
#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), strategy.key().as_ref()], bump)]
pub underlying_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, constraint = underlying_mint.key() == strategy.underlying_mint())]
pub underlying_mint: InterfaceAccount<'info, Mint>,

#[account(mut, constraint = signer.key() == strategy.manager() @ErrorCode::AccessDenied)]
pub signer: Signer<'info>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
}

pub fn handle_report<'info>(ctx: Context<'_, '_, '_, 'info, Report<'info>>) -> Result<()> {
Expand Down
10 changes: 5 additions & 5 deletions programs/strategy/src/instructions/report_loss.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anchor_lang::prelude::*;
use anchor_spl::{
token::Token,
token_interface::TokenAccount,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::utils::unchecked_strategy::UncheckedStrategy;
use crate::error::ErrorCode;
Expand All @@ -17,10 +14,13 @@ pub struct ReportLoss<'info> {
#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), strategy.key().as_ref()], bump)]
pub underlying_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, constraint = underlying_mint.key() == strategy.underlying_mint())]
pub underlying_mint: InterfaceAccount<'info, Mint>,

#[account(mut, constraint = signer.key() == strategy.manager() @ErrorCode::AccessDenied)]
pub signer: Signer<'info>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
}

pub fn handle_report_loss<'info>(ctx: Context<'_, '_, '_, 'info, ReportLoss<'info>>, loss: u64) -> Result<()> {
Expand Down
10 changes: 5 additions & 5 deletions programs/strategy/src/instructions/report_profit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anchor_lang::prelude::*;
use anchor_spl::{
token::Token,
token_interface::TokenAccount,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::utils::unchecked_strategy::UncheckedStrategy;
use crate::error::ErrorCode;
Expand All @@ -17,10 +14,13 @@ pub struct ReportProfit<'info> {
#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), strategy.key().as_ref()], bump)]
pub underlying_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, constraint = underlying_mint.key() == strategy.underlying_mint())]
pub underlying_mint: InterfaceAccount<'info, Mint>,

#[account(mut, constraint = signer.key() == strategy.manager() @ErrorCode::AccessDenied)]
pub signer: Signer<'info>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
}

pub fn handle_report_profit<'info>(ctx: Context<'_, '_, '_, 'info, ReportProfit<'info>>, profit: u64) -> Result<()> {
Expand Down
12 changes: 7 additions & 5 deletions programs/strategy/src/instructions/withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anchor_lang::prelude::*;
use anchor_spl::{
token::Token,
token_interface::TokenAccount,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::error::ErrorCode;
use crate::utils::unchecked_strategy::UncheckedStrategy;
Expand All @@ -19,13 +16,16 @@ pub struct Withdraw<'info> {
#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), strategy.key().as_ref()], bump)]
pub underlying_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, constraint = underlying_mint.key() == strategy.underlying_mint())]
pub underlying_mint: InterfaceAccount<'info, Mint>,

#[account(constraint = signer.key() == strategy.vault() @ErrorCode::AccessDenied)]
pub signer: Signer<'info>,

#[account(mut)]
pub vault_token_account: InterfaceAccount<'info, TokenAccount>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
}

pub fn handle_withdraw<'info>(
Expand All @@ -43,6 +43,7 @@ pub fn handle_withdraw<'info>(
let free_funds = &mut FreeFunds {
strategy: ctx.accounts.strategy.clone(),
underlying_token_account: ctx.accounts.underlying_token_account.clone(),
underlying_mint: ctx.accounts.underlying_mint.clone(),
signer: ctx.accounts.signer.clone(),
token_program: ctx.accounts.token_program.clone(),
};
Expand All @@ -57,6 +58,7 @@ pub fn handle_withdraw<'info>(
ctx.accounts.underlying_token_account.to_account_info(),
ctx.accounts.vault_token_account.to_account_info(),
ctx.accounts.strategy.to_account_info(),
&ctx.accounts.underlying_mint,
amount,
&strategy.seeds()
)
Expand Down
12 changes: 7 additions & 5 deletions programs/strategy/src/instructions/withdraw_fee.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anchor_lang::prelude::*;
use anchor_spl::{
token::Token,
token_interface::TokenAccount,
};
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::error::ErrorCode;
use crate::utils::unchecked_strategy::UncheckedStrategy;
Expand All @@ -19,14 +16,17 @@ pub struct WithdrawFee<'info> {

#[account(mut, seeds = [UNDERLYING_SEED.as_bytes(), strategy.key().as_ref()], bump)]
pub underlying_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(mut, constraint = underlying_mint.key() == strategy.underlying_mint())]
pub underlying_mint: InterfaceAccount<'info, Mint>,

#[account(mut)]
pub signer: Signer<'info>,

#[account(mut)]
pub recipient: InterfaceAccount<'info, TokenAccount>,

pub token_program: Program<'info, Token>,
pub token_program: Interface<'info, TokenInterface>,
}

pub fn handle_withdraw_fee<'info>(
Expand All @@ -51,6 +51,7 @@ pub fn handle_withdraw_fee<'info>(
let free_funds = &mut FreeFunds {
strategy: ctx.accounts.strategy.clone(),
underlying_token_account: ctx.accounts.underlying_token_account.clone(),
underlying_mint: ctx.accounts.underlying_mint.clone(),
signer: ctx.accounts.signer.clone(),
token_program: ctx.accounts.token_program.clone(),
};
Expand All @@ -65,6 +66,7 @@ pub fn handle_withdraw_fee<'info>(
ctx.accounts.underlying_token_account.to_account_info(),
ctx.accounts.recipient.to_account_info(),
ctx.accounts.strategy.to_account_info(),
&ctx.accounts.underlying_mint,
amount,
&strategy.seeds()
)
Expand Down
6 changes: 6 additions & 0 deletions programs/strategy/src/state/simple_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Strategy for SimpleStrategy {
remaining[0].to_account_info(),
accounts.underlying_token_account.to_account_info(),
accounts.signer.to_account_info(),
&accounts.underlying_mint,
profit,
)?;

Expand All @@ -122,6 +123,7 @@ impl Strategy for SimpleStrategy {
&mut Report {
strategy: accounts.strategy.clone(),
underlying_token_account: underlying_token_account.clone(),
underlying_mint: accounts.underlying_mint.clone(),
token_program: accounts.token_program.clone(),
signer: accounts.signer.clone(),
},
Expand All @@ -142,6 +144,7 @@ impl Strategy for SimpleStrategy {
accounts.underlying_token_account.to_account_info(),
remaining[0].to_account_info(),
accounts.strategy.to_account_info(),
&accounts.underlying_mint,
loss,
&self.seeds(),
)?;
Expand All @@ -153,6 +156,7 @@ impl Strategy for SimpleStrategy {
&mut Report {
strategy: accounts.strategy.clone(),
underlying_token_account: underlying_token_account.clone(),
underlying_mint: accounts.underlying_mint.clone(),
token_program: accounts.token_program.clone(),
signer: accounts.signer.clone(),
},
Expand Down Expand Up @@ -199,6 +203,7 @@ impl Strategy for SimpleStrategy {
accounts.underlying_token_account.to_account_info(),
remaining[0].to_account_info(),
accounts.strategy.to_account_info(),
&accounts.underlying_mint,
amount,
&seeds
)?;
Expand All @@ -218,6 +223,7 @@ impl Strategy for SimpleStrategy {
remaining[0].to_account_info(),
accounts.underlying_token_account.to_account_info(),
accounts.signer.to_account_info(),
&accounts.underlying_mint,
amount,
)
}
Expand Down
5 changes: 5 additions & 0 deletions programs/strategy/src/state/trade_fintech_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Strategy for TradeFintechStrategy {
remaining[0].to_account_info(),
accounts.underlying_token_account.to_account_info(),
accounts.signer.to_account_info(),
&accounts.underlying_mint,
amount_to_repay,
)?;

Expand All @@ -122,6 +123,7 @@ impl Strategy for TradeFintechStrategy {
&mut Report {
strategy: accounts.strategy.clone(),
underlying_token_account: underlying_token_account.clone(),
underlying_mint: accounts.underlying_mint.clone(),
token_program: accounts.token_program.clone(),
signer: accounts.signer.clone(),
},
Expand Down Expand Up @@ -150,6 +152,7 @@ impl Strategy for TradeFintechStrategy {
remaining[0].to_account_info(),
accounts.underlying_token_account.to_account_info(),
accounts.signer.to_account_info(),
&accounts.underlying_mint,
amount_to_repay,
)?;

Expand All @@ -160,6 +163,7 @@ impl Strategy for TradeFintechStrategy {
&mut Report {
strategy: accounts.strategy.clone(),
underlying_token_account: underlying_token_account.clone(),
underlying_mint: accounts.underlying_mint.clone(),
token_program: accounts.token_program.clone(),
signer: accounts.signer.clone(),
},
Expand Down Expand Up @@ -196,6 +200,7 @@ impl Strategy for TradeFintechStrategy {
accounts.underlying_token_account.to_account_info(),
remaining[0].to_account_info(),
accounts.strategy.to_account_info(),
&accounts.underlying_mint,
amount,
&seeds
)?;
Expand Down
Loading

0 comments on commit d6f88ec

Please sign in to comment.