From dd4cb715ad51a06be03b2c20b06c8c5406db27d4 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Tue, 9 Apr 2024 13:55:00 +0300 Subject: [PATCH 1/8] Github actions on rc branches --- .github/workflows/actions.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 56d357593..afa63aa92 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ main, feat/* ] + branches: [ main, feat/*, rc/* ] pull_request: - branches: [ main, feat/* ] + branches: [ main, feat/*, rc/* ] workflow_dispatch: permissions: From b21ea70bf19ffbdaea84585ce279e21c7e3fd126 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Wed, 10 Apr 2024 16:28:52 +0300 Subject: [PATCH 2/8] Router check pair refactor --- dex/router/src/config.rs | 39 +++++++++++++++++++++++----- dex/router/src/contract.rs | 25 ------------------ dex/router/src/factory.rs | 14 ---------- dex/router/tests/router_setup/mod.rs | 13 ---------- dex/router/tests/router_test.rs | 5 ---- dex/router/wasm/src/lib.rs | 8 +++--- 6 files changed, 36 insertions(+), 68 deletions(-) diff --git a/dex/router/src/config.rs b/dex/router/src/config.rs index 25c96a3e6..925725959 100644 --- a/dex/router/src/config.rs +++ b/dex/router/src/config.rs @@ -10,10 +10,29 @@ pub trait ConfigModule { } 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.first_token_id().get_from_address(&pair_address); + let second_token_id = self.second_token_id().get_from_address(&pair_address); + + 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)] @@ -43,9 +62,6 @@ pub trait ConfigModule { #[storage_mapper("pair_map")] fn pair_map(&self) -> MapMapper, ManagedAddress>; - #[storage_mapper("address_pair_map")] - fn address_pair_map(&self) -> MapMapper>; - #[view(getPairTemplateAddress)] #[storage_mapper("pair_template_address")] fn pair_template_address(&self) -> SingleValueMapper; @@ -66,4 +82,13 @@ pub trait ConfigModule { #[view(getCommonTokensForUserPairs)] #[storage_mapper("commonTokensForUserPairs")] fn common_tokens_for_user_pairs(&self) -> UnorderedSetMapper; + + // read from other storage + #[view(getFirstTokenId)] + #[storage_mapper("first_token_id")] + fn first_token_id(&self) -> SingleValueMapper; + + #[view(getSecondTokenId)] + #[storage_mapper("second_token_id")] + fn second_token_id(&self) -> SingleValueMapper; } diff --git a/dex/router/src/contract.rs b/dex/router/src/contract.rs index 04b787a03..f90416226 100644 --- a/dex/router/src/contract.rs +++ b/dex/router/src/contract.rs @@ -64,10 +64,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); @@ -317,8 +313,6 @@ pub trait Router: .unwrap_or_else(ManagedAddress::zero); } - self.address_pair_map().remove(&pair_address); - pair_address } @@ -385,23 +379,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" - ); - } } diff --git a/dex/router/src/factory.rs b/dex/router/src/factory.rs index 4994db235..37a476a8d 100644 --- a/dex/router/src/factory.rs +++ b/dex/router/src/factory.rs @@ -71,13 +71,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(), ( @@ -174,13 +167,6 @@ pub trait FactoryModule: config::ConfigModule { address } - #[view(getPairTokens)] - fn get_pair_tokens(&self, pair_address: ManagedAddress) -> PairTokens { - 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 { let result = self.pair_temporary_owner().get(pair_address); diff --git a/dex/router/tests/router_setup/mod.rs b/dex/router/tests/router_setup/mod.rs index c72b6a971..4339d502d 100644 --- a/dex/router/tests/router_setup/mod.rs +++ b/dex/router/tests/router_setup/mod.rs @@ -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(); - } } diff --git a/dex/router/tests/router_test.rs b/dex/router/tests/router_test.rs index 54c73c838..54765f2c4 100644 --- a/dex/router/tests/router_test.rs +++ b/dex/router/tests/router_test.rs @@ -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(); @@ -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), ]))); @@ -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), ]))); diff --git a/dex/router/wasm/src/lib.rs b/dex/router/wasm/src/lib.rs index e840498ec..ee22e6ba5 100644 --- a/dex/router/wasm/src/lib.rs +++ b/dex/router/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 33 +// Endpoints: 32 // Async Callback: 1 -// Total number of exported functions: 35 +// Total number of exported functions: 34 #![no_std] #![allow(internal_features)] @@ -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 @@ -40,11 +39,12 @@ multiversx_sc_wasm_adapter::endpoints! { getPairTemplateAddress => pair_template_address getTemporaryOwnerPeriod => temporary_owner_period getCommonTokensForUserPairs => common_tokens_for_user_pairs + getFirstTokenId => first_token_id + getSecondTokenId => second_token_id getAllPairsManagedAddresses => get_all_pairs_addresses 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 From 16f0d725d1d6717612023e349450d6bed79bf0de Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Wed, 10 Apr 2024 17:04:13 +0300 Subject: [PATCH 3/8] clippy fixes --- dex/router/src/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dex/router/src/config.rs b/dex/router/src/config.rs index 925725959..388bc0a44 100644 --- a/dex/router/src/config.rs +++ b/dex/router/src/config.rs @@ -10,8 +10,8 @@ pub trait ConfigModule { } fn check_is_pair_sc(&self, pair_address: &ManagedAddress) { - let first_token_id = self.first_token_id().get_from_address(&pair_address); - let second_token_id = self.second_token_id().get_from_address(&pair_address); + let first_token_id = self.first_token_id().get_from_address(pair_address); + let second_token_id = self.second_token_id().get_from_address(pair_address); let pair_tokens = PairTokens { first_token_id: first_token_id.clone(), From b01507b6a36397b717ac491835ff7d1a7ccbac70 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Thu, 11 Apr 2024 12:25:54 +0300 Subject: [PATCH 4/8] add check on enable swap by user --- dex/router/src/enable_swap_by_user.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/dex/router/src/enable_swap_by_user.rs b/dex/router/src/enable_swap_by_user.rs index 7c77036c1..f5929f4f7 100644 --- a/dex/router/src/enable_swap_by_user.rs +++ b/dex/router/src/enable_swap_by_user.rs @@ -83,6 +83,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); From 88ab1ca365e190dd9334820c739861aecd9cbcb4 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Sat, 13 Apr 2024 00:46:26 +0300 Subject: [PATCH 5/8] router fix after framework upgrade --- dex/router/src/config.rs | 7 ++++--- dex/router/src/contract.rs | 3 ++- dex/router/src/enable_swap_by_user.rs | 7 +++++-- dex/router/src/factory.rs | 3 ++- dex/router/src/multi_pair_swap.rs | 11 ++++++----- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/dex/router/src/config.rs b/dex/router/src/config.rs index 388bc0a44..c4f89c440 100644 --- a/dex/router/src/config.rs +++ b/dex/router/src/config.rs @@ -2,16 +2,17 @@ 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) { - let first_token_id = self.first_token_id().get_from_address(pair_address); - let second_token_id = self.second_token_id().get_from_address(pair_address); + 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(), diff --git a/dex/router/src/contract.rs b/dex/router/src/contract.rs index 6c425aa6c..e23d0f12f 100644 --- a/dex/router/src/contract.rs +++ b/dex/router/src/contract.rs @@ -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; @@ -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 diff --git a/dex/router/src/enable_swap_by_user.rs b/dex/router/src/enable_swap_by_user.rs index f5929f4f7..1a8c40770 100644 --- a/dex/router/src/enable_swap_by_user.rs +++ b/dex/router/src/enable_swap_by_user.rs @@ -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; @@ -27,7 +27,10 @@ pub struct SafePriceResult { #[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)] diff --git a/dex/router/src/factory.rs b/dex/router/src/factory.rs index 37a476a8d..fea434703 100644 --- a/dex/router/src/factory.rs +++ b/dex/router/src/factory.rs @@ -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; @@ -19,7 +20,7 @@ pub struct PairContractMetadata { } #[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; diff --git a/dex/router/src/multi_pair_swap.rs b/dex/router/src/multi_pair_swap.rs index dc5492f9d..1e47378f9 100644 --- a/dex/router/src/multi_pair_swap.rs +++ b/dex/router/src/multi_pair_swap.rs @@ -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 = MultiValue4, ManagedBuffer, TokenIdentifier, BigUint>; @@ -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)] From ef126033b1d4c7276636561c96e65fa89c19796c Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Mon, 15 Apr 2024 14:07:14 +0300 Subject: [PATCH 6/8] remove unneeded check --- dex/router/src/config.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/dex/router/src/config.rs b/dex/router/src/config.rs index c4f89c440..21a433084 100644 --- a/dex/router/src/config.rs +++ b/dex/router/src/config.rs @@ -19,15 +19,7 @@ pub trait ConfigModule: read_pair_storage::ReadPairStorageModule { 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); - } - + let pair_map_address_opt = self.pair_map().get(&pair_tokens); require!(pair_map_address_opt.is_some(), "Not a pair SC"); unsafe { From ec4b55003a00f36d21522d7cdab0df24b2c6e39e Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Tue, 16 Apr 2024 12:25:59 +0300 Subject: [PATCH 7/8] add back the router reverse check pair logic --- dex/router/src/config.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/dex/router/src/config.rs b/dex/router/src/config.rs index 21a433084..c4f89c440 100644 --- a/dex/router/src/config.rs +++ b/dex/router/src/config.rs @@ -19,7 +19,15 @@ pub trait ConfigModule: read_pair_storage::ReadPairStorageModule { second_token_id: second_token_id.clone(), }; - let pair_map_address_opt = self.pair_map().get(&pair_tokens); + 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 { From 954d65ce9e9bd888197392085a0db7fa5b2190d0 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Tue, 16 Apr 2024 13:45:56 +0300 Subject: [PATCH 8/8] code cleanup --- dex/router/src/config.rs | 9 --------- dex/router/wasm/src/lib.rs | 6 ++---- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/dex/router/src/config.rs b/dex/router/src/config.rs index c4f89c440..830d301c8 100644 --- a/dex/router/src/config.rs +++ b/dex/router/src/config.rs @@ -83,13 +83,4 @@ pub trait ConfigModule: read_pair_storage::ReadPairStorageModule { #[view(getCommonTokensForUserPairs)] #[storage_mapper("commonTokensForUserPairs")] fn common_tokens_for_user_pairs(&self) -> UnorderedSetMapper; - - // read from other storage - #[view(getFirstTokenId)] - #[storage_mapper("first_token_id")] - fn first_token_id(&self) -> SingleValueMapper; - - #[view(getSecondTokenId)] - #[storage_mapper("second_token_id")] - fn second_token_id(&self) -> SingleValueMapper; } diff --git a/dex/router/wasm/src/lib.rs b/dex/router/wasm/src/lib.rs index ee22e6ba5..ee424764b 100644 --- a/dex/router/wasm/src/lib.rs +++ b/dex/router/wasm/src/lib.rs @@ -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)] @@ -39,8 +39,6 @@ multiversx_sc_wasm_adapter::endpoints! { getPairTemplateAddress => pair_template_address getTemporaryOwnerPeriod => temporary_owner_period getCommonTokensForUserPairs => common_tokens_for_user_pairs - getFirstTokenId => first_token_id - getSecondTokenId => second_token_id getAllPairsManagedAddresses => get_all_pairs_addresses getAllPairTokens => get_all_token_pairs getAllPairContractMetadata => get_all_pair_contract_metadata