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

[Alloyx] Phase 2 - Rebalancing IX #310

Merged
merged 45 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
35be93e
saving-progress
crypto-vincent Oct 13, 2023
a0d541a
adding-alloyx-vault-everywhere
crypto-vincent Oct 13, 2023
4153dbd
work-in-progress-but-it-compiles
crypto-vincent Oct 13, 2023
7743544
saving-changes-on-unit-tests
crypto-vincent Oct 17, 2023
a3bedf4
fix-tests
crypto-vincent Oct 17, 2023
97ca5cd
integration-tests-fixes
crypto-vincent Oct 17, 2023
e6e55f5
fixing-more-integration-tests
crypto-vincent Oct 18, 2023
2ca1971
rebalance-ix-skeleton
crypto-vincent Oct 19, 2023
7e6c311
more-rebalancing-corrections
crypto-vincent Oct 19, 2023
9e9bd71
integration-test-tooling-for-rebalancing
crypto-vincent Oct 19, 2023
911446f
integration-test-changes
crypto-vincent Oct 21, 2023
4afe6a1
changing-mint-maths
crypto-vincent Oct 22, 2023
12bbec2
maths
crypto-vincent Oct 22, 2023
3e913a4
saving-progress
crypto-vincent Oct 23, 2023
77c99f1
resolve-conflicts
crypto-vincent Oct 23, 2023
5c98082
fix-unit-tests-for-mint-maths
crypto-vincent Oct 23, 2023
9114b3b
starting-testing-for-
crypto-vincent Oct 24, 2023
1017865
first-working-test-yay
crypto-vincent Oct 24, 2023
f2cb17c
fixing-unit-tests
crypto-vincent Oct 25, 2023
5c5d055
progress-on-integration-tests-and-rebalance-polishing
crypto-vincent Oct 25, 2023
e33beff
first-fully-functional-alloyx-rebalance-test-suite
crypto-vincent Oct 25, 2023
176b8bc
more-integration-testing-and-polishing-almost-there
crypto-vincent Oct 25, 2023
83b0f36
save
crypto-vincent Oct 26, 2023
bd81b26
saving-progress
crypto-vincent Oct 27, 2023
db4c000
starting-to-make-sense-a-little-bit-almost-there
crypto-vincent Oct 27, 2023
06ea4d4
all-tests-passing-yooo
crypto-vincent Oct 28, 2023
2213fc4
lint-and-fmt
crypto-vincent Oct 28, 2023
45c2720
rebalance-check-protocol-value
crypto-vincent Oct 30, 2023
bfe5b19
rebalancing-test-on-devnet
crypto-vincent Oct 30, 2023
b2b0bc0
nits
crypto-vincent Oct 30, 2023
b346be4
Merge branch 'vbrunet/2023_10_10-rebalancing-alloyx' of github.com:UX…
crypto-vincent Oct 30, 2023
9e79e8b
fix-mint-and-redeem-combo-integration-test
crypto-vincent Oct 30, 2023
32f9ac4
fix-lint
crypto-vincent Oct 30, 2023
5917691
nit-deploy-sols
crypto-vincent Nov 1, 2023
8bda25b
increase-compute-budget
crypto-vincent Nov 1, 2023
ff16ca7
polishing-for-review
crypto-vincent Nov 2, 2023
0834f1c
update-binary
crypto-vincent Nov 2, 2023
e76498f
added-comments
crypto-vincent Nov 3, 2023
13ddc1a
applying-pr-feedbacks
crypto-vincent Nov 13, 2023
f145320
ready-for-review
crypto-vincent Nov 13, 2023
a0f458f
download-binaries
crypto-vincent Nov 13, 2023
d28db5c
last-minute-nits
crypto-vincent Nov 14, 2023
59e0dd9
update-idl-and-typescript
crypto-vincent Nov 14, 2023
ac98653
merge-conflict
crypto-vincent Nov 14, 2023
c0bae85
update-package-for-uxd-client
crypto-vincent Nov 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use anchor_spl::token::TokenAccount;

use crate::error::UxdError;
use crate::events::RebalanceCreateWithdrawRequestFromCredixLpDepositoryEvent;
use crate::state::alloyx_vault_depository::AlloyxVaultDepository;
use crate::state::controller::Controller;
use crate::state::credix_lp_depository::CredixLpDepository;
use crate::state::identity_depository::IdentityDepository;
use crate::state::mercurial_vault_depository::MercurialVaultDepository;
use crate::utils::calculate_credix_lp_depository_target_amount;
use crate::utils::calculate_router_depositories_target_redeemable_amount;
use crate::utils::checked_add;
use crate::utils::checked_as_u64;
use crate::utils::checked_sub;
Expand All @@ -18,8 +19,6 @@ use crate::CONTROLLER_NAMESPACE;
use crate::CREDIX_LP_DEPOSITORY_NAMESPACE;
use crate::CREDIX_LP_EXTERNAL_PASS_NAMESPACE;
use crate::CREDIX_LP_EXTERNAL_WITHDRAW_EPOCH_NAMESPACE;
use crate::IDENTITY_DEPOSITORY_NAMESPACE;
use crate::MERCURIAL_VAULT_DEPOSITORY_NAMESPACE;

#[derive(Accounts)]
pub struct RebalanceCreateWithdrawRequestFromCredixLpDepository<'info> {
Expand All @@ -35,53 +34,45 @@ pub struct RebalanceCreateWithdrawRequestFromCredixLpDepository<'info> {
bump = controller.load()?.bump,
constraint = controller.load()?.identity_depository == identity_depository.key() @UxdError::InvalidDepository,
constraint = controller.load()?.mercurial_vault_depository == mercurial_vault_depository.key() @UxdError::InvalidDepository,
constraint = controller.load()?.credix_lp_depository == depository.key() @UxdError::InvalidDepository,
constraint = controller.load()?.credix_lp_depository == credix_lp_depository.key() @UxdError::InvalidDepository,
constraint = controller.load()?.alloyx_vault_depository == alloyx_vault_depository.key() @UxdError::InvalidDepository,
)]
pub controller: AccountLoader<'info, Controller>,

/// #3
pub collateral_mint: Box<Account<'info, Mint>>,

/// #4
#[account(
mut,
seeds = [IDENTITY_DEPOSITORY_NAMESPACE],
bump = identity_depository.load()?.bump,
has_one = collateral_mint @UxdError::InvalidCollateralMint,
)]
pub identity_depository: AccountLoader<'info, IdentityDepository>,

/// #4
/// #5
#[account(
mut,
seeds = [MERCURIAL_VAULT_DEPOSITORY_NAMESPACE, mercurial_vault_depository.load()?.mercurial_vault.key().as_ref(), mercurial_vault_depository.load()?.collateral_mint.as_ref()],
bump = mercurial_vault_depository.load()?.bump,
has_one = controller @UxdError::InvalidController,
has_one = collateral_mint @UxdError::InvalidCollateralMint,
)]
pub mercurial_vault_depository: AccountLoader<'info, MercurialVaultDepository>,

/// #5
/// #6
#[account(
mut,
seeds = [
CREDIX_LP_DEPOSITORY_NAMESPACE,
depository.load()?.credix_global_market_state.key().as_ref(),
depository.load()?.collateral_mint.as_ref()
],
bump = depository.load()?.bump,
has_one = controller @UxdError::InvalidController,
has_one = collateral_mint @UxdError::InvalidCollateralMint,
has_one = depository_shares @UxdError::InvalidDepositoryShares,
constraint = credix_lp_depository.load()?.depository_shares == credix_lp_depository_shares.key() @UxdError::InvalidDepositoryShares,
has_one = credix_global_market_state @UxdError::InvalidCredixGlobalMarketState,
has_one = credix_signing_authority @UxdError::InvalidCredixSigningAuthority,
has_one = credix_liquidity_collateral @UxdError::InvalidCredixLiquidityCollateral,
has_one = credix_shares_mint @UxdError::InvalidCredixSharesMint,
)]
pub depository: AccountLoader<'info, CredixLpDepository>,

/// #6
pub collateral_mint: Box<Account<'info, Mint>>,
pub credix_lp_depository: AccountLoader<'info, CredixLpDepository>,

/// #7
#[account(mut)]
pub depository_shares: Box<Account<'info, TokenAccount>>,
pub credix_lp_depository_shares: Box<Account<'info, TokenAccount>>,

/// #8
pub credix_global_market_state: Box<Account<'info, credix_client::GlobalMarketState>>,
Expand All @@ -103,12 +94,12 @@ pub struct RebalanceCreateWithdrawRequestFromCredixLpDepository<'info> {
owner = credix_client::ID,
seeds = [
credix_global_market_state.key().as_ref(),
depository.key().as_ref(),
credix_lp_depository.key().as_ref(),
CREDIX_LP_EXTERNAL_PASS_NAMESPACE
],
bump,
seeds::program = credix_client::ID,
constraint = credix_pass.user == depository.key() @UxdError::InvalidCredixPass,
constraint = credix_pass.user == credix_lp_depository.key() @UxdError::InvalidCredixPass,
constraint = credix_pass.disable_withdrawal_fee() @UxdError::InvalidCredixPassNoFees,
)]
pub credix_pass: Account<'info, credix_client::CredixPass>,
Expand All @@ -128,21 +119,33 @@ pub struct RebalanceCreateWithdrawRequestFromCredixLpDepository<'info> {
pub credix_withdraw_epoch: Account<'info, credix_client::WithdrawEpoch>,

/// #14
pub system_program: Program<'info, System>,
#[account(
mut,
has_one = controller @UxdError::InvalidController,
has_one = collateral_mint @UxdError::InvalidCollateralMint,
)]
pub alloyx_vault_depository: AccountLoader<'info, AlloyxVaultDepository>,

/// #15
pub system_program: Program<'info, System>,
/// #16
pub credix_program: Program<'info, credix_client::program::Credix>,
}

pub(crate) fn handler(
ctx: Context<RebalanceCreateWithdrawRequestFromCredixLpDepository>,
) -> Result<()> {
let credix_global_market_state = ctx.accounts.depository.load()?.credix_global_market_state;
let collateral_mint = ctx.accounts.depository.load()?.collateral_mint;
let credix_global_market_state = ctx
.accounts
.credix_lp_depository
.load()?
.credix_global_market_state;
let collateral_mint = ctx.accounts.credix_lp_depository.load()?.collateral_mint;
let depository_pda_signer: &[&[&[u8]]] = &[&[
CREDIX_LP_DEPOSITORY_NAMESPACE,
credix_global_market_state.as_ref(),
collateral_mint.as_ref(),
&[ctx.accounts.depository.load()?.bump],
&[ctx.accounts.credix_lp_depository.load()?.bump],
]];

// ---------------------------------------------------------------------
Expand All @@ -158,12 +161,12 @@ pub(crate) fn handler(

// ---------------------------------------------------------------------
// -- Phase 2
// -- Compute the profits and overflow amounts of the depository
// -- Compute the profits and overflow amounts of the credix_lp_depository
// ---------------------------------------------------------------------

let redeemable_amount_under_management = checked_as_u64(
ctx.accounts
.depository
.credix_lp_depository
.load()?
.redeemable_amount_under_management,
)?;
Expand All @@ -177,7 +180,7 @@ pub(crate) fn handler(
let total_shares_supply: u64 = ctx.accounts.credix_shares_mint.supply;
let total_shares_value: u64 =
checked_add(liquidity_collateral_amount, outstanding_collateral_amount)?;
let owned_shares_amount: u64 = ctx.accounts.depository_shares.amount;
let owned_shares_amount: u64 = ctx.accounts.credix_lp_depository_shares.amount;
let owned_shares_value: u64 = compute_value_for_shares_amount_floor(
owned_shares_amount,
total_shares_supply,
Expand All @@ -188,12 +191,14 @@ pub(crate) fn handler(

let overflow_value = {
let redeemable_amount_under_management_target_amount =
calculate_credix_lp_depository_target_amount(
calculate_router_depositories_target_redeemable_amount(
&ctx.accounts.controller,
&ctx.accounts.identity_depository,
&ctx.accounts.mercurial_vault_depository,
&ctx.accounts.depository,
)?;
&ctx.accounts.credix_lp_depository,
&ctx.accounts.alloyx_vault_depository,
)?
.credix_lp_depository_target_redeemable_amount;
if redeemable_amount_under_management < redeemable_amount_under_management_target_amount {
0
} else {
Expand Down Expand Up @@ -229,9 +234,9 @@ pub(crate) fn handler(

emit!(RebalanceCreateWithdrawRequestFromCredixLpDepositoryEvent {
controller_version: ctx.accounts.controller.load()?.version,
depository_version: ctx.accounts.depository.load()?.version,
depository_version: ctx.accounts.credix_lp_depository.load()?.version,
controller: ctx.accounts.controller.key(),
depository: ctx.accounts.depository.key(),
depository: ctx.accounts.credix_lp_depository.key(),
overflow_value,
profits_collateral_amount,
requested_collateral_amount,
Expand All @@ -249,8 +254,8 @@ impl<'info> RebalanceCreateWithdrawRequestFromCredixLpDepository<'info> {
{
let cpi_accounts = credix_client::cpi::accounts::CreateWithdrawRequest {
payer: self.payer.to_account_info(),
investor: self.depository.to_account_info(),
investor_lp_token_account: self.depository_shares.to_account_info(),
investor: self.credix_lp_depository.to_account_info(),
investor_lp_token_account: self.credix_lp_depository_shares.to_account_info(),
global_market_state: self.credix_global_market_state.to_account_info(),
signing_authority: self.credix_signing_authority.to_account_info(),
liquidity_pool_token_account: self.credix_liquidity_collateral.to_account_info(),
Expand Down
Loading
Loading