-
Notifications
You must be signed in to change notification settings - Fork 83
[WIP] Gauges and bribes #50
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
Open
sunmoon11100
wants to merge
28
commits into
staking
Choose a base branch
from
gauges_bribes
base: staking
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.
Open
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
f9dd71a
DAOStorage tests (#45)
sunmoon11100 8b41154
fix: add more tests for getter functions (#43)
gaquarius a24ee19
stable curve much more accurate, removed divisions (#44)
borispovod 94d7cbb
update to devnet 11-8-22 (#47)
borispovod 2bb3c75
Added structure for gauges and bribes
sunmoon11100 1768c6e
add early prototype of the gauge
mkurnikov 05d8290
Added seperated modules bribe, gauge, voter
sunmoon11100 4bafadb
Modified gauge
sunmoon11100 bb11378
Fixed reference name of functions
sunmoon11100 95f5ecf
Removed ve.move
sunmoon11100 ae9390e
Changed claim logic
sunmoon11100 51ae262
Merge branch 'staking' into gauges_bribes
sunmoon11100 6bed99c
Fixed tests
sunmoon11100 a6698af
removed drop ability from Gauge
sunmoon11100 dd2bbde
Changed structure variables in voter and gauges
sunmoon11100 5affde3
Added bribe
sunmoon11100 bc80735
Change PoolId resource from Strings to addresses
sunmoon11100 2c54051
Example of PoolId
borispovod f5d076d
Changed gauge&bribe logic
sunmoon11100 883f4bf
Added fee claiming logic
sunmoon11100 d20b1c5
Merge branch 'staking' into gauges_bribes
sunmoon11100 0ee4631
Some reference errors in function parameters
sunmoon11100 f9a4616
Fixed reference errors.
sunmoon11100 631a330
Collecting fees on each swap to gauge.
sunmoon11100 ea789bf
Fixed test errors in liquidity_pool_tests.move
sunmoon11100 c318976
Merge branch 'staking' into gauges_bribes
sunmoon11100 44113b7
Fixed tests
sunmoon11100 2398dc9
Added some docstrings for main functions
sunmoon11100 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
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,139 @@ | ||
| module liquidswap::bribe { | ||
| use std::signer; | ||
|
|
||
| use aptos_framework::coin::{Self, Coin}; | ||
| use aptos_framework::timestamp; | ||
| use aptos_std::event; | ||
|
|
||
| friend liquidswap::liquidity_pool; | ||
| friend liquidswap::voter; | ||
|
|
||
| const WEEK: u64 = 604800; | ||
|
|
||
| // Error codes. | ||
|
|
||
| /// When gauge doesn't exists | ||
| const ERR_NOT_REGISTERED: u64 = 401; | ||
|
|
||
| const ERR_INSUFFICIANT_REWARD: u64 = 402; | ||
|
|
||
| struct Bribe<phantom X, phantom Y, phantom LP> has key { | ||
| x_reward: Coin<X>, | ||
| y_reward: Coin<Y>, | ||
| x_reward_rate: u64, | ||
| y_reward_rate: u64, | ||
| period_finish: u64, | ||
| } | ||
|
|
||
| public(friend) fun register<X, Y, LP>(owner: &signer) { | ||
| let bribe = Bribe<X, Y, LP> { | ||
| x_reward: coin::zero(), | ||
| y_reward: coin::zero(), | ||
| x_reward_rate: 0, | ||
| y_reward_rate: 0, | ||
| period_finish: 0, | ||
| }; | ||
| move_to(owner, bribe); | ||
|
|
||
| let events_store = EventsStore<X, Y, LP>{ | ||
| bribe_created_handle: event::new_event_handle(owner), | ||
| bribe_add_reward_handle: event::new_event_handle(owner), | ||
| bribe_withdraw_reward_handle: event::new_event_handle(owner), | ||
| }; | ||
| event::emit_event( | ||
| &mut events_store.bribe_created_handle, | ||
| BribeCreatedEvent<X, Y, LP>{ pool_addr: signer::address_of(owner) } | ||
| ); | ||
|
|
||
| move_to(owner, events_store); | ||
| } | ||
|
|
||
| public fun add_reward<X, Y, LP>(pool_addr: address, coin_x: Coin<X>, coin_y: Coin<Y>) acquires Bribe, EventsStore { | ||
| assert!(exists<Bribe<X, Y, LP>>(pool_addr), ERR_NOT_REGISTERED); | ||
|
|
||
| let bribe = borrow_global_mut<Bribe<X, Y, LP>>(pool_addr); | ||
| let coin_x_val = coin::value(&coin_x); | ||
| let coin_y_val = coin::value(&coin_y); | ||
| let now = timestamp::now_seconds(); | ||
| if (now >= bribe.period_finish) { | ||
| bribe.x_reward_rate = coin_x_val / WEEK; | ||
| bribe.y_reward_rate = coin_y_val / WEEK; | ||
| } else { | ||
| let remaining = bribe.period_finish - now; | ||
| let x_left = remaining * bribe.x_reward_rate; | ||
| let y_left = remaining * bribe.y_reward_rate; | ||
| assert!(coin_x_val > x_left, 1); | ||
| assert!(coin_y_val > y_left, 2); | ||
| bribe.x_reward_rate = (coin_x_val + x_left) / WEEK; | ||
| bribe.y_reward_rate = (coin_y_val + y_left) / WEEK; | ||
| }; | ||
|
|
||
| coin::merge(&mut bribe.x_reward, coin_x); | ||
| coin::merge(&mut bribe.y_reward, coin_y); | ||
|
|
||
| assert!(bribe.x_reward_rate <= coin::value(&bribe.x_reward) / WEEK, 3); | ||
| assert!(bribe.y_reward_rate <= coin::value(&bribe.y_reward) / WEEK, 4); | ||
| bribe.period_finish = now + WEEK; | ||
|
|
||
| let events_store = borrow_global_mut<EventsStore<X, Y, LP>>(pool_addr); | ||
| event::emit_event( | ||
| &mut events_store.bribe_add_reward_handle, | ||
| BribeAddRewardEvent<X, Y, LP>{ pool_addr, coin_x_val, coin_y_val } | ||
| ); | ||
| } | ||
|
|
||
| public(friend) fun withdraw_reward<X, Y, LP>( | ||
| pool_addr: address, | ||
| token_votes: u64, | ||
| total_votes: u64 | ||
| ): (Coin<X>, Coin<Y>) acquires Bribe, EventsStore { | ||
| assert!(exists<Bribe<X, Y, LP>>(pool_addr), ERR_NOT_REGISTERED); | ||
|
|
||
| let bribe = borrow_global_mut<Bribe<X, Y, LP>>(pool_addr); | ||
|
|
||
| let now = timestamp::now_seconds(); | ||
| let time = if (now >= bribe.period_finish) { WEEK } else { now + WEEK - bribe.period_finish }; | ||
| let x_amount = time * bribe.x_reward_rate * token_votes / total_votes; | ||
| let y_amount = time * bribe.y_reward_rate * token_votes / total_votes; | ||
| assert!(x_amount <= coin::value(&bribe.x_reward), ERR_INSUFFICIANT_REWARD); | ||
| assert!(y_amount <= coin::value(&bribe.y_reward), ERR_INSUFFICIANT_REWARD); | ||
| let coin_x = coin::extract(&mut bribe.x_reward, x_amount); | ||
| let coin_y = coin::extract(&mut bribe.y_reward, y_amount); | ||
|
|
||
| let events_store = borrow_global_mut<EventsStore<X, Y, LP>>(pool_addr); | ||
| event::emit_event( | ||
| &mut events_store.bribe_withdraw_reward_handle, | ||
| BribeWithdrawRewardEvent<X, Y, LP>{ | ||
| pool_addr, | ||
| coin_x_val: coin::value(&coin_x), | ||
| coin_y_val: coin::value(&coin_y), | ||
| } | ||
| ); | ||
|
|
||
| (coin_x, coin_y) | ||
| } | ||
|
|
||
| // Events | ||
|
|
||
| struct EventsStore<phantom X, phantom Y, phantom LP> has key { | ||
| bribe_created_handle: event::EventHandle<BribeCreatedEvent<X, Y, LP>>, | ||
| bribe_add_reward_handle: event::EventHandle<BribeAddRewardEvent<X, Y, LP>>, | ||
| bribe_withdraw_reward_handle: event::EventHandle<BribeWithdrawRewardEvent<X, Y, LP>>, | ||
| } | ||
|
|
||
| struct BribeCreatedEvent<phantom X, phantom Y, phantom LP> has store, drop { | ||
| pool_addr: address, | ||
| } | ||
|
|
||
| struct BribeAddRewardEvent<phantom X, phantom Y, phantom LP> has store, drop { | ||
| pool_addr: address, | ||
| coin_x_val: u64, | ||
| coin_y_val: u64, | ||
| } | ||
|
|
||
| struct BribeWithdrawRewardEvent<phantom X, phantom Y, phantom LP> has store, drop { | ||
| pool_addr: address, | ||
| coin_x_val: u64, | ||
| coin_y_val: u64, | ||
| } | ||
| } |
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,121 @@ | ||
| module liquidswap::gauge { | ||
| use std::signer; | ||
|
|
||
| use aptos_framework::coin::{Self, Coin}; | ||
| use aptos_framework::timestamp; | ||
| use aptos_std::event; | ||
|
|
||
| use liquidswap::liquid::LAMM; | ||
|
|
||
| friend liquidswap::liquidity_pool; | ||
| friend liquidswap::voter; | ||
|
|
||
| const WEEK: u64 = 604800; | ||
|
|
||
| // Error codes. | ||
|
|
||
| /// When gauge doesn't exists | ||
| const ERR_NOT_REGISTERED: u64 = 401; | ||
|
|
||
| const ERR_INSUFFICIANT_REWARD: u64 = 402; | ||
|
|
||
| struct Gauge<phantom X, phantom Y, phantom LP> has key { | ||
| reward: Coin<LAMM>, | ||
| reward_rate: u64, | ||
| period_finish: u64, | ||
| } | ||
|
|
||
| public(friend) fun register<X, Y, LP>(owner: &signer) { | ||
| let gauge = Gauge<X, Y, LP> { | ||
| reward: coin::zero(), | ||
| reward_rate: 0, | ||
| period_finish: 0, | ||
| }; | ||
| move_to(owner, gauge); | ||
|
|
||
| let events_store = EventsStore<X, Y, LP>{ | ||
| gauge_created_handle: event::new_event_handle(owner), | ||
| gauge_add_reward_handle: event::new_event_handle(owner), | ||
| gauge_withdraw_reward_handle: event::new_event_handle(owner), | ||
| }; | ||
| event::emit_event( | ||
| &mut events_store.gauge_created_handle, | ||
| GaugeCreatedEvent<X, Y, LP>{ pool_addr: signer::address_of(owner) } | ||
| ); | ||
|
|
||
| move_to(owner, events_store); | ||
| } | ||
|
|
||
| public fun add_reward<X, Y, LP>(pool_addr: address, coin_in: Coin<LAMM>) acquires Gauge, EventsStore { | ||
| assert!(exists<Gauge<X, Y, LP>>(pool_addr), ERR_NOT_REGISTERED); | ||
|
|
||
| let gauge = borrow_global_mut<Gauge<X, Y, LP>>(pool_addr); | ||
| let coin_val = coin::value(&coin_in); | ||
| let now = timestamp::now_seconds(); | ||
| if (now >= gauge.period_finish) { | ||
| gauge.reward_rate = coin_val / WEEK; | ||
| } else { | ||
| let remaining = gauge.period_finish - now; | ||
| let left = remaining * gauge.reward_rate; | ||
| assert!(coin_val > left, 1); | ||
| gauge.reward_rate = (coin_val + left) / WEEK; | ||
| }; | ||
|
|
||
| coin::merge(&mut gauge.reward, coin_in); | ||
|
|
||
| assert!(gauge.reward_rate <= coin::value(&gauge.reward) / WEEK, 2); | ||
| gauge.period_finish = now + WEEK; | ||
|
|
||
| let events_store = borrow_global_mut<EventsStore<X, Y, LP>>(pool_addr); | ||
| event::emit_event( | ||
| &mut events_store.gauge_add_reward_handle, | ||
| GaugeAddRewardEvent<X, Y, LP>{ pool_addr, coin_val } | ||
| ); | ||
| } | ||
|
|
||
| public(friend) fun withdraw_reward<X, Y, LP>( | ||
| pool_addr: address, | ||
| token_votes: u64, | ||
| total_votes: u64 | ||
| ): Coin<LAMM> acquires Gauge, EventsStore { | ||
| assert!(exists<Gauge<X, Y, LP>>(pool_addr), ERR_NOT_REGISTERED); | ||
|
|
||
| let gauge = borrow_global_mut<Gauge<X, Y, LP>>(pool_addr); | ||
|
|
||
| let now = timestamp::now_seconds(); | ||
| let time = if (now >= gauge.period_finish) { WEEK } else { now + WEEK - gauge.period_finish }; | ||
| let amount = time * gauge.reward_rate * token_votes / total_votes; | ||
| assert!(amount <= coin::value(&gauge.reward), ERR_INSUFFICIANT_REWARD); | ||
| let coin_out = coin::extract(&mut gauge.reward, amount); | ||
|
|
||
| let events_store = borrow_global_mut<EventsStore<X, Y, LP>>(pool_addr); | ||
| event::emit_event( | ||
| &mut events_store.gauge_withdraw_reward_handle, | ||
| GaugeWithdrawRewardEvent<X, Y, LP>{ pool_addr, coin_val: coin::value(&coin_out) } | ||
| ); | ||
|
|
||
| coin_out | ||
| } | ||
|
|
||
| // Events | ||
|
|
||
| struct EventsStore<phantom X, phantom Y, phantom LP> has key { | ||
| gauge_created_handle: event::EventHandle<GaugeCreatedEvent<X, Y, LP>>, | ||
| gauge_add_reward_handle: event::EventHandle<GaugeAddRewardEvent<X, Y, LP>>, | ||
| gauge_withdraw_reward_handle: event::EventHandle<GaugeWithdrawRewardEvent<X, Y, LP>>, | ||
| } | ||
|
|
||
| struct GaugeCreatedEvent<phantom X, phantom Y, phantom LP> has store, drop { | ||
| pool_addr: address, | ||
| } | ||
|
|
||
| struct GaugeAddRewardEvent<phantom X, phantom Y, phantom LP> has store, drop { | ||
| pool_addr: address, | ||
| coin_val: u64, | ||
| } | ||
|
|
||
| struct GaugeWithdrawRewardEvent<phantom X, phantom Y, phantom LP> has store, drop { | ||
| pool_addr: address, | ||
| coin_val: u64, | ||
| } | ||
| } |
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,125 @@ | ||
| module liquidswap::voter { | ||
| use std::option; | ||
| use std::signer; | ||
|
|
||
| use aptos_framework::coin::Coin; | ||
| use aptos_std::iterable_table::{Self, IterableTable}; | ||
| use aptos_std::table::{Self, Table}; | ||
| use aptos_std::type_info::{Self, TypeInfo}; | ||
|
|
||
| use liquidswap::gauge; | ||
| use liquidswap::liquid::LAMM; | ||
| use liquidswap::liquidity_pool; | ||
| use liquidswap::ve::{Self, VE_NFT}; | ||
| use liquidswap::bribe; | ||
|
|
||
| const ERR_WRONG_INITIALIZER: u64 = 100; | ||
| const ERR_ALREADY_EXISTS: u64 = 101; | ||
| const ERR_NOT_REGISTERED: u64 = 102; | ||
|
|
||
| // TODO: can be replaced with hash? | ||
| struct PoolId has copy, drop, store { | ||
| pool_address: address, | ||
| x_type_info: TypeInfo, | ||
| y_type_info: TypeInfo, | ||
| lp_type_info: TypeInfo, | ||
| } | ||
|
|
||
| struct Voter has key { | ||
| total_vote: u64, | ||
| weights: Table<PoolId, IterableTable<u64, u64>>,// pool_id->token_id->weight | ||
| total_weight_per_toekn: Table<u64, u64>, // token_id->total_weight | ||
| voting_powers: Table<u64, u64>, // token_id->voting_power | ||
sunmoon11100 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public fun initialize(gov_admin: &signer) { | ||
| assert!(!exists<Voter>(@gov_admin), ERR_ALREADY_EXISTS); | ||
| assert!(signer::address_of(gov_admin) == @gov_admin, ERR_WRONG_INITIALIZER); | ||
|
|
||
| move_to(gov_admin, Voter { | ||
| total_vote: 0, | ||
| weights: table::new<PoolId, IterableTable<u64, u64>>(), | ||
| total_weight_per_toekn: table::new<u64, u64>(), | ||
| voting_powers: table::new<u64, u64>(), | ||
sunmoon11100 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| } | ||
|
|
||
| public fun vote<X, Y, LP>(pool_addr: address, ve_nft: &VE_NFT, weight: u64) acquires Voter { | ||
| assert!(exists<Voter>(pool_addr), ERR_NOT_REGISTERED); | ||
|
|
||
| let voter = borrow_global_mut<Voter>(@gov_admin); | ||
| let pool_id = get_liquidity_pool_id<X, Y, LP>(pool_addr); | ||
|
|
||
| if (!table::contains(&mut voter.weights, pool_id)) { | ||
| table::add(&mut voter.weights, pool_id, iterable_table::new<u64, u64>()); | ||
sunmoon11100 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| let ve_token_id = ve::get_nft_id(ve_nft); | ||
| let weights_per_token = table::borrow_mut(&mut voter.weights, pool_id); | ||
| let prev_weight = iterable_table::borrow_mut_with_default(weights_per_token, ve_token_id, 0); | ||
|
|
||
| let total_weight = table::borrow_mut_with_default( | ||
| &mut voter.total_weight_per_toekn, | ||
| ve_token_id, | ||
| 0 | ||
| ); | ||
| *total_weight = *total_weight - *prev_weight + weight; | ||
|
|
||
| *prev_weight = weight; | ||
sunmoon11100 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let voting_power = table::borrow_mut_with_default( | ||
| &mut voter.voting_powers, | ||
| ve_token_id, | ||
| 0 | ||
| ); | ||
| *voting_power = ve::get_nft_voting_power(ve_nft); | ||
| } | ||
|
|
||
| public fun claim_gauge<X, Y, LP>(pool_addr: address, ve_nft: &VE_NFT): Coin<LAMM> acquires Voter { | ||
| let (token_votes, total_votes) = get_votes<X, Y, LP>(pool_addr, ve_nft); | ||
| gauge::withdraw_reward<X, Y, LP>(pool_addr, token_votes, total_votes) | ||
| } | ||
|
|
||
| public fun claim_bribe<X, Y, LP>(pool_addr: address, ve_nft: &VE_NFT): (Coin<X>, Coin<Y>) acquires Voter { | ||
| let (token_votes, total_votes) = get_votes<X, Y, LP>(pool_addr, ve_nft); | ||
| bribe::withdraw_reward<X, Y, LP>(pool_addr, token_votes, total_votes) | ||
| } | ||
|
|
||
| fun get_votes<X, Y, LP>(pool_addr: address, ve_nft: &VE_NFT): (u64, u64) acquires Voter { | ||
| assert!(exists<Voter>(pool_addr), ERR_NOT_REGISTERED); | ||
|
|
||
| let voter = borrow_global_mut<Voter>(@gov_admin); | ||
| let pool_id = get_liquidity_pool_id<X, Y, LP>(pool_addr); | ||
|
|
||
| assert!(table::contains(&voter.weights, pool_id), 1); | ||
|
|
||
| let ve_token_id = ve::get_nft_id(ve_nft); | ||
| let weights_per_token = table::borrow(&voter.weights, pool_id); | ||
|
|
||
| let total_votes = 0; | ||
| let token_votes = 0; | ||
| let key = iterable_table::head_key(weights_per_token); | ||
| while (option::is_some(&key)) { | ||
| let token_id = *option::borrow(&key); | ||
| let (weight, _, next) = iterable_table::borrow_iter(weights_per_token, token_id); | ||
| let voting_power = table::borrow(&voter.voting_powers, token_id); | ||
| let total_weight = table::borrow(&voter.total_weight_per_toekn, token_id); | ||
| let votes = *voting_power * *weight / *total_weight; | ||
| total_votes = total_votes + votes; | ||
| if (token_id == ve_token_id) token_votes = votes; | ||
| key = next; | ||
| }; | ||
|
|
||
| (token_votes, total_votes) | ||
| } | ||
|
|
||
| public fun get_liquidity_pool_id<X, Y, LP>(pool_addr: address): PoolId { | ||
| assert!(liquidity_pool::pool_exists_at<X, Y, LP>(pool_addr), 1); | ||
| PoolId { | ||
| pool_address: pool_addr, | ||
| x_type_info: type_info::type_of<X>(), | ||
| y_type_info: type_info::type_of<Y>(), | ||
| lp_type_info: type_info::type_of<LP>(), | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.