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

Proxy increase energy #778

Merged
merged 7 commits into from
Oct 2, 2023
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
45 changes: 44 additions & 1 deletion locked-asset/energy-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ pub trait SimpleLockEnergy:
let dest_address = self.dest_from_optional(opt_destination);
let current_epoch = self.blockchain().get_block_epoch();
let unlock_epoch = self.unlock_epoch_to_start_of_month(current_epoch + lock_epochs);

require!(
unlock_epoch > current_epoch,
"Unlock epoch must be greater than the current epoch"
Expand Down Expand Up @@ -184,4 +183,48 @@ pub trait SimpleLockEnergy:

output_payment
}

/// Used internally by proxy-dex
#[payable("*")]
#[endpoint(extendLockPeriod)]
fn extend_lock_period(&self, lock_epochs: Epoch, user: ManagedAddress) -> EsdtTokenPayment {
self.require_not_paused();
self.require_is_listed_lock_option(lock_epochs);

let caller = self.blockchain().get_caller();
require!(
self.token_transfer_whitelist().contains(&caller),
"May not call this endpoint. Use lockTokens instead"
);

let payment = self.call_value().single_esdt();
self.locked_token()
.require_same_token(&payment.token_identifier);

let current_epoch = self.blockchain().get_block_epoch();
let unlock_epoch = self.unlock_epoch_to_start_of_month(current_epoch + lock_epochs);
require!(
unlock_epoch > current_epoch,
"Unlock epoch must be greater than the current epoch"
);

let output_tokens = self.update_energy(&user, |energy: &mut Energy<Self::Api>| {
self.extend_new_token_period(payment.clone(), unlock_epoch, current_epoch, energy)
});

self.send().esdt_local_burn(
&payment.token_identifier,
payment.token_nonce,
&payment.amount,
);

self.send().direct_esdt(
&caller,
&output_tokens.token_identifier,
output_tokens.token_nonce,
&output_tokens.amount,
);

output_tokens
}
}
20 changes: 15 additions & 5 deletions locked-asset/proxy_dex/src/energy_update.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
multiversx_sc::imports!();

use common_structs::Nonce;
use energy_factory::locked_token_transfer::ProxyTrait as _;
use common_structs::{Epoch, Nonce};
use energy_factory::{locked_token_transfer::ProxyTrait as _, ProxyTrait as _};
use energy_query::Energy;
use simple_lock::locked_token::LockedTokenAttributes;

Expand Down Expand Up @@ -46,9 +46,6 @@ pub trait EnergyUpdateModule:
.get_token_attributes(token_id, token_nonce);
energy.update_after_unlock_any(token_amount, attributes.unlock_epoch, current_epoch);
} else if token_id == &old_locked_token_id {
if self.blockchain().is_smart_contract(user) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed. Now contracts can have energy.

return;
}
let attributes = self.decode_legacy_token(token_id, token_nonce);
let epoch_amount_pairs = attributes.get_unlock_amounts_per_epoch(token_amount);
for pair in epoch_amount_pairs.pairs {
Expand All @@ -62,6 +59,19 @@ pub trait EnergyUpdateModule:
self.set_energy_in_factory(user.clone(), energy, energy_factory_addr);
}

fn call_increase_energy(
&self,
user: ManagedAddress,
old_tokens: EsdtTokenPayment,
lock_epochs: Epoch,
energy_factory_addr: ManagedAddress,
) -> EsdtTokenPayment {
self.energy_factory_proxy(energy_factory_addr)
.extend_lock_period(lock_epochs, user)
.with_esdt_transfer(old_tokens)
.execute_on_dest_context()
}

fn set_energy_in_factory(
&self,
user: ManagedAddress,
Expand Down
83 changes: 83 additions & 0 deletions locked-asset/proxy_dex/src/proxy_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use common_structs::Epoch;
use fixed_supply_token::FixedSupplyToken;

use crate::{
Expand Down Expand Up @@ -321,6 +322,88 @@ pub trait ProxyFarmModule:
(new_wrapped_token, claim_result.rewards).into()
}

#[payable("*")]
#[endpoint(increaseProxyFarmTokenEnergy)]
fn increase_proxy_farm_token_energy_endpoint(&self, lock_epochs: Epoch) -> EsdtTokenPayment {
self.require_wrapped_farm_token_id_not_empty();
self.require_wrapped_lp_token_id_not_empty();

let wrapped_farm_token_mapper = self.wrapped_farm_token();
let payment = self.call_value().single_esdt();
wrapped_farm_token_mapper.require_same_token(&payment.token_identifier);

let wrapped_farm_attributes: WrappedFarmTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&payment, &wrapped_farm_token_mapper);

let caller = self.blockchain().get_caller();
let new_locked_token_id = self.get_locked_token_id();
let wrapped_lp_token_id = self.wrapped_lp_token().get_token_id();

let new_attributes = if wrapped_farm_attributes.proxy_farming_token.token_identifier
== new_locked_token_id
{
let energy_factory_addr = self.energy_factory_address().get();
let new_locked_token = self.call_increase_energy(
caller.clone(),
wrapped_farm_attributes.proxy_farming_token,
lock_epochs,
energy_factory_addr,
);

WrappedFarmTokenAttributes {
farm_token: wrapped_farm_attributes.farm_token,
proxy_farming_token: new_locked_token,
}
} else if wrapped_farm_attributes.proxy_farming_token.token_identifier
== wrapped_lp_token_id
{
let wrapped_lp_mapper = self.wrapped_lp_token();
let wrapped_lp_attributes: WrappedLpTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&payment, &wrapped_lp_mapper);
let new_locked_tokens = self.increase_proxy_pair_token_energy(
caller.clone(),
lock_epochs,
&wrapped_lp_attributes,
);

let new_wrapped_lp_attributes = WrappedLpTokenAttributes {
locked_tokens: new_locked_tokens,
lp_token_id: wrapped_lp_attributes.lp_token_id,
lp_token_amount: wrapped_lp_attributes.lp_token_amount,
};

let new_token_amount = new_wrapped_lp_attributes.get_total_supply();
let new_wrapped_lp = wrapped_lp_mapper.nft_create_and_send(
&caller,
new_token_amount,
&new_wrapped_lp_attributes,
);

self.send().esdt_local_burn(
&wrapped_farm_attributes.proxy_farming_token.token_identifier,
wrapped_farm_attributes.proxy_farming_token.token_nonce,
&wrapped_farm_attributes.proxy_farming_token.amount,
);

WrappedFarmTokenAttributes {
farm_token: wrapped_farm_attributes.farm_token,
proxy_farming_token: new_wrapped_lp,
}
} else {
sc_panic!(INVALID_PAYMENTS_ERR_MSG)
};

self.send().esdt_local_burn(
&payment.token_identifier,
payment.token_nonce,
&payment.amount,
);

let new_token_amount = new_attributes.get_total_supply();

wrapped_farm_token_mapper.nft_create_and_send(&caller, new_token_amount, &new_attributes)
}

fn require_wrapped_farm_token_id_not_empty(&self) {
require!(!self.wrapped_farm_token().is_empty(), "Empty token id");
}
Expand Down
54 changes: 54 additions & 0 deletions locked-asset/proxy_dex/src/proxy_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::wrapped_lp_attributes::{WrappedLpToken, WrappedLpTokenAttributes};
use common_structs::Epoch;
use fixed_supply_token::FixedSupplyToken;

#[multiversx_sc::module]
Expand Down Expand Up @@ -221,6 +222,59 @@ pub trait ProxyPairModule:
output_payments.into()
}

#[payable("*")]
#[endpoint(increaseProxyPairTokenEnergy)]
fn increase_proxy_pair_token_energy_endpoint(&self, lock_epochs: Epoch) -> EsdtTokenPayment {
self.require_wrapped_lp_token_id_not_empty();

let payment = self.call_value().single_esdt();
let wrapped_lp_mapper = self.wrapped_lp_token();
wrapped_lp_mapper.require_same_token(&payment.token_identifier);

let caller = self.blockchain().get_caller();
let old_attributes: WrappedLpTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&payment, &wrapped_lp_mapper);

let new_locked_tokens =
self.increase_proxy_pair_token_energy(caller.clone(), lock_epochs, &old_attributes);
let new_token_attributes = WrappedLpTokenAttributes {
locked_tokens: new_locked_tokens,
lp_token_id: old_attributes.lp_token_id,
lp_token_amount: old_attributes.lp_token_amount,
};

self.send().esdt_local_burn(
&payment.token_identifier,
payment.token_nonce,
&payment.amount,
);

let new_token_amount = new_token_attributes.get_total_supply();

wrapped_lp_mapper.nft_create_and_send(&caller, new_token_amount, &new_token_attributes)
}

fn increase_proxy_pair_token_energy(
&self,
user: ManagedAddress,
lock_epochs: Epoch,
old_attributes: &WrappedLpTokenAttributes<Self::Api>,
) -> EsdtTokenPayment {
let new_locked_token_id = self.get_locked_token_id();
require!(
old_attributes.locked_tokens.token_identifier == new_locked_token_id,
"Invalid payment"
);

let energy_factory_addr = self.energy_factory_address().get();
self.call_increase_energy(
user,
old_attributes.locked_tokens.clone(),
lock_epochs,
energy_factory_addr,
)
}

fn require_wrapped_lp_token_id_not_empty(&self) {
require!(!self.wrapped_lp_token().is_empty(), "Empty token id");
}
Expand Down
2 changes: 1 addition & 1 deletion locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
farm_locked_builder: FarmLockedObjBuilder,
simple_lock_builder: SimpleLockObjBuilder,
) -> Self {
let _ = DebugApi::dummy();
DebugApi::dummy();

let rust_zero = rust_biguint!(0);
let mut b_mock = BlockchainStateWrapper::new();
Expand Down
Loading
Loading