From 851fa0803b3180691392bc06069339bba37927ef Mon Sep 17 00:00:00 2001 From: Tanya S <120410716+stubbsta@users.noreply.github.com> Date: Fri, 10 Oct 2025 08:12:59 +0200 Subject: [PATCH] Add minimum mint value to use mintWithEth function (#44) * mintWithEth function now requires minimum amount * Update README and comments to explain minting ratio and lower limit * Update test/TestStableToken.t.sol Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix linting --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- test/README.md | 3 ++- test/TestStableToken.sol | 3 ++- test/TestStableToken.t.sol | 43 +++++++++++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/test/README.md b/test/README.md index b229fdd..32acd2d 100644 --- a/test/README.md +++ b/test/README.md @@ -96,7 +96,8 @@ cast send $TOKEN_PROXY_ADDRESS "mint(address,uint256)" --r #### Option 2: Public minting by burning ETH (no privileges required) -The total tokens minted is determined by the amount of ETH sent with the transaction. +The total tokens minted is determined by the amount of ETH sent with the transaction. There is a lower limit to the +amount that can be minted in a single transaction to prevent spam. ```bash cast send $TOKEN_PROXY_ADDRESS "mintWithETH(address)" --value --rpc-url $RPC_URL --private-key $MINTING_ACCOUNT_PRIVATE_KEY --from $MINTING_ACCOUNT_ADDRESS diff --git a/test/TestStableToken.sol b/test/TestStableToken.sol index 81ef7be..f1ce5d9 100644 --- a/test/TestStableToken.sol +++ b/test/TestStableToken.sol @@ -26,6 +26,7 @@ contract TestStableToken is { mapping(address => bool) public isMinter; uint256 public maxSupply; + uint256 public constant MIN_MINT_WEI = 1 ether; event MinterAdded(address indexed account); event MinterRemoved(address indexed account); @@ -71,7 +72,7 @@ contract TestStableToken is } function mintWithETH(address to) external payable { - if (msg.value == 0) revert InsufficientETH(); + if (msg.value < MIN_MINT_WEI) revert InsufficientETH(); if (totalSupply() + msg.value > maxSupply) revert ExceedsMaxSupply(); // Burn ETH by sending to zero address diff --git a/test/TestStableToken.t.sol b/test/TestStableToken.t.sol index 9e445b7..d50f812 100644 --- a/test/TestStableToken.t.sol +++ b/test/TestStableToken.t.sol @@ -195,8 +195,31 @@ contract TestStableTokenTest is Test { token.mintWithETH(user1); } + function test__MintWithETH_RevertsBelowOneETH() external { + // Owner calling with less than 1 ETH should revert due to minimum requirement + uint256 sendAmount = 0.5 ether; + vm.deal(owner, sendAmount); + + vm.prank(owner); + vm.expectRevert(abi.encodeWithSelector(InsufficientETH.selector)); + token.mintWithETH{ value: sendAmount }(user1); + } + + function test__MintWithETH_SucceedsAtOneETH() external { + // Owner calling with exactly 1 ETH should succeed and mint 1 token (with 18 decimal places, i.e., 10^18 token + // units) + uint256 sendAmount = 1 ether; + address recipient = vm.addr(20); + + vm.deal(owner, sendAmount); + vm.prank(owner); + token.mintWithETH{ value: sendAmount }(recipient); + + assertEq(token.balanceOf(recipient), sendAmount); + } + function test__ERC20BasicFunctionality() external { - uint256 ethAmount = 0.1 ether; + uint256 ethAmount = 1 ether; vm.deal(user1, ethAmount); vm.prank(user1); @@ -206,14 +229,14 @@ contract TestStableTokenTest is Test { assertEq(token.totalSupply(), ethAmount); vm.prank(user2); - assertTrue(token.transfer(owner, 0.05 ether)); + assertTrue(token.transfer(owner, 0.5 ether)); - assertEq(token.balanceOf(user2), 0.05 ether); - assertEq(token.balanceOf(owner), 0.05 ether); + assertEq(token.balanceOf(user2), 0.5 ether); + assertEq(token.balanceOf(owner), 0.5 ether); } function test__ETHBurnedEventEmitted() external { - uint256 ethAmount = 0.1 ether; + uint256 ethAmount = 1 ether; vm.deal(owner, ethAmount); @@ -225,7 +248,7 @@ contract TestStableTokenTest is Test { } function test__ETHIsBurnedToZeroAddress() external { - uint256 ethAmount = 0.1 ether; + uint256 ethAmount = 1 ether; address zeroAddress = address(0); uint256 zeroBalanceBefore = zeroAddress.balance; @@ -239,7 +262,7 @@ contract TestStableTokenTest is Test { } function test__ContractDoesNotHoldETHAfterMint() external { - uint256 ethAmount = 0.1 ether; + uint256 ethAmount = 1 ether; uint256 contractBalanceBefore = address(token).balance; @@ -253,9 +276,9 @@ contract TestStableTokenTest is Test { function test__MintWithDifferentETHAmounts() external { uint256[] memory ethAmounts = new uint256[](3); - ethAmounts[0] = 0.01 ether; - ethAmounts[1] = 1 ether; - ethAmounts[2] = 10 ether; + ethAmounts[0] = 1 ether; + ethAmounts[1] = 10 ether; + ethAmounts[2] = 100 ether; for (uint256 i = 0; i < ethAmounts.length; i++) { address user = vm.addr(i + 10);