File size: 5,837 Bytes
f998fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "./IERC20.sol";
import "./Ownable.sol";
import "./Whitelist.sol";

contract PluTopee is IERC20, Ownable, Whitelist {
    string public constant name = "Plutopee";
    string public constant symbol = "PT";
    uint8 public constant decimals = 18;

    uint256 private constant _totalSupply = 1000000000 * 10 ** uint(decimals);
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;
    mapping(address => uint256) private _lockedAmount;

    event Lock(address indexed owner, address account, uint256 amount);
    event Unlock(address indexed owner, address account, uint256 amount);

    constructor() {
        _balances[msg.sender] = _totalSupply;
    }

    function totalSupply() public pure override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }

    function allowance(address owner, address spender) public view override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @return The amount of tokens locked to `account`.
     */
    function lockedAmountOf(address account) public view returns (uint256) {
        return _lockedAmount[account];
    }

    function transfer(address to, uint256 amount) external override returns (bool) {
        _transfer(msg.sender, to, amount);
        return true;
    }

    /**
     * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
     * `transferFrom`. This is semantically equivalent to an infinite approval.
     */
    function approve(address spender, uint256 amount) external override returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }

    // OPTIONAL
    function transferFrom(address from, address to, uint256 amount) external override returns (bool) {
        _spendAllowance(from, msg.sender, amount);
        _transfer(from, to, amount);
        return true;
    }

    function _transfer(address from, address to, uint256 amount) internal {
        require(from != address(0), "ERC20Basic: transfer from the zero address");
        require(to != address(0), "ERC20Basic: transfer to the zero address");

        uint256 currentBalance = balanceOf(from);
        uint256 lockedAmount = lockedAmountOf(from);
        uint256 availableAmount;

        require(currentBalance >= lockedAmount, "ERC20Basic: balance is not available by lock");
        unchecked { availableAmount = currentBalance - lockedAmount; }
        require(availableAmount >= amount, "ERC20Basic: transfer amount exceeds balance");

        unchecked {
            _balances[from] -= amount;
            _balances[to] += amount;
            require(_balances[to] >= amount, "ERC20Basic: overflow of the to's balance");
        }

        emit Transfer(from, to, amount);
    }

    function _approve(address owner, address spender, uint256 amount) internal {
        require(owner != address(0), "ERC20Basic: approve owner the zero address");
        require(spender != address(0), "ERC20Basic: approve spender the zero address");

        _allowances[owner][spender] = amount;

        emit Approval(owner, spender, amount);
    }

    /**
     * NOTE: Does not update the allowance if the current allowance
     * is the maximum `uint256`.
     */
    function _spendAllowance(address owner, address spender, uint256 amount) internal {
        uint256 currentAllowance = allowance(owner, spender);
        require(currentAllowance >= amount, "ERC20Basic: insufficient allowance");

        if (currentAllowance != type(uint256).max) {
            unchecked {
                _approve(owner, spender, currentAllowance - amount);
            }
        }
    }

    function increaseAllowance(address spender, uint256 addedValue) external returns (bool) {
        uint256 currentAllowance = allowance(msg.sender, spender);
        unchecked {
            uint256 newAllowance = currentAllowance + addedValue;
            require(newAllowance >= currentAllowance, "ERC20Basic: overflow of the allowance");

            _approve(msg.sender, spender, newAllowance);
        }

        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool) {
        uint256 currentAllowance = allowance(msg.sender, spender);
        require(currentAllowance >= subtractedValue, "ERC20Basic: decreased allowance below zero");

        unchecked {
            _approve(msg.sender, spender, currentAllowance - subtractedValue);
        }

        return true;
    }

    function lock(address account, uint256 amount) public onlyWhitelisted returns (bool) {
        require(balanceOf(account) >= amount, "ERC20Basic: Insufficient balance to lock");

        unchecked {
            _lockedAmount[account] += amount;
            require(_lockedAmount[account] >= amount, "ERC20Basic: overflow of locked amount");

            emit Lock(msg.sender, account, amount);
        }

        return true;
    }

    function unlock(address account, uint256 amount) external onlyWhitelisted returns (bool) {
        require(_lockedAmount[account] >= amount, "ERC20Basic: underflow of locked amount");

        unchecked {
            _lockedAmount[account] -= amount;

            emit Unlock(msg.sender, account, amount);
        }

        return true;
    }

    function transferWithLock(address to, uint256 amount) external onlyWhitelisted returns (bool) {
        _transfer(msg.sender, to, amount);
        lock(to, amount);

        return true;
    }
}