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

Feat/concentrated liq #839

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
41 changes: 41 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ members = [
"dex",
"dex/farm",
"dex/farm/meta",
"dex/farm-concentrated-liq",
"dex/farm-concentrated-liq/meta",
"dex/farm-with-locked-rewards",
"dex/farm-with-locked-rewards/meta",
"dex/pair",
Expand Down
31 changes: 0 additions & 31 deletions common/modules/farm/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,6 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio
}
}

fn is_old_farm_position(&self, token_nonce: Nonce) -> bool {
let farm_position_migration_nonce = self.farm_position_migration_nonce().get();
token_nonce > 0 && token_nonce < farm_position_migration_nonce
}

fn try_set_farm_position_migration_nonce(
&self,
farm_token_mapper: NonFungibleTokenMapper<Self::Api>,
) {
if !self.farm_position_migration_nonce().is_empty() {
return;
}

let migration_farm_token_nonce = if farm_token_mapper.get_token_state().is_set() {
let token_identifier = farm_token_mapper.get_token_id_ref();
let current_nonce = self
.blockchain()
.get_current_esdt_nft_nonce(&self.blockchain().get_sc_address(), token_identifier);
current_nonce + DEFAULT_FARM_POSITION_MIGRATION_NONCE
} else {
DEFAULT_FARM_POSITION_MIGRATION_NONCE
};

self.farm_position_migration_nonce()
.set(migration_farm_token_nonce);
}

#[view(getFarmingTokenId)]
#[storage_mapper("farming_token_id")]
fn farming_token_id(&self) -> SingleValueMapper<TokenIdentifier>;
Expand Down Expand Up @@ -125,8 +98,4 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio
&self,
user: &ManagedAddress,
) -> SingleValueMapper<UserTotalFarmPosition<Self::Api>>;

#[view(getFarmPositionMigrationNonce)]
#[storage_mapper("farm_position_migration_nonce")]
fn farm_position_migration_nonce(&self) -> SingleValueMapper<Nonce>;
}
6 changes: 3 additions & 3 deletions common/modules/farm/farm_base_impl/src/base_farm_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pub trait BaseFarmInitModule:

self.state().set(State::Inactive);
self.division_safety_constant()
.set_if_empty(&division_safety_constant);
.set(&division_safety_constant);

self.reward_token_id().set_if_empty(&reward_token_id);
self.farming_token_id().set_if_empty(&farming_token_id);
self.reward_token_id().set(&reward_token_id);
self.farming_token_id().set(&farming_token_id);

if !owner.is_zero() {
self.add_permissions(owner, Permissions::OWNER | Permissions::PAUSE);
Expand Down
58 changes: 26 additions & 32 deletions common/modules/farm/farm_base_impl/src/base_traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,38 @@ pub trait FarmContract {
) -> BigUint<<Self::FarmSc as ContractBase>::Api> {
let current_block_nonce = sc.blockchain().get_block_nonce();
let last_reward_nonce = sc.last_reward_block_nonce().get();
if current_block_nonce > last_reward_nonce {
let to_mint =
Self::calculate_per_block_rewards(sc, current_block_nonce, last_reward_nonce);
if to_mint != 0 {
Self::mint_rewards(sc, token_id, &to_mint);
}

sc.last_reward_block_nonce().set(current_block_nonce);
if current_block_nonce <= last_reward_nonce {
return BigUint::zero();
}

to_mint
} else {
BigUint::zero()
let to_mint = Self::calculate_per_block_rewards(sc, current_block_nonce, last_reward_nonce);
if to_mint != 0 {
Self::mint_rewards(sc, token_id, &to_mint);
}

sc.last_reward_block_nonce().set(current_block_nonce);

to_mint
}

fn generate_aggregated_rewards(
sc: &Self::FarmSc,
storage_cache: &mut StorageCache<Self::FarmSc>,
) {
let total_reward = Self::mint_per_block_rewards(sc, &storage_cache.reward_token_id);
if total_reward > 0u64 {
storage_cache.reward_reserve += &total_reward;
if total_reward == 0u64 {
return;
}

if storage_cache.farm_token_supply != 0u64 {
let increase = (&total_reward * &storage_cache.division_safety_constant)
/ &storage_cache.farm_token_supply;
storage_cache.reward_per_share += &increase;
}
storage_cache.reward_reserve += &total_reward;

if storage_cache.farm_token_supply == 0u64 {
return;
}

let increase = (&total_reward * &storage_cache.division_safety_constant)
/ &storage_cache.farm_token_supply;
storage_cache.reward_per_share += &increase;
}

fn calculate_rewards(
Expand All @@ -102,12 +105,12 @@ pub trait FarmContract {
storage_cache: &StorageCache<Self::FarmSc>,
) -> BigUint<<Self::FarmSc as ContractBase>::Api> {
let token_rps = token_attributes.get_reward_per_share();
if storage_cache.reward_per_share > token_rps {
let rps_diff = &storage_cache.reward_per_share - &token_rps;
farm_token_amount * &rps_diff / &storage_cache.division_safety_constant
} else {
BigUint::zero()
if storage_cache.reward_per_share <= token_rps {
return BigUint::zero();
}

let rps_diff = &storage_cache.reward_per_share - &token_rps;
farm_token_amount * &rps_diff / &storage_cache.division_safety_constant
}

fn create_enter_farm_initial_attributes(
Expand Down Expand Up @@ -198,10 +201,6 @@ pub trait FarmContract {
for farm_position in farm_positions {
farm_token_mapper.require_same_token(&farm_position.token_identifier);

if sc.is_old_farm_position(farm_position.token_nonce) {
continue;
}

let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

Expand All @@ -212,7 +211,6 @@ pub trait FarmContract {
}
}

#[inline]
fn increase_user_farm_position(
sc: &Self::FarmSc,
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
Expand All @@ -228,10 +226,6 @@ pub trait FarmContract {
sc: &Self::FarmSc,
farm_position: &EsdtTokenPayment<<Self::FarmSc as ContractBase>::Api>,
) {
if sc.is_old_farm_position(farm_position.token_nonce) {
return;
}

let farm_token_mapper = sc.farm_token();
let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);
Expand Down
91 changes: 91 additions & 0 deletions dex/farm-concentrated-liq/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
[package]
name = "farm-concentrated-liq"
version = "0.0.0"
authors = ["MultiversX <[email protected]>"]
edition = "2021"
publish = false

[lib]
path = "src/lib.rs"

[dependencies.farm_base_impl]
path = "../../common/modules/farm/farm_base_impl"

[dependencies.config]
path = "../../common/modules/farm/config"

[dependencies.farm_token]
path = "../../common/modules/farm/farm_token"

[dependencies.rewards]
path = "../../common/modules/farm/rewards"

[dependencies.events]
path = "../../common/modules/farm/events"

[dependencies.contexts]
path = "../../common/modules/farm/contexts"

[dependencies.token_send]
path = "../../common/modules/token_send"

[dependencies.utils]
path = "../../common/modules/utils"

[dependencies.pausable]
path = "../../common/modules/pausable"

[dependencies.permissions_module]
path = "../../common/modules/permissions_module"

[dependencies.sc_whitelist_module]
path = "../../common/modules/sc_whitelist_module"

[dependencies.pair]
path = "../pair"

[dependencies.common_structs]
path = "../../common/common_structs"

[dependencies.common_errors]
path = "../../common/common_errors"

[dependencies.mergeable]
path = "../../common/traits/mergeable"

[dependencies.fixed-supply-token]
path = "../../common/traits/fixed-supply-token"

[dependencies.farm-boosted-yields]
path = "../../energy-integration/farm-boosted-yields"

[dependencies.week-timekeeping]
path = "../../energy-integration/common-modules/week-timekeeping"

[dependencies.weekly-rewards-splitting]
path = "../../energy-integration/common-modules/weekly-rewards-splitting"

[dependencies.energy-query]
path = "../../energy-integration/common-modules/energy-query"

[dependencies.multiversx-sc]
version = "=0.46.1"
features = ["esdt-token-payment-legacy-decode"]

[dependencies.multiversx-sc-modules]
version = "=0.46.1"

[dev-dependencies]
num-bigint = "0.4.2"

[dev-dependencies.energy-update]
path = "../../energy-integration/energy-update"

[dev-dependencies.multiversx-sc-scenario]
version = "=0.46.1"

[dev-dependencies.energy-factory-mock]
path = "../../energy-integration/energy-factory-mock"

[dev-dependencies.simple-lock]
path = "../../locked-asset/simple-lock"
13 changes: 13 additions & 0 deletions dex/farm-concentrated-liq/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "farm-concentrated-liq-abi"
version = "0.0.0"
authors = ["MultiversX <[email protected]>"]
edition = "2021"
publish = false

[dependencies.farm-concentrated-liq]
path = ".."

[dependencies.multiversx-sc-meta]
version = "0.46.1"
default-features = false
3 changes: 3 additions & 0 deletions dex/farm-concentrated-liq/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta::cli_main::<farm_concentrated_liq::AbiProvider>();
}
3 changes: 3 additions & 0 deletions dex/farm-concentrated-liq/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
Loading
Loading