Skip to content

Commit

Permalink
improve airdrop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Mar 3, 2024
1 parent b47a39e commit a504d69
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/airdrop.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::array::{Array};
use governance::interfaces::erc20::{IERC20Dispatcher};
use starknet::{ContractAddress};

#[derive(Copy, Drop, Serde, Hash, PartialEq)]
#[derive(Copy, Drop, Serde, Hash, PartialEq, Debug)]
pub struct Claim {
// the unique ID of the claim
pub id: u64,
Expand Down Expand Up @@ -100,11 +100,11 @@ pub mod Airdrop {
}

fn claim(ref self: ContractState, claim: Claim, proof: Array::<felt252>) {
assert(!self.is_claimed(claim.id), 'ALREADY_CLAIMED');

let leaf = LegacyHash::hash(0, claim);
assert(self.root.read() == compute_pedersen_root(leaf, proof.span()), 'INVALID_PROOF');

assert(!self.is_claimed(claim.id), 'ALREADY_CLAIMED');

let (word, index) = claim_id_to_bitmap_index(claim.id);
let bitmap = self.claimed_bitmap.read(word);

Expand Down
60 changes: 21 additions & 39 deletions src/airdrop_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,43 @@ fn deploy(token: ContractAddress, root: felt252) -> IAirdropDispatcher {
}

#[test]
#[available_gas(3000000)]
fn test_compute_pedersen_root_example_lt() {
assert(
compute_pedersen_root(
1234, array![1235].span()
) == 0x24e78083d17aa2e76897f44cfdad51a09276dd00a3468adc7e635d76d432a3b,
'example'
assert_eq!(
compute_pedersen_root(1234, array![1235].span()),
0x24e78083d17aa2e76897f44cfdad51a09276dd00a3468adc7e635d76d432a3b
);
}

#[test]
#[available_gas(3000000)]
fn test_compute_pedersen_root_example_gt() {
assert(
compute_pedersen_root(
1234, array![1233].span()
) == 0x2488766c14e4bfd8299750797eeb07b7045398df03ea13cf33f0c0c6645d5f9,
'example'
assert_eq!(
compute_pedersen_root(1234, array![1233].span()),
0x2488766c14e4bfd8299750797eeb07b7045398df03ea13cf33f0c0c6645d5f9
);
}

#[test]
#[available_gas(3000000)]
fn test_compute_pedersen_root_example_eq() {
assert(
compute_pedersen_root(
1234, array![1234].span()
) == 0x7a7148565b76ae90576733160aa3194a41ce528ee1434a64a9da50dcbf6d3ca,
'example'
assert_eq!(
compute_pedersen_root(1234, array![1234].span()),
0x7a7148565b76ae90576733160aa3194a41ce528ee1434a64a9da50dcbf6d3ca
);
}

#[test]
#[available_gas(3000000)]
fn test_compute_pedersen_root_empty() {
assert(compute_pedersen_root(1234, array![].span()) == 1234, 'example');
assert_eq!(compute_pedersen_root(1234, array![].span()), 1234);
}

#[test]
#[available_gas(3000000)]
fn test_compute_pedersen_root_recursive() {
assert(
compute_pedersen_root(
1234, array![1234, 1234].span()
) == 0xc92a4f7aa8979b0202770b378e46de07bebe0836f8ceece5a47ccf3929c6b0,
'example'
assert_eq!(
compute_pedersen_root(1234, array![1234, 1234].span()),
0xc92a4f7aa8979b0202770b378e46de07bebe0836f8ceece5a47ccf3929c6b0
);
}

#[test]
#[available_gas(3000000)]
fn test_claim_single_recipient() {
let (_, token) = deploy_token('AIRDROP', 'AD', 1234567);

Expand All @@ -97,17 +83,16 @@ fn test_claim_single_recipient() {
airdrop.claim(claim, proof);

let log = pop_log::<Airdrop::Claimed>(airdrop.contract_address).unwrap();
assert(log.claim == claim, 'claim');
assert_eq!(log.claim, claim);

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');
assert(log.value == claim.amount.into(), 'amount');
assert_eq!(log.from, airdrop.contract_address);
assert_eq!(log.to, claim.claimee);
assert_eq!(log.value, claim.amount.into());
}


#[test]
#[available_gas(4000000)]
#[should_panic(expected: ('ALREADY_CLAIMED', 'ENTRYPOINT_FAILED'))]
Expand All @@ -127,9 +112,7 @@ fn test_double_claim() {
airdrop.claim(claim, proof);
}


#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('INVALID_PROOF', 'ENTRYPOINT_FAILED'))]
fn test_invalid_proof_single_entry() {
let (_, token) = deploy_token('AIRDROP', 'AD', 1234567);
Expand All @@ -145,7 +128,6 @@ fn test_invalid_proof_single_entry() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('INVALID_PROOF', 'ENTRYPOINT_FAILED'))]
fn test_invalid_proof_fake_entry() {
let (_, token) = deploy_token('AIRDROP', 'AD', 1234567);
Expand Down Expand Up @@ -187,10 +169,10 @@ fn test_claim_two_claims() {
token.transfer(airdrop.contract_address, 6789 + 789 + 1);

airdrop.claim(claim_a, array![leaf_b]);
assert(token.balance_of(airdrop.contract_address) == (789 + 1), 'claim a taken');
assert(token.balance_of(claim_a.claimee) == 6789, 'received');
assert_eq!(token.balance_of(airdrop.contract_address), (789 + 1));
assert_eq!(token.balance_of(claim_a.claimee), 6789);

airdrop.claim(claim_b, array![leaf_a]);
assert(token.balance_of(airdrop.contract_address) == 1, 'claim b taken');
assert(token.balance_of(claim_b.claimee) == 789, 'received');
assert_eq!(token.balance_of(airdrop.contract_address), 1);
assert_eq!(token.balance_of(claim_b.claimee), 789);
}
13 changes: 0 additions & 13 deletions src/governance_token_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ fn test_governance_token_delegated_snapshot_store_unpack() {
}

#[test]
#[available_gas(3000000)]
fn test_deploy_constructor() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);
assert(erc20.name() == 'Governor Token', 'name');
Expand All @@ -127,7 +126,6 @@ fn test_deploy_constructor() {
}

#[test]
#[available_gas(3000000)]
fn test_transfer_entire_balance() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);

Expand All @@ -144,7 +142,6 @@ fn test_transfer_entire_balance() {
}

#[test]
#[available_gas(3000000)]
fn test_transfer_lt_total_balance() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);

Expand All @@ -163,7 +160,6 @@ fn test_transfer_lt_total_balance() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('TRANSFER_INSUFFICIENT_BALANCE', 'ENTRYPOINT_FAILED'))]
fn test_transfer_gt_total_balance() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);
Expand All @@ -173,7 +169,6 @@ fn test_transfer_gt_total_balance() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('TRANSFER_AMOUNT_OVERFLOW', 'ENTRYPOINT_FAILED'))]
fn test_transfer_overflow() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);
Expand All @@ -183,7 +178,6 @@ fn test_transfer_overflow() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('ORDER', 'ENTRYPOINT_FAILED'))]
fn test_get_average_delegated_order_same() {
let (token, _) = deploy('Governor Token', 'GT', 12345);
Expand All @@ -192,7 +186,6 @@ fn test_get_average_delegated_order_same() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('ORDER', 'ENTRYPOINT_FAILED'))]
fn test_get_average_delegated_order_backwards() {
let (token, _) = deploy('Governor Token', 'GT', 12345);
Expand All @@ -201,7 +194,6 @@ fn test_get_average_delegated_order_backwards() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('FUTURE', 'ENTRYPOINT_FAILED'))]
fn test_get_average_delegated_future() {
let (token, _) = deploy('Governor Token', 'GT', 12345);
Expand All @@ -210,7 +202,6 @@ fn test_get_average_delegated_future() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('FUTURE', 'ENTRYPOINT_FAILED'))]
fn test_get_average_delegated_future_non_zero() {
let (token, _) = deploy('Governor Token', 'GT', 12345);
Expand All @@ -221,7 +212,6 @@ fn test_get_average_delegated_future_non_zero() {
}

#[test]
#[available_gas(3000000)]
fn test_approve_sets_allowance() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);

Expand All @@ -231,7 +221,6 @@ fn test_approve_sets_allowance() {
}

#[test]
#[available_gas(3000000)]
fn test_approve_allows_transfer_from() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);

Expand All @@ -248,7 +237,6 @@ fn test_approve_allows_transfer_from() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('TRANSFER_FROM_ALLOWANCE', 'ENTRYPOINT_FAILED'))]
fn test_transfer_from_insufficient_allowance() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);
Expand All @@ -262,7 +250,6 @@ fn test_transfer_from_insufficient_allowance() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('APPROVE_AMOUNT_OVERFLOW', 'ENTRYPOINT_FAILED'))]
fn test_approve_overflow() {
let (_, erc20) = deploy('Governor Token', 'GT', 12345);
Expand Down
3 changes: 0 additions & 3 deletions src/governor_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ fn create_proposal(governance: IGovernorDispatcher, token: IGovernanceTokenDispa
/////////////////////////////

#[test]
#[available_gas(3000000)]
fn test_governance_deploy() {
let (token, _) = deploy_token('Governor', 'GT', 1000);
let governance = deploy(
Expand All @@ -84,7 +83,6 @@ fn test_governance_deploy() {
// PROPOSAL CREATION TESTS
/////////////////////////////
#[test]
#[available_gas(3000000)]
fn test_propose() {
let (token, _) = deploy_token('Governor', 'GT', 1000);
let governance = deploy(
Expand Down Expand Up @@ -135,7 +133,6 @@ fn test_propose_already_exists_should_fail() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('THRESHOLD', 'ENTRYPOINT_FAILED'))]
fn test_propose_below_threshold_should_fail() {
let (token, _) = deploy_token('Governor', 'GT', 1000);
Expand Down
3 changes: 0 additions & 3 deletions src/timelock_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub(crate) fn deploy(owner: ContractAddress, delay: u64, window: u64) -> ITimelo
}

#[test]
#[available_gas(3000000)]
fn test_deploy() {
let timelock = deploy(contract_address_const::<2300>(), 10239, 3600);

Expand Down Expand Up @@ -77,7 +76,6 @@ fn test_queue_execute() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('DOES_NOT_EXIST', 'ENTRYPOINT_FAILED'))]
fn test_queue_cancel() {
set_block_timestamp(1);
Expand Down Expand Up @@ -136,7 +134,6 @@ fn test_queue_executed_too_early() {
}

#[test]
#[available_gas(3000000)]
#[should_panic(expected: ('TOO_LATE', 'ENTRYPOINT_FAILED'))]
fn test_queue_executed_too_late() {
set_block_timestamp(1);
Expand Down

0 comments on commit a504d69

Please sign in to comment.