Skip to content

Commit

Permalink
Merge pull request #30 from ElrondNetwork/feat/inter-pair-swap
Browse files Browse the repository at this point in the history
Feat/inter pair swap
  • Loading branch information
catalinnnn authored Apr 14, 2021
2 parents fe9fea1 + 0543090 commit 0f3378f
Show file tree
Hide file tree
Showing 7 changed files with 492 additions and 169 deletions.
22 changes: 11 additions & 11 deletions elrond_dex_pair/src/amm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
imports!();
derive_imports!();
elrond_wasm::imports!();
elrond_wasm::derive_imports!();

#[elrond_wasm_derive::module(AmmModuleImpl)]
pub trait AmmModule {
Expand Down Expand Up @@ -38,9 +38,9 @@ pub trait AmmModule {
reserve_in: BigUint,
reserve_out: BigUint,
) -> BigUint {
let amount_in_with_fee = amount_in * BigUint::from(1000 - self.total_fee_precent().get());
let amount_in_with_fee = amount_in * BigUint::from(100000 - self.total_fee_precent().get());
let numerator = amount_in_with_fee.clone() * reserve_out;
let denominator = (reserve_in * BigUint::from(1000u64)) + amount_in_with_fee;
let denominator = (reserve_in * BigUint::from(100000u64)) + amount_in_with_fee;

numerator / denominator
}
Expand All @@ -51,24 +51,24 @@ pub trait AmmModule {
reserve_in: BigUint,
reserve_out: BigUint,
) -> BigUint {
let numerator = (reserve_in * amount_out.clone()) * BigUint::from(1000u64);
let numerator = (reserve_in * amount_out.clone()) * BigUint::from(100000u64);
let denominator =
(reserve_out - amount_out) * BigUint::from(1000 - self.total_fee_precent().get());
(reserve_out - amount_out) * BigUint::from(100000 - self.total_fee_precent().get());

(numerator / denominator) + BigUint::from(1u64)
}

fn get_special_fee_from_fixed_input(&self, amount_in: BigUint) -> BigUint {
amount_in * BigUint::from(self.special_fee_precent().get()) / BigUint::from(1000u64)
amount_in * BigUint::from(self.special_fee_precent().get()) / BigUint::from(100000u64)
}

fn get_special_fee_from_optimal_input(&self, amount_in_optimal: BigUint) -> BigUint {
let amount_in_zero_fee = amount_in_optimal
* BigUint::from(1000 - self.total_fee_precent().get())
/ BigUint::from(1000u64);
* BigUint::from(100000 - self.total_fee_precent().get())
/ BigUint::from(100000u64);

amount_in_zero_fee.clone() * BigUint::from(1000u64)
/ BigUint::from(1000 - self.special_fee_precent().get())
amount_in_zero_fee.clone() * BigUint::from(100000u64)
/ BigUint::from(100000 - self.special_fee_precent().get())
- amount_in_zero_fee
}

Expand Down
29 changes: 17 additions & 12 deletions elrond_dex_pair/src/fee.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
imports!();
derive_imports!();
elrond_wasm::imports!();
elrond_wasm::derive_imports!();

#[derive(TopEncode, TopDecode, PartialEq, TypeAbi)]
pub struct TokenPair {
pub first_token: TokenIdentifier,
pub second_token: TokenIdentifier,
}

#[elrond_wasm_derive::module(FeeModuleImpl)]
pub trait FeeModule {
#[view(getFeeState)]
#[storage_mapper("fee_state")]
fn state(&self) -> SingleValueMapper<Self::Storage, bool>;
#[storage_mapper("fee_destination")]
fn destination_map(&self) -> MapMapper<Self::Storage, Address, TokenIdentifier>;

#[view(getFeeToAddress)]
#[storage_mapper("fee_address")]
fn address(&self) -> SingleValueMapper<Self::Storage, Address>;
#[storage_mapper("trusted_swap_pair")]
fn trusted_swap_pair(&self) -> MapMapper<Self::Storage, TokenPair, Address>;

#[view(getFeeTokenIdentifier)]
#[storage_mapper("fee_token_identifier")]
fn token_identifier(&self) -> SingleValueMapper<Self::Storage, TokenIdentifier>;
#[view(getWhitelistedAddresses)]
#[storage_mapper("whitelist")]
fn whitelist(&self) -> SetMapper<Self::Storage, Address>;

#[view(getFeeState)]
fn is_enabled(&self) -> bool {
self.state().get()
!self.destination_map().is_empty()
}
}
Loading

0 comments on commit 0f3378f

Please sign in to comment.