Skip to content

Commit

Permalink
🐛 minimum money amount
Browse files Browse the repository at this point in the history
  • Loading branch information
julienbrs authored and tekkac committed Oct 3, 2024
1 parent d262951 commit 0b42b0e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/components/minter/mint.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod MintComponent {
// Starknet imports

use openzeppelin::token::erc20::interface::ERC20ABIDispatcherTrait;
use starknet::ContractAddress;
use starknet::{get_caller_address, get_contract_address, get_block_timestamp};

Expand Down Expand Up @@ -416,9 +417,8 @@ mod MintComponent {
let unit_price = self.Mint_unit_price.read();
// If user wants to buy 1 carbon credit, the input should be 1*MULTIPLIER_TONS_TO_MGRAMS
let money_amount = cc_amount * unit_price / MULTIPLIER_TONS_TO_MGRAMS;

let min_money_amount_per_tx = self.Mint_min_money_amount_per_tx.read();
assert(money_amount >= min_money_amount_per_tx, 'Value too low');
// let min_money_amount_per_tx = 1;
assert(money_amount >= 1, 'Value too low');

let remaining_mintable_cc = self.Mint_remaining_mintable_cc.read();
assert(remaining_mintable_cc >= cc_amount, 'Minting limit reached');
Expand Down
99 changes: 93 additions & 6 deletions tests/test_mint.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use openzeppelin::token::erc20::interface::ERC20ABIDispatcherTrait;
use snforge_std::cheatcodes::events::EventSpyAssertionsTrait;

// Starknet deps
Expand Down Expand Up @@ -282,6 +283,92 @@ fn test_public_buy() {
assert(equals_with_error(balance_user_after, cc_to_buy, 100), 'balance should be the same');
}


#[test]
fn test_minimal_buy() {
let owner_address: ContractAddress = contract_address_const::<'OWNER'>();
let user_address: ContractAddress = contract_address_const::<'USER'>();
let project_address = default_setup_and_deploy();
let erc20_address = deploy_erc20();
let minter_address = deploy_minter(project_address, erc20_address);

start_cheat_caller_address(project_address, owner_address);
let project_contract = IProjectDispatcher { contract_address: project_address };
project_contract.grant_minter_role(minter_address);

stop_cheat_caller_address(project_address);
let minter = IMintDispatcher { contract_address: minter_address };
let erc20 = IERC20Dispatcher { contract_address: erc20_address };
let cc_to_buy: u256 = MULTIPLIER_TONS_TO_MGRAMS / minter.get_unit_price() + 1;
let money_amount = cc_to_buy * minter.get_unit_price() / MULTIPLIER_TONS_TO_MGRAMS;

start_cheat_caller_address(erc20_address, owner_address);
let success = erc20.transfer(user_address, money_amount);
assert(success, 'Transfer failed');

start_cheat_caller_address(erc20_address, user_address);
erc20.approve(minter_address, money_amount);

start_cheat_caller_address(minter_address, user_address);
start_cheat_caller_address(erc20_address, minter_address);

let mut spy = spy_events();
minter.public_buy(cc_to_buy);

let balance = helper_sum_balance(project_address, user_address);
assert(equals_with_error(balance, cc_to_buy, 100), 'balance should be the same');

let token_ids = helper_get_token_ids(project_address);

let expected_events = helper_expected_transfer_single_events(
project_address, minter_address, Zeroable::zero(), user_address, token_ids, cc_to_buy
);
spy.assert_emitted(@expected_events);
}

#[test]
#[should_panic(expected: 'Value too low')]
fn test_minimal_buy_error() {
let owner_address: ContractAddress = contract_address_const::<'OWNER'>();
let user_address: ContractAddress = contract_address_const::<'USER'>();
let project_address = default_setup_and_deploy();
let erc20_address = deploy_erc20();
let minter_address = deploy_minter(project_address, erc20_address);

start_cheat_caller_address(project_address, owner_address);
let project_contract = IProjectDispatcher { contract_address: project_address };
project_contract.grant_minter_role(minter_address);

stop_cheat_caller_address(project_address);
let minter = IMintDispatcher { contract_address: minter_address };
let erc20 = IERC20Dispatcher { contract_address: erc20_address };
let cc_to_buy: u256 = MULTIPLIER_TONS_TO_MGRAMS / minter.get_unit_price();
let money_amount = cc_to_buy * minter.get_unit_price() / MULTIPLIER_TONS_TO_MGRAMS;

start_cheat_caller_address(erc20_address, owner_address);
let success = erc20.transfer(user_address, money_amount);
assert(success, 'Transfer failed');

start_cheat_caller_address(erc20_address, user_address);
erc20.approve(minter_address, money_amount);

start_cheat_caller_address(minter_address, user_address);
start_cheat_caller_address(erc20_address, minter_address);

let mut spy = spy_events();
minter.public_buy(cc_to_buy);

let balance = helper_sum_balance(project_address, user_address);
assert(equals_with_error(balance, cc_to_buy, 100), 'balance should be the same');

let token_ids = helper_get_token_ids(project_address);

let expected_events = helper_expected_transfer_single_events(
project_address, minter_address, Zeroable::zero(), user_address, token_ids, cc_to_buy
);
spy.assert_emitted(@expected_events);
}

// set_max_mintable_cc
#[test]
#[should_panic(expected: 'Caller is not the owner')]
Expand Down Expand Up @@ -664,7 +751,7 @@ fn test_set_unit_price_without_owner_role() {
let minter_address = deploy_minter(project_address, erc20_address);

let minter = IMintDispatcher { contract_address: minter_address };
let price: u256 = 100;
let price: u256 = 100000000;
minter.set_unit_price(price);
}

Expand All @@ -679,11 +766,11 @@ fn test_set_unit_price_with_owner_role() {
start_cheat_caller_address(minter_address, owner_address);

let minter = IMintDispatcher { contract_address: minter_address };
let price: u256 = 100;
let price: u256 = 100000000; // $100
minter.set_unit_price(price);

let expected_event = MintComponent::Event::UnitPriceUpdated(
MintComponent::UnitPriceUpdated { old_price: 11, new_price: price }
MintComponent::UnitPriceUpdated { old_price: 11000000, new_price: price }
);
spy.assert_emitted(@array![(minter_address, expected_event)]);
}
Expand All @@ -701,7 +788,7 @@ fn test_set_unit_price_to_zero_panic() {

// Ensure the unit price is not set initially
let unit_price = minter.get_unit_price();
assert(unit_price == 11, 'unit price should be 11');
assert(unit_price == 11000000, 'unit price should be 11000000');

// Set the unit price to 0 and it should panic
let new_unit_price: u256 = 0;
Expand All @@ -722,11 +809,11 @@ fn test_get_unit_price() {
let minter = IMintDispatcher { contract_address: minter_address };

let unit_price = minter.get_unit_price();
assert(unit_price == 11, 'unit price should be 11');
assert(unit_price == 11000000, 'unit price should be 11000000');
stop_cheat_caller_address(minter_address);

start_cheat_caller_address(minter_address, owner_address);
let new_unit_price: u256 = 1000;
let new_unit_price: u256 = 100000000;
start_cheat_caller_address(minter_address, owner_address);
minter.set_unit_price(new_unit_price);
stop_cheat_caller_address(minter_address);
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fn deploy_minter(
let public_sale: bool = true;
let low: felt252 = DEFAULT_REMAINING_MINTABLE_CC.low.into();
let high: felt252 = DEFAULT_REMAINING_MINTABLE_CC.high.into();
let unit_price: felt252 = 11;
let unit_price: felt252 = 11000000; // $11, 6 decimals like USDC
let mut calldata: Array<felt252> = array![
project_address.into(),
payment_address.into(),
Expand All @@ -205,7 +205,7 @@ fn deploy_minter_specific_max_mintable(
let high: felt252 = max_mintable_cc.high.into();
start_cheat_caller_address(project_address, owner);
let public_sale: bool = true;
let unit_price: felt252 = 11;
let unit_price: felt252 = 11000000;
let mut calldata: Array<felt252> = array![
project_address.into(),
payment_address.into(),
Expand Down

0 comments on commit 0b42b0e

Please sign in to comment.