Skip to content

Commit

Permalink
Staking (#107)
Browse files Browse the repository at this point in the history
* Add pseudocode

* Add upgradability and ownership to FloatingToken

* Add staking

* Add staking to main contract

* Add test setup function with both tokens

* Fix some tests to work with staked voting token

* Polish format

* Fix proposals tests with staking

* Fix all remaining tests

* Bump version to 0.5.0

* Update to have contract deploy tokens upon creation

* Add staking events

* Use struct to store Stake, implement get_total_staked_accounted

* Add more staking tests

* Add even more tests and get_total_voting_power

* Add test unstake before unlock

* Format

* Fix minting of floating token

* Refactor staking contract to use DivRem for calculating amount of voting tokens

* Remove unlock date in airdrop unstake
  • Loading branch information
tensojka authored Jun 18, 2024
1 parent cff93d0 commit 1d7306e
Show file tree
Hide file tree
Showing 14 changed files with 836 additions and 147 deletions.
2 changes: 1 addition & 1 deletion Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "konoha"
version = "0.4.0"
version = "0.5.0"
dependencies = [
"openzeppelin",
"snforge_std",
Expand Down
3 changes: 1 addition & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
[package]
name = "konoha"
description = "Flexible monolithic governance components for Starknet"
version = "0.4.0"
version = "0.5.0"
cairo-version = "2.6.3"

[dependencies]
starknet = ">=2.0.0"
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.10.0" }

# can be fixed by doing import super::testing from tests
[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.23.0" }

Expand Down
55 changes: 51 additions & 4 deletions src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ mod Governance {
use konoha::airdrop::airdrop as airdrop_component;
use konoha::vesting::vesting as vesting_component;
use konoha::discussion::discussion as discussion_component;
use konoha::staking::staking as staking_component;
use konoha::staking::{IStakingDispatcher, IStakingDispatcherTrait};

use starknet::ContractAddress;
use starknet::{ContractAddress, ClassHash};


component!(path: airdrop_component, storage: airdrop, event: AirdropEvent);
component!(path: vesting_component, storage: vesting, event: VestingEvent);
component!(path: proposals_component, storage: proposals, event: ProposalsEvent);
component!(path: upgrades_component, storage: upgrades, event: UpgradesEvent);
component!(path: discussion_component, storage: discussions, event: DiscussionEvent);
component!(path: staking_component, storage: staking, event: StakingEvent);

#[abi(embed_v0)]
impl Airdrop = airdrop_component::AirdropImpl<ContractState>;
Expand All @@ -60,6 +63,9 @@ mod Governance {
#[abi(embed_v0)]
impl Discussions = discussion_component::DiscussionImpl<ContractState>;

#[abi(embed_v0)]
impl Staking = staking_component::StakingImpl<ContractState>;

#[storage]
struct Storage {
proposal_initializer_run: LegacyMap::<u64, bool>,
Expand All @@ -73,7 +79,9 @@ mod Governance {
#[substorage(v0)]
upgrades: upgrades_component::Storage,
#[substorage(v0)]
discussions: discussion_component::Storage
discussions: discussion_component::Storage,
#[substorage(v0)]
staking: staking_component::Storage,
}

// PROPOSALS
Expand Down Expand Up @@ -102,12 +110,51 @@ mod Governance {
ProposalsEvent: proposals_component::Event,
UpgradesEvent: upgrades_component::Event,
DiscussionEvent: discussion_component::Event,
StakingEvent: staking_component::Event,
}

use starknet::syscalls::deploy_syscall;
use starknet::get_contract_address;

#[constructor]
fn constructor(ref self: ContractState, govtoken_address: ContractAddress) {
fn constructor(
ref self: ContractState,
voting_token_class: ClassHash,
floating_token_class: ClassHash,
recipient: ContractAddress
) {
// This is not used in production on mainnet, because the governance token is already deployed (and distributed).
self.governance_token_address.write(govtoken_address);

let governance_address = get_contract_address();

let mut voting_token_calldata: Array<felt252> = ArrayTrait::new();
voting_token_calldata.append(governance_address.into());
let (voting_token_address, _) = deploy_syscall(
voting_token_class, 42, voting_token_calldata.span(), true
)
.unwrap();
self.governance_token_address.write(voting_token_address);

let mut floating_token_calldata: Array<felt252> = ArrayTrait::new();
floating_token_calldata.append(10000000000000000000); // 10**19, 10 tokens overall
floating_token_calldata.append(0); // high for u256 supply
floating_token_calldata.append(recipient.into());
floating_token_calldata.append(governance_address.into());
let (floating_token_address, _) = deploy_syscall(
floating_token_class, 42, floating_token_calldata.span(), true
)
.unwrap();

let staking = IStakingDispatcher { contract_address: governance_address };
staking.set_floating_token_address(floating_token_address);
let ONE_MONTH: u64 = 2629743; // 30.44 days
let THREE_MONTHS = ONE_MONTH * 3;
let SIX_MONTHS = ONE_MONTH * 6;
let ONE_YEAR: u64 = 31536000; // 365 days
staking.set_curve_point(ONE_MONTH, 100);
staking.set_curve_point(THREE_MONTHS, 120);
staking.set_curve_point(SIX_MONTHS, 160);
staking.set_curve_point(ONE_YEAR, 250);
}

#[abi(embed_v0)]
Expand Down
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod treasury_types {
mod constants;
mod contract;
mod discussion;
mod staking;
mod merkle_tree;
mod proposals;
mod token;
Expand Down
Loading

0 comments on commit 1d7306e

Please sign in to comment.