diff --git a/.gitignore b/.gitignore index 14ed834..da23162 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ cache/ # node things node_modules/ + +# sol shit +*.abi diff --git a/Justfile b/Justfile index aefe057..28550d4 100644 --- a/Justfile +++ b/Justfile @@ -2,5 +2,10 @@ @default: just -l +# To lanch the tests via foundry-rs @test: forge test + +# To generate the ABI in the deployment/ folder +@abi-gen: + solc --abi code/Kanel42_token.sol -o deployment --overwrite diff --git a/README.md b/README.md index 9f38395..891a914 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ Made using foundry-rs toolkit. - Following the ERC20 standard : the most used standard when it comes to fungible token and compatible with every wallet - foundry-rs : A simple but yet cli tool kit for Solidity developement with integrated fuzzer and test perfect for a simple project as this +## Documenation +- [How it works](HOWITWORK.md) +- [How to use it](USAGE.md) + ## Some resource for more information [ERC20](https://ethereum.org/en/developers/docs/standards/tokens/erc-20) \ [Solidity documenation](https://docs.soliditylang.org/en/v0.8.28/) \ diff --git a/code/KNL_wallet.sol b/code/KNL_wallet.sol new file mode 100644 index 0000000..e69de29 diff --git a/code/Kanel42_token.sol b/code/Kanel42_token.sol index 6ee16ff..feec27d 100644 --- a/code/Kanel42_token.sol +++ b/code/Kanel42_token.sol @@ -8,7 +8,6 @@ contract Kanel42_token { 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; @@ -42,27 +41,18 @@ contract Kanel42_token { success = true; } - function mint() public payable { - uint256 minted = msg.value / mintCost * (10 ** decimals); + function mint(uint256 _value) public { + if ((totalSupply / 16) <= _value) { + revert("can't mint more than 1/16 of the supply at a time"); + } - balanceOf[msg.sender] += minted; - totalMinted += minted; + balanceOf[msg.sender] += _value; + totalMinted += _value; - emit Transfer(address(0), msg.sender, minted); + emit Transfer(address(0), msg.sender, _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 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 index 3711caf..bf837d1 100644 --- a/code/test/Kanel42_token.t.sol +++ b/code/test/Kanel42_token.t.sol @@ -11,6 +11,7 @@ contract Kanel42TokenTest is Test { // This function runs before each test function setUp() public { token = new Kanel42_token(); + vm.deal(address(token), 1 ether); } // Test the initial supply of the token @@ -19,28 +20,49 @@ contract Kanel42TokenTest is Test { assertEq(token.totalMinted(), 0); } - // Test minting tokens - function testMint() public { - uint256 initialBalance = token.balanceOf(address(this)); - uint256 mintAmount = 1 * (10 ** 6); // Mint 1 token (considering 6 decimals) + function testMint() public { + // Define the amount of ether to send for minting + uint256 mintAmount = 1 * (10 ** token.decimals()); - // Mint tokens by sending ether - vm.deal(address(this), 0.01 ether); - token.mint{ value: 0.01 ether }(); + // Get the initial balance of the sender + uint256 initialBalance = token.balanceOf(address(this)); - uint256 finalBalance = token.balanceOf(address(this)); - assertEq(finalBalance - initialBalance, mintAmount); - } + // Mint tokens by sending ether + token.mint(mintAmount); + + // Check if the balance increased as expected + uint256 expectedMintedAmount = mintAmount; + uint256 finalBalance = token.balanceOf(address(this)); + assertEq(finalBalance - initialBalance, expectedMintedAmount, "Minted amount is incorrect"); + } + + function testBurn() public { + // First, mint some tokens to burn + uint256 mintAmount = 1 * (10 ** token.decimals()); + token.mint(mintAmount); + + // Get the initial balance of the sender + uint256 initialBalance = token.balanceOf(address(this)); + + // Define the amount of tokens to burn + uint256 burnAmount = mintAmount; + + // Burn tokens + token.burn(burnAmount); + + // Check if the balance decreased as expected + uint256 finalBalance = token.balanceOf(address(this)); + assertEq(initialBalance - finalBalance, burnAmount, "Burned amount is incorrect"); + } // Test transferring tokens function testTransfer() public { - address sender = address(this); + // address sender = address(this); address recipient = address(1); - uint256 transferAmount = 1 * (10 ** 6); // Transfer 1 token (considering 6 decimals) + uint256 transferAmount = 1 * (10 ** token.decimals()); // Transfer 1 token (considering 6 decimals) // Mint tokens to the sender - vm.deal(sender, 0.01 ether); - token.mint{ value: 0.01 ether }(); + token.mint(10 * (10 ** token.decimals())); // Transfer tokens to the recipient token.transfer(recipient, transferAmount); @@ -48,33 +70,15 @@ contract Kanel42TokenTest is Test { 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) + uint256 approveAmount = 1 * (10 ** token.decimals()); // Approve 1 token (considering 6 decimals) // Mint tokens to the owner - vm.deal(owner, 0.01 ether); - token.mint{ value: 0.01 ether }(); + token.mint(10 * (10 ** token.decimals())); // Approve spender to spend tokens on behalf of owner token.approve(spender, approveAmount); diff --git a/documentation/HOWITWORK.md b/documentation/HOWITWORK.md new file mode 100644 index 0000000..ddf4929 --- /dev/null +++ b/documentation/HOWITWORK.md @@ -0,0 +1,45 @@ +# How it works ? + +## The standard + +This is a ERC20 token so it has all the function you would expect from it. + +The getters : +```Solidity +// to get the full name of the token (in this case Kanel42) +function name() public view returns (string) +// to get the symbol of the token (in this case KNL42) +function symbol() public view returns (string) +// to get the number of decimals (number of token * 10^decimals to get the number of token) +function decimals() public view returns (uint8) +// to get the total supply that can be minted (in this case 8000(000000)) +function totalSupply() public view returns (uint256) +// to get the balance of an address +function balanceOf(address _owner) public view returns (uint256 balance) +// to know how much the _spender can send on behalf of the _owner +function allowance(address _owner, address _spender) public view returns (uint256 remaining) +``` + +The member functions : +```Solidity +// to transfer tokens to another address (_to) +function transfer(address _to, uint256 _value) public returns (bool success) +// to transfer tokens on behalf of _from to another address (_to) +function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) +// to approve a certain amount to a _spender that can send token on your behalf +function approve(address _spender, uint256 _value) public returns (bool success) +``` + +Events: +```Solidity +// is sent when a transfer is successful +event Transfer(address indexed _from, address indexed _to, uint256 _value) +// is sent when an approval is successful +event Approval(address indexed _owner, address indexed _spender, uint256 _value) +``` + +And there is also another function for minting the token: +```Solidity +// the _value determine how much to mint (in *(10^decimals)) +function mint(uint256 _value) public +``` diff --git a/documentation/USAGE.md b/documentation/USAGE.md new file mode 100644 index 0000000..e69de29