Skip to content

Commit 851fa08

Browse files
stubbstaCopilot
andauthored
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>
1 parent e75ac91 commit 851fa08

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

test/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ cast send $TOKEN_PROXY_ADDRESS "mint(address,uint256)" <TO_ADDRESS> <AMOUNT> --r
9696

9797
#### Option 2: Public minting by burning ETH (no privileges required)
9898

99-
The total tokens minted is determined by the amount of ETH sent with the transaction.
99+
The total tokens minted is determined by the amount of ETH sent with the transaction. There is a lower limit to the
100+
amount that can be minted in a single transaction to prevent spam.
100101

101102
```bash
102103
cast send $TOKEN_PROXY_ADDRESS "mintWithETH(address)" <TO_ACCOUNT> --value <ETH_AMOUNT> --rpc-url $RPC_URL --private-key $MINTING_ACCOUNT_PRIVATE_KEY --from $MINTING_ACCOUNT_ADDRESS

test/TestStableToken.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ contract TestStableToken is
2626
{
2727
mapping(address => bool) public isMinter;
2828
uint256 public maxSupply;
29+
uint256 public constant MIN_MINT_WEI = 1 ether;
2930

3031
event MinterAdded(address indexed account);
3132
event MinterRemoved(address indexed account);
@@ -71,7 +72,7 @@ contract TestStableToken is
7172
}
7273

7374
function mintWithETH(address to) external payable {
74-
if (msg.value == 0) revert InsufficientETH();
75+
if (msg.value < MIN_MINT_WEI) revert InsufficientETH();
7576
if (totalSupply() + msg.value > maxSupply) revert ExceedsMaxSupply();
7677

7778
// Burn ETH by sending to zero address

test/TestStableToken.t.sol

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,31 @@ contract TestStableTokenTest is Test {
195195
token.mintWithETH(user1);
196196
}
197197

198+
function test__MintWithETH_RevertsBelowOneETH() external {
199+
// Owner calling with less than 1 ETH should revert due to minimum requirement
200+
uint256 sendAmount = 0.5 ether;
201+
vm.deal(owner, sendAmount);
202+
203+
vm.prank(owner);
204+
vm.expectRevert(abi.encodeWithSelector(InsufficientETH.selector));
205+
token.mintWithETH{ value: sendAmount }(user1);
206+
}
207+
208+
function test__MintWithETH_SucceedsAtOneETH() external {
209+
// Owner calling with exactly 1 ETH should succeed and mint 1 token (with 18 decimal places, i.e., 10^18 token
210+
// units)
211+
uint256 sendAmount = 1 ether;
212+
address recipient = vm.addr(20);
213+
214+
vm.deal(owner, sendAmount);
215+
vm.prank(owner);
216+
token.mintWithETH{ value: sendAmount }(recipient);
217+
218+
assertEq(token.balanceOf(recipient), sendAmount);
219+
}
220+
198221
function test__ERC20BasicFunctionality() external {
199-
uint256 ethAmount = 0.1 ether;
222+
uint256 ethAmount = 1 ether;
200223

201224
vm.deal(user1, ethAmount);
202225
vm.prank(user1);
@@ -206,14 +229,14 @@ contract TestStableTokenTest is Test {
206229
assertEq(token.totalSupply(), ethAmount);
207230

208231
vm.prank(user2);
209-
assertTrue(token.transfer(owner, 0.05 ether));
232+
assertTrue(token.transfer(owner, 0.5 ether));
210233

211-
assertEq(token.balanceOf(user2), 0.05 ether);
212-
assertEq(token.balanceOf(owner), 0.05 ether);
234+
assertEq(token.balanceOf(user2), 0.5 ether);
235+
assertEq(token.balanceOf(owner), 0.5 ether);
213236
}
214237

215238
function test__ETHBurnedEventEmitted() external {
216-
uint256 ethAmount = 0.1 ether;
239+
uint256 ethAmount = 1 ether;
217240

218241
vm.deal(owner, ethAmount);
219242

@@ -225,7 +248,7 @@ contract TestStableTokenTest is Test {
225248
}
226249

227250
function test__ETHIsBurnedToZeroAddress() external {
228-
uint256 ethAmount = 0.1 ether;
251+
uint256 ethAmount = 1 ether;
229252
address zeroAddress = address(0);
230253

231254
uint256 zeroBalanceBefore = zeroAddress.balance;
@@ -239,7 +262,7 @@ contract TestStableTokenTest is Test {
239262
}
240263

241264
function test__ContractDoesNotHoldETHAfterMint() external {
242-
uint256 ethAmount = 0.1 ether;
265+
uint256 ethAmount = 1 ether;
243266

244267
uint256 contractBalanceBefore = address(token).balance;
245268

@@ -253,9 +276,9 @@ contract TestStableTokenTest is Test {
253276

254277
function test__MintWithDifferentETHAmounts() external {
255278
uint256[] memory ethAmounts = new uint256[](3);
256-
ethAmounts[0] = 0.01 ether;
257-
ethAmounts[1] = 1 ether;
258-
ethAmounts[2] = 10 ether;
279+
ethAmounts[0] = 1 ether;
280+
ethAmounts[1] = 10 ether;
281+
ethAmounts[2] = 100 ether;
259282

260283
for (uint256 i = 0; i < ethAmounts.length; i++) {
261284
address user = vm.addr(i + 10);

0 commit comments

Comments
 (0)