Skip to content

Commit

Permalink
fixed errors.rs and structured it for better and clear understanding
Browse files Browse the repository at this point in the history
  • Loading branch information
itsHaseebSaeed committed Feb 8, 2024
1 parent 606c102 commit 442d26f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 98 deletions.
151 changes: 61 additions & 90 deletions contracts/liquidity_book/lb_pair/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! ### Custom Errors for LB_Pair contract.
use ethnum::U256;
use shade_protocol::{
c_std::{StdError, Uint128},
c_std::{StdError, Uint128, Uint256},
lb_libraries::{
bin_helper::BinError,
fee_helper::FeeError,
Expand All @@ -18,149 +17,121 @@ use shade_protocol::{

#[derive(thiserror::Error, Debug)]
pub enum LBPairError {
// Generic Errors
#[error("Generic {0}")]
Generic(String),

#[error("Zero borrow amount!")]
ZeroBorrowAmount,

#[error("Address is zero!")]
AddressZero,

#[error("Serilization Failed is zero!")]
SerializationError,
#[error("Invalid input!")]
InvalidInput,
#[error("value greater than u24!")]
U24Overflow,
#[error("Token not supported!")]
TokenNotSupported(),
#[error("Transaction is blocked by contract status")]
TransactionBlock(),
#[error("Not enough funds")]
NotEnoughFunds,

// Permission Errors
#[error("Only the Factory can do that!")]
OnlyFactory,

#[error("Only the Protocol Fee Recipient can do that!")]
OnlyProtocolFeeRecipient,

// Market Configuration Errors
#[error("Empty Market Configuration")]
EmptyMarketConfigs,
#[error("Invalid static fee parameters!")]
InvalidStaticFeeParameters,

// Liquidity and Flash Loan Errors
#[error("Not enough liquidity!")]
OutOfLiquidity,
#[error("Flash loan callback failed!")]
FlashLoanCallbackFailed,

#[error("Flash loan insufficient amount!")]
FlashLoanInsufficientAmount,

#[error("Insufficient amount in!")]
InsufficientAmountIn,

#[error("Insufficient amount out!")]
InsufficientAmountOut,

#[error("Invalid input!")]
InvalidInput,

#[error("Invalid static fee parameters!")]
InvalidStaticFeeParameters,

#[error("Not enough liquidity!")]
OutOfLiquidity,

#[error("value greater than u24!")]
U24Overflow,

#[error("Token not supported!")]
TokenNotSupported(),

#[error("Transaction is blocked by contract status")]
TransactionBlock(),

#[error("Zero amount for bin id: {id}")]
ZeroAmount { id: u32 },

// Oracle Errors
#[error("Oracle not active!")]
OracleNotActive,

// TODO - why return amount_to_burn and total_supply? They will be illegible as U256 anyway.
// Would like to remove U256 dependency for error messages.
#[error(
"Zero amounts out for bin id: {id} amount to burn: {amount_to_burn} total supply: {total_supply} "
)]
ZeroAmountsOut {
id: u32,
// bin_reserves: [u8; 32],
amount_to_burn: U256,
total_supply: U256,
// amounts_out_from_bin: [u8; 32],
// Interface and Callback Errors
#[error("Use the receive interface")]
UseReceiveInterface,
#[error("Receiver callback \"msg\" parameter cannot be empty.")]
ReceiverMsgEmpty,

// Time and Deadline Errors
#[error("Deadline exceeded. Deadline: {deadline}, Current timestamp: {current_timestamp}")]
DeadlineExceeded {
deadline: u64,
current_timestamp: u64,
},

// Specific Errors with Parameters
#[error("Zero amount for bin id: {id}")]
ZeroAmount { id: u32 },
#[error("Zero Shares for bin id: {id}")]
ZeroShares { id: u32 },

#[error("Max total fee exceeded!")]
MaxTotalFeeExceeded,
#[error("Wrong Pair")]
WrongPair,

// TODO - organize errors better. move error conversions to separate section perhaps.
// Error Wrappings from Dependencies
#[error(transparent)]
CwErr(#[from] StdError),

#[error(transparent)]
BinErr(#[from] BinError),

#[error(transparent)]
FeeErr(#[from] FeeError),

#[error(transparent)]
OracleErr(#[from] OracleError),

#[error(transparent)]
ParamsErr(#[from] PairParametersError),

#[error(transparent)]
LiquidityConfigErr(#[from] LiquidityConfigurationsError),

#[error(transparent)]
U128Err(#[from] U128x128MathError),

#[error(transparent)]
U256Err(#[from] U256x256MathError),

#[error("Wrong Pair")]
WrongPair,

#[error("Use the receive interface")]
UseReceiveInterface,

#[error("Receiver callback \"msg\" parameter cannot be empty.")]
ReceiverMsgEmpty,

#[error("Not enough funds")]
NotEnoughFunds,

#[error("No matching token in pair")]
NoMatchingTokenInPair,

#[error("Deadline exceeded. Deadline: {deadline}, Current timestamp: {current_timestamp}")]
DeadlineExceeded {
deadline: u64,
current_timestamp: u64,
// Complex Scenarios and Calculations Errors
#[error(
"Zero amounts out for bin id: {id} amount to burn: {amount_to_burn} total supply: {total_supply}"
)]
ZeroAmountsOut {
id: u32,
amount_to_burn: Uint256,
total_supply: Uint256,
},

#[error("Lengths mismatch")]
LengthsMismatch,

#[error("time_of_last_update was later than look_up_timestamp")]
LastUpdateTimestampGreaterThanLookupTimestamp,

// Id and Calculation Related Errors
#[error("Id desired overflows. Id desired: {id_desired}, Id slippage: {id_slippage}")]
IdDesiredOverflows { id_desired: u32, id_slippage: u32 },

#[error("could not get bin reserves for active id: {active_id}")]
ZeroBinReserve { active_id: u32 },

#[error("Delta id overflows. Delta Id: {delta_id}")]
DeltaIdOverflows { delta_id: i64 },

#[error("Id underflow. Id: {id} Delta Id: {delta_id}")]
IdUnderflows { id: u32, delta_id: u32 },

#[error("Id overflows. Id: {id}")]
IdOverflows { id: u32 },
#[error("could not get bin reserves for active id: {active_id}")]
ZeroBinReserve { active_id: u32 },
#[error("Lengths mismatch")]
LengthsMismatch,
#[error("time_of_last_update was later than look_up_timestamp")]
LastUpdateTimestampGreaterThanLookupTimestamp,

// Slippage and Trading Errors
#[error(
"Amount left unswapped. : Amount Left In: {amount_left_in}, Total Amount: {total_amount}, swapped_amount: {swapped_amount}"
)]
Expand All @@ -169,7 +140,6 @@ pub enum LBPairError {
total_amount: Uint128,
swapped_amount: Uint128,
},

#[error(
"Id slippage caught. Active id desired: {active_id_desired}, Id slippage: {id_slippage}, Active id: {active_id}"
)]
Expand All @@ -178,13 +148,6 @@ pub enum LBPairError {
id_slippage: u32,
active_id: u32,
},

#[error("Pair not created: {token_x} and {token_y}, binStep: {bin_step}")]
PairNotCreated {
token_x: String,
token_y: String,
bin_step: u16,
},
#[error(
"Amount slippage caught. AmountXMin: {amount_x_min}, AmountX: {amount_x}, AmountYMin: {amount_y_min}, AmountY: {amount_y}"
)]
Expand All @@ -194,4 +157,12 @@ pub enum LBPairError {
amount_y_min: Uint128,
amount_y: Uint128,
},
#[error("Pair not created: {token_x} and {token_y}, binStep: {bin_step}")]
PairNotCreated {
token_x: String,
token_y: String,
bin_step: u16,
},
#[error("No matching token in pair")]
NoMatchingTokenInPair,
}
6 changes: 2 additions & 4 deletions contracts/liquidity_book/lb_pair/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,10 +952,8 @@ fn burn(
if amounts_out_from_bin.iter().all(|&x| x == 0) {
return Err(Error::ZeroAmountsOut {
id,
// bin_reserves,
amount_to_burn: amount_to_burn_u256,
total_supply,
// amounts_out_from_bin,
amount_to_burn: amount_to_burn_u256.u256_to_uint256(),
total_supply: total_supply.u256_to_uint256(),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
- ephemeral_storage_w
- ephemeral_storage_r
- types.rs

- lb_pair
- src
- contract.rs
Expand Down Expand Up @@ -120,7 +119,6 @@
- query_total_supply
- query_rewards_distribution
- state.rs

- lb_staking
- src
- contract.rs
Expand Down Expand Up @@ -173,7 +171,6 @@
- append_stake_tx_for_addr
- append_unstake_tx_for_addr
- append_claim_rewards_tx_for_addr

- lb_token
- src
- contract.rs
Expand Down Expand Up @@ -292,7 +289,6 @@
- registered_tokens_list_r
- epheral_storage_w
- epheral_storage_r

- tests
- src
- lib.rs
Expand Down

0 comments on commit 442d26f

Please sign in to comment.