Skip to content

Commit

Permalink
feat: moved function validation from lib.rs to each instruction file (#…
Browse files Browse the repository at this point in the history
…91)

* feat: moved function validation from lib.rs to each instruction file
  • Loading branch information
LexBrill authored Jan 13, 2022
1 parent e71f16f commit 6f650b6
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 128 deletions.
2 changes: 1 addition & 1 deletion Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ wallet = "~/.config/solana/id.json"
uxd = "UXD8m9cvwk4RcSxnX2HZ9VudQCEeDH6fRnB4CAP57Dr"

[programs.devnet]
uxd = "Hto7hH47haUP3Vrinym98BHkVdC3jtYWLXUhmfBSBT5N"
uxd = "4jv7rFE2DJ7HXDtJ812xLLnugXjPTU4iSpBUq8Dm6uEM"

[registry]
url = "https://anchor.projectserum.com"
Expand Down
16 changes: 16 additions & 0 deletions programs/uxd/src/instructions/initialize_controller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::declare_check_assert_macros;
use crate::error::check_assert;
use crate::error::SourceFileId;
use crate::Controller;
use crate::UxdError;
Expand Down Expand Up @@ -84,3 +85,18 @@ pub fn handler(
});
Ok(())
}

// Validate
impl<'info> InitializeController<'info> {
// Asserts that the redeemable mint decimals is between 0 and 9.
pub fn validate(
&self,
decimals: u8,
) -> ProgramResult {
check!(
decimals <= SOLANA_MAX_MINT_DECIMALS,
UxdErrorCode::InvalidRedeemableMintDecimals
)?;
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use anchor_spl::token::Transfer;
use crate::AccountingEvent;
use crate::Controller;
use crate::UxdResult;
use crate::error::check_assert;
use crate::error::UxdErrorCode;
use crate::error::SourceFileId;
use crate::error::UxdIdlErrorCode;
use crate::CONTROLLER_NAMESPACE;
use crate::MANGO_DEPOSITORY_NAMESPACE;
Expand All @@ -16,6 +19,8 @@ use crate::MangoDepository;
use crate::mango_program;
use crate::events::DepositInsuranceToMangoDepositoryEvent;

declare_check_assert_macros!(SourceFileId::InstructionMangoDexDepositInsuranceToMangoDepository);

#[derive(Accounts)]
pub struct DepositInsuranceToMangoDepository<'info> {
pub authority: Signer<'info>,
Expand Down Expand Up @@ -166,4 +171,19 @@ impl<'info> DepositInsuranceToMangoDepository<'info> {
.update_insurance_amount_deposited(&AccountingEvent::Deposit, insurance_delta)?;
Ok(())
}
}
}

// Validate
impl<'info> DepositInsuranceToMangoDepository<'info> {
pub fn validate(
&self,
insurance_amount: u64,
) -> ProgramResult {
check!(insurance_amount > 0, UxdErrorCode::InvalidInsuranceAmount)?;
check!(
self.authority_insurance.amount >= insurance_amount,
UxdErrorCode::InsufficientAuthorityInsuranceAmount
)?;
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::MangoDepository;
use crate::UxdError;
use crate::UxdErrorCode;
use crate::UxdResult;
use crate::SLIPPAGE_BASIS;
use crate::COLLATERAL_PASSTHROUGH_NAMESPACE;
use crate::CONTROLLER_NAMESPACE;
use crate::MANGO_ACCOUNT_NAMESPACE;
Expand All @@ -37,7 +38,7 @@ use mango::state::MangoAccount;
use mango::state::PerpAccount;
use mango::state::PerpMarket;

declare_check_assert_macros!(SourceFileId::InstructionMangoDexRedeemFromMangoDepository);
declare_check_assert_macros!(SourceFileId::InstructionMangoDexMintWithMangoDepository);

#[derive(Accounts)]
pub struct MintWithMangoDepository<'info> {
Expand Down Expand Up @@ -429,3 +430,22 @@ impl<'info> MintWithMangoDepository<'info> {
Ok(())
}
}

// Validate
impl<'info> MintWithMangoDepository<'info> {
pub fn validate(
&self,
collateral_amount: u64,
slippage: u32
) -> ProgramResult {
// Valid slippage check
check!(slippage <= SLIPPAGE_BASIS, UxdErrorCode::InvalidSlippage)?;

check!(collateral_amount > 0, UxdErrorCode::InvalidCollateralAmount)?;
check!(
self.user_collateral.amount >= collateral_amount,
UxdErrorCode::InsufficientCollateralAmount
)?;
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::Controller;
use crate::MangoDepository;
use crate::UxdError;
use crate::UxdResult;
use crate::SLIPPAGE_BASIS;
use crate::COLLATERAL_PASSTHROUGH_NAMESPACE;
use crate::CONTROLLER_NAMESPACE;
use crate::MANGO_ACCOUNT_NAMESPACE;
Expand Down Expand Up @@ -404,3 +405,22 @@ impl<'info> RedeemFromMangoDepository<'info> {
Ok(())
}
}

// Validate
impl<'info> RedeemFromMangoDepository<'info> {
pub fn validate(
&self,
redeemable_amount: u64,
slippage: u32,
) -> ProgramResult {
// Valid slippage check
check!(slippage <= SLIPPAGE_BASIS, UxdErrorCode::InvalidSlippage)?;

check!(redeemable_amount > 0, UxdErrorCode::InvalidRedeemableAmount)?;
check!(
self.user_redeemable.amount >= redeemable_amount,
UxdErrorCode::InsufficientRedeemableAmount
)?;
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ use crate::INSURANCE_PASSTHROUGH_NAMESPACE;
use crate::MangoDepository;
use crate::UxdResult;
use crate::mango_program;
use crate::error::check_assert;
use crate::error::UxdErrorCode;
use crate::error::SourceFileId;
use crate::error::UxdIdlErrorCode;
use crate::events::WithdrawInsuranceFromMangoDeposirotyEvent;

declare_check_assert_macros!(SourceFileId::InstructionMangoDexWithdrawInsuranceFromMangoDepository);

#[derive(Accounts)]
pub struct WithdrawInsuranceFromMangoDepository<'info> {
pub authority: Signer<'info>,
Expand Down Expand Up @@ -170,4 +176,16 @@ impl<'info> WithdrawInsuranceFromMangoDepository<'info> {
.update_insurance_amount_deposited(&AccountingEvent::Withdraw, insurance_delta)?;
Ok(())
}
}
}

// Validate
impl<'info> WithdrawInsuranceFromMangoDepository<'info> {
pub fn validate(
&self,
insurance_amount: u64,
) -> ProgramResult {
check!(insurance_amount > 0, UxdErrorCode::InvalidInsuranceAmount)?;
// Mango withdraw will fail with proper error thanks to `disabled borrow` set to true if the balance is not enough.
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use anchor_lang::prelude::*;
use crate::{Controller, UxdResult};
use crate::Controller;
use crate::UxdResult;
use crate::MAX_MANGO_DEPOSITORIES_REDEEMABLE_SOFT_CAP;
use crate::error::check_assert;
use crate::error::UxdErrorCode;
use crate::error::SourceFileId;
use crate::error::UxdIdlErrorCode;
use crate::CONTROLLER_NAMESPACE;
use crate::events::SetMangoDepositoryRedeemableSoftCapEvent;

declare_check_assert_macros!(SourceFileId::InstructionSetMangoDepositoriesRedeemableSoftCap);

#[derive(Accounts)]
pub struct SetMangoDepositoriesRedeemableSoftCap<'info> {
pub authority: Signer<'info>,
Expand All @@ -29,4 +36,19 @@ pub fn handler(
redeemable_soft_cap
});
Ok(())
}
}

// Validate
impl<'info> SetMangoDepositoriesRedeemableSoftCap<'info> {
// Asserts that the Mango Depositories redeemable soft cap is between 0 and MAX_REDEEMABLE_GLOBAL_SUPPLY_CAP.
pub fn validate(
&self,
redeemable_soft_cap: u64,
) -> ProgramResult {
check!(
redeemable_soft_cap <= MAX_MANGO_DEPOSITORIES_REDEEMABLE_SOFT_CAP,
UxdErrorCode::InvalidMangoDepositoriesRedeemableSoftCap
)?;
Ok(())
}
}
26 changes: 24 additions & 2 deletions programs/uxd/src/instructions/set_redeemable_global_supply_cap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use anchor_lang::prelude::*;
use crate::{Controller, UxdResult};
use crate::Controller;
use crate::UxdResult;
use crate::MAX_REDEEMABLE_GLOBAL_SUPPLY_CAP;
use crate::error::check_assert;
use crate::error::UxdErrorCode;
use crate::error::SourceFileId;
use crate::error::UxdIdlErrorCode;
use crate::CONTROLLER_NAMESPACE;
use crate::events::SetRedeemableGlobalSupplyCapEvent;

declare_check_assert_macros!(SourceFileId::InstructionSetRedeemableGlobalSupplyCap);

#[derive(Accounts)]
pub struct SetRedeemableGlobalSupplyCap<'info> {
pub authority: Signer<'info>,
Expand All @@ -27,4 +34,19 @@ pub fn handler(
redeemable_global_supply_cap
});
Ok(())
}
}

// Validate
impl<'info> SetRedeemableGlobalSupplyCap<'info> {
// Asserts that the redeemable global supply cap is between 0 and MAX_REDEEMABLE_GLOBAL_SUPPLY_CAP.
pub fn validate(
&self,
redeemable_global_supply_cap: u128,
) -> ProgramResult {
check!(
redeemable_global_supply_cap <= MAX_REDEEMABLE_GLOBAL_SUPPLY_CAP,
UxdErrorCode::InvalidRedeemableGlobalSupplyCap
)?;
Ok(())
}
}
Loading

0 comments on commit 6f650b6

Please sign in to comment.