-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #921 from multiversx/simple-lock-legacy-contract
Simple lock legacy contract
- Loading branch information
Showing
29 changed files
with
955 additions
and
971 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: On pull request, build contracts | ||
|
||
on: | ||
pull_request: | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
build: | ||
uses: multiversx/mx-sc-actions/.github/workflows/[email protected] | ||
with: | ||
image_tag: v7.0.0 | ||
package_whole_project_src: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
13 changes: 5 additions & 8 deletions
13
...ed-asset/simple-lock-whitelist/Cargo.toml → ...y-contracts/simple-lock-legacy/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...set/simple-lock-whitelist/meta/Cargo.toml → ...tracts/simple-lock-legacy/meta/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
multiversx_sc_meta::cli_main::<simple_lock_legacy::AbiProvider>(); | ||
} |
File renamed without changes.
61 changes: 61 additions & 0 deletions
61
legacy-contracts/simple-lock-legacy/src/basic_lock_unlock.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
multiversx_sc::imports!(); | ||
|
||
use crate::{ | ||
error_messages::{CANNOT_UNLOCK_YET_ERR_MSG, NO_PAYMENT_ERR_MSG}, | ||
locked_token::LockedTokenAttributes, | ||
}; | ||
|
||
#[multiversx_sc::module] | ||
pub trait BasicLockUnlock: | ||
crate::locked_token::LockedTokenModule | ||
{ | ||
fn unlock_and_send( | ||
&self, | ||
to: &ManagedAddress, | ||
payment: EsdtTokenPayment<Self::Api>, | ||
) -> EgldOrEsdtTokenPayment<Self::Api> { | ||
let out_payment = self.unlock_tokens(payment); | ||
self.send().direct( | ||
to, | ||
&out_payment.token_identifier, | ||
out_payment.token_nonce, | ||
&out_payment.amount, | ||
); | ||
|
||
out_payment | ||
} | ||
|
||
fn unlock_tokens( | ||
&self, | ||
payment: EsdtTokenPayment<Self::Api>, | ||
) -> EgldOrEsdtTokenPayment<Self::Api> { | ||
let locked_token_mapper = self.locked_token(); | ||
locked_token_mapper.require_same_token(&payment.token_identifier); | ||
|
||
let attributes: LockedTokenAttributes<Self::Api> = | ||
locked_token_mapper.get_token_attributes(payment.token_nonce); | ||
let current_epoch = self.blockchain().get_block_epoch(); | ||
require!( | ||
current_epoch >= attributes.unlock_epoch, | ||
CANNOT_UNLOCK_YET_ERR_MSG | ||
); | ||
|
||
locked_token_mapper.nft_burn(payment.token_nonce, &payment.amount); | ||
|
||
self.unlock_tokens_unchecked(payment, &attributes) | ||
} | ||
|
||
fn unlock_tokens_unchecked( | ||
&self, | ||
payment: EsdtTokenPayment<Self::Api>, | ||
attributes: &LockedTokenAttributes<Self::Api>, | ||
) -> EgldOrEsdtTokenPayment<Self::Api> { | ||
require!(payment.amount > 0, NO_PAYMENT_ERR_MSG); | ||
|
||
EgldOrEsdtTokenPayment::new( | ||
attributes.original_token_id.clone(), | ||
attributes.original_token_nonce, | ||
payment.amount, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub static NO_PAYMENT_ERR_MSG: &[u8] = b"No payment"; | ||
pub static CANNOT_UNLOCK_YET_ERR_MSG: &[u8] = b"Cannot unlock yet"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#![no_std] | ||
|
||
multiversx_sc::imports!(); | ||
|
||
pub mod basic_lock_unlock; | ||
pub mod error_messages; | ||
pub mod locked_token; | ||
pub mod proxy_farm; | ||
pub mod proxy_lp; | ||
|
||
#[multiversx_sc::contract] | ||
pub trait SimpleLockLegacy: | ||
basic_lock_unlock::BasicLockUnlock | ||
+ locked_token::LockedTokenModule | ||
+ proxy_lp::ProxyLpModule | ||
+ proxy_farm::ProxyFarmModule | ||
{ | ||
#[init] | ||
fn init(&self) {} | ||
|
||
#[upgrade] | ||
fn upgrade(&self) {} | ||
|
||
#[payable("*")] | ||
#[endpoint(unlockTokens)] | ||
fn unlock_tokens_endpoint( | ||
&self, | ||
opt_destination: OptionalValue<ManagedAddress>, | ||
) -> EgldOrEsdtTokenPayment<Self::Api> { | ||
let payment = self.call_value().single_esdt(); | ||
let dest_address = self.dest_from_optional(opt_destination); | ||
self.unlock_and_send(&dest_address, payment) | ||
} | ||
|
||
fn dest_from_optional(&self, opt_destination: OptionalValue<ManagedAddress>) -> ManagedAddress { | ||
match opt_destination { | ||
OptionalValue::Some(dest) => dest, | ||
OptionalValue::None => self.blockchain().get_caller(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
multiversx_sc::imports!(); | ||
multiversx_sc::derive_imports!(); | ||
|
||
#[derive(TypeAbi, TopEncode, TopDecode, NestedDecode, NestedEncode, PartialEq, Debug, Clone)] | ||
pub struct LockedTokenAttributes<M: ManagedTypeApi> { | ||
pub original_token_id: EgldOrEsdtTokenIdentifier<M>, | ||
pub original_token_nonce: u64, | ||
pub unlock_epoch: u64, | ||
} | ||
|
||
#[multiversx_sc::module] | ||
pub trait LockedTokenModule { | ||
#[view(getLockedTokenId)] | ||
#[storage_mapper("lockedTokenId")] | ||
fn locked_token(&self) -> NonFungibleTokenMapper; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
multiversx_sc::imports!(); | ||
multiversx_sc::derive_imports!(); | ||
|
||
use crate::error_messages::*; | ||
|
||
#[derive( | ||
TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Debug, Clone, Copy, | ||
)] | ||
pub enum FarmType { | ||
SimpleFarm, | ||
FarmWithLockedRewards, | ||
} | ||
|
||
#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Debug)] | ||
pub struct FarmProxyTokenAttributes<M: ManagedTypeApi> { | ||
pub farm_type: FarmType, | ||
pub farm_token_id: TokenIdentifier<M>, | ||
pub farm_token_nonce: u64, | ||
pub farming_token_id: TokenIdentifier<M>, | ||
pub farming_token_locked_nonce: u64, | ||
} | ||
|
||
#[multiversx_sc::module] | ||
pub trait ProxyFarmModule: | ||
crate::locked_token::LockedTokenModule + crate::proxy_lp::ProxyLpModule | ||
{ | ||
/// Output payments: the underlying farm tokens | ||
#[payable("*")] | ||
#[endpoint(exitFarmLockedToken)] | ||
fn exit_farm_locked_token(&self) -> EsdtTokenPayment { | ||
let payment: EsdtTokenPayment<Self::Api> = self.call_value().single_esdt(); | ||
let caller = self.blockchain().get_caller(); | ||
|
||
let farm_proxy_token_attributes: FarmProxyTokenAttributes<Self::Api> = | ||
self.validate_payment_and_get_farm_proxy_token_attributes(&payment); | ||
|
||
let _ = self.check_and_get_unlocked_lp_token( | ||
&self.lp_proxy_token().get_token_id(), | ||
farm_proxy_token_attributes.farming_token_locked_nonce, | ||
); | ||
|
||
self.send().esdt_local_burn( | ||
&self.lp_proxy_token().get_token_id(), | ||
farm_proxy_token_attributes.farming_token_locked_nonce, | ||
&payment.amount, | ||
); | ||
|
||
let output_token_payment = EsdtTokenPayment::new( | ||
farm_proxy_token_attributes.farm_token_id, | ||
farm_proxy_token_attributes.farm_token_nonce, | ||
payment.amount, | ||
); | ||
|
||
self.send().direct_esdt( | ||
&caller, | ||
&output_token_payment.token_identifier, | ||
output_token_payment.token_nonce, | ||
&output_token_payment.amount, | ||
); | ||
|
||
output_token_payment | ||
} | ||
|
||
/// Output payments: the underlying farm tokens | ||
#[payable("*")] | ||
#[endpoint(farmClaimRewardsLockedToken)] | ||
fn farm_claim_rewards_locked_token(&self) -> EsdtTokenPayment { | ||
self.exit_farm_locked_token() | ||
} | ||
|
||
fn validate_payment_and_get_farm_proxy_token_attributes( | ||
&self, | ||
payment: &EsdtTokenPayment<Self::Api>, | ||
) -> FarmProxyTokenAttributes<Self::Api> { | ||
require!(payment.amount > 0, NO_PAYMENT_ERR_MSG); | ||
|
||
let farm_proxy_token_mapper = self.farm_proxy_token(); | ||
farm_proxy_token_mapper.require_same_token(&payment.token_identifier); | ||
|
||
let farm_proxy_token_attributes: FarmProxyTokenAttributes<Self::Api> = | ||
farm_proxy_token_mapper.get_token_attributes(payment.token_nonce); | ||
|
||
farm_proxy_token_mapper.nft_burn(payment.token_nonce, &payment.amount); | ||
|
||
farm_proxy_token_attributes | ||
} | ||
|
||
#[view(getFarmProxyTokenId)] | ||
#[storage_mapper("farmProxyTokenId")] | ||
fn farm_proxy_token(&self) -> NonFungibleTokenMapper; | ||
} |
Oops, something went wrong.