Skip to content

Commit

Permalink
Fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad committed Aug 1, 2024
1 parent 9e4cda0 commit a81ecad
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
5 changes: 4 additions & 1 deletion listings/applications/coin_flip/src/contract.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use starknet::ContractAddress;

#[starknet::interface]
pub trait ICoinFlip<TContractState> {
fn flip(ref self: TContractState);
Expand Down Expand Up @@ -70,7 +72,8 @@ pub mod CoinFlip {
pub const PUBLISH_DELAY: u64 = 0; // return the random value asap
pub const NUM_OF_WORDS: u64 = 1; // one random value is sufficient
pub const CALLBACK_FEE_LIMIT: u128 = 100_000_000_000_000; // 0.0001 ETH
pub const MAX_CALLBACK_FEE_DEPOSIT: u256 = CALLBACK_FEE_LIMIT * 5; // needs to cover the Premium fee
pub const MAX_CALLBACK_FEE_DEPOSIT: u256 =
500_000_000_000_000; // CALLBACK_FEE_LIMIT * 5; needs to cover the Premium fee

#[constructor]
fn constructor(
Expand Down
85 changes: 57 additions & 28 deletions listings/applications/coin_flip/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,39 @@ fn deploy() -> (ICoinFlipDispatcher, IRandomnessDispatcher, IERC20Dispatcher, Co
let randomness_dispatcher = IRandomnessDispatcher { contract_address: randomness_address };
let coin_flip_dispatcher = ICoinFlipDispatcher { contract_address: coin_flip_address };

// fund the CoinFlip contract
start_cheat_caller_address(eth_address, deployer);
eth_dispatcher
.transfer(
coin_flip_address, CoinFlip::CALLBACK_FEE_LIMIT.into() * 100
);
stop_cheat_caller_address(eth_address);

(coin_flip_dispatcher, randomness_dispatcher, eth_dispatcher, deployer)
}

#[test]
#[fuzzer(runs: 10, seed: 22)]
fn test_two_flips(random_word_1: felt252, random_word_2: felt252) {
fn test_two_flips(random_word: felt252) {
let (coin_flip, randomness, eth, deployer) = deploy();

_flip_request(coin_flip, randomness, eth, deployer, 0, CoinFlip::CALLBACK_FEE_LIMIT / 5 * 3);
_flip_request(coin_flip, randomness, eth, deployer, 1, CoinFlip::CALLBACK_FEE_LIMIT / 4 * 3);
_flip_request(coin_flip, randomness, eth, deployer, 2, CoinFlip::CALLBACK_FEE_LIMIT);
// fund the CoinFlip contract
start_cheat_caller_address(eth.contract_address, deployer);
eth.transfer(coin_flip.contract_address, CoinFlip::CALLBACK_FEE_LIMIT.into() * 50);
stop_cheat_caller_address(eth.contract_address);

_flip_request(
coin_flip, randomness, eth, deployer, 0, CoinFlip::CALLBACK_FEE_LIMIT / 5 * 3, random_word
);
_flip_request(
coin_flip, randomness, eth, deployer, 1, CoinFlip::CALLBACK_FEE_LIMIT / 4 * 3, random_word
);
_flip_request(
coin_flip, randomness, eth, deployer, 2, CoinFlip::CALLBACK_FEE_LIMIT, random_word
);
}

fn _flip_request(coin_flip: ICoinFlipDispatcher, randomness: IRandomnessDispatcher, eth: IERC20Dispatcher, deployer: ContractAddress, expected_request_id: u64, expected_callback_fee: u128) {
fn _flip_request(
coin_flip: ICoinFlipDispatcher,
randomness: IRandomnessDispatcher,
eth: IERC20Dispatcher,
deployer: ContractAddress,
expected_request_id: u64,
expected_callback_fee: u128,
random_word: felt252
) {
let mut spy = spy_events(SpyOn::One(coin_flip.contract_address));

let original_balance = eth.balance_of(coin_flip.contract_address);
Expand All @@ -86,7 +97,11 @@ fn _flip_request(coin_flip: ICoinFlipDispatcher, randomness: IRandomnessDispatch
);

let post_flip_balance = eth.balance_of(coin_flip.contract_address);
assert_eq!(post_flip_balance, original_balance - randomness.get_total_fees(coin_flip.contract_address, expected_request_id));
assert_eq!(
post_flip_balance,
original_balance
- randomness.get_total_fees(coin_flip.contract_address, expected_request_id)
);

randomness
.submit_random(
Expand All @@ -97,12 +112,12 @@ fn _flip_request(coin_flip: ICoinFlipDispatcher, randomness: IRandomnessDispatch
coin_flip.contract_address,
CoinFlip::CALLBACK_FEE_LIMIT,
expected_callback_fee,
array![random_word_1].span(),
array![random_word].span(),
array![].span(),
array![]
);

let random_value: u256 = random_word_1.into() % 12000;
let random_value: u256 = random_word.into() % 12000;
let expected_side = if random_value < 5999 {
CoinFlip::Side::Heads
} else if random_value > 6000 {
Expand All @@ -125,13 +140,21 @@ fn _flip_request(coin_flip: ICoinFlipDispatcher, randomness: IRandomnessDispatch
]
);

assert_eq!(eth.balance_of(coin_flip.contract_address), post_flip_balance + (CoinFlip::CALLBACK_FEE_LIMIT - expected_callback_fee));
assert_eq!(
eth.balance_of(coin_flip.contract_address),
post_flip_balance + (CoinFlip::CALLBACK_FEE_LIMIT - expected_callback_fee).into()
);
}

#[test]
fn test_two_consecutive_flips() {
let (coin_flip, randomness, eth, deployer) = deploy();

// fund the CoinFlip contract
start_cheat_caller_address(eth.contract_address, deployer);
eth.transfer(coin_flip.contract_address, CoinFlip::CALLBACK_FEE_LIMIT.into() * 50);
stop_cheat_caller_address(eth.contract_address);

let mut spy = spy_events(SpyOn::One(coin_flip.contract_address));

let original_balance = eth.balance_of(coin_flip.contract_address);
Expand All @@ -149,9 +172,7 @@ fn test_two_consecutive_flips() {
@array![
(
coin_flip.contract_address,
CoinFlip::Event::Flipped(
CoinFlip::Flipped { flip_id: 0, flipper: deployer }
)
CoinFlip::Event::Flipped(CoinFlip::Flipped { flip_id: 0, flipper: deployer })
),
(
coin_flip.contract_address,
Expand All @@ -163,11 +184,16 @@ fn test_two_consecutive_flips() {
);

let post_flip_balance = eth.balance_of(coin_flip.contract_address);
assert_eq!(post_flip_balance, original_balance - randomness.get_total_fees(coin_flip.contract_address, expected_request_id) * 2);
assert_eq!(
post_flip_balance,
original_balance
- randomness.get_total_fees(coin_flip.contract_address, 0)
- randomness.get_total_fees(coin_flip.contract_address, 1)
);

let expected_callback_fee = CoinFlip::CALLBACK_FEE_LIMIT / 5 * 3;
let random_word_deployer = 'this is a string representation of some felt';
let random_word_other_user = 'this is another felt value that we need';
let random_word_deployer = 'this is some random word value';
let random_word_other_flipper = 'this is another random word';

randomness
.submit_random(
Expand All @@ -191,7 +217,7 @@ fn test_two_consecutive_flips() {
coin_flip.contract_address,
CoinFlip::CALLBACK_FEE_LIMIT,
expected_callback_fee,
array![random_word_other_user].span(),
array![random_word_other_flipper].span(),
array![].span(),
array![]
);
Expand All @@ -204,8 +230,8 @@ fn test_two_consecutive_flips() {
} else {
CoinFlip::Side::Sideways
};
let random_value: u256 = random_word_other_user.into() % 12000;
let expected_side_other_user = if random_value < 5999 {
let random_value: u256 = random_word_other_flipper.into() % 12000;
let expected_side_other_flipper = if random_value < 5999 {
CoinFlip::Side::Heads
} else if random_value > 6000 {
CoinFlip::Side::Tails
Expand All @@ -223,7 +249,7 @@ fn test_two_consecutive_flips() {
flip_id: 0, flipper: deployer, side: expected_side_deployer
}
)
)
),
(
coin_flip.contract_address,
CoinFlip::Event::Landed(
Expand All @@ -235,7 +261,10 @@ fn test_two_consecutive_flips() {
]
);

assert_eq!(eth.balance_of(coin_flip.contract_address), post_flip_balance + (CoinFlip::CALLBACK_FEE_LIMIT - expected_callback_fee) * 2);
assert_eq!(
eth.balance_of(coin_flip.contract_address),
post_flip_balance + (CoinFlip::CALLBACK_FEE_LIMIT - expected_callback_fee).into() * 2
);
}

#[test]
Expand Down

0 comments on commit a81ecad

Please sign in to comment.