Skip to content

Commit

Permalink
[Controller] Remove the need to register depositories (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-vincent authored Oct 10, 2023
1 parent 368973b commit 1e1e22a
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 78 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci-cargo-audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ jobs:
# - RUSTSEC-2020-0071 -> No fixes available, ignored by solana-labs: https://github.com/solana-labs/solana/blob/master/ci/do-audit.sh
# - RUSTSEC-2022-0093 -> No fixes available, ignored by solana-labs: https://github.com/solana-labs/solana/blob/master/ci/do-audit.sh
# - RUSTSEC-2023-0001 -> No fixes available, ignored by solana-labs: https://github.com/solana-labs/solana/blob/master/ci/do-audit.sh
# - RUSTSEC-2023-0052 -> Fixed by upgrading to v1.16 solana, ignored by solana-labs before v1.16: https://github.com/solana-labs/solana/issues/32933
# - RUSTSEC-2023-0063 -> Fixed by upgrading to v1.16 solana, ignored by solana-labs before v1.16: https://github.com/solana-labs/solana/pull/33355
# - RUSTSEC-2023-0065 -> Only a problem on solana-program-test, used for local testing only
run: >
cargo audit -c always
--ignore RUSTSEC-2020-0071
--ignore RUSTSEC-2022-0093
--ignore RUSTSEC-2023-0001
--ignore RUSTSEC-2023-0052
--ignore RUSTSEC-2023-0063
--ignore RUSTSEC-2023-0065
2 changes: 1 addition & 1 deletion programs/uxd/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub enum UxdError {
InvalidAuthority,
#[msg("The Depository's controller doesn't match the provided Controller.")]
InvalidController,
#[msg("The Depository provided is not registered with the Controller.")]
#[msg("The Depository provided is not matching the one stored in the Controller.")]
InvalidDepository,
#[msg("The provided collateral mint does not match the depository's collateral mint.")]
InvalidCollateralMint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct CollectProfitsOfCredixLpDepository<'info> {
mut,
seeds = [CONTROLLER_NAMESPACE],
bump = controller.load()?.bump,
constraint = controller.load()?.registered_credix_lp_depositories.contains(&depository.key()) @UxdError::InvalidDepository,
constraint = controller.load()?.credix_lp_depository == depository.key() @UxdError::InvalidDepository,
)]
pub controller: AccountLoader<'info, Controller>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub struct EditCredixLpDepository<'info> {
mut,
seeds = [CONTROLLER_NAMESPACE],
bump = controller.load()?.bump,
constraint = controller.load()?.registered_credix_lp_depositories.contains(&depository.key()) @UxdError::InvalidDepository,
has_one = authority @UxdError::InvalidAuthority,
)]
pub controller: AccountLoader<'info, Controller>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct MintWithCredixLpDepository<'info> {
mut,
seeds = [CONTROLLER_NAMESPACE],
bump = controller.load()?.bump,
constraint = controller.load()?.registered_credix_lp_depositories.contains(&depository.key()) @UxdError::InvalidDepository,
constraint = controller.load()?.credix_lp_depository == depository.key() @UxdError::InvalidDepository,
has_one = redeemable_mint @UxdError::InvalidRedeemableMint
)]
pub controller: AccountLoader<'info, Controller>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct RedeemFromCredixLpDepository<'info> {
mut,
seeds = [CONTROLLER_NAMESPACE],
bump = controller.load()?.bump,
constraint = controller.load()?.registered_credix_lp_depositories.contains(&depository.key()) @UxdError::InvalidDepository,
constraint = controller.load()?.credix_lp_depository == depository.key() @UxdError::InvalidDepository,
has_one = redeemable_mint @UxdError::InvalidRedeemableMint
)]
pub controller: AccountLoader<'info, Controller>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,6 @@ pub(crate) fn handler(
// Profits collection
depository.profits_total_collected = u128::MIN;

// Add the depository to the controller
ctx.accounts
.controller
.load_mut()?
.add_registered_credix_lp_depository_entry(ctx.accounts.depository.key())?;

// Emit event
emit!(RegisterCredixLpDepositoryEvent {
controller_version: ctx.accounts.controller.load()?.version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub struct EditMercurialVaultDepository<'info> {
seeds = [CONTROLLER_NAMESPACE],
bump = controller.load()?.bump,
has_one = authority @UxdError::InvalidAuthority,
constraint = controller.load()?.registered_mercurial_vault_depositories.contains(&depository.key()) @UxdError::InvalidDepository
)]
pub controller: AccountLoader<'info, Controller>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct CollectProfitsOfMercurialVaultDepository<'info> {
mut,
seeds = [CONTROLLER_NAMESPACE],
bump = controller.load()?.bump,
constraint = controller.load()?.registered_mercurial_vault_depositories.contains(&depository.key()) @UxdError::InvalidDepository,
constraint = controller.load()?.mercurial_vault_depository == depository.key() @UxdError::InvalidDepository,
)]
pub controller: AccountLoader<'info, Controller>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct MintWithMercurialVaultDepository<'info> {
mut,
seeds = [CONTROLLER_NAMESPACE],
bump = controller.load()?.bump,
constraint = controller.load()?.registered_mercurial_vault_depositories.contains(&depository.key()) @UxdError::InvalidDepository,
constraint = controller.load()?.mercurial_vault_depository == depository.key() @UxdError::InvalidDepository,
has_one = redeemable_mint @UxdError::InvalidRedeemableMint
)]
pub controller: AccountLoader<'info, Controller>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct RedeemFromMercurialVaultDepository<'info> {
mut,
seeds = [CONTROLLER_NAMESPACE],
bump = controller.load()?.bump,
constraint = controller.load()?.registered_mercurial_vault_depositories.contains(&depository.key()) @UxdError::InvalidDepository,
constraint = controller.load()?.mercurial_vault_depository == depository.key() @UxdError::InvalidDepository,
has_one = redeemable_mint @UxdError::InvalidRedeemableMint
)]
pub controller: AccountLoader<'info, Controller>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ pub fn handler(
// enable minting by default
depository.minting_disabled = false;

// 2 - Update Controller state
ctx.accounts
.controller
.load_mut()?
.add_registered_mercurial_vault_depository_entry(ctx.accounts.depository.key())?;

// 3 - Emit event
emit!(RegisterMercurialVaultDepositoryEvent {
version: ctx.accounts.controller.load()?.version,
Expand Down
59 changes: 3 additions & 56 deletions programs/uxd/src/state/controller.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use std::mem::size_of;

use crate::error::UxdError;
use crate::utils::checked_add;
use crate::utils::checked_add_u128_and_i128;
use anchor_lang::prelude::*;

pub const MAX_REGISTERED_MERCURIAL_VAULT_DEPOSITORIES: usize = 4;
pub const MAX_REGISTERED_CREDIX_LP_DEPOSITORIES: usize = 4;

// Total should be 885 bytes
pub const CONTROLLER_RESERVED_SPACE: usize = 94;
pub const CONTROLLER_SPACE: usize = 8
Expand All @@ -23,11 +19,7 @@ pub const CONTROLLER_SPACE: usize = 8
+ size_of::<u128>() // redeemable_global_supply_cap
+ 8 // _unused3
+ size_of::<u128>() // redeemable_circulating_supply
+ 8 // _unused4
+ size_of::<Pubkey>() * MAX_REGISTERED_MERCURIAL_VAULT_DEPOSITORIES // registered_mercurial_vault_depositories
+ size_of::<u8>() // registered_mercurial_vault_depositories_count
+ size_of::<Pubkey>() * MAX_REGISTERED_CREDIX_LP_DEPOSITORIES // registered_credix_lp_depositories
+ size_of::<u8>() // registered_credix_lp_depositories_count
+ 266 // _unused4
+ size_of::<u128>() // profits_total_collected
+ size_of::<u16>() // identity_depository_weight_bps
+ size_of::<u16>() // mercurial_vault_depository_weight_bps
Expand Down Expand Up @@ -76,16 +68,8 @@ pub struct Controller {
// This should always be equal to the sum of all Depositories' `redeemable_amount_under_management`
// in redeemable Redeemable Native Amount
pub redeemable_circulating_supply: u128,
pub _unused4: [u8; 8],
//
// The Mercurial Vault Depositories registered with this Controller
pub registered_mercurial_vault_depositories:
[Pubkey; MAX_REGISTERED_MERCURIAL_VAULT_DEPOSITORIES],
pub registered_mercurial_vault_depositories_count: u8,
//
// The Credix Lp Depositories registered with this Controller
pub registered_credix_lp_depositories: [Pubkey; MAX_REGISTERED_CREDIX_LP_DEPOSITORIES],
pub registered_credix_lp_depositories_count: u8,
// Padding for data that is no longer needed
pub _unused4: [u8; 266],
//
// Total amount of profits collected into the treasury by any depository
pub profits_total_collected: u128,
Expand All @@ -112,43 +96,6 @@ pub struct Controller {
}

impl Controller {
pub fn add_registered_mercurial_vault_depository_entry(
&mut self,
mercurial_vault_depository_id: Pubkey,
) -> Result<()> {
let current_size = usize::from(self.registered_mercurial_vault_depositories_count);
require!(
current_size < MAX_REGISTERED_MERCURIAL_VAULT_DEPOSITORIES,
UxdError::MaxNumberOfMercurialVaultDepositoriesRegisteredReached
);
// Increment registered Mercurial Pool Depositories count
self.registered_mercurial_vault_depositories_count =
checked_add(self.registered_mercurial_vault_depositories_count, 1)?;
// Add the new Mercurial Vault Depository ID to the array of registered Depositories
let new_entry_index = current_size;
self.registered_mercurial_vault_depositories[new_entry_index] =
mercurial_vault_depository_id;
Ok(())
}

pub(crate) fn add_registered_credix_lp_depository_entry(
&mut self,
credix_lp_depository_id: Pubkey,
) -> Result<()> {
let current_size = usize::from(self.registered_credix_lp_depositories_count);
require!(
current_size < MAX_REGISTERED_CREDIX_LP_DEPOSITORIES,
UxdError::MaxNumberOfCredixLpDepositoriesRegisteredReached
);
// Increment registered Credix Lp Depositories count
self.registered_credix_lp_depositories_count =
checked_add(self.registered_credix_lp_depositories_count, 1)?;
// Add the new Credix Lp Depository ID to the array of registered Depositories
let new_entry_index = current_size;
self.registered_credix_lp_depositories[new_entry_index] = credix_lp_depository_id;
Ok(())
}

// provides numbers + or - depending on the change
pub fn update_onchain_accounting_following_mint_or_redeem(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ pub async fn process_deploy_program(
)
.await?;

// Make sure the controller has the proper router depositories set
program_uxd::procedures::process_set_router_depositories(
program_context,
payer,
authority,
&collateral_mint.pubkey(),
)
.await?;

// Done
Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use solana_program_test::tokio;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signer::keypair::Keypair;
use solana_sdk::signer::Signer;

Expand All @@ -7,6 +8,7 @@ use uxd::instructions::EditCredixLpDepositoryFields;
use uxd::instructions::EditDepositoriesRoutingWeightBps;
use uxd::instructions::EditIdentityDepositoryFields;
use uxd::instructions::EditMercurialVaultDepositoryFields;
use uxd::instructions::EditRouterDepositories;

use crate::integration_tests::api::program_context;
use crate::integration_tests::api::program_spl;
Expand Down Expand Up @@ -186,6 +188,26 @@ async fn test_mint_and_redeem() -> Result<(), program_context::ProgramError> {
// -- Verify that mint is not possible until we set the depositories address on controller
// ---------------------------------------------------------------------

// Artificially unset the router_depositories
program_uxd::instructions::process_edit_controller(
&mut program_context,
&payer,
&authority,
&EditControllerFields {
redeemable_global_supply_cap: None,
depositories_routing_weight_bps: None,
router_depositories: Some(EditRouterDepositories {
identity_depository: Pubkey::default(),
mercurial_vault_depository: Pubkey::default(),
credix_lp_depository: Pubkey::default(),
}),
outflow_limit_per_epoch_amount: None,
outflow_limit_per_epoch_bps: None,
slots_per_epoch: None,
},
)
.await?;

// Minting should fail now, as the depositories are not set yet
assert!(program_uxd::instructions::process_mint(
&mut program_context,
Expand Down
10 changes: 10 additions & 0 deletions tests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ export async function editController({
controller: Controller;
uiFields: {
redeemableGlobalSupplyCap?: number;
depositoriesRoutingWeightBps?: {
identityDepositoryWeightBps: number;
mercurialVaultDepositoryWeightBps: number;
credixLpDepositoryWeightBps: number;
};
routerDepositories?: {
identityDepository: PublicKey;
mercurialVaultDepository: PublicKey;
credixLpDepository: PublicKey;
};
};
}): Promise<string> {
const editControllerIx = uxdClient.createEditControllerInstruction(
Expand Down
12 changes: 11 additions & 1 deletion tests/cases/editControllerTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Signer } from '@solana/web3.js';
import { PublicKey, Signer } from '@solana/web3.js';
import { Controller, nativeToUi } from '@uxd-protocol/uxd-client';
import { expect } from 'chai';
import { editController } from '../api';
Expand All @@ -14,6 +14,16 @@ export const editControllerTest = async function ({
controller: Controller;
uiFields: {
redeemableGlobalSupplyCap?: number;
depositoriesRoutingWeightBps?: {
identityDepositoryWeightBps: number;
mercurialVaultDepositoryWeightBps: number;
credixLpDepositoryWeightBps: number;
};
routerDepositories?: {
identityDepository: PublicKey;
mercurialVaultDepository: PublicKey;
credixLpDepository: PublicKey;
};
};
}) {
const connection = getConnection();
Expand Down
22 changes: 22 additions & 0 deletions tests/suite/controllerIntegrationSuite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Signer } from '@solana/web3.js';
import { Controller } from '@uxd-protocol/uxd-client';
import { initializeControllerTest } from '../cases/initializeControllerTest';
import { editControllerTest } from '../cases/editControllerTest';
import { createIdentityDepositoryDevnet } from '../utils';
import { createCredixLpDepositoryDevnetUSDC } from '../utils';
import { createMercurialVaultDepositoryDevnet } from '../utils';

export class controllerIntegrationSuiteParameters {
public globalSupplyCap: number;
Expand All @@ -25,4 +29,22 @@ export const controllerIntegrationSuite = function ({
controller,
payer,
}));

it('Initialize router depositories', async function () {
const identityDepository = createIdentityDepositoryDevnet();
const credixLpDepository = await createCredixLpDepositoryDevnetUSDC();
const mercurialVaultDepository =
await createMercurialVaultDepositoryDevnet();
editControllerTest({
authority,
controller,
uiFields: {
routerDepositories: {
identityDepository: identityDepository.pda,
mercurialVaultDepository: mercurialVaultDepository.pda,
credixLpDepository: credixLpDepository.pda,
},
},
});
});
};

0 comments on commit 1e1e22a

Please sign in to comment.