-
Notifications
You must be signed in to change notification settings - Fork 67
Simple dex #612
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
Simple dex #612
Changes from 1 commit
c7c0e96
71d9c48
9f71ad0
79b9e0a
974f056
ed2ad45
56a4b04
4476eaa
b12d883
23f0cf3
2012cfe
3be6987
99952a4
e4d8211
1e31342
67cb4c8
ff4e744
c2a36fc
71523bf
be7a316
b70278c
2c4213c
d3fef1c
7210d54
e767379
ab507bf
bed038f
bb854e9
00224a5
40a42de
65be1ac
9a67828
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
could use promils for better precision but meh
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,13 +74,13 @@ mod simple_dex { | |
| pub struct SwapFeeSet { | ||
| #[ink(topic)] | ||
| caller: AccountId, | ||
| swap_fee: u128, | ||
| swap_fee_percentage: u128, | ||
| } | ||
|
|
||
| #[ink(storage)] | ||
| #[derive(SpreadAllocate)] | ||
| pub struct SimpleDex { | ||
| pub swap_fee: u128, | ||
| pub swap_fee_percentage: u128, | ||
| pub access_control: AccountId, | ||
| // pool tokens | ||
| pub tokens: [AccountId; 4], | ||
|
|
@@ -127,7 +127,6 @@ mod simple_dex { | |
| ) -> Result<(), DexError> { | ||
| let this = self.env().account_id(); | ||
| let caller = self.env().caller(); | ||
| let swap_fee = self.swap_fee; | ||
|
|
||
| // check if tokens are supported by the pool | ||
| if !self.tokens.contains(&token_in) { | ||
|
|
@@ -155,7 +154,7 @@ mod simple_dex { | |
| amount_token_in, | ||
| balance_token_in, | ||
| balance_token_out, | ||
| swap_fee, | ||
| self.swap_fee_percentage, | ||
| )?; | ||
|
|
||
| if balance_token_out < amount_token_out { | ||
|
|
@@ -261,8 +260,11 @@ mod simple_dex { | |
| /// | ||
| /// Can only be called by the contract's Admin. | ||
| #[ink(message)] | ||
| pub fn set_swap_fee(&mut self, swap_fee: u128) -> Result<(), DexError> { | ||
| if swap_fee.gt(&100) || swap_fee.lt(&0) { | ||
| pub fn set_swap_fee_percentage( | ||
| &mut self, | ||
| swap_fee_percentage: u128, | ||
| ) -> Result<(), DexError> { | ||
| if swap_fee_percentage.gt(&100) { | ||
| return Err(DexError::WrongParameterValue); | ||
| } | ||
|
|
||
|
|
@@ -280,17 +282,20 @@ mod simple_dex { | |
| // emit event | ||
| Self::emit_event( | ||
| self.env(), | ||
| Event::SwapFeeSet(SwapFeeSet { caller, swap_fee }), | ||
| Event::SwapFeeSet(SwapFeeSet { | ||
| caller, | ||
| swap_fee_percentage, | ||
| }), | ||
| ); | ||
|
|
||
| self.swap_fee = swap_fee; | ||
| self.swap_fee_percentage = swap_fee_percentage; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Returns current value of the swap_fee parameter | ||
| /// Returns current value of the swap_fee_percentage parameter | ||
| #[ink(message)] | ||
| pub fn swap_fee(&mut self) -> Balance { | ||
| self.swap_fee | ||
| pub fn swap_fee_percentage(&mut self) -> Balance { | ||
| self.swap_fee_percentage | ||
| } | ||
|
|
||
| /// Sets access_control to a new contract address | ||
|
|
@@ -352,7 +357,7 @@ mod simple_dex { | |
| fn new_init(&mut self, tokens: [AccountId; 4]) { | ||
| self.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); | ||
| self.tokens = tokens; | ||
| self.swap_fee = 0; | ||
| self.swap_fee_percentage = 0; | ||
| } | ||
|
|
||
| /// Transfers a given amount of a PSP22 token to a specified using the callers own balance | ||
|
|
@@ -427,30 +432,36 @@ mod simple_dex { | |
| .fire() | ||
| } | ||
|
|
||
| /// Swap trade output given a curve with equal token weights | ||
| /// | ||
| /// swap_fee_percentage (integer) is a percentage of the trade that goes towards the pool | ||
| /// B_0 - (100 * B_0 * B_i) / (100 * (B_i + A_i) -A_i * fee) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just FYI, apparently Balancer deals with 0.0001 fee precision: https://docs.balancer.fi/getting-started/faqs/fees#what-are-the-fees-for-a-trade |
||
| fn out_given_in( | ||
| amount_token_in: Balance, | ||
| balance_token_in: Balance, | ||
| balance_token_out: Balance, | ||
| swap_fee: u128, | ||
| swap_fee_percentage: u128, | ||
| ) -> Result<Balance, DexError> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to have unit tests for this function. It's standalone (i.e. not hanging off of the contract) so should be relatively easy. |
||
| let op0 = amount_token_in | ||
| .checked_mul(swap_fee) | ||
| .and_then(|result| result.checked_div(100)) | ||
| .checked_mul(swap_fee_percentage) | ||
| .ok_or(DexError::Arithmethic)?; | ||
|
|
||
| let op1 = balance_token_in | ||
| .checked_add(amount_token_in) | ||
| .and_then(|result| result.checked_sub(op0)) | ||
| .and_then(|result| result.checked_mul(100)) | ||
| .ok_or(DexError::Arithmethic)?; | ||
|
|
||
| let op2 = balance_token_out | ||
| .checked_mul(balance_token_in) | ||
| let op2 = op1.checked_sub(op0).ok_or(DexError::Arithmethic)?; | ||
|
|
||
| let op3 = balance_token_in | ||
| .checked_mul(balance_token_out) | ||
| .and_then(|result| result.checked_mul(100)) | ||
| .ok_or(DexError::Arithmethic)?; | ||
|
|
||
| let op3 = op2.checked_div(op1).ok_or(DexError::Arithmethic)?; | ||
| let op4 = op3.checked_div(op2).ok_or(DexError::Arithmethic)?; | ||
|
|
||
| balance_token_out | ||
| .checked_sub(op3) | ||
| .checked_sub(op4) | ||
| .ok_or(DexError::Arithmethic) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it have to be
&mut?