Skip to content

Commit

Permalink
separate airdrop from the other contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Mar 15, 2024
1 parent 367ddb0 commit 28f9c19
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/airdrop.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Claim {
pub amount: u128,
}

#[derive(Copy, Drop, Serde, Hash, PartialEq, Debug, starknet::Store)]
#[derive(Copy, Drop, Serde, PartialEq, Debug, starknet::Store)]
pub struct Config {
// the root of the airdrop
pub root: felt252,
Expand Down
15 changes: 1 addition & 14 deletions src/airdrop_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,13 @@ use governance::airdrop::{
Airdrop::{compute_pedersen_root, hash_function, hash_claim, compute_root_of_group}, Claim
};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use governance::test::test_token::{TestToken};
use governance::test::test_token::{TestToken, deploy as deploy_token};
use starknet::testing::{pop_log, set_block_timestamp};
use starknet::{
get_contract_address, syscalls::{deploy_syscall}, ClassHash, contract_address_const,
ContractAddress
};


pub(crate) fn deploy_token(owner: ContractAddress, amount: u128) -> IERC20Dispatcher {
let mut constructor_args: Array<felt252> = ArrayTrait::new();
Serde::serialize(@(owner, amount), ref constructor_args);

let (address, _) = deploy_syscall(
TestToken::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_args.span(), true
)
.expect('DEPLOY_TOKEN_FAILED');
IERC20Dispatcher { contract_address: address }
}


fn deploy_with_refundee(
token: ContractAddress, root: felt252, refundable_timestamp: u64, refund_to: ContractAddress
) -> IAirdropDispatcher {
Expand Down
2 changes: 1 addition & 1 deletion src/call_trait_test.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::array::{Array, ArrayTrait};
use core::hash::{LegacyHash};
use core::serde::{Serde};
use governance::airdrop_test::{deploy_token};
use governance::test::test_token::{deploy as deploy_token};
use governance::call_trait::{CallTrait, HashCall};
use starknet::{contract_address_const, get_contract_address, account::{Call}};

Expand Down
5 changes: 1 addition & 4 deletions src/e2e_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use core::option::{OptionTrait};

use core::result::{Result};
use core::traits::{TryInto};
use governance::airdrop::{Airdrop};
use governance::airdrop::{IAirdropDispatcherTrait};
use governance::airdrop_test::{deploy_token};
use governance::test::test_token::{deploy as deploy_token};
use governance::factory::{
IFactoryDispatcher, IFactoryDispatcherTrait, Factory, DeploymentParameters, DeploymentResult,
};
Expand Down Expand Up @@ -45,7 +43,6 @@ impl DefaultDeploymentParameters of Default<DeploymentParameters> {
proposal_creation_threshold: 20
},
timelock_config: TimelockConfig { delay: 30, window: 90, },
airdrop_config: Option::None
}
}
}
Expand Down
28 changes: 2 additions & 26 deletions src/factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ use starknet::{ContractAddress};
pub struct DeploymentParameters {
pub governor_config: GovernorConfig,
pub timelock_config: TimelockConfig,
pub airdrop_config: Option<AirdropConfig>,
}

#[derive(Copy, Drop, Serde)]
pub struct DeploymentResult {
pub staker: IStakerDispatcher,
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
Expand All @@ -35,13 +33,12 @@ pub mod Factory {
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use starknet::{ClassHash, syscalls::{deploy_syscall}, get_caller_address, get_contract_address};
use super::{
IFactory, DeploymentParameters, DeploymentResult, ContractAddress, IAirdropDispatcher,
IGovernorDispatcher, ITimelockDispatcher, IStakerDispatcher
IFactory, DeploymentParameters, DeploymentResult, ContractAddress, IGovernorDispatcher,
ITimelockDispatcher, IStakerDispatcher
};

#[storage]
struct Storage {
airdrop_class_hash: ClassHash,
staker_class_hash: ClassHash,
governor_class_hash: ClassHash,
timelock_class_hash: ClassHash,
Expand All @@ -50,12 +47,10 @@ pub mod Factory {
#[constructor]
fn constructor(
ref self: ContractState,
airdrop_class_hash: ClassHash,
staker_class_hash: ClassHash,
governor_class_hash: ClassHash,
timelock_class_hash: ClassHash
) {
self.airdrop_class_hash.write(airdrop_class_hash);
self.staker_class_hash.write(staker_class_hash);
self.governor_class_hash.write(governor_class_hash);
self.timelock_class_hash.write(timelock_class_hash);
Expand Down Expand Up @@ -89,24 +84,6 @@ pub mod Factory {
)
.unwrap();

let airdrop = match params.airdrop_config {
Option::Some(config) => {
let mut airdrop_constructor_args: Array<felt252> = ArrayTrait::new();
Serde::serialize(@(token, config), ref airdrop_constructor_args);

let (airdrop_address, _) = deploy_syscall(
class_hash: self.airdrop_class_hash.read(),
contract_address_salt: 0,
calldata: airdrop_constructor_args.span(),
deploy_from_zero: false,
)
.unwrap();

Option::Some(IAirdropDispatcher { contract_address: airdrop_address })
},
Option::None => { Option::None }
};

let mut timelock_constructor_args: Array<felt252> = ArrayTrait::new();
Serde::serialize(
@(governor_address, params.timelock_config.delay, params.timelock_config.window),
Expand All @@ -122,7 +99,6 @@ pub mod Factory {
.unwrap();

DeploymentResult {
airdrop,
staker: IStakerDispatcher { contract_address: staker_address },
governor: IGovernorDispatcher { contract_address: governor_address },
timelock: ITimelockDispatcher { contract_address: timelock_address }
Expand Down
22 changes: 0 additions & 22 deletions src/factory_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use core::option::{OptionTrait};

use core::result::{Result};
use core::traits::{TryInto};
use governance::airdrop::{Airdrop, Config as AirdropConfig};
use governance::airdrop::{IAirdropDispatcherTrait};
use governance::airdrop_test::{deploy_token};
use governance::factory::{
IFactoryDispatcher, IFactoryDispatcherTrait, Factory, DeploymentParameters,
};
Expand All @@ -25,7 +22,6 @@ pub(crate) fn deploy() -> IFactoryDispatcher {
let mut constructor_args: Array<felt252> = ArrayTrait::new();
Serde::serialize(
@(
Airdrop::TEST_CLASS_HASH,
Staker::TEST_CLASS_HASH,
Governor::TEST_CLASS_HASH,
Timelock::TEST_CLASS_HASH
Expand Down Expand Up @@ -54,13 +50,6 @@ fn test_deploy() {
.deploy(
token,
DeploymentParameters {
airdrop_config: Option::Some(
AirdropConfig {
root: 'root',
refundable_timestamp: 312,
refund_to: contract_address_const::<'refund'>(),
}
),
governor_config: GovernorConfig {
voting_start_delay: 0,
voting_period: 180,
Expand All @@ -72,17 +61,6 @@ fn test_deploy() {
}
);

let drop = result.airdrop.unwrap();
assert_eq!(
drop.get_config(),
AirdropConfig {
root: 'root',
refundable_timestamp: 312,
refund_to: contract_address_const::<'refund'>(),
}
);
assert_eq!(drop.get_token(), token);

assert_eq!(result.staker.get_token(), token);

assert_eq!(result.governor.get_staker().contract_address, result.staker.contract_address);
Expand Down
2 changes: 1 addition & 1 deletion src/staker_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::num::traits::zero::{Zero};
use core::option::{OptionTrait};
use core::result::{Result, ResultTrait};
use core::traits::{TryInto};
use governance::airdrop_test::{deploy_token};
use governance::test::test_token::{deploy as deploy_token};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};

use governance::staker::{
Expand Down
15 changes: 15 additions & 0 deletions src/test/test_token.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,18 @@ pub(crate) mod TestToken {
}
}
}

use starknet::{ContractAddress, syscalls::{deploy_syscall}};
use governance::interfaces::erc20::{IERC20Dispatcher};

#[cfg(test)]
pub(crate) fn deploy(owner: ContractAddress, amount: u128) -> IERC20Dispatcher {
let mut constructor_args: Array<felt252> = ArrayTrait::new();
Serde::serialize(@(owner, amount), ref constructor_args);

let (address, _) = deploy_syscall(
TestToken::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_args.span(), true
)
.expect('DEPLOY_TOKEN_FAILED');
IERC20Dispatcher { contract_address: address }
}
2 changes: 1 addition & 1 deletion src/timelock_test.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::array::{Array, ArrayTrait, SpanTrait};
use governance::airdrop_test::{deploy_token};
use governance::test::test_token::{deploy as deploy_token};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use governance::timelock::{
ITimelockDispatcher, ITimelockDispatcherTrait, Timelock, TimelockConfig,
Expand Down

0 comments on commit 28f9c19

Please sign in to comment.