Skip to content
Merged
Show file tree
Hide file tree
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
added swap pairs
  • Loading branch information
fbielejec committed Sep 23, 2022
commit 65be1ac32cc3a1aedf6a549526c99511b4c966cd
2 changes: 1 addition & 1 deletion contracts/access_control/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/game_token/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 72 additions & 26 deletions contracts/simple_dex/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,24 @@ mod simple_dex {
reflect::ContractEventBase,
};
use ink_prelude::{format, string::String, vec, vec::Vec};
use ink_storage::traits::SpreadAllocate;
use openbrush::contracts::traits::errors::PSP22Error;
use ink_storage::traits::{PackedLayout, SpreadAllocate, SpreadLayout};
use openbrush::{contracts::traits::errors::PSP22Error, storage::Mapping};

type Event = <SimpleDex as ContractEventBase>::Type;

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode, SpreadLayout, PackedLayout)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct SwapPair {
pub from: AccountId,
pub to: AccountId,
}

impl SwapPair {
pub fn new(from: AccountId, to: AccountId) -> Self {
Self { from, to }
}
}

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum DexError {
Expand All @@ -42,7 +55,7 @@ mod simple_dex {
CrossContractCall(String),
TooMuchSlippage,
NotEnoughLiquidityOf(AccountId),
UnsupportedToken(AccountId),
UnsupportedSwapPair(SwapPair),
}

impl From<PSP22Error> for DexError {
Expand All @@ -58,7 +71,18 @@ mod simple_dex {
}

#[ink(event)]
#[derive(Debug)]
pub struct SwapPairAdded {
#[ink(topic)]
pair: SwapPair,
}

#[ink(event)]
pub struct SwapPairRemoved {
#[ink(topic)]
pair: SwapPair,
}

#[ink(event)]
pub struct Swapped {
caller: AccountId,
#[ink(topic)]
Expand All @@ -70,7 +94,6 @@ mod simple_dex {
}

#[ink(event)]
#[derive(Debug)]
pub struct SwapFeeSet {
#[ink(topic)]
caller: AccountId,
Expand All @@ -82,8 +105,8 @@ mod simple_dex {
pub struct SimpleDex {
pub swap_fee_percentage: u128,
pub access_control: AccountId,
// pool tokens
pub tokens: [AccountId; 4],
// a set of pairs that are availiable for swapping between
pub swap_pairs: Mapping<SwapPair, ()>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent to Mapping<AccountID, Vec<AccountID>>, correct?

I'm just wondering whether it wouldn't be cheaper to use the contract later on if instead of initializing a SwapPair we would do

swap_pairs
    .get(&token_in)
    .map(|tokens_out| tokens_out.find(&token_out).is_some())
    .or_else(false)

}

impl AccessControlled for SimpleDex {
Expand All @@ -92,7 +115,7 @@ mod simple_dex {

impl SimpleDex {
#[ink(constructor)]
pub fn new(tokens: [AccountId; 4]) -> Self {
pub fn new() -> Self {
let caller = Self::env().caller();
let code_hash = Self::env()
.own_code_hash()
Expand All @@ -109,7 +132,7 @@ mod simple_dex {
);

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

// 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));
let swap_pair = SwapPair::new(token_in, token_out);
if !self.swap_pairs.contains(&swap_pair) {
return Err(DexError::UnsupportedSwapPair(swap_pair));
}

// check allowance
Expand Down Expand Up @@ -199,10 +218,6 @@ 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 All @@ -226,10 +241,6 @@ 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 @@ -298,6 +309,42 @@ mod simple_dex {
self.access_control
}

/// Whitelists a token pair for swapping between
///
/// Token pair is understood as a swap between tokens in one direction
/// Can only be called by an Admin
#[ink(message)]
pub fn add_swap_pair(&mut self, from: AccountId, to: AccountId) -> Result<(), DexError> {
let caller = self.env().caller();
let this = self.env().account_id();
self.check_role(caller, Role::Admin(this))?;

let pair = SwapPair::new(from, to);
self.swap_pairs.insert(&pair, &());

Self::emit_event(self.env(), Event::SwapPairAdded(SwapPairAdded { pair }));

Ok(())
}

/// Blacklists a token pair from swapping
///
/// Token pair is understood as a swap between tokens in one direction
/// Can only be called by an Admin
#[ink(message)]
pub fn remove_swap_pair(&mut self, from: AccountId, to: AccountId) -> Result<(), DexError> {
let caller = self.env().caller();
let this = self.env().account_id();
self.check_role(caller, Role::Admin(this))?;

let pair = SwapPair::new(from, to);
self.swap_pairs.remove(&pair);

Self::emit_event(self.env(), Event::SwapPairRemoved(SwapPairRemoved { pair }));

Ok(())
}

/// Terminates the contract.
///
/// Can only be called by the contract's Owner.
Expand All @@ -317,9 +364,8 @@ mod simple_dex {
.map_err(|why| DexError::InkEnv(format!("Can't retrieve own code hash: {:?}", why)))
}

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

Expand Down