Skip to content

Commit

Permalink
Fix erc20 proposition strategy (#511)
Browse files Browse the repository at this point in the history
* rename block_number to timestamp in proposition_power.cairo

* use timestamp - 1 for proposing power validation strategy
  • Loading branch information
pscott authored Aug 25, 2023
1 parent d243d3c commit 1174181
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ mod tests {
use sx::tests::test_merkle_whitelist::merkle_utils::{
generate_merkle_data, generate_merkle_root, generate_proof
};
use sx::tests::mocks::erc20_votes_preset::{ERC20VotesPreset};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::governance::utils::interfaces::votes::{
IVotes, IVotesDispatcher, IVotesDispatcherTrait
};
use traits::Into;
use sx::voting_strategies::erc20_votes::ERC20VotesVotingStrategy;
use starknet::ContractAddress;

// #[test]
// #[available_gas(10000000000)]
#[test]
#[available_gas(10000000000)]
fn test_vanilla_works() {
starknet::testing::set_block_timestamp(1);

// deploy vanilla voting strategy
let (vanilla_contract, _) = deploy_syscall(
VanillaVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
Expand Down Expand Up @@ -98,6 +108,8 @@ mod tests {
#[test]
#[available_gas(10000000000)]
fn test_merkle_whitelist_works() {
starknet::testing::set_block_timestamp(1);

// deploy merkle whitelist contract
let (merkle_contract, _) = deploy_syscall(
MerkleWhitelistVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(),
Expand Down Expand Up @@ -233,4 +245,84 @@ mod tests {
let is_validated = contract.validate(author, params.clone(), user_params.clone());
assert(!is_validated, 'should not have enough VP');
}

fn strategy_from_contract(token_contract: ContractAddress) -> Strategy {
let (contract, _) = deploy_syscall(
ERC20VotesVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(),
0,
array![].span(),
false,
)
.unwrap();

let params: Array<felt252> = array![token_contract.into()];

Strategy { address: contract, params, }
}

#[test]
#[available_gas(10000000000)]
fn test_erc20votes_works() {
let SUPPLY = 100_u256;

// deploy erc20 voting strategy
let owner = contract_address_const::<'owner'>();
let mut constructor = array!['TEST', 'TST'];
SUPPLY.serialize(ref constructor);
owner.serialize(ref constructor);

let (erc20_contract, _) = deploy_syscall(
ERC20VotesPreset::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor.span(), false
)
.unwrap();

let erc20 = IVotesDispatcher { contract_address: erc20_contract, };

let erc20_strategy = strategy_from_contract(erc20_contract);

starknet::testing::set_contract_address(owner);
erc20.delegate(owner);

// advance block timestamp
starknet::testing::set_block_timestamp(1);

// create a proposal validation strategy
let (proposal_validation_contract, _) = deploy_syscall(
ProposingPowerProposalValidationStrategy::TEST_CLASS_HASH.try_into().unwrap(),
0,
array![].span(),
false
)
.unwrap();

let allowed_strategies = array![erc20_strategy.clone()];
let mut params = array![];
let proposal_threshold = SUPPLY;
proposal_threshold.serialize(ref params);
allowed_strategies.serialize(ref params);

// used strategies
let used_strategy = IndexedStrategy { index: 0, params: array![], };
let used_strategies = array![used_strategy.clone()];
let mut user_params = array![];
used_strategies.serialize(ref user_params);

let contract = IProposalValidationStrategyDispatcher {
contract_address: proposal_validation_contract,
};

let author = UserAddress::Starknet(owner);

let is_validated = contract.validate(author, params, user_params.clone());
assert(is_validated, 'not enough VP');

// Now increase threshold
let proposal_threshold = SUPPLY + 1;
let mut params = array![];
proposal_threshold.serialize(ref params);
allowed_strategies.serialize(ref params);

let is_validated = contract.validate(author, params.clone(), user_params);
assert(!is_validated, 'Threshold should not be reached');
}
}
1 change: 0 additions & 1 deletion starknet/src/types/proposal.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use sx::utils::math::pow;
use traits::{Into, TryInto};
use sx::types::{FinalizationStatus, UserAddress};
use option::OptionTrait;
use debug::PrintTrait;

const BITMASK_32: u128 = 0xffffffff;
const BITMASK_64: u128 = 0xffffffffffffffff;
Expand Down
6 changes: 3 additions & 3 deletions starknet/src/utils/proposition_power.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use clone::Clone;

fn _get_cumulative_power(
voter: UserAddress,
block_number: u32,
timestamp: u32,
mut user_strategies: Array<IndexedStrategy>,
allowed_strategies: Array<Strategy>,
) -> u256 {
Expand All @@ -30,7 +30,7 @@ fn _get_cumulative_power(
contract_address: strategy.address
}
.get_voting_power(
block_number, voter, strategy.params, indexed_strategy.params,
timestamp, voter, strategy.params, indexed_strategy.params,
);
},
Option::None => {
Expand Down Expand Up @@ -60,7 +60,7 @@ fn _validate(
let user_strategies = Serde::<Array<IndexedStrategy>>::deserialize(ref user_params_span)
.unwrap();

let timestamp: u32 = info::get_block_timestamp().try_into().unwrap();
let timestamp: u32 = info::get_block_timestamp().try_into().unwrap() - 1;
let voting_power = _get_cumulative_power(
author, timestamp, user_strategies, allowed_strategies
);
Expand Down

0 comments on commit 1174181

Please sign in to comment.