Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c7c0e96
implementation sur course
fbielejec Sep 8, 2022
71d9c48
swaps done
fbielejec Sep 8, 2022
9f71ad0
add new role
fbielejec Sep 8, 2022
79b9e0a
added events
fbielejec Sep 8, 2022
974f056
wut
fbielejec Sep 8, 2022
ed2ad45
edited events
fbielejec Sep 8, 2022
56a4b04
set AC
fbielejec Sep 8, 2022
4476eaa
give why
fbielejec Sep 8, 2022
b12d883
docstring
fbielejec Sep 8, 2022
23f0cf3
modified docstring
fbielejec Sep 8, 2022
2012cfe
cleanup
fbielejec Sep 8, 2022
3be6987
Update contracts/simple_dex/lib.rs
fbielejec Sep 14, 2022
99952a4
simplified artihmetic
fbielejec Sep 14, 2022
e4d8211
moved roles to own mod
fbielejec Sep 15, 2022
1e31342
wZERO
fbielejec Sep 15, 2022
67cb4c8
dumbed down LP
fbielejec Sep 15, 2022
ff4e744
slippage check
fbielejec Sep 15, 2022
c2a36fc
check pool composition
fbielejec Sep 15, 2022
71523bf
explicitely check swap fee value that is set
fbielejec Sep 15, 2022
be7a316
change parameter name to avoid confusion, use integer operations
fbielejec Sep 16, 2022
b70278c
not payabale
fbielejec Sep 16, 2022
2c4213c
refactor
fbielejec Sep 16, 2022
d3fef1c
ok semver
fbielejec Sep 16, 2022
7210d54
refactor role check
fbielejec Sep 19, 2022
e767379
remove duplicated check
fbielejec Sep 19, 2022
ab507bf
Update contracts/wrapped_azero/lib.rs
fbielejec Sep 19, 2022
bed038f
remove unused dep
fbielejec Sep 19, 2022
bb854e9
Merge branch 'benjamin' into simple-dex
fbielejec Sep 20, 2022
00224a5
Update contracts/simple_dex/lib.rs
fbielejec Sep 23, 2022
40a42de
Update contracts/simple_dex/lib.rs
fbielejec Sep 23, 2022
65be1ac
added swap pairs
fbielejec Sep 23, 2022
9a67828
edited docstring
fbielejec Sep 23, 2022
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
Prev Previous commit
Next Next commit
check pool composition
  • Loading branch information
fbielejec committed Sep 15, 2022
commit c2a36fc7e5efff28f1bfa80a85a86929e9f72d1e
43 changes: 36 additions & 7 deletions contracts/simple_dex/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ mod simple_dex {
InkEnv(String),
CrossContractCall(String),
TooMuchSlippage,
NotEnoughLiquidityOf(AccountId),
UnsupportedToken(AccountId),
}

impl From<PSP22Error> for DexError {
Expand Down Expand Up @@ -79,6 +81,8 @@ mod simple_dex {
pub struct SimpleDex {
pub swap_fee: Balance,
pub access_control: AccountId,
// pool tokens
pub tokens: [AccountId; 4],
}

impl AccessControlled for SimpleDex {
Expand All @@ -87,9 +91,7 @@ mod simple_dex {

impl SimpleDex {
#[ink(constructor)]
pub fn new() -> Self {
// TODO : token addresses in the pool (4)

pub fn new(tokens: [AccountId; 4]) -> Self {
let caller = Self::env().caller();
let code_hash = Self::env()
.own_code_hash()
Expand All @@ -106,7 +108,7 @@ mod simple_dex {
);

match role_check {
Ok(_) => initialize_contract(|contract| Self::new_init(contract)),
Ok(_) => initialize_contract(|contract| Self::new_init(contract, tokens)),
Err(why) => panic!("Could not initialize the contract {:?}", why),
}
}
Expand All @@ -126,7 +128,14 @@ mod simple_dex {
let caller = self.env().caller();
let swap_fee = self.swap_fee;

// TODO : check if tokens are supported by the pool
// check if tokens are supported by the pool
if !self.tokens.contains(&token_in) {
return Err(DexError::UnsupportedToken(token_in));
}

if !self.tokens.contains(&token_out) {
return Err(DexError::UnsupportedToken(token_out));
}

// check allowance
if self.allowance(token_in, caller, this)? < amount_token_in {
Expand All @@ -136,7 +145,10 @@ mod simple_dex {
let balance_token_in = self.balance_of(token_in, this)?;
let balance_token_out = self.balance_of(token_out, this)?;

// TODO : check if we have enough liquidity in the pool
if balance_token_out < min_amount_token_out {
// throw early if we cannot support this swap anyway due to liquidity being too low
return Err(DexError::NotEnoughLiquidityOf(token_out));
}

let amount_token_out = Self::out_given_in(
amount_token_in,
Expand All @@ -145,7 +157,14 @@ mod simple_dex {
swap_fee,
)?;

if balance_token_out < amount_token_out {
// liquidity too low
return Err(DexError::NotEnoughLiquidityOf(token_out));
}

if amount_token_out < min_amount_token_out {
// thrown if too much slippage occured before this tx gets executed
// as a sandwitch atack prevention
return Err(DexError::TooMuchSlippage);
}

Expand Down Expand Up @@ -191,6 +210,10 @@ mod simple_dex {
deposits
.into_iter()
.try_for_each(|(token_in, amount)| -> Result<(), DexError> {
if !self.tokens.contains(&token_in) {
return Err(DexError::UnsupportedToken(token_in));
}

// transfer token_in from the caller to the contract
// will revert if the contract does not have enough allowance from the caller
// in which case the whole tx is reverted
Expand Down Expand Up @@ -220,6 +243,10 @@ mod simple_dex {

withdrawals.into_iter().try_for_each(
|(token_out, amount)| -> Result<(), DexError> {
if !self.tokens.contains(&token_out) {
return Err(DexError::UnsupportedToken(token_out));
}

// transfer token_out from the contract to the caller
self.transfer_tx(token_out, caller, amount)??;
Ok(())
Expand Down Expand Up @@ -317,8 +344,10 @@ mod simple_dex {
.map_err(|why| DexError::InkEnv(format!("Can't retrieve own code hash: {:?}", why)))
}

fn new_init(&mut self) {
fn new_init(&mut self, tokens: [AccountId; 4]) {
self.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY);
self.tokens = tokens;
self.swap_fee = 0;
}

/// Transfers a given amount of a PSP22 token to a specified using the callers own balance
Expand Down