forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 12
initialization of poel & i_asset: descriptions for types #137
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
Draft
phydy-dev
wants to merge
12
commits into
dev
Choose a base branch
from
poel
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6118f7e
initialization of poel & i_asset: decriptions for types
phydy-dev 30e96c0
added reward_distribution & barebone function signatures
phydy-dev d0a6962
poel implementations
phydy-dev 8aab7d9
Update poel.move
abgaryan1 e6e34f3
Update poel.move
abgaryan1 196609a
oracle integration and changes to previous comments. frew comments ye…
phydy-dev bdae234
Merge branch 'dev' into poel
phydy-dev 674ecc2
added comments
abgaryan1 305ed6b
added comments
abgaryan1 ec844e5
added comments
abgaryan1 34b0cfa
added comments
abgaryan1 d19dc3d
Update aptos-move/framework/supra-framework/sources/poel/iAsset.move
phydy-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
1 change: 1 addition & 0 deletions
1
aptos-move/framework/supra-framework/sources/configs/poel_config.move
This file contains hidden or 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 @@ | ||
| /// configs for poel |
971 changes: 971 additions & 0 deletions
971
aptos-move/framework/supra-framework/sources/poel/iAsset.move
Large diffs are not rendered by default.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
aptos-move/framework/supra-framework/sources/poel/iAsset.spec.move
This file contains hidden or 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,6 @@ | ||
| spec supra_framework::iAsset { | ||
|
|
||
| spec module { | ||
| pragma verify = true; | ||
| } | ||
| } |
748 changes: 748 additions & 0 deletions
748
aptos-move/framework/supra-framework/sources/poel/poel.move
Large diffs are not rendered by default.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
aptos-move/framework/supra-framework/sources/poel/poel.spec.move
This file contains hidden or 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,6 @@ | ||
| spec supra_framework::poel { | ||
|
|
||
| spec module { | ||
| pragma verify = true; | ||
| } | ||
| } |
147 changes: 147 additions & 0 deletions
147
aptos-move/framework/supra-framework/sources/poel/reward_distribution.move
This file contains hidden or 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,147 @@ | ||
| /// This is the reward distribution module | ||
| /// it manages the reward metrics of the Proof of Efficiency Liquidity module and iAssets module | ||
| /// Its main purposes include: updating reward indexes, calculating user rewards, and updating user rewards | ||
|
|
||
|
|
||
| module supra_framework::reward_distribution { | ||
| use supra_framework::table; | ||
| use supra_framework::object::{Self, Object}; | ||
| use supra_framework::error; | ||
|
|
||
| use aptos_std::fungible_asset::Metadata; | ||
| use aptos_std::primary_fungible_store; | ||
| friend supra_framework::poel; | ||
|
|
||
| const ENO_AVAILABLE_DESIRABLE_LIQUIDITY: u64 = 1; | ||
|
|
||
| const DISTRIBUTABLE_REWARDS: vector<u8> = b"DistributableRewards"; | ||
|
|
||
| struct TotalLiquidityProvidedItems has store, copy { | ||
| ///reward_index_asset: Index tracking the reward distribution of the asset (u64). | ||
| reward_index_asset: u64, | ||
| ///available_rewards_for_asset: tracks the available rewards that are distributable for each asset | ||
| available_rewards_for_asset: u64, | ||
| } | ||
|
|
||
| #[resource_group_member(group = supra_framework::object::ObjectGroup)] | ||
| struct DistributableRewards has key { | ||
| // Maps AssetID to TotalLiquidityProvidedItems | ||
| total_liquidity_provided: table::Table<Object<Metadata>, TotalLiquidityProvidedItems>, | ||
| } | ||
|
|
||
|
|
||
| fun init_module(account: &signer) { | ||
| let constructor_ref = &object::create_named_object(account, DISTRIBUTABLE_REWARDS); | ||
|
|
||
| let obj_signer = &object::generate_signer(constructor_ref); | ||
| move_to(obj_signer, DistributableRewards { | ||
| total_liquidity_provided: table::new(), | ||
| }); | ||
| } | ||
|
|
||
| ///Update_reward_index(asseID, rewards): | ||
| ///Purpose: Updates the reward index for an asset based on newly distributed rewards. The function is applied for each asset after rewards are withdrawn from | ||
| ///the delegation pools at the end of each OLC, updating the asset's global reward index | ||
| public(friend) fun update_reward_index( | ||
| asset: Object<Metadata>, | ||
| rewards: u64, | ||
| asset_supply: u64, | ||
| desirability_score: u64, | ||
| ) acquires DistributableRewards { | ||
| if (asset_supply == 0 || desirability_score == 0) { | ||
| abort(error::aborted(ENO_AVAILABLE_DESIRABLE_LIQUIDITY)) | ||
| }; | ||
| let reward_index_increase = rewards / asset_supply; | ||
|
|
||
| let distributable_rewards_ref = borrow_global_mut<DistributableRewards>( | ||
| object::create_object_address(&@supra_framework, DISTRIBUTABLE_REWARDS) | ||
| ); | ||
| let distributable_table_item = table::borrow_mut( | ||
| &mut distributable_rewards_ref.total_liquidity_provided, | ||
| asset | ||
| ); | ||
|
|
||
| distributable_table_item.reward_index_asset = distributable_table_item.reward_index_asset + reward_index_increase; | ||
|
|
||
| table::add( | ||
| &mut distributable_rewards_ref.total_liquidity_provided, | ||
| asset, | ||
| *distributable_table_item | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| ///Purpose: Computes the rewards for a user based on assets held on their account for some period of time and asset specific global reward index(at the point of the function call) and | ||
| /// user specific reward(updated for the user at the point of the last asset balance change). | ||
| ///Calculation: To determine the total amount of rewards r_u allocated to user u for maintaining their iAsset a holdings from t_k to t_n, we use the following formula: | ||
| /// r_u = D_u^a \cdot \left(\sum_{i=0}^{e_n} \frac{R_i^a}{D_i^a} - \sum_{i=0}^{e_k} \frac{R_i^a}{D_i^a}\right) | ||
| ///Here, R_i^a represents the total rewards distributed to all holders of iAsset a at the i-th timestep, and D_i^a is the total supply | ||
| ///of iAsset a at the same timestep. D_u^a represent the total holdings of the iAsset a owned by user u. Over the interval from t_k to t_n, | ||
| /// D_u^a remains constant in the user's wallet.Since block rewards are distributed at the end of each epoch, let e_n denote the final reward | ||
| /// distribution timestep immediately before t_n, and let e_k denote the reward distribution timestep that occurred just before t_k.\\\\ | ||
| /// We define the general reward index for asset a as | ||
| /// \sum_{i=0}^{e_n} \frac{R_i^a}{D_i^a}, | ||
| /// and the user's reward index for asset a as | ||
| /// \sum_{i=0}^{e_k} \frac{R_i^a}{D_i^a}. | ||
| /// Both indices are asset-specific and pertain to the given asset a. | ||
|
|
||
| public fun calculate_rewards( | ||
| user_address: address, | ||
| user_reward_index: u64, | ||
| asset: Object<Metadata> | ||
| ): u64 acquires DistributableRewards { | ||
| let iAsset_balance = primary_fungible_store::balance(user_address, asset); | ||
|
|
||
| let distributable_rewards_ref = borrow_global<DistributableRewards>( | ||
| object::create_object_address(&@supra_framework, DISTRIBUTABLE_REWARDS) | ||
| ); | ||
| let reward_index_asset = table::borrow( | ||
| &distributable_rewards_ref.total_liquidity_provided, | ||
| asset | ||
| ).reward_index_asset; | ||
|
|
||
| //Compute Rewards using the formula: Rewards= iAssetBalance * (reward_index_asset - rewardIndexOf[_account]) | ||
|
|
||
| let rewards = iAsset_balance * (reward_index_asset - user_reward_index); | ||
|
|
||
| rewards | ||
| } | ||
|
|
||
|
|
||
| ///Purpose: Updates and distributes rewards for a specific asset to an account. | ||
| public fun update_rewards( | ||
| user_address: address, | ||
| user_reward_index: u64, | ||
| asset: Object<Metadata> | ||
| ): u64 acquires DistributableRewards { | ||
| let new_user_reward_index: u64 = 0; | ||
|
|
||
|
|
||
| let distributable_rewards_ref = borrow_global<DistributableRewards>( | ||
| object::create_object_address(&@supra_framework, DISTRIBUTABLE_REWARDS) | ||
| ); | ||
|
|
||
| let distributable_rewards_table_item = table::borrow(&distributable_rewards_ref.total_liquidity_provided, asset); | ||
| let reward_index_asset = distributable_rewards_table_item.reward_index_asset; | ||
|
|
||
| if (user_reward_index == new_user_reward_index) { | ||
| new_user_reward_index = reward_index_asset; | ||
| } else { | ||
| let _distributable_rewards = calculate_rewards(user_address, user_reward_index, asset); | ||
|
|
||
| //assert!(PoEL::has_sufficient_tokens(distributable_rewards), INSUFFICIENT_TOKENS_ERROR); | ||
|
|
||
| //PoEL::transfer_tokens(distributable_rewards, user_address); | ||
|
|
||
| new_user_reward_index = reward_index_asset; | ||
| }; | ||
| new_user_reward_index | ||
| } | ||
|
|
||
|
|
||
| #[test(creator = @supra_framework)] | ||
| fun test_init(creator: &signer) { | ||
| init_module(creator); | ||
| } | ||
|
|
||
| } | ||
6 changes: 6 additions & 0 deletions
6
aptos-move/framework/supra-framework/sources/poel/reward_distribution.spec.move
This file contains hidden or 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,6 @@ | ||
| spec supra_framework::reward_distribution { | ||
|
|
||
| spec module { | ||
| pragma verify = true; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add one more field here:
reward_allocation_OLC_index: u64,
It is applied in the reward_allocation function (in PoEL module)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added this to the
AdminManagementstruct. TheDistributableRewardsis removed because theTotalLiquidityProvidedItemshave been moved to the iAsset module because it tracks data specific to an asset which is best tracked in the iAsset.