Skip to content

Commit

Permalink
Refactor for Components (#57)
Browse files Browse the repository at this point in the history
* Refactor for Components and Cairo 2.6.3

* Polish with Scarb fmt

* Update CI

* Remove duplicit install from CI

* Trigger CI

* Update CI

* Update CI to use foundry-rs/setup-snfoundry

* Trigger CI
  • Loading branch information
tensojka authored Mar 18, 2024
1 parent f8946d6 commit 083d6f5
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 440 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 2.4.0",
"postCreateCommand": "curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 2.6.3",
"customizations": {
"vscode": {
"extensions": [
Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
- 'Scarb.toml'

env:
SCARB_VERSION: 2.4.0
FOUNDRY_VERSION: 0.14.0
SCARB_VERSION: 2.6.3
FOUNDRY_VERSION: 0.19.0

jobs:
build:
Expand All @@ -20,11 +20,10 @@ jobs:
uses: actions/checkout@v3
- name: Install Scarb
run: curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v $SCARB_VERSION
- name: Install SnFoundryUp
run: curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh
- name: Install SnFoundry
run: snfoundryup -v $FOUNDRY_VERSION
- name: Check formatting
run: scarb fmt --check
- uses: foundry-rs/setup-snfoundry@v3
with:
starknet-foundry-version: ${{ env.FOUNDRY_VERSION }}
- name: Run tests with snforge
run: snforge test
8 changes: 4 additions & 4 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ version = 1
[[package]]
name = "cubit"
version = "1.3.0"
source = "git+https://github.com/akhercha/cubit.git?branch=chore%2Fcairo_upgrade#d3869a3f0c47e5ed229bbbfe2fce3fc0510cbc8a"
source = "git+https://github.com/influenceth/cubit.git#62756082bf2555d7ab25c69d9c7bc30574ff1ce8"

[[package]]
name = "governance"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"cubit",
"snforge_std",
]

[[package]]
name = "snforge_std"
version = "0.14.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git#a1caebbfffd00e612c09ed89da006f6c48a92fd9"
version = "0.19.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.19.0#a3391dce5bdda51c63237032e6cfc64fb7a346d4"
16 changes: 7 additions & 9 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
[package]
name = "governance"
description = "A flexible monolithic governance contract, developed for use with Carmine Options AMM"
version = "0.2.0"
cairo-version = "2.4.0"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest
version = "0.3.0"
cairo-version = "2.6.3"

[dependencies]
cubit = { git = "https://github.com/akhercha/cubit.git", branch = "chore/cairo_upgrade" }
cubit = { git = "https://github.com/influenceth/cubit.git", commit = "62756082bf2555d7ab25c69d9c7bc30574ff1ce8" }
starknet = ">=1.3.0"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", commit = "a1caebbfffd00e612c09ed89da006f6c48a92fd9"}
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.19.0" }

[[target.starknet-contract]]

[[tool.snforge.fork]]
name = "MAINNET"
url = "http://34.22.208.73:6060/"
url = "http://34.22.208.73:6060/v0_6"
block_id.tag = "Latest"

[[tool.snforge.fork]]
name = "GOERLI"
url = "https://limited-rpc.nethermind.io/goerli-juno"
block_id.tag = "Latest"
url = "http://34.22.208.73:6061/v0_6"
block_id.tag = "Latest"
89 changes: 18 additions & 71 deletions src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,13 @@ use governance::types::{ContractType, PropDetails, VoteStatus};
trait IGovernance<TContractState> {
// PROPOSALS

fn vote(ref self: TContractState, prop_id: felt252, opinion: felt252);
fn get_proposal_details(self: @TContractState, prop_id: felt252) -> PropDetails;
fn get_vote_counts(self: @TContractState, prop_id: felt252) -> (u128, u128);
fn submit_proposal(
ref self: TContractState, impl_hash: felt252, to_upgrade: ContractType
) -> felt252;
fn get_proposal_status(self: @TContractState, prop_id: felt252) -> felt252;
fn get_live_proposals(self: @TContractState) -> Array<felt252>;
fn get_user_voted(
self: @TContractState, user_address: ContractAddress, prop_id: felt252
) -> VoteStatus;
// in component

// UPGRADES

fn get_governance_token_address(self: @TContractState) -> ContractAddress;
fn get_amm_address(self: @TContractState) -> ContractAddress;
fn apply_passed_proposal(ref self: TContractState, prop_id: felt252);
// fn apply_passed_proposal(ref self: TContractState, prop_id: felt252);
// AIRDROPS

// in component
Expand All @@ -37,38 +27,36 @@ trait IGovernance<TContractState> {
mod Governance {
use governance::types::BlockNumber;
use governance::types::VoteStatus;
use governance::proposals::Proposals;
use governance::types::ContractType;
use governance::types::PropDetails;
use governance::upgrades::Upgrades;
use governance::proposals::proposals as proposals_component;
use governance::upgrades::upgrades as upgrades_component;
use governance::airdrop::airdrop as airdrop_component;

use starknet::ContractAddress;


component!(path: airdrop_component, storage: airdrop, event: AirdropEvent);
component!(path: proposals_component, storage: proposals, event: ProposalsEvent);
component!(path: upgrades_component, storage: upgrades, event: UpgradesEvent);

#[abi(embed_v0)]
impl Airdrop = airdrop_component::AirdropImpl<ContractState>;

#[abi(embed_v0)]
impl Proposals = proposals_component::ProposalsImpl<ContractState>;

#[storage]
struct Storage {
proposal_details: LegacyMap::<felt252, PropDetails>,
proposal_vote_ends: LegacyMap::<felt252, BlockNumber>,
proposal_vote_end_timestamp: LegacyMap::<felt252, u64>,
proposal_voted_by: LegacyMap::<(felt252, ContractAddress), VoteStatus>,
proposal_total_yay: LegacyMap::<felt252, felt252>,
proposal_total_nay: LegacyMap::<felt252, felt252>,
proposal_applied: LegacyMap::<felt252, felt252>, // should be Bool after migration
proposal_initializer_run: LegacyMap::<u64, bool>,
investor_voting_power: LegacyMap::<ContractAddress, felt252>,
total_investor_distributed_power: felt252,
governance_token_address: ContractAddress,
amm_address: ContractAddress,
delegate_hash: LegacyMap::<ContractAddress, felt252>,
total_delegated_to: LegacyMap::<ContractAddress, u128>,
#[substorage(v0)]
airdrop: airdrop_component::Storage
proposals: proposals_component::Storage,
#[substorage(v0)]
airdrop: airdrop_component::Storage,
#[substorage(v0)]
upgrades: upgrades_component::Storage
}

// PROPOSALS
Expand All @@ -92,7 +80,9 @@ mod Governance {
enum Event {
Proposed: Proposed,
Voted: Voted,
AirdropEvent: airdrop_component::Event
AirdropEvent: airdrop_component::Event,
ProposalsEvent: proposals_component::Event,
UpgradesEvent: upgrades_component::Event
}

#[constructor]
Expand All @@ -101,57 +91,14 @@ mod Governance {
self.governance_token_address.write(govtoken_address);
}

#[external(v0)]
#[abi(embed_v0)]
impl Governance of super::IGovernance<ContractState> {
// PROPOSALS

fn get_proposal_details(self: @ContractState, prop_id: felt252) -> PropDetails {
Proposals::get_proposal_details(prop_id)
}

// This should ideally return VoteCounts, but it seems like structs can't be returned from
// C1.0 external fns as they can't be serialized
// Actually it can, TODO do the same as I did with PropDetails for this
fn get_vote_counts(self: @ContractState, prop_id: felt252) -> (u128, u128) {
Proposals::get_vote_counts(prop_id)
}

fn submit_proposal(
ref self: ContractState, impl_hash: felt252, to_upgrade: ContractType
) -> felt252 {
Proposals::submit_proposal(impl_hash, to_upgrade)
}

fn vote(ref self: ContractState, prop_id: felt252, opinion: felt252) {
Proposals::vote(prop_id, opinion)
}

fn get_proposal_status(self: @ContractState, prop_id: felt252) -> felt252 {
Proposals::get_proposal_status(prop_id)
}

fn get_live_proposals(self: @ContractState) -> Array<felt252> {
Proposals::get_live_proposals()
}

fn get_user_voted(
self: @ContractState, user_address: ContractAddress, prop_id: felt252
) -> VoteStatus {
Proposals::get_user_voted(user_address, prop_id)
}

// UPGRADES

fn get_governance_token_address(self: @ContractState) -> ContractAddress {
self.governance_token_address.read()
}

fn get_amm_address(self: @ContractState) -> ContractAddress {
self.amm_address.read()
}

fn apply_passed_proposal(ref self: ContractState, prop_id: felt252) {
Upgrades::apply_passed_proposal(prop_id)
}
}
}
2 changes: 1 addition & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod amm_types {
mod constants;
mod contract;
mod merkle_tree;
mod options;
//mod options;
mod proposals;
mod traits;
mod types;
Expand Down
Loading

0 comments on commit 083d6f5

Please sign in to comment.