Skip to content

A0-1140: Deploy simple dex #720

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

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
73 changes: 55 additions & 18 deletions contracts/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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"

Expand All @@ -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 )"
Expand Down
95 changes: 49 additions & 46 deletions contracts/simple_dex/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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<Balance>,
) -> Result<Balance, DexError> {
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;
Expand Down Expand Up @@ -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<Balance, DexError> {
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)
}
Expand Down