all files / contracts/ THXCToken.sol

100% Statements 53/53
95.83% Branches 46/48
100% Functions 12/12
100% Lines 94/94
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319                                                                                                                                                136×   134×     132× 132×     131×               138×     137× 137×     134×       133× 133×   133×   133×     133× 133×           84×             496×     493× 493× 493×             11×     10×   10× 10× 10×     10×         10× 10×                                       16×                                             12× 12×                                                                             12×                                       137× 137×   137× 137×      
// SPDX-License-Identifier: MIT
 
pragma solidity 0.8.24;
 
import './interfaces/THXCTokenInterface.sol';
 
contract THXCToken is THXCTokenInterface {
    /// Custom Errors
    error ERC20InvalidSender(address sender);
    error ERC20InvalidReceiver(address receiver);
    error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed);
    error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed);
    error ERC20InvalidSpender(address spender);
    error MismatchedArrays(uint256 len1, uint256 len2);
    error TooManyRecipients(uint256 count, uint256 maxCount);
    error TooManySpenders(uint256 count, uint256 maxCount);
    error BulkValueTooHigh(uint256 value, uint256 maxValue);
 
    /* This is a slight change to the ERC20 base standard.
    function totalSupply() constant returns (uint256 supply);
    is replaced with:
    uint256 public totalSupply;
    This automatically creates a getter function for the totalSupply.
    This is moved to the base contract since public getter functions are not
    currently recognised as an implementation of the matching abstract
    function by the compiler.
    */
    /// total amount of tokens
    uint256 public totalSupply;
 
    uint256 private constant MAX_UINT256 = ~uint256(0);
    uint256 private BULK_MAX_VALUE;
    uint32 private constant BULK_MAX_COUNT = 100;
 
    uint256 private txCounter;
 
    event BulkTransfer(uint256 indexed _txId, uint256 _bulkCount);
    event BulkApproval(uint256 indexed _txId, uint256 _bulkCount);
 
    mapping(address => uint256) private balances;
    mapping(address => mapping(address => uint256)) private allowed;
 
    string public name;
    uint8 public decimals;
    string public symbol;
 
    constructor(
        uint256 _totalSupply,
        string memory _name,
        uint8 _decimals,
        string memory _symbol
    ) {
        totalSupply = _totalSupply * (10 ** uint256(_decimals));
        name = _name;
        decimals = _decimals;
        symbol = _symbol;
        balances[msg.sender] = totalSupply;
        BULK_MAX_VALUE = (10 ** 9) * (10 ** uint256(_decimals));
        txCounter = 0;
    }
 
    /**
     * @dev Transfer tokens for a specified address
     *
     * @param _to The address that will receive the tokens.
     * @param _value The amount of tokens to send.
     *
     * Requirements:
     *
     * - `_to` cannot be the zero address.
     * - `_to` cannot be the the contract.
     * - the caller must have a balance of at least `_value`.
     *
     * @return success A boolean that indicates if the operation was successful.
     */
    function transfer(
        address _to,
        uint256 _value
    ) external override returns (bool success) {
        if (_to == address(0)) {
            revert ERC20InvalidReceiver(address(0));
        }
        if (_to == address(this)) {
            revert ERC20InvalidReceiver(address(this));
        }
 
        uint256 fromBalance = balances[msg.sender];
        if (fromBalance < _value) {
            revert ERC20InsufficientBalance(msg.sender, fromBalance, _value);
        }
 
        return _transfer(_to, _value);
    }
 
    function transferFrom(
        address _spender,
        address _to,
        uint256 _value
    ) external override returns (bool success) {
        if (_spender == address(0)) {
            revert ERC20InvalidSpender(address(0));
        }
 
        uint256 _allowance = allowed[_spender][msg.sender];
        if (_allowance < _value) {
            revert ERC20InsufficientAllowance(_spender, _allowance, _value);
        }
 
        if (_to == address(0)) {
            revert ERC20InvalidReceiver(address(0));
 
        }
 
        balances[_spender] -= _value;
        balances[_to] += _value;
 
        Eif (_allowance != MAX_UINT256) {
            // Special case to approve unlimited transfers
           allowed[_spender][msg.sender] -= _value;
        }
 
        emit Transfer(_spender, _to, _value);
        return true;
    }
 
    function balanceOf(
        address _owner
    ) external view override returns (uint256 balance) {
        return balances[_owner];
    }
 
    function approve(
        address _spender,
        uint256 _value
    ) external override returns (bool success) {
        if (_spender == address(0)) {
            revert ERC20InvalidSpender(address(0));
        }
 
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value); //solhint-disable-line indent, no-unused-vars
        return true;
    }
 
    function increaseApproval(
        address _spender,
        uint256 _delta
    ) public returns (bool success) {
        if (_spender == address(0)) {
            revert ERC20InvalidSpender(address(0));
        }
 
        uint256 _oldValue = allowed[msg.sender][_spender];
 
        uint256 newValue;
        unchecked {
            newValue = _oldValue + _delta;
        }
 
        if (newValue < _oldValue || newValue == MAX_UINT256) {
            // Truncate upon overflow.
            allowed[msg.sender][_spender] = MAX_UINT256 - 1;
        } else {
            allowed[msg.sender][_spender] = newValue;
        }
 
        emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
        return true;
    }
 
    function decreaseApproval(
        address _spender,
        uint256 _delta
    ) external returns (bool success) {
        if (_spender == address(0)) {
            revert ERC20InvalidSpender(address(0));
        }
 
        uint256 _oldValue = allowed[msg.sender][_spender];
        if (_delta > _oldValue) {
            // Truncate upon overflow.
            allowed[msg.sender][_spender] = 0;
        } else {
            allowed[msg.sender][_spender] =
                _oldValue -
                _delta;
        }
        emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
        return true;
    }
 
    function allowance(
        address _owner,
        address _spender
    ) external view override returns (uint256 remaining) {
        return allowed[_owner][_spender];
    }
 
    /**
     * @dev Transfer tokens to multiple addresses
     * if one of the transfers fails the whole transaction will not be reverted
     *
     * @param _tos The addresses that will receive the tokens.
     * @param _values The amount of tokens to send per address
     *     *
     * @return _bulkCount The sum of transfers that were successful
     */
    function transferBulk(
        address[] memory _tos,
        uint256[] memory _values
    ) external override returns (uint256 _bulkCount) {
        if (_tos.length != _values.length) {
            revert MismatchedArrays(_tos.length, _values.length);
        }
 
        if (_tos.length > BULK_MAX_COUNT) {
            revert TooManyRecipients(_tos.length, BULK_MAX_COUNT);
        }
 
        uint256 _bulkValue = 0;
        for (uint256 j = 0; j < _tos.length; ++j) {
            _bulkValue += _values[j];
        }
        if (_bulkValue > BULK_MAX_VALUE) {
            revert BulkValueTooHigh(_bulkValue, BULK_MAX_VALUE);
        }
 
        txCounter++; // Increment the counter for each new transaction
        bool _success;
        for (uint256 i = 0; i < _tos.length; ++i) {
            _success = transferQuiet(_tos[i], _values[i]);
            if (_success) {
                _bulkCount++;
            }
        }
        emit BulkTransfer(txCounter, _bulkCount);
        return _bulkCount;
    }
 
    function increaseApprovalBulk(
        address[] memory _spenders,
        uint256[] memory _values
    ) external returns (uint256 _bulkCount) {
        if (_spenders.length != _values.length) {
            revert MismatchedArrays(_spenders.length, _values.length);
        }
 
        if (_spenders.length > BULK_MAX_COUNT) {
            revert TooManySpenders(_spenders.length, BULK_MAX_COUNT);
        }
 
        uint256 _bulkValue = 0;
        for (uint256 j = 0; j < _spenders.length; ++j) {
            _bulkValue += _values[j];
        }
        if (_bulkValue > BULK_MAX_VALUE) {
            revert BulkValueTooHigh(_bulkValue, BULK_MAX_VALUE);
        }
 
        txCounter++; // Increment the counter for each new transaction
        bool _success;
        for (uint256 i = 0; i < _spenders.length; ++i) {
            _success = increaseApproval(_spenders[i], _values[i]);
            Eif (_success) {
                _bulkCount++;
            }
        }
        emit BulkApproval(txCounter, _bulkCount);
        return _bulkCount;
    }
 
    /**
     * @dev like transfer, but it fails silently
     * on transferBulk we have to fail silently if one of the transfers fails, if not the whole transaction will fail
     *
     * @param _to The address that will receive the tokens.
     * @param _value The amount of tokens to send.
     *
     * Requirements:
     *
     * - `_to` cannot be the zero address.
     * - `_to` cannot be the the contract.
     * - the caller must have a balance of at least `_value`.
     *
     * @return success A boolean that indicates if the operation was successful.
     */
    function transferQuiet(
        address _to,
        uint256 _value
    ) internal returns (bool success) {
        if (
            _to == address(0) ||
            _to == address(this) ||
            balances[msg.sender] < _value
        ) return false; // Preclude burning tokens to uninitialized address, or sending tokens to the contract.
 
        return _transfer(_to, _value);
    }
 
    /**
     * @dev internal transfer function
     *
     * @param _to The address that will receive the tokens.
     * @param _value The amount of tokens to send.
     *
     * @return success A boolean that indicates if the operation was successful.
     */
    function _transfer(
        address _to,
        uint256 _value
    ) internal returns (bool success) {
        balances[msg.sender] -= _value;
        balances[_to] += _value;
 
        emit Transfer(msg.sender, _to, _value);
        return true;
    }
}