Skip to content
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

Price discovery restrictions #292

Merged
merged 13 commits into from
Mar 2, 2022
1 change: 1 addition & 0 deletions price-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "src/lib.rs"

[dependencies.elrond-wasm]
version = "0.28.0"
features = ["cb_closure_managed_deser"]

[dependencies]
hex-literal = "0.3.1"
Expand Down
20 changes: 14 additions & 6 deletions price-discovery/src/common_storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
elrond_wasm::imports!();

pub const MAX_PERCENTAGE: u64 = 10_000_000_000_000; // 100%

#[elrond_wasm::module]
pub trait CommonStorageModule {
#[view(getLaunchedTokenId)]
Expand All @@ -26,11 +28,17 @@ pub trait CommonStorageModule {
#[storage_mapper("totalLpTokensReceived")]
fn total_lp_tokens_received(&self) -> SingleValueMapper<BigUint>;

#[view(getStartEpoch)]
#[storage_mapper("startEpoch")]
fn start_epoch(&self) -> SingleValueMapper<u64>;
#[storage_mapper("extraLpTokens")]
fn extra_lp_tokens(&self) -> SingleValueMapper<BigUint>;

#[view(getStartBlock)]
#[storage_mapper("startBlock")]
fn start_block(&self) -> SingleValueMapper<u64>;

#[view(getEndBlock)]
#[storage_mapper("endBlock")]
fn end_block(&self) -> SingleValueMapper<u64>;

#[view(getEndEpoch)]
#[storage_mapper("endEpoch")]
fn end_epoch(&self) -> SingleValueMapper<u64>;
#[storage_mapper("accumulatedPenalty")]
fn accumulated_penalty(&self, redeem_token_nonce: u64) -> SingleValueMapper<BigUint>;
}
66 changes: 58 additions & 8 deletions price-discovery/src/create_pool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::redeem_token::{ACCEPTED_TOKEN_REDEEM_NONCE, LAUNCHED_TOKEN_REDEEM_NONCE};

elrond_wasm::imports!();

mod liquidity_pool_proxy {
Expand Down Expand Up @@ -42,30 +44,38 @@ pub trait CreatePoolModule: crate::common_storage::CommonStorageModule {

let launched_token_balance = self.blockchain().get_sc_balance(&launched_token_id, 0);
let accepted_token_balance = self.blockchain().get_sc_balance(&accepted_token_id, 0);
let launched_token_accumulated_penalty =
self.accumulated_penalty(LAUNCHED_TOKEN_REDEEM_NONCE).get();
let accepted_token_accumulated_penalty =
self.accumulated_penalty(ACCEPTED_TOKEN_REDEEM_NONCE).get();

require!(
launched_token_balance > 0,
"No Launched tokens were deposited"
);
require!(accepted_token_balance > 0, "No users deposited tokens");

let launched_token_final_amount =
&launched_token_balance - &launched_token_accumulated_penalty;
let accepted_token_final_amount =
&accepted_token_balance - &accepted_token_accumulated_penalty;
self.launched_token_final_amount()
.set(&launched_token_balance);
.set(&launched_token_final_amount);
self.accepted_token_final_amount()
.set(&accepted_token_balance);
.set(&accepted_token_final_amount);

let mut payments = ManagedVec::<Self::Api, EsdtTokenPayment<Self::Api>>::new();
payments.push(EsdtTokenPayment {
token_type: EsdtTokenType::Fungible,
token_identifier: launched_token_id,
token_nonce: 0,
amount: launched_token_balance,
amount: launched_token_balance.clone(),
});
payments.push(EsdtTokenPayment {
token_type: EsdtTokenType::Fungible,
token_identifier: accepted_token_id,
token_nonce: 0,
amount: accepted_token_balance,
amount: accepted_token_balance.clone(),
});

let dex_sc_address = self.dex_sc_address().get();
Expand All @@ -74,16 +84,53 @@ pub trait CreatePoolModule: crate::common_storage::CommonStorageModule {
.add_initial_liquidity(payments, OptionalValue::None);

let (lp_token, _, _) = contract_call.execute_on_dest_context().into_tuple();
let extra_lp_tokens = self.calculate_extra_lp_tokens(
&launched_token_balance,
&accepted_token_balance,
&launched_token_accumulated_penalty,
&accepted_token_accumulated_penalty,
&lp_token.amount,
);

self.lp_token_id().set(&lp_token.token_identifier);
self.total_lp_tokens_received().set(&lp_token.amount);
self.extra_lp_tokens().set(&extra_lp_tokens);
self.total_lp_tokens_received()
.set(&(lp_token.amount - extra_lp_tokens));

let current_epoch = self.blockchain().get_block_epoch();
self.pool_creation_epoch().set(&current_epoch);
}

// private

fn require_deposit_period_ended(&self) {
let current_epoch = self.blockchain().get_block_epoch();
let end_epoch = self.end_epoch().get();
require!(current_epoch >= end_epoch, "Deposit period has not ended");
let current_block = self.blockchain().get_block_nonce();
let end_block = self.end_block().get();
require!(current_block >= end_block, "Deposit period has not ended");
}

fn require_dex_address_set(&self) {
require!(!self.dex_sc_address().is_empty(), "Pair address not set");
}

fn calculate_extra_lp_tokens(
&self,
launched_token_final_amount: &BigUint,
accepted_token_final_amount: &BigUint,
launched_token_accumulated_penalty: &BigUint,
accepted_token_accumulated_penalty: &BigUint,
total_lp_tokens: &BigUint,
) -> BigUint {
let unusable_lp_tokens_for_launched_tokens = &(launched_token_accumulated_penalty
* total_lp_tokens)
/ launched_token_final_amount
/ 2u32;
let unusable_lp_tokens_for_accepted_tokens = &(accepted_token_accumulated_penalty
* total_lp_tokens)
/ accepted_token_final_amount
/ 2u32;

unusable_lp_tokens_for_launched_tokens + unusable_lp_tokens_for_accepted_tokens
}

#[proxy]
Expand All @@ -92,4 +139,7 @@ pub trait CreatePoolModule: crate::common_storage::CommonStorageModule {
#[view(getDexScAddress)]
#[storage_mapper("dexScAddress")]
fn dex_sc_address(&self) -> SingleValueMapper<ManagedAddress>;

#[storage_mapper("poolCreationEpoch")]
fn pool_creation_epoch(&self) -> SingleValueMapper<u64>;
}
Loading