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

try to fix strategy data serialization issue #61

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
19 changes: 12 additions & 7 deletions programs/tokenized_vault/src/instructions/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anchor_spl::{
use strategy::program::Strategy;

use crate::events::VaultWithdrawlEvent;
use crate::state::{StrategyData, UserData, Vault};
use crate::state::{StrategyData, StrategyDataAccInfo, UserData, Vault};
use crate::utils::{accountant, strategy as strategy_utils, token, unchecked::*};
use crate::errors::ErrorCode;
use crate::constants::{
Expand Down Expand Up @@ -192,7 +192,7 @@ fn handle_internal<'info>(
)?;

if !ctx.accounts.user_data.data_is_empty() {
let mut user_data: UserData = ctx.accounts.user_data.deserialize()?;
let mut user_data = deserialize_user_data(&ctx.accounts.user_data)?;
user_data.handle_withdraw(assets_to_transfer)?;
ctx.accounts.user_data.serialize(&user_data)?;
}
Expand Down Expand Up @@ -264,7 +264,7 @@ fn validate_max_withdraw<'info>(
let mut loss = 0;

for strategy_accounts in strategies {
let current_debt = strategy_accounts.strategy_data.deserialize::<StrategyData>()?.current_debt;
let current_debt = strategy_accounts.strategy_data.current_debt();

let mut to_withdraw = std::cmp::min(max_assets - have, current_debt);
let mut unrealised_loss = strategy_utils::assess_share_of_unrealised_losses(
Expand Down Expand Up @@ -326,7 +326,7 @@ fn withdraw_assets<'info>(

for i in 0..strategies.len() {
let strategy_acc = &strategies[i].strategy_acc;
let mut current_debt = strategies[i].strategy_data.deserialize::<StrategyData>()?.current_debt;
let mut current_debt = strategies[i].strategy_data.current_debt();

let mut to_withdraw = std::cmp::min(assets_needed as u64, current_debt);
let strategy_limit = strategy_utils::get_max_withdraw(&strategy_acc)?;
Expand Down Expand Up @@ -393,9 +393,7 @@ fn withdraw_assets<'info>(

let vault_mut = &mut vault_acc.load_mut()?;

let mut strategy_data: StrategyData = strategies[i].strategy_data.deserialize()?;
strategy_data.update_current_debt(new_debt)?;
strategies[i].strategy_data.serialize(strategy_data)?;
strategies[i].strategy_data.set_current_debt(new_debt)?;

vault_mut.total_debt = total_debt;
vault_mut.total_idle = total_idle;
Expand All @@ -414,3 +412,10 @@ fn withdraw_assets<'info>(

Ok(requested_assets)
}

fn deserialize_user_data<'info>(
acc_info: &AccountInfo<'info>
) -> Result<UserData> {
let data = acc_info.try_borrow_data()?;
Ok(UserData::try_from_slice(&data[8..]).unwrap())
}
32 changes: 32 additions & 0 deletions programs/tokenized_vault/src/state/strategy_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,36 @@ impl StrategyData {
self.last_update = Clock::get()?.unix_timestamp;
Ok(())
}
}

pub fn deserialize(acc_info: &AccountInfo) -> Result<Box<StrategyData>> {
let data = acc_info.try_borrow_data()?;
Ok(Box::new(StrategyData::try_from_slice(&data[8..]).unwrap()))
}

pub trait StrategyDataAccInfo {
fn set_current_debt(&self, amount: u64) -> Result<()>;
fn deserialize(&self) -> Result<Box<StrategyData>>;
fn current_debt(&self) -> u64;

}

impl<'a> StrategyDataAccInfo for AccountInfo<'a> {
fn deserialize(&self) -> Result<Box<StrategyData>> {
let data = self.try_borrow_data()?;
Ok(Box::new(StrategyData::try_from_slice(&data[8..]).unwrap()))
}

fn current_debt(&self) -> u64 {
let data = self.try_borrow_data().unwrap();
StrategyData::try_from_slice(&data[8..]).unwrap().current_debt
}

fn set_current_debt(&self, amount: u64) -> Result<()> {
let mut data = self.try_borrow_mut_data()?;
let mut strategy_data = StrategyData::try_from_slice(&data[8..]).unwrap();
strategy_data.update_current_debt(amount)?;
strategy_data.serialize(&mut &mut data[8..])?;
Ok(())
}
}
Loading