From 969015f7c4336eefef04ec189778619df9170598 Mon Sep 17 00:00:00 2001 From: Mztacat <31314340+mztacat@users.noreply.github.com> Date: Sun, 21 Apr 2024 19:36:49 +0100 Subject: [PATCH] Create Minimal token Exercise.sol Minimal exercise --- Minimal token Exercise.sol | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Minimal token Exercise.sol diff --git a/Minimal token Exercise.sol b/Minimal token Exercise.sol new file mode 100644 index 0000000..2d0e075 --- /dev/null +++ b/Minimal token Exercise.sol @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +// Contract for an unburnable token +contract UnburnableToken { + string private salt = "value"; // A private string variable + + // Mapping to track token balances of addresses + mapping(address => uint256) public balances; + + uint256 public totalSupply; // Total supply of tokens + uint256 public totalClaimed; // Total number of tokens claimed + mapping(address => bool) private claimed; // Mapping to track whether an address has claimed tokens + + // Custom errors + error TokensClaimed(); // Error for attempting to claim tokens again + error AllTokensClaimed(); // Error for attempting to claim tokens when all are already claimed + error UnsafeTransfer(address _to); // Error for unsafe token transfer + + // Constructor to set the total supply of tokens + constructor() { + totalSupply = 100000000; // Set the total supply of tokens + } + + // Public function to claim tokens + function claim() public { + // Check if all tokens have been claimed + if (totalClaimed >= totalSupply) revert AllTokensClaimed(); + + // Check if the caller has already claimed tokens + if (claimed[msg.sender]) revert TokensClaimed(); + + // Update balances and claimed status + balances[msg.sender] += 1000; + totalClaimed += 1000; + claimed[msg.sender] = true; + } + + // Public function for safe token transfer + function safeTransfer(address _to, uint256 _amount) public { + // Check for unsafe transfer conditions, including if the target address has a non-zero ether balance + if (_to == address(0) || _to.balance == 0) revert UnsafeTransfer(_to); + + // Ensure the sender has enough balance to transfer + require(balances[msg.sender] >= _amount, "Insufficient balance"); + + // Perform the transfer + balances[msg.sender] -= _amount; + balances[_to] += _amount; + } +}