Skip to content

Commit

Permalink
2.5.0 latest edition
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Jan 23, 2024
1 parent 1322886 commit 99ad019
Show file tree
Hide file tree
Showing 19 changed files with 221 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.4.3
scarb 2.5.0
6 changes: 3 additions & 3 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ name = "governance"
version = "0.1.0"
description = "Contracts for governance of Starknet-native protocols"
homepage = "https://ekubo.org"
cairo-version = "2.4.3"
edition = '2023_10'
cairo-version = "2.5.0"
edition = '2023_11'



[dependencies]
starknet = "=2.4.3"
starknet = "=2.5.0"

[[target.starknet-contract]]
allowed-libfuncs-list.name = "audited"
Expand Down
21 changes: 10 additions & 11 deletions src/airdrop.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use governance::interfaces::erc20::{IERC20Dispatcher};
use starknet::{ContractAddress};

#[derive(Copy, Drop, Serde, Hash, PartialEq)]
struct Claim {
claimee: ContractAddress,
amount: u128,
pub struct Claim {
pub claimee: ContractAddress,
pub amount: u128,
}

#[starknet::interface]
trait IAirdrop<TStorage> {
pub trait IAirdrop<TStorage> {
// Claims the given allotment of tokens
fn claim(ref self: TStorage, claim: Claim, proof: Array<felt252>);

Expand All @@ -24,21 +24,20 @@ trait IAirdrop<TStorage> {
}

#[starknet::contract]
mod Airdrop {
pub mod Airdrop {
use core::array::{ArrayTrait, SpanTrait};
use core::hash::{LegacyHash};
use governance::interfaces::erc20::{IERC20DispatcherTrait};
use starknet::{ContractAddressIntoFelt252};
use super::{IAirdrop, ContractAddress, Claim, IERC20Dispatcher};

fn lt<X, +Copy<X>, +Into<X, u256>>(lhs: @X, rhs: @X) -> bool {
pub(crate) fn lt<X, +Copy<X>, +Into<X, u256>>(lhs: @X, rhs: @X) -> bool {
let a: u256 = (*lhs).into();
let b: u256 = (*rhs).into();
return a < b;
}

// Compute the pedersen root of a merkle tree by combining the current node with each sibling up the tree
fn compute_pedersen_root(current: felt252, mut proof: Span<felt252>) -> felt252 {
pub(crate) fn compute_pedersen_root(current: felt252, mut proof: Span<felt252>) -> felt252 {
match proof.pop_front() {
Option::Some(proof_element) => {
compute_pedersen_root(
Expand All @@ -62,8 +61,8 @@ mod Airdrop {
}

#[derive(Drop, starknet::Event)]
struct Claimed {
claim: Claim
pub(crate) struct Claimed {
pub claim: Claim
}

#[derive(starknet::Event, Drop)]
Expand All @@ -78,7 +77,7 @@ mod Airdrop {
self.token.write(token);
}

#[external(v0)]
#[abi(embed_v0)]
impl AirdropImpl of IAirdrop<ContractState> {
fn claim(ref self: ContractState, claim: Claim, proof: Array::<felt252>) {
let leaf = LegacyHash::hash(0, claim);
Expand Down
8 changes: 4 additions & 4 deletions src/airdrop_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use governance::governance_token::{
};
use governance::governance_token_test::{deploy as deploy_token};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use starknet::class_hash::Felt252TryIntoClassHash;
use starknet::testing::{pop_log};
use starknet::{
get_contract_address, deploy_syscall, ClassHash, contract_address_const, ContractAddress
get_contract_address, syscalls::{deploy_syscall}, ClassHash, contract_address_const,
ContractAddress
};

fn deploy(token: ContractAddress, root: felt252) -> IAirdropDispatcher {
Expand Down Expand Up @@ -99,8 +99,8 @@ fn test_claim_single_recipient() {
let log = pop_log::<Airdrop::Claimed>(airdrop.contract_address).unwrap();
assert(log.claim == claim, 'claim');

pop_log::<GovernanceToken::Transfer>(token.contract_address);
pop_log::<GovernanceToken::Transfer>(token.contract_address);
pop_log::<GovernanceToken::Transfer>(token.contract_address).unwrap();
pop_log::<GovernanceToken::Transfer>(token.contract_address).unwrap();
let log = pop_log::<GovernanceToken::Transfer>(token.contract_address).unwrap();
assert(log.from == airdrop.contract_address, 'from');
assert(log.to == claim.claimee, 'to');
Expand Down
14 changes: 7 additions & 7 deletions src/call_trait.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use core::array::{ArrayTrait, SpanTrait};
use core::hash::{LegacyHash, HashStateTrait, Hash};
use core::hash::{LegacyHash, HashStateTrait, HashStateExTrait, Hash};
use core::result::{ResultTrait};
use core::traits::{Into};
use starknet::account::{Call};
use starknet::{ContractAddress, ContractAddressIntoFelt252};
use starknet::{ContractAddress};
use starknet::{SyscallResult, syscalls::call_contract_syscall};

impl HashCall<S, +HashStateTrait<S>, +Drop<S>, +Copy<S>> of Hash<@Call, S> {
pub impl HashCall<S, +HashStateTrait<S>, +Drop<S>, +Copy<S>> of Hash<@Call, S> {
fn update_state(state: S, value: @Call) -> S {
let mut s = state.update((*value.to).into()).update(*value.selector);
let mut s = state.update_with((*value.to)).update_with(*value.selector);

let mut data_span = value.calldata.span();
let mut data_span: Span<felt252> = *value.calldata;
loop {
match data_span.pop_front() {
Option::Some(word) => { s = s.update(*word); },
Expand All @@ -23,9 +23,9 @@ impl HashCall<S, +HashStateTrait<S>, +Drop<S>, +Copy<S>> of Hash<@Call, S> {
}

#[generate_trait]
impl CallTraitImpl of CallTrait {
pub impl CallTraitImpl of CallTrait {
fn execute(self: @Call) -> Span<felt252> {
let result = call_contract_syscall(*self.to, *self.selector, self.calldata.span());
let result = call_contract_syscall(*self.to, *self.selector, *self.calldata);

if (result.is_err()) {
panic(result.unwrap_err());
Expand Down
25 changes: 12 additions & 13 deletions src/call_trait_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starknet::{contract_address_const, account::{Call}};
#[test]
#[available_gas(300000000)]
fn test_hash_empty() {
let call = Call { to: contract_address_const::<0>(), selector: 0, calldata: ArrayTrait::new() };
let call = Call { to: contract_address_const::<0>(), selector: 0, calldata: array![].span() };
assert(
LegacyHash::hash(
0, @call
Expand All @@ -20,7 +20,7 @@ fn test_hash_empty() {
#[test]
#[available_gas(300000000)]
fn test_hash_address_one() {
let call = Call { to: contract_address_const::<1>(), selector: 0, calldata: ArrayTrait::new() };
let call = Call { to: contract_address_const::<1>(), selector: 0, calldata: array![].span() };
assert(
LegacyHash::hash(
0, @call
Expand All @@ -32,7 +32,7 @@ fn test_hash_address_one() {
#[test]
#[available_gas(300000000)]
fn test_hash_address_entry_point_one() {
let call = Call { to: contract_address_const::<0>(), selector: 1, calldata: ArrayTrait::new() };
let call = Call { to: contract_address_const::<0>(), selector: 1, calldata: array![].span() };
assert(
LegacyHash::hash(
0, @call
Expand All @@ -44,7 +44,7 @@ fn test_hash_address_entry_point_one() {
#[test]
#[available_gas(300000000)]
fn test_hash_address_data_one() {
let call = Call { to: contract_address_const::<0>(), selector: 0, calldata: array![1] };
let call = Call { to: contract_address_const::<0>(), selector: 0, calldata: array![1].span() };

assert(
LegacyHash::hash(
Expand All @@ -57,7 +57,9 @@ fn test_hash_address_data_one() {
#[test]
#[available_gas(300000000)]
fn test_hash_address_data_one_two() {
let call = Call { to: contract_address_const::<0>(), selector: 0, calldata: array![1, 2] };
let call = Call {
to: contract_address_const::<0>(), selector: 0, calldata: array![1, 2].span()
};

assert(
LegacyHash::hash(
Expand All @@ -71,8 +73,7 @@ fn test_hash_address_data_one_two() {
#[available_gas(300000000)]
#[should_panic(expected: ('CONTRACT_NOT_DEPLOYED',))]
fn test_execute_contract_not_deployed() {
let mut calldata: Array<felt252> = ArrayTrait::new();
let call = Call { to: contract_address_const::<0>(), selector: 0, calldata: calldata };
let call = Call { to: contract_address_const::<0>(), selector: 0, calldata: array![].span() };
call.execute();
}

Expand All @@ -83,8 +84,7 @@ fn test_execute_contract_not_deployed() {
fn test_execute_invalid_entry_point() {
let (token, _) = deploy_token('TIMELOCK', 'TL', 1);

let mut calldata: Array<felt252> = ArrayTrait::new();
let call = Call { to: token.contract_address, selector: 0, calldata: calldata };
let call = Call { to: token.contract_address, selector: 0, calldata: array![].span() };

call.execute();
}
Expand All @@ -96,12 +96,11 @@ fn test_execute_invalid_entry_point() {
fn test_execute_invalid_call_data_too_short() {
let (token, _) = deploy_token('TIMELOCK', 'TL', 1);

let mut calldata: Array<felt252> = ArrayTrait::new();
let call = Call {
to: token.contract_address,
// transfer
selector: 0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e,
calldata: calldata
calldata: array![].span()
};

call.execute();
Expand All @@ -113,14 +112,14 @@ fn test_execute_invalid_call_data_too_short() {
fn test_execute_valid_call_data() {
let (token, _) = deploy_token('TIMELOCK', 'TL', 1);

let mut calldata: Array<felt252> = ArrayTrait::new();
let mut calldata: Array<felt252> = array![];
Serde::serialize(@(contract_address_const::<1>(), 1_u256), ref calldata);

let call = Call {
to: token.contract_address,
// transfer
selector: 0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e,
calldata: calldata
calldata: calldata.span()
};

call.execute();
Expand Down
38 changes: 19 additions & 19 deletions src/factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ use governance::timelock::{ITimelockDispatcher, TimelockConfig};
use starknet::{ContractAddress};

#[derive(Copy, Drop, Serde)]
struct AirdropConfig {
root: felt252,
total: u128,
pub struct AirdropConfig {
pub root: felt252,
pub total: u128,
}

#[derive(Copy, Drop, Serde)]
struct DeploymentParameters {
name: felt252,
symbol: felt252,
total_supply: u128,
governor_config: GovernorConfig,
timelock_config: TimelockConfig,
airdrop_config: Option<AirdropConfig>,
pub struct DeploymentParameters {
pub name: felt252,
pub symbol: felt252,
pub total_supply: u128,
pub governor_config: GovernorConfig,
pub timelock_config: TimelockConfig,
pub airdrop_config: Option<AirdropConfig>,
}

#[derive(Copy, Drop, Serde)]
struct DeploymentResult {
token: IGovernanceTokenDispatcher,
governor: IGovernorDispatcher,
timelock: ITimelockDispatcher,
airdrop: Option<IAirdropDispatcher>,
pub struct DeploymentResult {
pub token: IGovernanceTokenDispatcher,
pub governor: IGovernorDispatcher,
pub timelock: ITimelockDispatcher,
pub airdrop: Option<IAirdropDispatcher>,
}

// This contract makes it easy to deploy a set of governance contracts from a block explorer just by specifying parameters
#[starknet::interface]
trait IFactory<TContractState> {
pub trait IFactory<TContractState> {
fn deploy(self: @TContractState, params: DeploymentParameters) -> DeploymentResult;
}

#[starknet::contract]
mod Factory {
pub mod Factory {
use core::result::{ResultTrait};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use starknet::{ClassHash, deploy_syscall, get_caller_address, get_contract_address};
use starknet::{ClassHash, syscalls::{deploy_syscall}, get_caller_address, get_contract_address};
use super::{
IFactory, DeploymentParameters, DeploymentResult, ContractAddress,
IGovernanceTokenDispatcher, IAirdropDispatcher, IGovernorDispatcher, ITimelockDispatcher
Expand Down Expand Up @@ -67,7 +67,7 @@ mod Factory {
self.timelock.write(timelock);
}

#[external(v0)]
#[abi(embed_v0)]
impl FactoryImpl of IFactory<ContractState> {
fn deploy(self: @ContractState, params: DeploymentParameters) -> DeploymentResult {
let mut token_constructor_args: Array<felt252> = ArrayTrait::new();
Expand Down
4 changes: 2 additions & 2 deletions src/factory_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use governance::governor::{Governor};
use governance::governor::{IGovernorDispatcherTrait};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use governance::timelock::{Timelock, ITimelockDispatcherTrait, TimelockConfig};
use starknet::class_hash::{Felt252TryIntoClassHash};
use starknet::testing::{set_contract_address, set_block_timestamp, pop_log};
use starknet::{
get_contract_address, deploy_syscall, ClassHash, contract_address_const, ContractAddress,
get_contract_address, syscalls::deploy_syscall, ClassHash, contract_address_const,
ContractAddress,
};

fn deploy() -> IFactoryDispatcher {
Expand Down
Loading

0 comments on commit 99ad019

Please sign in to comment.