diff --git a/code/Kanel42_token.sol b/code/Kanel42_token.sol index 5513776..f09fefa 100644 --- a/code/Kanel42_token.sol +++ b/code/Kanel42_token.sol @@ -2,6 +2,9 @@ pragma solidity 0.8.28; +import "../../lib/forge-std/src/Test.sol"; +import { console } from "../lib/forge-std/src/console.sol"; + contract Kanel42_token { string public name = "Kanel42"; string public symbol = "KNL42"; @@ -43,7 +46,7 @@ contract Kanel42_token { } function mint() public payable { - uint256 minted = msg.value / mintCost * (10 ^ decimals); + uint256 minted = msg.value / mintCost * (10 ** decimals); balanceOf[msg.sender] += minted; totalMinted += minted; @@ -51,16 +54,16 @@ contract Kanel42_token { emit Transfer(address(0), msg.sender, minted); } - // function burn(uint256 _value) public { - // uint256 sendBack = _value / (10 ^ decimals) * mintCost * 1 ether; - // - // balanceOf[msg.sender] -= _value; - // totalMinted -= _value; - // - // msg.sender.call{ value: sendBack }; - // - // emit Transfer(msg.sender, address(0), _value); - // } + function burn(uint256 _value) public { + uint256 sendBack = _value / (10 ** decimals) * mintCost * 1 ether; + + balanceOf[msg.sender] -= _value; + totalMinted -= _value; + + msg.sender.call{ value: sendBack }; + + emit Transfer(msg.sender, address(0), _value); + } event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval( diff --git a/code/test/Kanel42_token.t.sol b/code/test/Kanel42_token.t.sol index 7e21399..49d4612 100644 --- a/code/test/Kanel42_token.t.sol +++ b/code/test/Kanel42_token.t.sol @@ -2,6 +2,7 @@ pragma solidity 0.8.28; import "../../lib/forge-std/src/Test.sol"; +import { console } from "../../lib/forge-std/src/console.sol"; import "../Kanel42_token.sol"; contract Kanel42TokenTest is Test { @@ -15,13 +16,13 @@ contract Kanel42TokenTest is Test { // Test the initial supply of the token function testInitialSupply() public { assertEq(token.totalSupply(), 8000000000); - assertEq(token.totalMinted(), 0); + assertEq(token.totalMinted(), 0); } // Test minting tokens function testMint() public { uint256 initialBalance = token.balanceOf(address(this)); - uint256 mintAmount = 1000000; // Mint 1 token (considering 6 decimals) + uint256 mintAmount = 1 * (10 ** 6); // Mint 1 token (considering 6 decimals) // Mint tokens by sending ether vm.deal(address(this), 0.01 ether); @@ -31,56 +32,57 @@ contract Kanel42TokenTest is Test { assertEq(finalBalance - initialBalance, mintAmount); } - // // Test transferring tokens - // function testTransfer() public { - // address sender = address(this); - // address recipient = address(1); - // uint256 transferAmount = 1000000; // Transfer 1 token (considering 6 decimals) - // - // // Mint tokens to the sender - // vm.deal(sender, 0.01 ether); - // token.mint{ value: 0.01 ether }(); - // - // // Transfer tokens to the recipient - // token.transfer(recipient, transferAmount); - // - // assertEq(token.balanceOf(recipient), transferAmount); - // } - // - // // Test burning tokens - // function testBurn() public { - // uint256 initialBalance = token.balanceOf(address(this)); - // uint256 burnAmount = 1000000; // Burn 1 token (considering 6 decimals) - // - // // Mint tokens to have something to burn - // vm.deal(address(this), 0.01 ether); - // token.mint{ value: 0.01 ether }(); - // - // // Burn tokens - // token.burn(burnAmount); - // - // uint256 finalBalance = token.balanceOf(address(this)); - // assertEq(initialBalance, finalBalance + burnAmount); - // } - // - // // Test approval and transferFrom - // function testApproveAndTransferFrom() public { - // address owner = address(this); - // address spender = address(1); - // address recipient = address(2); - // uint256 approveAmount = 1000000; // Approve 1 token (considering 6 decimals) - // - // // Mint tokens to the owner - // vm.deal(owner, 0.01 ether); - // token.mint{ value: 0.01 ether }(); - // - // // Approve spender to spend tokens on behalf of owner - // token.approve(spender, approveAmount); - // - // // Transfer tokens from owner to recipient using transferFrom - // vm.prank(spender); - // token.transferFrom(owner, recipient, approveAmount); - // - // assertEq(token.balanceOf(recipient), approveAmount); - // } + // Test transferring tokens + function testTransfer() public { + address sender = address(this); + address recipient = address(1); + uint256 transferAmount = 1 * (10 ** 6); // Transfer 1 token (considering 6 decimals) + + // Mint tokens to the sender + vm.deal(sender, 0.01 ether); + token.mint{ value: 0.01 ether }(); + + // Transfer tokens to the recipient + token.transfer(recipient, transferAmount); + + assertEq(token.balanceOf(recipient), transferAmount); + } + + // Test burning tokens + function testBurn() public { + uint256 burnAmount = 1 * (10 ** 6); // Burn 1 token (considering 6 decimals) + + // Mint tokens to have something to burn + vm.deal(address(this), 0.01 ether); + token.mint{ value: 0.01 ether }(); + + uint256 initialBalance = token.balanceOf(address(this)); + + // Burn tokens + token.burn(burnAmount); + + uint256 finalBalance = token.balanceOf(address(this)); + assertEq(initialBalance, finalBalance + burnAmount); + } + + // Test approval and transferFrom + function testApproveAndTransferFrom() public { + address owner = address(this); + address spender = address(1); + address recipient = address(2); + uint256 approveAmount = 1 * (10 ** 6); // Approve 1 token (considering 6 decimals) + + // Mint tokens to the owner + vm.deal(owner, 0.01 ether); + token.mint{ value: 0.01 ether }(); + + // Approve spender to spend tokens on behalf of owner + token.approve(spender, approveAmount); + + // Transfer tokens from owner to recipient using transferFrom + vm.prank(spender); + token.transferFrom(owner, recipient, approveAmount); + + assertEq(token.balanceOf(recipient), approveAmount); + } }