From 0f77697c62cee9cb5070644683489937ffd07ae1 Mon Sep 17 00:00:00 2001 From: adjoly Date: Wed, 6 Aug 2025 20:08:13 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20test=20not=20passing=20:sob:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 3 ++ Justfile | 3 ++ code/Kanel42_token.sol | 62 +++++++++++++++++++++++++ code/test/Kanel42_token.t.sol | 86 +++++++++++++++++++++++++++++++++++ foundry.toml | 26 +++++++++++ lib/forge-std | 1 + 6 files changed, 181 insertions(+) create mode 100644 .gitmodules create mode 100644 code/test/Kanel42_token.t.sol create mode 160000 lib/forge-std diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..888d42d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/forge-std"] + path = lib/forge-std + url = https://github.com/foundry-rs/forge-std diff --git a/Justfile b/Justfile index e69de29..7e8ab67 100644 --- a/Justfile +++ b/Justfile @@ -0,0 +1,3 @@ +# The default one (the one you are seeing right now) +@default: + just -l diff --git a/code/Kanel42_token.sol b/code/Kanel42_token.sol index fd3fc8f..5513776 100644 --- a/code/Kanel42_token.sol +++ b/code/Kanel42_token.sol @@ -3,5 +3,67 @@ pragma solidity 0.8.28; contract Kanel42_token { + string public name = "Kanel42"; + string public symbol = "KNL42"; + uint8 public decimals = 6; + uint256 public totalSupply = 8000000000; // 8000 + uint256 public totalMinted; + uint256 public mintCost = 0.01 ether; + mapping(address => uint256) public balanceOf; + mapping(address => mapping(address => uint256)) public allowance; + function transfer(address _to, uint256 _value) + public + returns (bool success) + { + balanceOf[msg.sender] -= _value; + balanceOf[_to] += _value; + emit Transfer(msg.sender, _to, _value); + success = true; + } + + function transferFrom(address _from, address _to, uint256 _value) + public + returns (bool success) + { + allowance[_from][msg.sender] -= _value; + balanceOf[_from] -= _value; + balanceOf[_to] += _value; + emit Transfer(_from, _to, _value); + success = true; + } + + function approve(address _spender, uint256 _value) + public + returns (bool success) + { + allowance[msg.sender][_spender] = _value; + emit Approval(msg.sender, _spender, _value); + success = true; + } + + function mint() public payable { + uint256 minted = msg.value / mintCost * (10 ^ decimals); + + balanceOf[msg.sender] += minted; + totalMinted += minted; + + 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); + // } + + event Transfer(address indexed _from, address indexed _to, uint256 _value); + event Approval( + address indexed _owner, address indexed _spender, uint256 _value + ); } diff --git a/code/test/Kanel42_token.t.sol b/code/test/Kanel42_token.t.sol new file mode 100644 index 0000000..7e21399 --- /dev/null +++ b/code/test/Kanel42_token.t.sol @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.28; + +import "../../lib/forge-std/src/Test.sol"; +import "../Kanel42_token.sol"; + +contract Kanel42TokenTest is Test { + Kanel42_token public token; + + // This function runs before each test + function setUp() public { + token = new Kanel42_token(); + } + + // Test the initial supply of the token + function testInitialSupply() public { + assertEq(token.totalSupply(), 8000000000); + 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) + + // Mint tokens by sending ether + vm.deal(address(this), 0.01 ether); + token.mint{ value: 0.01 ether }(); + + uint256 finalBalance = token.balanceOf(address(this)); + 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); + // } +} diff --git a/foundry.toml b/foundry.toml index 0e39d12..2baf1e3 100644 --- a/foundry.toml +++ b/foundry.toml @@ -1,6 +1,32 @@ [profile.default] src = "code" out = "out" +test = "code/test" libs = ["node_modules", "lib"] +solc = '0.8.28' +# Enable or disable the optimizer +optimizer = true +# The number of optimizer runs +optimizer_runs = 200 + +# Fuzzing settings +[fuzz] +# The number of fuzz runs +runs = 500 +# The seed for the fuzz runs +seed = '42' + +# Testing settings +[fmt] +# Line length for the formatter +line_length = 80 +# Number of spaces per tab +tab_width = 4 +# Bracket spacing +bracket_spacing = true +# Integer type formatting +int_types = 'long' +# Multiline function signature formatting +multiline_func_header = 'attributes_first' # See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options diff --git a/lib/forge-std b/lib/forge-std new file mode 160000 index 0000000..8bbcf6e --- /dev/null +++ b/lib/forge-std @@ -0,0 +1 @@ +Subproject commit 8bbcf6e3f8f62f419e5429a0bd89331c85c37824