- 
                Notifications
    
You must be signed in to change notification settings  - Fork 82
 
[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 17 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,18 @@ | ||
| module liquidswap::bribe { | ||
| use aptos_framework::coin::Coin; | ||
| 
     | 
||
| use liquidswap::liquid::LAMM; | ||
| use aptos_framework::coin; | ||
| 
     | 
||
| friend liquidswap::gauge; | ||
| 
     | 
||
| struct Bribe has store { | ||
| rewards: Coin<LAMM> | ||
| } | ||
| 
     | 
||
| public(friend) fun zero_bribe(): Bribe { | ||
| Bribe { | ||
| rewards: coin::zero() | ||
| } | ||
| } | ||
| } | 
  
    
      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,71 @@ | ||
| module liquidswap::gauge { | ||
| use aptos_framework::coin::{Self, Coin}; | ||
| use aptos_framework::timestamp; | ||
| use aptos_std::iterable_table::{Self, IterableTable}; | ||
| 
     | 
||
| use liquidswap::bribe::{Self, Bribe}; | ||
| use liquidswap::liquid::LAMM; | ||
| use liquidswap::ve::{Self, VE_NFT}; | ||
| 
     | 
||
| friend liquidswap::voter; | ||
| 
     | 
||
| const WEEK: u64 = 604800; | ||
| 
     | 
||
| struct Gauge has store { | ||
| bribe: Bribe, | ||
| rewards: Coin<LAMM>, | ||
| reward_rate: u64, | ||
| period_finish: u64, | ||
| total_voted: u64, | ||
| voted_per_token: IterableTable<u64, u64> // table<token_id, vote> | ||
| } | ||
| 
     | 
||
| public(friend) fun vote(gauge: &mut Gauge, ve_nft: &VE_NFT, vote: u64) { | ||
| let ve_token_id = ve::get_nft_id(ve_nft); | ||
| let votes = iterable_table::borrow_mut_with_default(&mut gauge.voted_per_token, ve_token_id, 0); | ||
| *votes = vote; | ||
| 
     | 
||
| gauge.total_voted = gauge.total_voted + vote; | ||
| } | ||
| 
     | 
||
| public(friend) fun add_rewards(gauge: &mut Gauge, coin_in: Coin<LAMM>) { | ||
| let coin_in_value = coin::value(&coin_in); | ||
| let now = timestamp::now_seconds(); | ||
| if (now >= gauge.period_finish) { | ||
| gauge.reward_rate = coin_in_value / WEEK; | ||
| } else { | ||
| let remaining = gauge.period_finish - now; | ||
| let left = remaining * gauge.reward_rate; | ||
| assert!(coin_in_value > left, 2); | ||
| gauge.reward_rate = (coin_in_value + left) / WEEK; | ||
| }; | ||
| 
     | 
||
| coin::merge(&mut gauge.rewards, coin_in); | ||
| 
     | 
||
| assert!(gauge.reward_rate <= coin::value(&gauge.rewards) / WEEK, 3); | ||
| gauge.period_finish = now + WEEK; | ||
| } | ||
| 
     | 
||
| public(friend) fun withdraw_rewards(gauge: &mut Gauge, ve_nft: &VE_NFT): Coin<LAMM> { | ||
| let ve_token_id = ve::get_nft_id(ve_nft); | ||
| let token_votes = iterable_table::borrow(&gauge.voted_per_token, ve_token_id); | ||
| 
     | 
||
| 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 / gauge.total_voted; | ||
| assert!(amount <= coin::value(&gauge.rewards), 1); | ||
| 
     | 
||
| coin::extract(&mut gauge.rewards, amount) | ||
| } | ||
| 
     | 
||
| public(friend) fun zero_gauge(): Gauge { | ||
| Gauge { | ||
| bribe: bribe::zero_bribe(), | ||
| rewards: coin::zero(), | ||
| reward_rate: 0, | ||
| period_finish: 0, | ||
| total_voted: 0, | ||
| voted_per_token: iterable_table::new<u64, 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,105 @@ | ||
| 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; | ||
| 
     | 
||
| use liquidswap::gauge::{Self, Gauge}; | ||
| use liquidswap::ve::{Self, VE_NFT}; | ||
| use liquidswap::liquid::LAMM; | ||
| use liquidswap::liquidity_pool; | ||
| 
     | 
||
| const ERR_WRONG_INITIALIZER: u64 = 100; | ||
| 
     | 
||
| struct PoolId has copy, drop, store { | ||
| pool_address: address, | ||
| x_address: address, | ||
| y_address: address, | ||
| lp_address: address, | ||
| } | ||
| 
     | 
||
| struct Voter has key { | ||
| gauges: Table<PoolId, Gauge>, | ||
| total_vote: u64, | ||
| } | ||
| 
     | 
||
| public fun initialize(gov_admin: &signer) { | ||
| assert!(signer::address_of(gov_admin) == @gov_admin, ERR_WRONG_INITIALIZER); | ||
| move_to(gov_admin, Voter { | ||
| gauges: table::new<PoolId, Gauge>(), | ||
| total_vote: 0, | ||
| }); | ||
| } | ||
| 
     | 
||
| public fun register<X, Y, LP>(owner: &signer) acquires Voter { | ||
| let voter = borrow_global_mut<Voter>(@gov_admin); | ||
| let owner_addr = signer::address_of(owner); | ||
| let pool_id = get_liquidity_pool_id<X, Y, LP>(owner_addr); | ||
| table::add(&mut voter.gauges, pool_id, gauge::zero_gauge()); | ||
| } | ||
| 
     | 
||
| public fun vote(ve_nft: &VE_NFT, weights: IterableTable<PoolId, u64>) acquires Voter { | ||
| let voter = borrow_global_mut<Voter>(@gov_admin); | ||
| 
     | 
||
| let total_weight = 0; | ||
| let key = iterable_table::head_key(&weights); | ||
| while (option::is_some(&key)) { | ||
| let (weight, _, next) = | ||
| iterable_table::borrow_iter(&weights, *option::borrow(&key)); | ||
| total_weight = total_weight + *weight; | ||
| key = next; | ||
| }; | ||
| 
     | 
||
| let ve_voting_power = ve::get_nft_voting_power(ve_nft); | ||
| key = iterable_table::head_key(&weights); | ||
| while (option::is_some(&key)) { | ||
| let (weight, _, next) = | ||
| iterable_table::borrow_iter(&weights, *option::borrow(&key)); | ||
| let pool_id = *option::borrow(&key); | ||
| let votes = *weight * ve_voting_power / total_weight; | ||
| 
     | 
||
| if(!table::contains(&voter.gauges, pool_id)) | ||
| table::add(&mut voter.gauges, pool_id, gauge::zero_gauge()); | ||
| let gauge = table::borrow_mut(&mut voter.gauges, pool_id); | ||
| gauge::vote(gauge, ve_nft, votes); | ||
| 
     | 
||
| key = next; | ||
| }; | ||
| iterable_table::destroy_empty(weights); | ||
                
      
                  sunmoon11100 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| 
     | 
||
| public fun deposit(pool_id: PoolId, coin_in: Coin<LAMM>) acquires Voter { | ||
| let voter = borrow_global_mut<Voter>(@gov_admin); | ||
| 
     | 
||
| if(!table::contains(&voter.gauges, pool_id)) | ||
| table::add(&mut voter.gauges, pool_id, gauge::zero_gauge()); | ||
| let gauge = table::borrow_mut(&mut voter.gauges, pool_id); | ||
| gauge::add_rewards(gauge, coin_in); | ||
| } | ||
| 
     | 
||
| public fun claim(pool_id: PoolId, ve_nft: &VE_NFT): Coin<LAMM> acquires Voter { | ||
| let voter = borrow_global_mut<Voter>(@gov_admin); | ||
| 
     | 
||
| assert!(table::contains(&voter.gauges, pool_id), 1); | ||
| let gauge = table::borrow_mut(&mut voter.gauges, pool_id); | ||
| gauge::withdraw_rewards(gauge, ve_nft) | ||
| } | ||
| 
     | 
||
| 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_address: coin_address<X>(), | ||
| y_address: coin_address<Y>(), | ||
| lp_address: coin_address<LP>(), | ||
| } | ||
| } | ||
| 
     | 
||
| fun coin_address<CoinType>(): address { | ||
| let type_info = type_info::type_of<CoinType>(); | ||
| type_info::account_address(&type_info) | ||
| } | ||
| } | ||
  
    
      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,37 @@ | ||
| #[test_only] | ||
| module liquidswap::voter_tests { | ||
| use std::string::utf8; | ||
| 
     | 
||
| use aptos_framework::genesis; | ||
| 
     | 
||
| use liquidswap::voter; | ||
| use liquidswap::liquidity_pool; | ||
| 
     | 
||
| use test_coin_admin::test_coins::{Self, BTC, USDT}; | ||
| use test_helpers::test_account::create_account; | ||
| use test_pool_owner::test_lp::LP; | ||
| 
     | 
||
| #[test( | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs much more tests, really as much tests as possible!  | 
||
| coin_admin = @test_coin_admin, | ||
| pool_owner = @test_pool_owner, | ||
| gov_admin = @gov_admin | ||
| )] | ||
| fun test_vote_for_liquidity_pool(coin_admin: signer, pool_owner: signer, gov_admin: signer) { | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still small amount of tests.  | 
||
| genesis::setup(); | ||
| 
     | 
||
| create_account(&coin_admin); | ||
| create_account(&pool_owner); | ||
| create_account(&gov_admin); | ||
| 
     | 
||
| voter::initialize(&gov_admin); | ||
| 
     | 
||
| test_coins::register_coins(&coin_admin); | ||
| 
     | 
||
| liquidity_pool::register<BTC, USDT, LP>( | ||
| &pool_owner, | ||
| utf8(b"LiquidSwap LP"), | ||
| utf8(b"LP-BTC-USDT"), | ||
| 2 | ||
| ); | ||
| } | ||
| } | ||
  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.