Skip to content

Commit

Permalink
Merge pull request #184 from blend-capital/errors
Browse files Browse the repository at this point in the history
Errors and backstop rate normalization to 7 decimals
  • Loading branch information
mootz12 authored Jan 8, 2024
2 parents 4d43049 + 7488e81 commit 3a9f959
Show file tree
Hide file tree
Showing 40 changed files with 351 additions and 280 deletions.
8 changes: 4 additions & 4 deletions backstop/src/backstop/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_execute_deposit_negative_tokens() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand All @@ -145,7 +145,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_execute_deposit_from_is_to() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand All @@ -167,7 +167,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_execute_deposit_from_self() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand All @@ -189,7 +189,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #10)")]
#[should_panic(expected = "Error(Contract, #1004)")]
fn text_execute_deposit_not_pool() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down
20 changes: 10 additions & 10 deletions backstop/src/backstop/fund_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_execute_donate_negative_amount() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -180,7 +180,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_execute_donate_from_is_to() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -210,7 +210,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_execute_donate_from_is_self() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -240,7 +240,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #10)")]
#[should_panic(expected = "Error(Contract, #1004)")]
fn test_execute_donate_not_pool() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -298,7 +298,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #6)")]
#[should_panic(expected = "Error(Contract, #1003)")]
fn test_execute_draw_only_can_take_from_pool() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -330,7 +330,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_execute_draw_negative_amount() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -393,7 +393,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_execute_donate_usdc_negative_amount() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand All @@ -416,7 +416,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_execute_donate_usdc_from_is_to() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand All @@ -439,7 +439,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_execute_donate_usdc_from_is_self() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand All @@ -462,7 +462,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #10)")]
#[should_panic(expected = "Error(Contract, #1004)")]
fn test_execute_donate_usdc_not_pool() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down
6 changes: 3 additions & 3 deletions backstop/src/backstop/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #10)")]
#[should_panic(expected = "Error(Contract, #1004)")]
fn test_require_is_from_pool_factory_not_valid() {
let e = Env::default();

Expand Down Expand Up @@ -417,7 +417,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #6)")]
#[should_panic(expected = "Error(Contract, #1003)")]
fn test_withdraw_too_much() {
let e = Env::default();
let mut pool_balance = PoolBalance {
Expand Down Expand Up @@ -446,7 +446,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #6)")]
#[should_panic(expected = "Error(Contract, #1003)")]
fn test_dequeue_q4w_too_much() {
let e = Env::default();
let mut pool_balance = PoolBalance {
Expand Down
14 changes: 7 additions & 7 deletions backstop/src/backstop/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl UserBalance {
/// If the amount to queue is greater than the available shares
pub fn queue_shares_for_withdrawal(&mut self, e: &Env, to_q: i128) {
if self.shares < to_q {
panic_with_error!(e, BackstopError::InvalidBalance);
panic_with_error!(e, BackstopError::BalanceError);
}
self.shares = self.shares - to_q;

Expand Down Expand Up @@ -106,7 +106,7 @@ impl UserBalance {
}

if left_to_dequeue > 0 {
panic_with_error!(e, BackstopError::InvalidBalance);
panic_with_error!(e, BackstopError::BalanceError);
}
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #2)")]
#[should_panic(expected = "Error(Contract, #10)")]
fn test_q4w_over_shares_panics() {
let e = Env::default();

Expand Down Expand Up @@ -243,7 +243,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #2)")]
#[should_panic(expected = "Error(Contract, #10)")]
fn test_withdraw_shares_no_q4w_panics() {
let e = Env::default();

Expand Down Expand Up @@ -396,7 +396,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #3)")]
#[should_panic(expected = "Error(Contract, #1001)")]
fn test_withdraw_shares_multiple_entries_not_exp() {
let e = Env::default();

Expand Down Expand Up @@ -436,7 +436,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #3)")]
#[should_panic(expected = "Error(Contract, #1001)")]
fn test_try_dequeue_shares_require_expired_expect_panic() {
let e = Env::default();

Expand Down Expand Up @@ -475,7 +475,7 @@ mod tests {
user.dequeue_shares_for_withdrawal(&e, to_dequeue, true);
}
#[test]
#[should_panic(expected = "Error(Contract, #2)")]
#[should_panic(expected = "Error(Contract, #10)")]
fn test_try_withdraw_shares_over_total() {
let e = Env::default();

Expand Down
6 changes: 3 additions & 3 deletions backstop/src/backstop/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_execute_queue_withdrawal_negative_amount() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -247,7 +247,7 @@ mod tests {
});
}
#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_execute_dequeue_withdrawal_negative_amount() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -364,7 +364,7 @@ mod tests {
});
}
#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_execute_withdrawal_negative_amount() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();
Expand Down
4 changes: 2 additions & 2 deletions backstop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl Backstop for BackstopContract {
) {
storage::extend_instance(&e);
if storage::get_is_init(&e) {
panic_with_error!(e, BackstopError::AlreadyInitialized);
panic_with_error!(e, BackstopError::AlreadyInitializedError);
}

storage::set_backstop_token(&e, &backstop_token);
Expand Down Expand Up @@ -389,6 +389,6 @@ impl Backstop for BackstopContract {
/// If the number is negative
pub fn require_nonnegative(e: &Env, amount: i128) {
if amount.is_negative() {
panic_with_error!(e, BackstopError::NegativeAmount);
panic_with_error!(e, BackstopError::NegativeAmountError);
}
}
4 changes: 2 additions & 2 deletions backstop/src/emissions/distributor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ mod tests {
// in to ensure any bad state does not result in incorrect emissions.

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_update_emissions_more_q4w_than_shares_panics() {
let e = Env::default();
let block_timestamp = BACKSTOP_EPOCH + 1234;
Expand Down Expand Up @@ -662,7 +662,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Contract, #8)")]
fn test_update_emissions_negative_user_index() {
let e = Env::default();
let block_timestamp = BACKSTOP_EPOCH + 1234;
Expand Down
16 changes: 8 additions & 8 deletions backstop/src/emissions/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_gulp_emissions_too_soon() {
let e = Env::default();
e.budget().reset_unlimited();
Expand Down Expand Up @@ -505,7 +505,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_gulp_pool_emissions_no_emissions() {
let e = Env::default();
e.budget().reset_unlimited();
Expand Down Expand Up @@ -570,7 +570,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #4)")]
#[should_panic(expected = "Error(Contract, #1002)")]
fn test_add_to_rz_empty_pool_under_backstop_threshold() {
let e = Env::default();
e.ledger().set(LedgerInfo {
Expand Down Expand Up @@ -656,7 +656,7 @@ mod tests {
});
}
#[test]
#[should_panic(expected = "Error(Contract, #4)")]
#[should_panic(expected = "Error(Contract, #1002)")]
fn test_add_to_rz_takes_floor_for_size() {
let e = Env::default();
e.ledger().set(LedgerInfo {
Expand Down Expand Up @@ -770,7 +770,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #4)")]
#[should_panic(expected = "Error(Contract, #1002)")]
fn test_add_to_rz_swap_not_enough_tokens() {
let e = Env::default();
e.ledger().set(LedgerInfo {
Expand Down Expand Up @@ -830,7 +830,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_add_to_rz_swap_distribution_too_long_ago() {
let e = Env::default();
e.ledger().set(LedgerInfo {
Expand Down Expand Up @@ -890,7 +890,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #4)")]
#[should_panic(expected = "Error(Contract, #1002)")]
fn test_add_to_rz_to_remove_not_in_rz() {
let e = Env::default();
e.ledger().set(LedgerInfo {
Expand Down Expand Up @@ -950,7 +950,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
#[should_panic(expected = "Error(Contract, #1000)")]
fn test_add_to_rz_already_exists_panics() {
let e = Env::default();
e.ledger().set(LedgerInfo {
Expand Down
27 changes: 18 additions & 9 deletions backstop/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ use soroban_sdk::contracterror;
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
/// Error codes for the backstop contract. Common errors are codes that match up with the built-in
/// contracts error reporting. Backstop specific errors start at 1000.
pub enum BackstopError {
BadRequest = 1,
InvalidBalance = 2,
NotExpired = 3,
InvalidRewardZoneEntry = 4,
NotAuthorized = 5,
InsufficientFunds = 6,
AlreadyInitialized = 7,
NotPool = 10,
NegativeAmount = 11,
// Common Errors
InternalError = 1,
AlreadyInitializedError = 3,

UnauthorizedError = 4,

NegativeAmountError = 8,
BalanceError = 10,
OverflowError = 12,

// Backstop
BadRequest = 1000,
NotExpired = 1001,
InvalidRewardZoneEntry = 1002,
InsufficientFunds = 1003,
NotPool = 1004,
}
Loading

0 comments on commit 3a9f959

Please sign in to comment.