Skip to content

Commit

Permalink
tuple store & factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Flydexo committed Jan 8, 2024
1 parent a0f4542 commit 75c69b6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
8 changes: 1 addition & 7 deletions src/factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ use governance::governor::{Config as GovernorConfig};
use governance::governor::{IGovernorDispatcher};
use governance::governance_token::{IGovernanceTokenDispatcher};
use governance::airdrop::{IAirdropDispatcher};
use governance::timelock::{ITimelockDispatcher};
use governance::timelock::{ITimelockDispatcher, TimelockConfig};

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

#[derive(Copy, Drop, Serde)]
struct TimelockConfig {
delay: u64,
window: u64,
}

#[derive(Copy, Drop, Serde)]
struct DeploymentParameters {
name: felt252,
Expand Down
3 changes: 1 addition & 2 deletions src/factory_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use governance::governor::{Config as GovernorConfig};
use governance::factory::{
IFactoryDispatcher, IFactoryDispatcherTrait, Factory, DeploymentParameters, AirdropConfig,
TimelockConfig,
};
use governance::governance_token::{GovernanceToken};
use governance::governor::{Governor};
use governance::timelock::{Timelock, DelayAndWindow};
use governance::timelock::{Timelock, TimelockConfig};
use governance::airdrop::{Airdrop};
use starknet::{
get_contract_address, deploy_syscall, ClassHash, contract_address_const, ContractAddress,
Expand Down
85 changes: 57 additions & 28 deletions src/timelock.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,86 @@ trait ITimelock<TStorage> {
fn get_owner(self: @TStorage) -> ContractAddress;

// Returns the delay and the window for call execution
fn get_configuration(self: @TStorage) -> DelayAndWindow;
fn get_configuration(self: @TStorage) -> TimelockConfig;

// Transfer ownership, i.e. the address that can queue and cancel calls. This must be self-called via #queue.
fn transfer(ref self: TStorage, to: ContractAddress);
// Configure the delay and the window for call execution. This must be self-called via #queue.
fn configure(ref self: TStorage, delay_and_window: DelayAndWindow);
fn configure(ref self: TStorage, delay_and_window: TimelockConfig);
}


const U64_MASK: u128 = 0xFFFFFFFFFFFFFFFF;
const TWO_POW_64: u128 = 0x10000000000000000;

impl ThreeU64TupleStorePacking of StorePacking<(u64, u64, u64), felt252> {
fn pack(value: (u64, u64, u64)) -> felt252 {
let (a, b, c) = value;
u256 {
low: a.into()+b.into()*TWO_POW_64,
high: c.into()
}.try_into().unwrap()
}
fn unpack(value: felt252) -> (u64, u64, u64) {
let u256_value: u256 = value.into();
(
(u256_value.low & U64_MASK).try_into().unwrap(),
(u256_value.low/TWO_POW_64).try_into().unwrap(),
(u256_value.high).try_into().unwrap()
)
}
}

impl TwoU64TupleStorePacking of StorePacking<(u64, u64), u128> {
fn pack(value: (u64, u64)) -> u128 {
let (a, b) = value;
a.into()+b.into()*TWO_POW_64
}
fn unpack(value: u128) -> (u64, u64) {
(
(value & U64_MASK).try_into().unwrap(),
(value/TWO_POW_64).try_into().unwrap()
)
}
}

#[derive(Copy, Drop, Serde)]
struct ExecutionState {
started: u64,
executed: u64,
canceled: u64
}

impl ExecutionStateStorePacking of StorePacking<ExecutionState, felt252> {
fn pack(value: ExecutionState) -> felt252 {
u256 {
low: value.started.into()+value.executed.into()*TWO_POW_64,
high: value.canceled.into()
}.try_into().unwrap()
#[inline(always)]
impl ExecutionStateStorePacking of StorePacking<ExecutionState, (u64, u64, u64)> {
fn pack(value: ExecutionState) -> (u64, u64, u64) {
(value.started, value.executed, value.canceled)
}
fn unpack(value: felt252) -> ExecutionState {
let u256_value: u256 = value.into();
fn unpack(value: (u64, u64, u64)) -> ExecutionState {
let (started, executed, canceled) = value;
ExecutionState {
started: (u256_value.low & U64_MASK).try_into().unwrap(),
executed: (u256_value.low/TWO_POW_64).try_into().unwrap(),
canceled: (u256_value.high).try_into().unwrap()
started,
executed,
canceled
}
}
}

#[derive(Copy, Drop, Serde)]
struct DelayAndWindow {
struct TimelockConfig {
delay: u64,
window: u64
window: u64,
}

impl DelayAndWindowStorePacking of StorePacking<DelayAndWindow, felt252> {
fn pack(value: DelayAndWindow) -> felt252 {
(value.delay.into()+value.window.into()*TWO_POW_64).into()
impl TimelockConfigStorePacking of StorePacking<TimelockConfig, (u64, u64)> {
fn pack(value: TimelockConfig) -> (u64, u64) {
(value.delay, value.window)
}
fn unpack(value: felt252) -> DelayAndWindow {
let u256_value: u256 = value.into();
DelayAndWindow {
delay: (u256_value.low & U64_MASK).try_into().unwrap(),
window: (u256_value.low/TWO_POW_64).try_into().unwrap(),
fn unpack(value: (u64, u64)) -> TimelockConfig {
let (delay, window) = value;
TimelockConfig {
delay,
window
}
}
}
Expand All @@ -101,7 +130,7 @@ impl ExecutionWindowStorePacking of StorePacking<ExecutionWindow, felt252> {

#[starknet::contract]
mod Timelock {
use super::{ITimelock, ContractAddress, Call, DelayAndWindow, ExecutionState, DelayAndWindowStorePacking, ExecutionStateStorePacking, ExecutionWindow, ExecutionWindowStorePacking};
use super::{ITimelock, ContractAddress, Call, TimelockConfig, ExecutionState, TimelockConfigStorePacking, ExecutionStateStorePacking, ExecutionWindow, ExecutionWindowStorePacking, TwoU64TupleStorePacking, ThreeU64TupleStorePacking};
use governance::call_trait::{CallTrait, HashCall};
use hash::{LegacyHash};
use array::{ArrayTrait, SpanTrait};
Expand Down Expand Up @@ -140,13 +169,13 @@ mod Timelock {
#[storage]
struct Storage {
owner: ContractAddress,
delay_and_window: DelayAndWindow,
delay_and_window: TimelockConfig,
// started_executed_canceled
execution_state: LegacyMap<felt252, ExecutionState>,
}

#[constructor]
fn constructor(ref self: ContractState, owner: ContractAddress, delay_and_window: DelayAndWindow) {
fn constructor(ref self: ContractState, owner: ContractAddress, delay_and_window: TimelockConfig) {
self.owner.write(owner);
self.delay_and_window.write(delay_and_window);
}
Expand Down Expand Up @@ -251,7 +280,7 @@ mod Timelock {
self.owner.read()
}

fn get_configuration(self: @ContractState) -> DelayAndWindow {
fn get_configuration(self: @ContractState) -> TimelockConfig {
self.delay_and_window.read()
}

Expand All @@ -261,7 +290,7 @@ mod Timelock {
self.owner.write(to);
}

fn configure(ref self: ContractState, delay_and_window: DelayAndWindow) {
fn configure(ref self: ContractState, delay_and_window: TimelockConfig) {
self.check_self_call();

self.delay_and_window.write(delay_and_window);
Expand Down
2 changes: 1 addition & 1 deletion src/timelock_test.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use array::{Array, ArrayTrait, SpanTrait};
use debug::PrintTrait;
use governance::timelock::{ITimelockDispatcher, ITimelockDispatcherTrait, Timelock, DelayAndWindow, DelayAndWindowStorePacking, ExecutionState, ExecutionStateStorePacking};
use governance::timelock::{ITimelockDispatcher, ITimelockDispatcherTrait, Timelock, TimelockConfig, TimelockConfigStorePacking, ExecutionState, ExecutionStateStorePacking};
use governance::governance_token_test::{deploy as deploy_token};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use governance::governance_token::{IGovernanceTokenDispatcher, IGovernanceTokenDispatcherTrait};
Expand Down

0 comments on commit 75c69b6

Please sign in to comment.