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
add new role
  • Loading branch information
fbielejec committed Sep 15, 2022
commit 9f71ad047a574b53f9b2bc686d1dc7d2c1c5537d
2 changes: 2 additions & 0 deletions contracts/access_control/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ mod access_control {
Owner(AccountId),
/// Indicates account can initialize a contract from a given code hash.
Initializer(Hash),
/// Indicates account can add liquidity to a DEX contract (call certain functions)
LiquidityProvider(AccountId),
}

#[ink(storage)]
Expand Down
118 changes: 102 additions & 16 deletions contracts/simple_dex/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ mod simple_dex {
};
use ink_env::{
call::{build_call, Call, ExecutionInput, Selector},
CallFlags, DefaultEnvironment, Environment as EnvironmentTrait, Error as InkEnvError,
CallFlags, DefaultEnvironment, Error as InkEnvError,
};
use ink_lang::{
codegen::{initialize_contract, EmitEvent},
reflect::ContractEventBase,
};
use ink_prelude::{format, string::String, vec};
use ink_storage::{traits::SpreadAllocate, Mapping};
use num::integer::Roots;
use openbrush::contracts::traits::errors::PSP22Error;

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

// |why: InkEnvError| {
// DexError::CrossContractCall(format!(
// "Calling access control has failed: {:?}",
// why
// ))
// },

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum DexError {
Expand Down Expand Up @@ -91,13 +99,8 @@ mod simple_dex {
access_control,
caller,
required_role,
|why: InkEnvError| {
DexError::CrossContractCall(format!(
"Calling access control has failed: {:?}",
why
))
},
DexError::MissingRole,
Self::cross_contract_call_error_handler,
Self::access_control_error_handler,
);

match role_check {
Expand Down Expand Up @@ -215,6 +218,89 @@ mod simple_dex {
Ok(())
}

/// LP: single-asset native deposit
/// Can be called only by an account carrying a LiquidityProvider role for this contract
#[ink(message, payable)]
pub fn native_asset_deposit(&mut self) -> Result<(), DexError> {
// calculates number of pool shares corresponding to a token deposit amount and current balance in the pool (assuming all other stay constant)
let pool_shares_issued = |deposit_amount: u128,
token_balance: u128,
total_liquidity: u128|
-> Result<u128, DexError> {
let op1 = deposit_amount
.checked_div(token_balance)
.ok_or(DexError::Arithmethic)?;

let op2 = op1.checked_add(1).ok_or(DexError::Arithmethic)?;

let op3 = op2.nth_root(4);

let op4 = op3.checked_sub(1).ok_or(DexError::Arithmethic)?;

let op5 = op4
.checked_mul(total_liquidity)
.ok_or(DexError::Arithmethic)?;

Ok(op5)
};

let total_liquidity = self.total_liquidity;
let caller = Self::env().caller();

let native_deposit = Self::env().transferred_value();
let native_balance = Self::env()
.balance()
.checked_sub(native_deposit)
.ok_or(DexError::Arithmethic)?;

let minted_liquidity =
pool_shares_issued(native_deposit, native_balance, total_liquidity)?;

// issue liquidity shares to the caller
let caller_liquidity = self
.liquidity
.get(&caller)
.unwrap_or(0)
.checked_add(minted_liquidity)
.ok_or(DexError::Arithmethic)?;
self.liquidity.insert(&caller, &caller_liquidity);

// increase total liquidity
self.total_liquidity = self
.total_liquidity
.checked_add(minted_liquidity)
.ok_or(DexError::Arithmethic)?;

Ok(())
}

/// Terminates the contract.
///
/// can only be called by the contract's Owner
#[ink(message, selector = 7)]
pub fn terminate(&mut self) -> Result<(), DexError> {
let caller = self.env().caller();
let this = self.env().account_id();

<Self as AccessControlled>::check_role(
self.access_control,
caller,
Role::Owner(this),
Self::cross_contract_call_error_handler,
Self::access_control_error_handler,
)?;

self.env().terminate_contract(caller)
}

/// Returns own code hash
#[ink(message)]
pub fn code_hash(&self) -> Result<Hash, DexError> {
self.env()
.own_code_hash()
.map_err(|why| DexError::InkEnv(format!("Can't retrieve own code hash: {:?}", why)))
}

// END: mesages

fn new_init(&mut self, ubik: AccountId, cyberiad: AccountId, lono: AccountId) {
Expand Down Expand Up @@ -322,19 +408,19 @@ mod simple_dex {
.ok_or(DexError::Arithmethic)
}

fn access_control_error_handler(role: Role) -> DexError {
DexError::MissingRole(role)
}

fn cross_contract_call_error_handler(why: InkEnvError) -> DexError {
DexError::CrossContractCall(format!("Calling access control has failed: {:?}", why))
}

// fn emit_event<EE>(emitter: EE, event: Event)
// where
// EE: EmitEvent<SimpleDex>,
// {
// emitter.emit_event(event);
// }

/// Returns own code hash
#[ink(message)]
pub fn code_hash(&self) -> Result<Hash, DexError> {
self.env()
.own_code_hash()
.map_err(|why| DexError::InkEnv(format!("Can't retrieve own code hash: {:?}", why)))
}
}
}