From e20b83b7325be206e3dacc1d105bf9a7c234f861 Mon Sep 17 00:00:00 2001 From: Zack Williams Date: Fri, 3 Jan 2025 13:40:55 -0500 Subject: [PATCH] adds test for minting with full balance --- contracts/src/kudos.cairo | 4 ++-- contracts/tests/lib.cairo | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/contracts/src/kudos.cairo b/contracts/src/kudos.cairo index 9a76c30..732c81a 100644 --- a/contracts/src/kudos.cairo +++ b/contracts/src/kudos.cairo @@ -62,6 +62,7 @@ pub mod Kudos { pub const RECEIVER_UNREGISTERED: felt252 = 'Receiver not registered'; pub const MINTER_UNREGISTERED: felt252 = 'Minter not registered'; pub const MINTED_BALANCE_ZERO: felt252 = 'Minted balance is zero'; + pub const MINTED_BALANCE_IS_FULL: felt252 = 'Minted balance is full'; pub const MINTED_AMOUNT_IS_ZERO: felt252 = 'Minted amount is zero'; pub const MINTED_LESS_THAN_30_DAYS_AGO: felt252 = 'Minted less than 30 days ago'; } @@ -86,7 +87,6 @@ pub mod Kudos { assert(self.credential_registry.is_registered(receiver), Errors::RECEIVER_UNREGISTERED); let minted_balance = self.minted_balance.entry(sender).read(); - println!("minted_balance: {}", minted_balance); assert(minted_balance > 0, Errors::MINTED_BALANCE_ZERO); self.erc20.transfer(receiver, ONE); @@ -112,7 +112,7 @@ pub mod Kudos { assert(time_since_last_mint > SECONDS_IN_30_DAYS, Errors::MINTED_LESS_THAN_30_DAYS_AGO); let amount_to_mint = MONTHLT_MINT_AMOUNT - self.minted_balance.entry(address).read(); - assert(amount_to_mint > 0, Errors::MINTED_AMOUNT_IS_ZERO); + assert(amount_to_mint > 0, Errors::MINTED_BALANCE_IS_FULL); self.erc20.mint(address, amount_to_mint); self.minted_balance.entry(address).write(MONTHLT_MINT_AMOUNT); diff --git a/contracts/tests/lib.cairo b/contracts/tests/lib.cairo index d3f4234..cd849ee 100644 --- a/contracts/tests/lib.cairo +++ b/contracts/tests/lib.cairo @@ -9,7 +9,10 @@ use kudos::utils::constants::{ ZERO_ADDRESS, RECEIVER, DUMMY, CREDENTIAL_HASH_BAD, SECONDS_IN_30_DAYS, FIVE, ZERO }; use kudos::{Kudos, IKudosDispatcher, IKudosDispatcherTrait}; -use snforge_std::{spy_events, EventSpyAssertionsTrait, start_cheat_caller_address, start_cheat_block_timestamp_global, stop_cheat_block_timestamp_global}; +use snforge_std::{ + spy_events, EventSpyAssertionsTrait, start_cheat_caller_address, + start_cheat_block_timestamp_global, stop_cheat_block_timestamp_global +}; use starknet::get_block_timestamp; use utils::{setup, setup_registered, test_description, one, send_5_kudos}; @@ -128,15 +131,41 @@ fn test_give_kudos_minted_balance_zero() { #[test] fn test_monthly_mint_full_amount() { let kudos = IKudosDispatcher { contract_address: setup_registered() }; + start_cheat_caller_address(kudos.contract_address, CALLER()); send_5_kudos(kudos); + let thirty_days_pass = get_block_timestamp() + SECONDS_IN_30_DAYS + 1; start_cheat_block_timestamp_global(block_timestamp: thirty_days_pass); assert(kudos.get_minted_balance(CALLER()) == ZERO, 'Minted balance is not zero'); - println!("minted balance before: {}", kudos.get_minted_balance(CALLER())); + kudos.monthly_mint(); -println!("minted balance after: {}", kudos.get_minted_balance(CALLER())); + assert(kudos.get_minted_balance(CALLER()) == FIVE, 'Minted balance is not five'); stop_cheat_block_timestamp_global() } + +#[test] +#[should_panic(expected: 'Minted balance is full')] +fn test_monthly_mint_with_a_full_balance() { + let kudos = IKudosDispatcher { contract_address: setup_registered() }; + + start_cheat_caller_address(kudos.contract_address, CALLER()); + + let thirty_days_pass = get_block_timestamp() + SECONDS_IN_30_DAYS + 1; + start_cheat_block_timestamp_global(block_timestamp: thirty_days_pass); + assert(kudos.get_minted_balance(CALLER()) == FIVE, 'Minted balance is not five'); + + kudos.monthly_mint(); + + stop_cheat_block_timestamp_global() +} + +#[test] +#[should_panic(expected: 'Minter not registered')] +fn test_monthly_mint_minter_unregistered() { + let kudos = IKudosDispatcher { contract_address: setup_registered() }; + start_cheat_caller_address(kudos.contract_address, DUMMY()); + kudos.monthly_mint(); +}