Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f9dd71a
DAOStorage tests (#45)
sunmoon11100 Aug 11, 2022
8b41154
fix: add more tests for getter functions (#43)
gaquarius Aug 11, 2022
a24ee19
stable curve much more accurate, removed divisions (#44)
borispovod Aug 11, 2022
94d7cbb
update to devnet 11-8-22 (#47)
borispovod Aug 11, 2022
2bb3c75
Added structure for gauges and bribes
sunmoon11100 Aug 15, 2022
1768c6e
add early prototype of the gauge
mkurnikov Aug 15, 2022
05d8290
Added seperated modules bribe, gauge, voter
sunmoon11100 Aug 16, 2022
4bafadb
Modified gauge
sunmoon11100 Aug 17, 2022
bb11378
Fixed reference name of functions
sunmoon11100 Aug 18, 2022
95f5ecf
Removed ve.move
sunmoon11100 Aug 18, 2022
ae9390e
Changed claim logic
sunmoon11100 Aug 18, 2022
51ae262
Merge branch 'staking' into gauges_bribes
sunmoon11100 Aug 19, 2022
6bed99c
Fixed tests
sunmoon11100 Aug 19, 2022
a6698af
removed drop ability from Gauge
sunmoon11100 Aug 19, 2022
dd2bbde
Changed structure variables in voter and gauges
sunmoon11100 Aug 22, 2022
5affde3
Added bribe
sunmoon11100 Aug 22, 2022
bc80735
Change PoolId resource from Strings to addresses
sunmoon11100 Aug 23, 2022
2c54051
Example of PoolId
borispovod Aug 23, 2022
f5d076d
Changed gauge&bribe logic
sunmoon11100 Aug 24, 2022
883f4bf
Added fee claiming logic
sunmoon11100 Aug 26, 2022
d20b1c5
Merge branch 'staking' into gauges_bribes
sunmoon11100 Aug 28, 2022
0ee4631
Some reference errors in function parameters
sunmoon11100 Aug 30, 2022
f9a4616
Fixed reference errors.
sunmoon11100 Aug 31, 2022
631a330
Collecting fees on each swap to gauge.
sunmoon11100 Sep 1, 2022
ea789bf
Fixed test errors in liquidity_pool_tests.move
sunmoon11100 Sep 2, 2022
c318976
Merge branch 'staking' into gauges_bribes
sunmoon11100 Sep 7, 2022
44113b7
Fixed tests
sunmoon11100 Sep 7, 2022
2398dc9
Added some docstrings for main functions
sunmoon11100 Sep 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ liquidswap = "0x43417434fd869edee76cca2a4d2301e528a1551b1d719b75c350c3c97d15b8b9
dao_admin = "0xb4d7b2466d211c1f4629e8340bb1a9e75e7f8fb38cc145c54c5c9f9d5017a318"
emergency_admin = "0xb4d7b2466d211c1f4629e8340bb1a9e75e7f8fb38cc145c54c5c9f9d5017a318"
staking_pool = "0x42"
gov_admin = "0x43"

# Addresses for tests.
test_coin_admin = "0x11"
Expand Down
18 changes: 18 additions & 0 deletions sources/gov/bribe.move
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()
}
}
}
71 changes: 71 additions & 0 deletions sources/gov/gauge.move
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>()
}
}
}
105 changes: 105 additions & 0 deletions sources/gov/voter.move
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);
}

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)
}
}
8 changes: 8 additions & 0 deletions sources/staking/ve.move
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ module liquidswap::ve {
*table_with_length::borrow(&nft.point_history, epoch)
}

/// Get current VE NFT voting power.
/// `nft` - reference to `VE_NFT`.
public fun get_nft_voting_power(nft: &VE_NFT): u64 acquires StakingPool {
let epoch = get_current_epoch();
let point = get_nft_history_point(nft, epoch);
get_voting_power(&point)
}

// Point getters.

/// Get a time when `point` created.
Expand Down
37 changes: 37 additions & 0 deletions tests/voter_tests.move
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(
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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
);
}
}