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

router pair map migration #742

Merged
merged 4 commits into from
Sep 5, 2023
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
16 changes: 16 additions & 0 deletions dex/router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ pub trait Router:
.unwrap_or_else(ManagedAddress::zero);
}

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

pair_address
}

Expand Down Expand Up @@ -394,6 +396,20 @@ pub trait Router:
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);
}
}

#[view(getPairCreationEnabled)]
#[storage_mapper("pair_creation_enabled")]
fn pair_creation_enabled(&self) -> SingleValueMapper<bool>;
Expand Down
14 changes: 11 additions & 3 deletions dex/router/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ pub trait FactoryModule {
},
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 @@ -167,9 +174,7 @@ pub trait FactoryModule {

fn check_is_pair_sc(&self, pair_address: &ManagedAddress) {
require!(
self.pair_map()
.values()
.any(|address| &address == pair_address),
self.address_pair_map().contains_key(pair_address),
"Not a pair SC"
);
}
Expand Down Expand Up @@ -215,6 +220,9 @@ pub trait FactoryModule {
#[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
13 changes: 13 additions & 0 deletions dex/router/tests/router_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,17 @@ 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: 5 additions & 0 deletions dex/router/tests/router_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ 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 @@ -253,6 +254,8 @@ 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 @@ -438,6 +441,8 @@ 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
Loading