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

pool_stable: adds more contracterrrors #268

Merged
merged 7 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
73 changes: 47 additions & 26 deletions contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::storage::{
get_config, is_initialized, save_config, set_initialized, Asset, Config, DataKey,
LiquidityPoolInfo, LpPortfolio, PairTupleKey, StakePortfolio, StakedResponse, UserPortfolio,
};
use crate::utils::deploy_multihop_contract;
use crate::{
storage::{get_lp_vec, save_lp_vec, save_lp_vec_with_tuple_as_key},
utils::deploy_lp_contract,
error::ContractError,
storage::{
get_config, get_lp_vec, is_initialized, save_config, save_lp_vec,
save_lp_vec_with_tuple_as_key, set_initialized, Asset, Config, DataKey, LiquidityPoolInfo,
LpPortfolio, PairTupleKey, StakePortfolio, StakedResponse, UserPortfolio,
},
utils::{deploy_lp_contract, deploy_multihop_contract},
};
use phoenix::utils::{LiquidityPoolInitInfo, StakeInitInfo, TokenInitInfo};
use phoenix::validate_bps;
use soroban_sdk::{
contract, contractimpl, contractmeta, log, vec, Address, BytesN, Env, IntoVal, String, Symbol,
Val, Vec,
contract, contractimpl, contractmeta, log, panic_with_error, vec, Address, BytesN, Env,
IntoVal, String, Symbol, Val, Vec,
};

// Metadata that is added on to the WASM custom section
Expand Down Expand Up @@ -77,11 +77,16 @@ impl FactoryTrait for Factory {
lp_token_decimals: u32,
) {
if is_initialized(&env) {
panic!("Factory: Initialize: initializing contract twice is not allowed");
log!(
&env,
"Factory: Initialize: initializing contract twice is not allowed"
);
panic_with_error!(&env, ContractError::AlreadyInitialized);
}

if whitelisted_accounts.is_empty() {
panic!("Factory: Initialize: there must be at least one whitelisted account able to create liquidity pools.")
log!(&env, "Factory: Initialize: there must be at least one whitelisted account able to create liquidity pools.");
panic_with_error!(&env, ContractError::WhiteListedEmpty);
}

set_initialized(&env);
Expand Down Expand Up @@ -117,9 +122,11 @@ impl FactoryTrait for Factory {
) -> Address {
sender.require_auth();
if !get_config(&env).whitelisted_accounts.contains(sender) {
panic!(
log!(
&env,
"Factory: Create Liquidity Pool: You are not authorized to create liquidity pool!"
)
);
panic_with_error!(&env, ContractError::NotAuthorized);
};

validate_token_info(
Expand Down Expand Up @@ -188,9 +195,11 @@ impl FactoryTrait for Factory {
let config = get_config(&env);

if config.admin != sender {
panic!(
"Factory: Create Liquidity Pool: You are not authorized to create liquidity pool!"
)
log!(
&env,
"Factory: Update whitelisted accounts: You are not authorized!"
);
panic_with_error!(&env, ContractError::NotAuthorized);
};

let mut whitelisted_accounts = config.whitelisted_accounts;
Expand Down Expand Up @@ -264,7 +273,11 @@ impl FactoryTrait for Factory {
return addr;
}

panic!("Factory: query_for_pool_by_token_pair failed: No liquidity pool found");
log!(
&env,
"Factory: query_for_pool_by_token_pair failed: No liquidity pool found"
);
panic_with_error!(&env, ContractError::LiquidityPoolNotFound);
}

fn get_admin(env: Env) -> Address {
Expand Down Expand Up @@ -355,21 +368,27 @@ fn validate_token_info(
stake_init_info: &StakeInitInfo,
) {
if token_init_info.token_a >= token_init_info.token_b {
log!(env, "token_a must be less than token_b");
panic!("Factory: validate_token_info failed: First token must be smaller then second");
log!(
env,
"Factory: validate_token info failed: token_a must be less than token_b"
);
panic_with_error!(&env, ContractError::TokenABiggerThanTokenB);
}

if stake_init_info.min_bond <= 0 {
log!(
env,
"Minimum amount of lp share tokens to bond can not be smaller or equal to 0"
"Factory: validate_token_info: Minimum amount of lp share tokens to bond can not be smaller or equal to 0"
);
panic!("Factory: validate_token_info failed: min stake is less or equal to zero");
panic_with_error!(&env, ContractError::MinStakeInvalid);
}

if stake_init_info.min_reward <= 0 {
log!(env, "min_reward must be bigger then 0!");
panic!("Factory: validate_token_info failed: min reward too small");
log!(
&env,
"Factory: validate_token_info failed: min_reward must be bigger then 0!"
);
panic_with_error!(&env, ContractError::MinRewardInvalid);
}
}

Expand All @@ -380,7 +399,7 @@ mod tests {

#[test]
#[should_panic(
expected = "Factory: validate_token_info failed: First token must be smaller then second"
expected = "Factory: validate_token info failed: token_a must be less than token_b"
)]
fn validate_token_info_should_fail_on_token_a_less_than_token_b() {
let env = Env::default();
Expand All @@ -406,7 +425,7 @@ mod tests {

#[test]
#[should_panic(
expected = "Factory: validate_token_info failed: min stake is less or equal to zero"
expected = "Factory: validate_token_info: Minimum amount of lp share tokens to bond can not be smaller or equal to 0"
)]
fn validate_token_info_should_fail_on_min_bond_less_than_zero() {
let env = Env::default();
Expand All @@ -426,7 +445,9 @@ mod tests {
}

#[test]
#[should_panic(expected = "Factory: validate_token_info failed: min reward too small")]
#[should_panic(
expected = "Factory: validate_token_info failed: min_reward must be bigger then 0!"
)]
fn validate_token_info_should_fail_on_min_reward_less_than_zero() {
let env = Env::default();

Expand Down
14 changes: 14 additions & 0 deletions contracts/factory/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use soroban_sdk::contracterror;

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum ContractError {
AlreadyInitialized = 1,
WhiteListedEmpty = 2,
NotAuthorized = 3,
LiquidityPoolNotFound = 4,
TokenABiggerThanTokenB = 5,
MinStakeInvalid = 6,
MinRewardInvalid = 7,
}
1 change: 1 addition & 0 deletions contracts/factory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
mod contract;
mod error;
mod storage;
mod utils;

Expand Down
26 changes: 18 additions & 8 deletions contracts/multihop/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use soroban_sdk::{contract, contractimpl, contractmeta, vec, Address, Env, Vec};
use soroban_sdk::{
contract, contractimpl, contractmeta, log, panic_with_error, vec, Address, Env, Vec,
};

use crate::error::ContractError;
// FIXM: Disable Referral struct
// use crate::lp_contract::Referral;
use crate::storage::{
Expand Down Expand Up @@ -44,7 +47,11 @@ pub trait MultihopTrait {
impl MultihopTrait for Multihop {
fn initialize(env: Env, admin: Address, factory: Address) {
if is_initialized(&env) {
panic!("Multihop: Initialize: initializing contract twice is not allowed");
log!(
&env,
"Multihop: Initialize: initializing contract twice is not allowed"
);
panic_with_error!(&env, ContractError::AlreadyInitialized);
}

set_initialized(&env);
Expand All @@ -65,9 +72,10 @@ impl MultihopTrait for Multihop {
amount: i128,
) {
if operations.is_empty() {
panic!("Multihop: Swap: operations is empty!");
log!(&env, "Multihop: Swap: operations is empty!");
panic_with_error!(&env, ContractError::OperationsEmpty);
}
verify_swap(&operations);
verify_swap(&env, &operations);

recipient.require_auth();

Expand Down Expand Up @@ -96,10 +104,11 @@ impl MultihopTrait for Multihop {

fn simulate_swap(env: Env, operations: Vec<Swap>, amount: i128) -> SimulateSwapResponse {
if operations.is_empty() {
panic!("Multihop: Simulate swap: operations empty");
log!(&env, "Multihop: Simulate swap: operations empty");
panic_with_error!(&env, ContractError::OperationsEmpty);
}

verify_swap(&operations);
verify_swap(&env, &operations);

let mut next_offer_amount: i128 = amount;

Expand Down Expand Up @@ -140,10 +149,11 @@ impl MultihopTrait for Multihop {
amount: i128,
) -> SimulateReverseSwapResponse {
if operations.is_empty() {
panic!("Multihop: Simulate reverse swap: operations empty");
log!(&env, "Multihop: Simulate reverse swap: operations empty");
panic_with_error!(&env, ContractError::OperationsEmpty);
}

verify_reverse_swap(&operations);
verify_reverse_swap(&env, &operations);

let mut next_ask_amount: i128 = amount;

Expand Down
10 changes: 10 additions & 0 deletions contracts/multihop/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use soroban_sdk::contracterror;

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum ContractError {
AlreadyInitialized = 1,
OperationsEmpty = 2,
BadSwap = 3,
}
3 changes: 1 addition & 2 deletions contracts/multihop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![no_std]
mod contract;

mod error;
mod storage;

mod utils;

#[allow(clippy::too_many_arguments)]
Expand Down
22 changes: 12 additions & 10 deletions contracts/multihop/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use soroban_sdk::Vec;
use soroban_sdk::{log, panic_with_error, Env, Vec};

use crate::storage::Swap;
use crate::{error::ContractError, storage::Swap};

pub fn verify_swap(operations: &Vec<Swap>) {
pub fn verify_swap(env: &Env, operations: &Vec<Swap>) {
for (current, next) in operations.iter().zip(operations.iter().skip(1)) {
if current.ask_asset != next.offer_asset {
panic!("Multihop: Swap: Provided bad swap order");
log!(&env, "Multihop: Swap: Provided bad swap order");
panic_with_error!(&env, ContractError::BadSwap);
}
}
}

pub fn verify_reverse_swap(operations: &Vec<Swap>) {
pub fn verify_reverse_swap(env: &Env, operations: &Vec<Swap>) {
for (current, next) in operations.iter().zip(operations.iter().skip(1)) {
if current.offer_asset != next.ask_asset {
panic!("Multihop: Reverse swap: Provided bad swap order");
log!(&env, "Multihop: Reverse swap: Provided bad swap order");
panic_with_error!(&env, ContractError::BadSwap);
}
}
}
Expand Down Expand Up @@ -52,7 +54,7 @@ mod tests {

let operations = vec![&env, swap1, swap2, swap3];

verify_swap(&operations);
verify_swap(&env, &operations);
}

#[test]
Expand Down Expand Up @@ -82,7 +84,7 @@ mod tests {

let operations = vec![&env, swap1, swap2, swap3];

verify_reverse_swap(&operations);
verify_reverse_swap(&env, &operations);
}

#[test]
Expand All @@ -108,7 +110,7 @@ mod tests {

let operations = vec![&env, swap1, swap2];

verify_swap(&operations);
verify_swap(&env, &operations);
}

#[test]
Expand All @@ -134,6 +136,6 @@ mod tests {

let operations = vec![&env, swap1, swap2];

verify_reverse_swap(&operations);
verify_reverse_swap(&env, &operations);
}
}
Loading
Loading