From 7bc1af117c538d5c7c1a301007ac64850cc45f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Obrok?= Date: Fri, 4 Nov 2022 17:54:22 +0100 Subject: [PATCH 1/2] Add simple_dex to deploy.sh --- contracts/scripts/deploy.sh | 73 ++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/contracts/scripts/deploy.sh b/contracts/scripts/deploy.sh index ea59c266fb..ef8b046726 100755 --- a/contracts/scripts/deploy.sh +++ b/contracts/scripts/deploy.sh @@ -159,6 +159,30 @@ function deploy_marketplace { eval "$__resultvar='$contract_address'" } +function deploy_simple_dex { + local __resultvar=$1 + + # --- CREATE AN INSTANCE OF THE CONTRACT + + cd "$CONTRACTS_PATH"/simple_dex + + local contract_address + contract_address=$(cargo contract instantiate --url "$NODE" --constructor new --suri "$AUTHORITY_SEED" --skip-confirm) + contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 14-) + + echo "Simple dex contract instance address: $contract_address" + + # --- GRANT PRIVILEGES ON THE CONTRACT + + cd "$CONTRACTS_PATH"/access_control + + cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Owner('"$contract_address"')' --suri "$AUTHORITY_SEED" --skip-confirm + cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Admin('"$contract_address"')' --suri "$AUTHORITY_SEED" --skip-confirm + cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'LiquidityProvider('"$contract_address"')' --suri "$AUTHORITY_SEED" --skip-confirm + + eval "$__resultvar='$contract_address'" +} + function link_bytecode() { local contract=$1 local placeholder=$2 @@ -190,6 +214,9 @@ cargo contract build --release cd "$CONTRACTS_PATH"/marketplace cargo contract build --release +cd "$CONTRACTS_PATH"/simple_dex +cargo contract build --release + # --- DEPLOY ACCESS CONTROL CONTRACT cd "$CONTRACTS_PATH"/access_control @@ -209,6 +236,7 @@ upload_contract TICKET_TOKEN_CODE_HASH ticket_token upload_contract GAME_TOKEN_CODE_HASH game_token upload_contract BUTTON_CODE_HASH button upload_contract MARKETPLACE_CODE_HASH marketplace +upload_contract SIMPLE_DEX_CODE_HASH simple_dex start=$(date +%s.%N) @@ -245,6 +273,9 @@ deploy_game_token THE_PRESSIAH_COMETH_TOKEN Lono LON $salt deploy_marketplace THE_PRESSIAH_COMETH_MARKETPLACE "$MARKETPLACE_CODE_HASH" the_pressiah_cometh "$salt" "$THE_PRESSIAH_COMETH_TICKET" "$THE_PRESSIAH_COMETH_TOKEN" deploy_button_game THE_PRESSIAH_COMETH ThePressiahCometh "$THE_PRESSIAH_COMETH_TICKET" "$THE_PRESSIAH_COMETH_TOKEN" "$THE_PRESSIAH_COMETH_MARKETPLACE" "$salt" +echo "Simple Dex" +deploy_simple_dex SIMPLE_DEX + # spit adresses to a JSON file cd "$CONTRACTS_PATH" @@ -266,24 +297,30 @@ jq -n --arg early_bird_special "$EARLY_BIRD_SPECIAL" \ --arg marketplace_code_hash "$MARKETPLACE_CODE_HASH" \ --arg access_control "$ACCESS_CONTROL" \ --arg access_control_code_hash "$ACCESS_CONTROL_CODE_HASH" \ - '{early_bird_special: $early_bird_special, - early_bird_special_marketplace: $early_bird_special_marketplace, - early_bird_special_ticket: $early_bird_special_ticket, - early_bird_special_token: $early_bird_special_token, - back_to_the_future: $back_to_the_future, - back_to_the_future_ticket: $back_to_the_future_ticket, - back_to_the_future_token: $back_to_the_future_token, - back_to_the_future_marketplace: $back_to_the_future_marketplace, - the_pressiah_cometh: $the_pressiah_cometh, - the_pressiah_cometh_ticket: $the_pressiah_cometh_ticket, - the_pressiah_cometh_token: $the_pressiah_cometh_token, - the_pressiah_cometh_marketplace: $the_pressiah_cometh_marketplace, - access_control: $access_control, - button_code_hash: $button_code_hash, - ticket_token_code_hash: $ticket_token_code_hash, - game_token_code_hash: $game_token_code_hash, - marketplace_code_hash: $marketplace_code_hash, - access_control_code_hash: $access_control_code_hash}' > addresses.json + --arg simple_dex "$SIMPLE_DEX" \ + --arg simple_dex_code_hash "$SIMPLE_DEX_CODE_HASH" \ + '{ + early_bird_special: $early_bird_special, + early_bird_special_marketplace: $early_bird_special_marketplace, + early_bird_special_ticket: $early_bird_special_ticket, + early_bird_special_token: $early_bird_special_token, + back_to_the_future: $back_to_the_future, + back_to_the_future_ticket: $back_to_the_future_ticket, + back_to_the_future_token: $back_to_the_future_token, + back_to_the_future_marketplace: $back_to_the_future_marketplace, + the_pressiah_cometh: $the_pressiah_cometh, + the_pressiah_cometh_ticket: $the_pressiah_cometh_ticket, + the_pressiah_cometh_token: $the_pressiah_cometh_token, + the_pressiah_cometh_marketplace: $the_pressiah_cometh_marketplace, + access_control: $access_control, + simple_dex: $simple_dex, + button_code_hash: $button_code_hash, + ticket_token_code_hash: $ticket_token_code_hash, + game_token_code_hash: $game_token_code_hash, + marketplace_code_hash: $marketplace_code_hash, + access_control_code_hash: $access_control_code_hash, + simple_dex_code_hash: $simple_dex_code_hash + }' > addresses.json end=`date +%s.%N` echo "Time elapsed: $( echo "$end - $start" | bc -l )" From 52aa5dce57e71f7d5d196724ab0273c897c2e861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Obrok?= Date: Wed, 9 Nov 2022 12:25:59 +0100 Subject: [PATCH 2/2] Expose out_given_in in simple_dex --- contracts/simple_dex/lib.rs | 95 +++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/contracts/simple_dex/lib.rs b/contracts/simple_dex/lib.rs index b058994c80..71748d217e 100644 --- a/contracts/simple_dex/lib.rs +++ b/contracts/simple_dex/lib.rs @@ -162,19 +162,11 @@ mod simple_dex { return Err(DexError::InsufficientAllowanceOf(token_in)); } - let balance_token_in = self.balance_of(token_in, this)?; - let balance_token_out = self.balance_of(token_out, this)?; - - 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( + let amount_token_out = self.out_given_in( + token_in, + token_out, amount_token_in, - balance_token_in, - balance_token_out, - self.swap_fee_percentage, + Some(min_amount_token_out), )?; if amount_token_out < min_amount_token_out { @@ -283,7 +275,7 @@ mod simple_dex { /// Returns current value of the swap_fee_percentage parameter #[ink(message)] - pub fn swap_fee_percentage(&mut self) -> Balance { + pub fn swap_fee_percentage(&self) -> Balance { self.swap_fee_percentage } @@ -365,6 +357,50 @@ mod simple_dex { .map_err(|why| DexError::InkEnv(format!("Can't retrieve own code hash: {:?}", why))) } + /// 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) + #[ink(message)] + pub fn out_given_in( + &self, + token_in: AccountId, + token_out: AccountId, + amount_token_in: Balance, + min_amount_token_out: Option, + ) -> Result { + let this = self.env().account_id(); + let balance_token_in = self.balance_of(token_in, this)?; + let balance_token_out = self.balance_of(token_out, this)?; + + if min_amount_token_out.map_or(false, |x| balance_token_out < x) { + // throw early if we cannot support this swap anyway due to liquidity being too low + return Err(DexError::NotEnoughLiquidityOf(token_out)); + } + + let op0 = amount_token_in + .checked_mul(self.swap_fee_percentage) + .ok_or(DexError::Arithmethic)?; + + let op1 = balance_token_in + .checked_add(amount_token_in) + .and_then(|result| result.checked_mul(100)) + .ok_or(DexError::Arithmethic)?; + + 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 op4 = op3.checked_div(op2).ok_or(DexError::Arithmethic)?; + + balance_token_out + .checked_sub(op4) + .ok_or(DexError::Arithmethic) + } + fn new_init(&mut self) { self.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY); self.swap_fee_percentage = 0; @@ -442,39 +478,6 @@ 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) - fn out_given_in( - amount_token_in: Balance, - balance_token_in: Balance, - balance_token_out: Balance, - swap_fee_percentage: u128, - ) -> Result { - let op0 = amount_token_in - .checked_mul(swap_fee_percentage) - .ok_or(DexError::Arithmethic)?; - - let op1 = balance_token_in - .checked_add(amount_token_in) - .and_then(|result| result.checked_mul(100)) - .ok_or(DexError::Arithmethic)?; - - 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 op4 = op3.checked_div(op2).ok_or(DexError::Arithmethic)?; - - balance_token_out - .checked_sub(op4) - .ok_or(DexError::Arithmethic) - } - fn access_control_error_handler(role: Role) -> DexError { DexError::MissingRole(role) }