Skip to content

Commit

Permalink
Factory legacy contract
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Jul 16, 2024
1 parent d291936 commit 04dbeda
Show file tree
Hide file tree
Showing 27 changed files with 1,487 additions and 146 deletions.
22 changes: 21 additions & 1 deletion 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 @@ -45,6 +45,8 @@ members = [
"legacy-contracts/farm-v-13/meta",
"legacy-contracts/proxy-dex-legacy",
"legacy-contracts/proxy-dex-legacy/meta",
"legacy-contracts/factory-legacy",
"legacy-contracts/factory-legacy/meta",

"locked-asset/",
"locked-asset/distribution",
Expand Down
7 changes: 7 additions & 0 deletions legacy-contracts/factory-legacy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
*/target/

# The erdpy output
output
27 changes: 27 additions & 0 deletions legacy-contracts/factory-legacy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "factory-legacy"
publish = false
version = "0.0.0"
edition = "2018"

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

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

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

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

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

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

[dependencies.token_merge_helper]
path = "../../common/modules/token_merge_helper"
26 changes: 26 additions & 0 deletions legacy-contracts/factory-legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Locked Asset Factory Smart Contract

This document presents how one can deploy and configure a Locked Asset Factory Contract.
The bigger picture about what a Locked Asset Factory Contract can do can be found in the Repository's Root Readme.

## Deployment

The Locked Asset Factory contract can be deployed using `erdpy` and using the interraction snippets.

The init parameters are:

- asset_token_id. The TokenId of the asset that a locked asset represents. In case of Maiar Exchange it will be MEX.

- default_unlock_period. A vector of unlock milestones. This represents a period since each epoch in the vector will be added with a starting epoch.

The Contract requires LocalMint and LocalBurn for asset token.

## Creating and Forwarding SFTs

Before creating LockedAssetLockens, the owner has to issue those tokens using `issueLockedAssetToken` and after this he also has to give the NftCreate, NftAddQuantity and NftBurn roles to the contract unsing `setLocalRolesLockedAssetToken`.

The Contract has an endpoint `createAndForward` that can be called in order to request an amount of Locked MEX. Only those addresses in the `whitelisted_contracts` set can call this endpoint. This whitelist can be configured by the admin using `whitelist` and `removeWhitelist` endpoints.

## Unlocking MEX

A user that has Locked MEX can unlock it and can receive the Locked MEX "remaining" and the unlocked MEX amount. The newly created Locked MEX will have its unlock milestones re-calculated such that the percents unlocking schedule will be updated to the new locked amount. For example: if default_unlock_period is `0x000000000000000232`, `0x000000000000000432` it would mean that after `0000000000000002` epochs, should unlock `32`.to_dec() (`50`) percent of the amount. After the first unlock at epoch 3 let's say, the next unlock milestone will be recalculated as `0x000000000000000464`. Notice the `50%` become `100%`.
14 changes: 14 additions & 0 deletions legacy-contracts/factory-legacy/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "factory-legacy-meta"

version = "0.0.0"
authors = [ "you",]
edition = "2018"
publish = false

[dependencies.factory-legacy]
path = ".."

[dependencies.multiversx-sc-meta]
version = "0.50.5"
default-features = false
3 changes: 3 additions & 0 deletions legacy-contracts/factory-legacy/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::<factory_legacy::AbiProvider>();
}
4 changes: 4 additions & 0 deletions legacy-contracts/factory-legacy/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"language": "rust"
}

68 changes: 68 additions & 0 deletions legacy-contracts/factory-legacy/src/attr_ex_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use common_structs::LockedAssetTokenAttributes;
use common_structs::LockedAssetTokenAttributesEx;
use common_structs::UnlockMilestoneEx;
use common_structs::UnlockScheduleEx;

// 1% = 1_000;
pub const _PRECISION_EXTENDED: u64 = 100_000u64;

// From 1 to 1_000;
pub const PRECISION_EX_INCREASE: u64 = 1_000u64;

#[multiversx_sc::module]
pub trait AttrExHelper {
fn get_attributes_ex(
&self,
token_id: &TokenIdentifier,
token_nonce: u64,
attr_ex_activation: u64,
) -> LockedAssetTokenAttributesEx<Self::Api> {
let token_info = self.blockchain().get_esdt_token_data(
&self.blockchain().get_sc_address(),
token_id,
token_nonce,
);

if token_nonce < attr_ex_activation {
let attr = self
.serializer()
.top_decode_from_managed_buffer::<LockedAssetTokenAttributes<Self::Api>>(
&token_info.attributes,
);
self.convert_attr_to_attr_ex(&attr)
} else {
self.serializer()
.top_decode_from_managed_buffer::<LockedAssetTokenAttributesEx<Self::Api>>(
&token_info.attributes,
)
}
}

fn convert_attr_to_attr_ex(
&self,
attr: &LockedAssetTokenAttributes<Self::Api>,
) -> LockedAssetTokenAttributesEx<Self::Api> {
let mut new_milestones: ManagedVec<UnlockMilestoneEx> = ManagedVec::new();

for milestones in attr.unlock_schedule.unlock_milestones.iter() {
new_milestones.push(UnlockMilestoneEx {
unlock_epoch: milestones.unlock_epoch,
unlock_percent: (milestones.unlock_percent as u64) * PRECISION_EX_INCREASE,
});
}

LockedAssetTokenAttributesEx {
unlock_schedule: UnlockScheduleEx {
unlock_milestones: new_milestones,
},
is_merged: attr.is_merged,
}
}

#[view(getExtendedAttributesActivationNonce)]
#[storage_mapper("extended_attributes_activation_nonce")]
fn extended_attributes_activation_nonce(&self) -> SingleValueMapper<u64>;
}
49 changes: 49 additions & 0 deletions legacy-contracts/factory-legacy/src/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use common_structs::{Nonce, UnlockScheduleEx};

use crate::attr_ex_helper;

use super::locked_asset;

#[multiversx_sc::module]
pub trait CacheModule: locked_asset::LockedAssetModule + attr_ex_helper::AttrExHelper {
#[inline(always)]
fn get_sft_nonce_for_unlock_schedule(
&self,
unlock_schedule: &UnlockScheduleEx<Self::Api>,
) -> Option<Nonce> {
self.nonce_cache_ex().get(unlock_schedule)
}

#[view(getUnlockScheduleForSFTNonce)]
fn get_unlock_schedule_for_sft_nonce(
&self,
nonce: Nonce,
) -> Option<UnlockScheduleEx<Self::Api>> {
self.unlock_schedule_cache_ex().get(&nonce)
}

#[inline(always)]
fn cache_unlock_schedule_and_nonce(
&self,
unlock_schedule: &UnlockScheduleEx<Self::Api>,
nonce: Nonce,
) {
self.nonce_cache_ex().insert(unlock_schedule.clone(), nonce);
self.unlock_schedule_cache_ex()
.insert(nonce, unlock_schedule.clone());
}

#[view(getCacheSize)]
fn get_cache_size(&self) -> usize {
self.nonce_cache_ex().len()
}

#[storage_mapper("nonce_cache_ex")]
fn nonce_cache_ex(&self) -> MapMapper<UnlockScheduleEx<Self::Api>, Nonce>;

#[storage_mapper("unlock_schedule_cache_ex")]
fn unlock_schedule_cache_ex(&self) -> MapMapper<Nonce, UnlockScheduleEx<Self::Api>>;
}
123 changes: 123 additions & 0 deletions legacy-contracts/factory-legacy/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use common_structs::LockedAssetTokenAttributesEx;

#[derive(TypeAbi, TopEncode)]
pub struct CreateAndForwardEvent<M: ManagedTypeApi> {
caller: ManagedAddress<M>,
destination: ManagedAddress<M>,
locked_asset_token_id: TokenIdentifier<M>,
locked_asset_token_nonce: u64,
locked_asset_token_amount: BigUint<M>,
locked_assets_attributes: LockedAssetTokenAttributesEx<M>,
start_epoch: u64,
block: u64,
epoch: u64,
timestamp: u64,
}

#[derive(TypeAbi, TopEncode)]
pub struct UnlockAssetsEvent<M: ManagedTypeApi> {
caller: ManagedAddress<M>,
input_locked_assets_token_id: TokenIdentifier<M>,
input_locked_assets_token_nonce: u64,
input_locked_assets_token_amount: BigUint<M>,
output_locked_assets_token_id: TokenIdentifier<M>,
output_locked_assets_token_nonce: u64,
output_locked_assets_token_amount: BigUint<M>,
asset_token_id: TokenIdentifier<M>,
asset_token_amount: BigUint<M>,
input_assets_attributes: LockedAssetTokenAttributesEx<M>,
output_assets_attributes: LockedAssetTokenAttributesEx<M>,
block: u64,
epoch: u64,
timestamp: u64,
}

#[multiversx_sc::module]
pub trait EventsModule {
fn emit_create_and_forward_event(
self,
caller: &ManagedAddress,
destination: &ManagedAddress,
locked_asset_token_id: &TokenIdentifier,
locked_asset_token_nonce: u64,
locked_asset_token_amount: &BigUint,
locked_assets_attributes: &LockedAssetTokenAttributesEx<Self::Api>,
start_epoch: u64,
) {
let epoch = self.blockchain().get_block_epoch();
self.create_and_forward_event(
caller,
destination,
epoch,
&CreateAndForwardEvent {
caller: caller.clone(),
destination: destination.clone(),
locked_asset_token_id: locked_asset_token_id.clone(),
locked_asset_token_nonce,
locked_asset_token_amount: locked_asset_token_amount.clone(),
locked_assets_attributes: locked_assets_attributes.clone(),
start_epoch,
block: self.blockchain().get_block_nonce(),
epoch,
timestamp: self.blockchain().get_block_timestamp(),
},
)
}

fn emit_unlock_assets_event(
self,
caller: &ManagedAddress,
input_locked_assets_token_id: &TokenIdentifier,
input_locked_assets_token_nonce: u64,
input_locked_assets_token_amount: &BigUint,
output_locked_assets_token_id: &TokenIdentifier,
output_locked_assets_token_nonce: u64,
output_locked_assets_token_amount: &BigUint,
asset_token_id: &TokenIdentifier,
asset_token_amount: &BigUint,
input_assets_attributes: &LockedAssetTokenAttributesEx<Self::Api>,
output_assets_attributes: &LockedAssetTokenAttributesEx<Self::Api>,
) {
let epoch = self.blockchain().get_block_epoch();
self.unlock_assets_event(
caller,
epoch,
&UnlockAssetsEvent {
caller: caller.clone(),
input_locked_assets_token_id: input_locked_assets_token_id.clone(),
input_locked_assets_token_nonce,
input_locked_assets_token_amount: input_locked_assets_token_amount.clone(),
output_locked_assets_token_id: output_locked_assets_token_id.clone(),
output_locked_assets_token_nonce,
output_locked_assets_token_amount: output_locked_assets_token_amount.clone(),
asset_token_id: asset_token_id.clone(),
asset_token_amount: asset_token_amount.clone(),
input_assets_attributes: input_assets_attributes.clone(),
output_assets_attributes: output_assets_attributes.clone(),
block: self.blockchain().get_block_nonce(),
epoch,
timestamp: self.blockchain().get_block_timestamp(),
},
)
}

#[event("create_and_forward")]
fn create_and_forward_event(
self,
#[indexed] caller: &ManagedAddress,
#[indexed] destination: &ManagedAddress,
#[indexed] epoch: u64,
swap_event: &CreateAndForwardEvent<Self::Api>,
);

#[event("unlock_assets")]
fn unlock_assets_event(
self,
#[indexed] caller: &ManagedAddress,
#[indexed] epoch: u64,
swap_event: &UnlockAssetsEvent<Self::Api>,
);
}
Loading

0 comments on commit 04dbeda

Please sign in to comment.