Skip to content

Commit

Permalink
Merge pull request #886 from multiversx/router-check-is-pair-refactor
Browse files Browse the repository at this point in the history
Router check pair refactor
  • Loading branch information
psorinionut authored Apr 16, 2024
2 parents cb32307 + 954d65c commit a97b9c3
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 78 deletions.
33 changes: 25 additions & 8 deletions dex/router/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@ multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::{enable_swap_by_user::EnableSwapByUserConfig, factory::PairTokens};
use pair::read_pair_storage;

#[multiversx_sc::module]
pub trait ConfigModule {
pub trait ConfigModule: read_pair_storage::ReadPairStorageModule {
fn is_active(&self) -> bool {
self.state().get()
}

fn check_is_pair_sc(&self, pair_address: &ManagedAddress) {
require!(
self.address_pair_map().contains_key(pair_address),
"Not a pair SC"
);
let first_token_id = self.get_first_token_id_mapper(pair_address.clone()).get();
let second_token_id = self.get_second_token_id_mapper(pair_address.clone()).get();

let pair_tokens = PairTokens {
first_token_id: first_token_id.clone(),
second_token_id: second_token_id.clone(),
};

let mut pair_map_address_opt = self.pair_map().get(&pair_tokens);
if pair_map_address_opt.is_none() {
let reverse_pair_tokens = PairTokens {
first_token_id: second_token_id.clone(),
second_token_id: first_token_id.clone(),
};
pair_map_address_opt = self.pair_map().get(&reverse_pair_tokens);
}

require!(pair_map_address_opt.is_some(), "Not a pair SC");

unsafe {
let pair_map_address = pair_map_address_opt.unwrap_unchecked();
require!(&pair_map_address == pair_address, "Not a pair SC");
}
}

#[view(getPairCreationEnabled)]
Expand Down Expand Up @@ -43,9 +63,6 @@ pub trait ConfigModule {
#[storage_mapper("pair_map")]
fn pair_map(&self) -> MapMapper<PairTokens<Self::Api>, ManagedAddress>;

#[storage_mapper("address_pair_map")]
fn address_pair_map(&self) -> MapMapper<ManagedAddress, PairTokens<Self::Api>>;

#[view(getPairTemplateAddress)]
#[storage_mapper("pair_template_address")]
fn pair_template_address(&self) -> SingleValueMapper<ManagedAddress>;
Expand Down
28 changes: 2 additions & 26 deletions dex/router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod multi_pair_swap;
use factory::PairTokens;
use pair::config::ProxyTrait as _;
use pair::fee::ProxyTrait as _;
use pair::ProxyTrait as _;
use pair::{read_pair_storage, ProxyTrait as _};
use pausable::ProxyTrait as _;

const LP_TOKEN_DECIMALS: usize = 18;
Expand All @@ -26,6 +26,7 @@ const USER_DEFINED_TOTAL_FEE_PERCENT: u64 = 1_000;
#[multiversx_sc::contract]
pub trait Router:
config::ConfigModule
+ read_pair_storage::ReadPairStorageModule
+ factory::FactoryModule
+ events::EventsModule
+ multi_pair_swap::MultiPairSwap
Expand Down Expand Up @@ -64,10 +65,6 @@ pub trait Router:
#[endpoint]
fn resume(&self, address: ManagedAddress) {
if address == self.blockchain().get_sc_address() {
require!(
self.pair_map().len() == self.address_pair_map().len(),
"The size of the 2 pair maps is not the same"
);
self.state().set(true);
} else {
self.check_is_pair_sc(&address);
Expand Down Expand Up @@ -317,8 +314,6 @@ pub trait Router:
.unwrap_or_else(ManagedAddress::zero);
}

self.address_pair_map().remove(&pair_address);

pair_address
}

Expand Down Expand Up @@ -385,23 +380,4 @@ pub trait Router:
fn set_pair_creation_enabled(&self, enabled: bool) {
self.pair_creation_enabled().set(enabled);
}

#[only_owner]
#[endpoint(migratePairMap)]
fn migrate_pair_map(&self) {
let pair_map = self.pair_map();
let mut address_pair_map = self.address_pair_map();
require!(
address_pair_map.is_empty(),
"The destination mapper must be empty"
);
for (pair_tokens, address) in pair_map.iter() {
address_pair_map.insert(address, pair_tokens);
}

require!(
pair_map.len() == address_pair_map.len(),
"The size of the 2 pair maps is not the same"
);
}
}
8 changes: 6 additions & 2 deletions dex/router/src/enable_swap_by_user.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use pair::{config::ProxyTrait as _, pair_actions::views::ProxyTrait as _};
use pair::{config::ProxyTrait as _, pair_actions::views::ProxyTrait as _, read_pair_storage};
use pausable::{ProxyTrait as _, State};
use simple_lock::locked_token::LockedTokenAttributes;

Expand All @@ -27,7 +27,10 @@ pub struct SafePriceResult<M: ManagedTypeApi> {

#[multiversx_sc::module]
pub trait EnableSwapByUserModule:
config::ConfigModule + crate::factory::FactoryModule + crate::events::EventsModule
config::ConfigModule
+ read_pair_storage::ReadPairStorageModule
+ crate::factory::FactoryModule
+ crate::events::EventsModule
{
#[only_owner]
#[endpoint(configEnableByUserParameters)]
Expand Down Expand Up @@ -83,6 +86,7 @@ pub trait EnableSwapByUserModule:
#[payable("*")]
#[endpoint(setSwapEnabledByUser)]
fn set_swap_enabled_by_user(&self, pair_address: ManagedAddress) {
require!(self.is_active(), "Not active");
self.check_is_pair_sc(&pair_address);
self.require_state_active_no_swaps(&pair_address);

Expand Down
17 changes: 2 additions & 15 deletions dex/router/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::config;
use pair::read_pair_storage;

const TEMPORARY_OWNER_PERIOD_BLOCKS: u64 = 50;

Expand All @@ -19,7 +20,7 @@ pub struct PairContractMetadata<M: ManagedTypeApi> {
}

#[multiversx_sc::module]
pub trait FactoryModule: config::ConfigModule {
pub trait FactoryModule: config::ConfigModule + read_pair_storage::ReadPairStorageModule {
#[proxy]
fn pair_contract_deploy_proxy(&self) -> pair::Proxy<Self::Api>;

Expand Down Expand Up @@ -71,13 +72,6 @@ pub trait FactoryModule: config::ConfigModule {
},
new_address.clone(),
);
self.address_pair_map().insert(
new_address.clone(),
PairTokens {
first_token_id: first_token_id.clone(),
second_token_id: second_token_id.clone(),
},
);
self.pair_temporary_owner().insert(
new_address.clone(),
(
Expand Down Expand Up @@ -174,13 +168,6 @@ pub trait FactoryModule: config::ConfigModule {
address
}

#[view(getPairTokens)]
fn get_pair_tokens(&self, pair_address: ManagedAddress) -> PairTokens<Self::Api> {
let pair_tokens_opt = self.address_pair_map().get(&pair_address);
require!(pair_tokens_opt.is_some(), "Pair address not found");
pair_tokens_opt.unwrap()
}

fn get_pair_temporary_owner(&self, pair_address: &ManagedAddress) -> Option<ManagedAddress> {
let result = self.pair_temporary_owner().get(pair_address);

Expand Down
11 changes: 6 additions & 5 deletions dex/router/src/multi_pair_swap.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use pair::pair_actions::swap::ProxyTrait as _;

use crate::config;

use super::factory;
use crate::config;
use pair::{pair_actions::swap::ProxyTrait as _, read_pair_storage};

type SwapOperationType<M> =
MultiValue4<ManagedAddress<M>, ManagedBuffer<M>, TokenIdentifier<M>, BigUint<M>>;
Expand All @@ -15,7 +13,10 @@ pub const SWAP_TOKENS_FIXED_OUTPUT_FUNC_NAME: &[u8] = b"swapTokensFixedOutput";

#[multiversx_sc::module]
pub trait MultiPairSwap:
config::ConfigModule + factory::FactoryModule + token_send::TokenSendModule
config::ConfigModule
+ read_pair_storage::ReadPairStorageModule
+ factory::FactoryModule
+ token_send::TokenSendModule
{
#[payable("*")]
#[endpoint(multiPairSwap)]
Expand Down
13 changes: 0 additions & 13 deletions dex/router/tests/router_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,4 @@ where
)
.assert_ok();
}

pub fn migrate_pair_map(&mut self) {
self.blockchain_wrapper
.execute_tx(
&self.owner_address,
&self.router_wrapper,
&rust_biguint!(0u64),
|sc| {
sc.migrate_pair_map();
},
)
.assert_ok();
}
}
5 changes: 0 additions & 5 deletions dex/router/tests/router_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ fn test_router_upgrade_pair() {
#[test]
fn test_multi_pair_swap() {
let mut router_setup = RouterSetup::new(router::contract_obj, pair::contract_obj);
router_setup.migrate_pair_map();

router_setup.add_liquidity();

Expand Down Expand Up @@ -255,8 +254,6 @@ fn user_enable_pair_swaps_through_router_test() {
managed_address!(pair_wrapper.address_ref()),
);

sc.migrate_pair_map();

sc.add_common_tokens_for_user_pairs(MultiValueEncoded::from(ManagedVec::from(vec![
managed_token_id!(USDC_TOKEN_ID),
])));
Expand Down Expand Up @@ -442,8 +439,6 @@ fn user_enable_pair_swaps_fail_test() {
managed_address!(pair_wrapper.address_ref()),
);

sc.migrate_pair_map();

sc.add_common_tokens_for_user_pairs(MultiValueEncoded::from(ManagedVec::from(vec![
managed_token_id!(USDC_TOKEN_ID),
])));
Expand Down
6 changes: 2 additions & 4 deletions dex/router/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 32
// Endpoints: 30
// Async Callback: 1
// Total number of exported functions: 34
// Total number of exported functions: 32

#![no_std]
#![allow(internal_features)]
Expand All @@ -31,7 +31,6 @@ multiversx_sc_wasm_adapter::endpoints! {
setFeeOn => set_fee_on
setFeeOff => set_fee_off
setPairCreationEnabled => set_pair_creation_enabled
migratePairMap => migrate_pair_map
getPairCreationEnabled => pair_creation_enabled
getState => state
getOwner => owner
Expand All @@ -44,7 +43,6 @@ multiversx_sc_wasm_adapter::endpoints! {
getAllPairTokens => get_all_token_pairs
getAllPairContractMetadata => get_all_pair_contract_metadata
getPair => get_pair
getPairTokens => get_pair_tokens
clearPairTemporaryOwnerStorage => clear_pair_temporary_owner_storage
multiPairSwap => multi_pair_swap
configEnableByUserParameters => config_enable_by_user_parameters
Expand Down

0 comments on commit a97b9c3

Please sign in to comment.