@pru.pinya/contracts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +7 -0
  2. package/artifacts/contracts/access/AccessController.sol/AccessController.json +304 -0
  3. package/artifacts/contracts/access/AccessControllerKAP.sol/AccessControllerKAP.json +352 -0
  4. package/artifacts/contracts/access/AccessControllerKAP.sol/IKYCBitkubChain.json +33 -0
  5. package/artifacts/contracts/access/AccessControllerKAP.sol/KYCHandler.json +116 -0
  6. package/artifacts/contracts/access/Authorization.sol/Authorization.json +78 -0
  7. package/artifacts/contracts/access/AuthorizationKAP.sol/AuthorizationKAP.json +155 -0
  8. package/artifacts/contracts/access/Committee.sol/Committee.json +65 -0
  9. package/artifacts/contracts/access/Ownable.sol/Ownable.json +66 -0
  10. package/artifacts/contracts/interfaces/IAdminProjectRouter.sol/IAdminProjectRouter.json +62 -0
  11. package/artifacts/contracts/interfaces/IKYCBitkubChain.sol/IKYCBitkubChain.json +51 -0
  12. package/artifacts/contracts/kyc/KYCHandler.sol/KYCHandler.json +53 -0
  13. package/artifacts/contracts/libs/Address.sol/Address.json +13 -0
  14. package/artifacts/contracts/libs/EnumerableSetAddress.sol/EnumerableSetAddress.json +13 -0
  15. package/artifacts/contracts/libs/EnumerableSetUint.sol/EnumerableSetUint.json +13 -0
  16. package/artifacts/contracts/libs/EnumerateMap.sol/EnumerableMap.json +13 -0
  17. package/artifacts/contracts/libs/String.sol/Strings.json +13 -0
  18. package/artifacts/contracts/token/KAP-1155/KAP1155.sol/KAP1155.json +810 -0
  19. package/artifacts/contracts/token/KAP-1155/interfaces/IKAP1155.sol/IKAP1155.json +403 -0
  20. package/artifacts/contracts/token/KAP-1155/interfaces/IKAP1155Enumerable.sol/IKAP1155Enumerable.json +27 -0
  21. package/artifacts/contracts/token/KAP-1155/interfaces/IKAP1155Metadata.sol/IKAP1155Metadata.json +78 -0
  22. package/artifacts/contracts/token/KAP-1155/interfaces/IKAP1155Receiver.sol/IKAP1155Receiver.json +92 -0
  23. package/artifacts/contracts/token/KAP-20/KAP20.sol/KAP20.json +729 -0
  24. package/artifacts/contracts/token/KAP-20/interfaces/IKAP20.sol/IKAP20.json +294 -0
  25. package/artifacts/contracts/token/KAP-20/interfaces/IKToken.sol/IKToken.json +72 -0
  26. package/artifacts/contracts/token/KAP-22/KAP22.sol/KAP22.json +1022 -0
  27. package/artifacts/contracts/token/KAP-22/interfaces/IKAP22.sol/IKAP22.json +176 -0
  28. package/artifacts/contracts/token/KAP-22/utils/TransferRouter.sol/TransferRouter.json +52 -0
  29. package/artifacts/contracts/token/KAP-22/utils/WhitelistAddress.sol/WhitelistAddress.json +115 -0
  30. package/artifacts/contracts/token/KAP-721/KAP721.sol/KAP721.json +912 -0
  31. package/artifacts/contracts/token/KAP-721/interfaces/IKAP721.sol/IKAP721.json +380 -0
  32. package/artifacts/contracts/token/KAP-721/interfaces/IKAP721Enumerable.sol/IKAP721Enumerable.json +70 -0
  33. package/artifacts/contracts/token/KAP-721/interfaces/IKAP721Metadata.sol/IKAP721Metadata.json +59 -0
  34. package/artifacts/contracts/token/KAP-721/interfaces/IKAP721Receiver.sol/IKAP721Receiver.json +48 -0
  35. package/artifacts/contracts/token/KAP-721/interfaces/IKAP721V2.sol/IKAP721V2.json +103 -0
  36. package/artifacts/contracts/utils/Context.sol/Context.json +13 -0
  37. package/artifacts/contracts/utils/Pausable.sol/Pausable.json +53 -0
  38. package/artifacts/contracts/utils/introspections/IKAP165.sol/IKAP165.json +33 -0
  39. package/artifacts/contracts/utils/introspections/KAP165.sol/KAP165.json +33 -0
  40. package/contracts/access/AccessController.sol +96 -0
  41. package/contracts/access/AccessControllerKAP.sol +116 -0
  42. package/contracts/access/Authorization.sol +36 -0
  43. package/contracts/access/AuthorizationKAP.sol +60 -0
  44. package/contracts/access/Committee.sol +26 -0
  45. package/contracts/access/Ownable.sol +39 -0
  46. package/contracts/interfaces/IAdminProjectRouter.sol +9 -0
  47. package/contracts/interfaces/IKYCBitkubChain.sol +8 -0
  48. package/contracts/kyc/KYCHandler.sol +24 -0
  49. package/contracts/libs/Address.sol +16 -0
  50. package/contracts/libs/EnumerableSetAddress.sol +70 -0
  51. package/contracts/libs/EnumerableSetUint.sol +74 -0
  52. package/contracts/libs/EnumerateMap.sol +136 -0
  53. package/contracts/libs/String.sol +26 -0
  54. package/contracts/token/KAP-1155/KAP1155.sol +493 -0
  55. package/contracts/token/KAP-1155/interfaces/IKAP1155.sol +69 -0
  56. package/contracts/token/KAP-1155/interfaces/IKAP1155Enumerable.sol +7 -0
  57. package/contracts/token/KAP-1155/interfaces/IKAP1155Metadata.sol +12 -0
  58. package/contracts/token/KAP-1155/interfaces/IKAP1155Receiver.sol +20 -0
  59. package/contracts/token/KAP-20/KAP20.sol +276 -0
  60. package/contracts/token/KAP-20/interfaces/IKAP20.sol +43 -0
  61. package/contracts/token/KAP-20/interfaces/IKToken.sol +17 -0
  62. package/contracts/token/KAP-22/KAP22.sol +358 -0
  63. package/contracts/token/KAP-22/interfaces/IKAP22.sol +23 -0
  64. package/contracts/token/KAP-22/utils/TransferRouter.sol +29 -0
  65. package/contracts/token/KAP-22/utils/WhitelistAddress.sol +35 -0
  66. package/contracts/token/KAP-721/KAP721.sol +416 -0
  67. package/contracts/token/KAP-721/interfaces/IKAP721.sol +60 -0
  68. package/contracts/token/KAP-721/interfaces/IKAP721Enumerable.sol +10 -0
  69. package/contracts/token/KAP-721/interfaces/IKAP721Metadata.sol +10 -0
  70. package/contracts/token/KAP-721/interfaces/IKAP721Receiver.sol +11 -0
  71. package/contracts/token/KAP-721/interfaces/IKAP721V2.sol +20 -0
  72. package/contracts/utils/Context.sol +13 -0
  73. package/contracts/utils/Pausable.sol +34 -0
  74. package/contracts/utils/introspections/IKAP165.sol +6 -0
  75. package/contracts/utils/introspections/KAP165.sol +12 -0
  76. package/index.js +5 -0
  77. package/package.json +35 -0
@@ -0,0 +1,276 @@
1
+ //SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity ^0.8.0;
4
+
5
+ import {IKAP20} from "./interfaces/IKAP20.sol";
6
+ import {IKToken} from "./interfaces/IKToken.sol";
7
+ import {Pausable} from "../../utils/Pausable.sol";
8
+ import {AccessController} from "../../access/AccessController.sol";
9
+ import {IKYCBitkubChain} from "../../interfaces/IKYCBitkubChain.sol";
10
+ import {IAdminProjectRouter} from "../../interfaces/IAdminProjectRouter.sol";
11
+
12
+ abstract contract KAP20 is IKAP20, IKToken, Pausable, AccessController {
13
+ mapping(address => uint256) _balances;
14
+
15
+ mapping(address => mapping(address => uint256)) internal _allowances;
16
+
17
+ uint256 public override totalSupply;
18
+ string public override name;
19
+ string public override symbol;
20
+ uint8 public override decimals;
21
+
22
+ constructor(
23
+ string memory _name,
24
+ string memory _symbol,
25
+ string memory _projectName,
26
+ uint8 _decimals,
27
+ address _kyc,
28
+ address _adminProjectRouter,
29
+ address _committee,
30
+ address _transferRouter,
31
+ uint256 _acceptedKYCLevel
32
+ ) {
33
+ name = _name;
34
+ symbol = _symbol;
35
+ PROJECT = _projectName;
36
+ decimals = _decimals;
37
+ kyc = IKYCBitkubChain(_kyc);
38
+ adminProjectRouter = IAdminProjectRouter(_adminProjectRouter);
39
+ committee = _committee;
40
+ transferRouter = _transferRouter;
41
+ acceptedKYCLevel = _acceptedKYCLevel;
42
+ }
43
+
44
+ function balanceOf(address account)
45
+ public
46
+ view
47
+ virtual
48
+ override
49
+ returns (uint256)
50
+ {
51
+ return _balances[account];
52
+ }
53
+
54
+ function transfer(address recipient, uint256 amount)
55
+ public
56
+ virtual
57
+ override
58
+ whenNotPaused
59
+ returns (bool)
60
+ {
61
+ _transfer(msg.sender, recipient, amount);
62
+ return true;
63
+ }
64
+
65
+ function allowance(address owner, address spender)
66
+ public
67
+ view
68
+ virtual
69
+ override
70
+ returns (uint256)
71
+ {
72
+ return _allowances[owner][spender];
73
+ }
74
+
75
+ function approve(address spender, uint256 amount)
76
+ public
77
+ virtual
78
+ override
79
+ returns (bool)
80
+ {
81
+ _approve(msg.sender, spender, amount);
82
+ return true;
83
+ }
84
+
85
+ function adminApprove(
86
+ address owner,
87
+ address spender,
88
+ uint256 amount
89
+ )
90
+ public
91
+ virtual
92
+ override
93
+ whenNotPaused
94
+ onlySuperAdminOrAdmin
95
+ returns (bool)
96
+ {
97
+ require(
98
+ kyc.kycsLevel(owner) >= acceptedKYCLevel &&
99
+ kyc.kycsLevel(spender) >= acceptedKYCLevel,
100
+ "KAP20: owner or spender address is not a KYC user"
101
+ );
102
+
103
+ _approve(owner, spender, amount);
104
+ return true;
105
+ }
106
+
107
+ function transferFrom(
108
+ address sender,
109
+ address recipient,
110
+ uint256 amount
111
+ ) public virtual override whenNotPaused returns (bool) {
112
+ _transfer(sender, recipient, amount);
113
+
114
+ uint256 currentAllowance = _allowances[sender][msg.sender];
115
+ require(
116
+ currentAllowance >= amount,
117
+ "KAP20: transfer amount exceeds allowance"
118
+ );
119
+ unchecked {
120
+ _approve(sender, msg.sender, currentAllowance - amount);
121
+ }
122
+
123
+ return true;
124
+ }
125
+
126
+ function increaseAllowance(address spender, uint256 addedValue)
127
+ public
128
+ virtual
129
+ returns (bool)
130
+ {
131
+ _approve(
132
+ msg.sender,
133
+ spender,
134
+ _allowances[msg.sender][spender] + addedValue
135
+ );
136
+ return true;
137
+ }
138
+
139
+ function decreaseAllowance(address spender, uint256 subtractedValue)
140
+ public
141
+ virtual
142
+ returns (bool)
143
+ {
144
+ uint256 currentAllowance = _allowances[msg.sender][spender];
145
+ require(
146
+ currentAllowance >= subtractedValue,
147
+ "KAP20: decreased allowance below zero"
148
+ );
149
+ unchecked {
150
+ _approve(msg.sender, spender, currentAllowance - subtractedValue);
151
+ }
152
+
153
+ return true;
154
+ }
155
+
156
+ function _transfer(
157
+ address sender,
158
+ address recipient,
159
+ uint256 amount
160
+ ) internal virtual {
161
+ require(sender != address(0), "KAP20: transfer from the zero address");
162
+ require(recipient != address(0), "KAP20: transfer to the zero address");
163
+
164
+ uint256 senderBalance = _balances[sender];
165
+ require(
166
+ senderBalance >= amount,
167
+ "KAP20: transfer amount exceeds balance"
168
+ );
169
+ unchecked {
170
+ _balances[sender] = senderBalance - amount;
171
+ }
172
+ _balances[recipient] += amount;
173
+
174
+ emit Transfer(sender, recipient, amount);
175
+ }
176
+
177
+ function _mint(address account, uint256 amount) internal virtual {
178
+ require(account != address(0), "KAP20: mint to the zero address");
179
+
180
+ totalSupply += amount;
181
+ _balances[account] += amount;
182
+ emit Transfer(address(0), account, amount);
183
+ }
184
+
185
+ function _burn(address account, uint256 amount) internal virtual {
186
+ require(account != address(0), "KAP20: burn from the zero address");
187
+
188
+ uint256 accountBalance = _balances[account];
189
+ require(accountBalance >= amount, "KAP20: burn amount exceeds balance");
190
+ unchecked {
191
+ _balances[account] = accountBalance - amount;
192
+ }
193
+ totalSupply -= amount;
194
+
195
+ emit Transfer(account, address(0), amount);
196
+ }
197
+
198
+ function _approve(
199
+ address owner,
200
+ address spender,
201
+ uint256 amount
202
+ ) internal virtual {
203
+ require(owner != address(0), "KAP20: approve from the zero address");
204
+ require(spender != address(0), "KAP20: approve to the zero address");
205
+
206
+ _allowances[owner][spender] = amount;
207
+ emit Approval(owner, spender, amount);
208
+ }
209
+
210
+ function adminTransfer(
211
+ address sender,
212
+ address recipient,
213
+ uint256 amount
214
+ ) public virtual override onlyCommittee returns (bool) {
215
+ if (isActivatedOnlyKYCAddress) {
216
+ require(
217
+ kyc.kycsLevel(sender) > 0 && kyc.kycsLevel(recipient) > 0,
218
+ "KAP20: only internal purpose"
219
+ );
220
+ }
221
+ uint256 senderBalance = _balances[sender];
222
+ require(
223
+ senderBalance >= amount,
224
+ "KAP20: transfer amount exceeds balance"
225
+ );
226
+ unchecked {
227
+ _balances[sender] = senderBalance - amount;
228
+ }
229
+ _balances[recipient] += amount;
230
+
231
+ emit Transfer(sender, recipient, amount);
232
+
233
+ return true;
234
+ }
235
+
236
+ function internalTransfer(
237
+ address sender,
238
+ address recipient,
239
+ uint256 amount
240
+ )
241
+ external
242
+ override
243
+ whenNotPaused
244
+ onlySuperAdminOrTransferRouter
245
+ returns (bool)
246
+ {
247
+ require(
248
+ kyc.kycsLevel(sender) >= acceptedKYCLevel &&
249
+ kyc.kycsLevel(recipient) >= acceptedKYCLevel,
250
+ "KAP20: only internal purpose"
251
+ );
252
+
253
+ _transfer(sender, recipient, amount);
254
+ return true;
255
+ }
256
+
257
+ function externalTransfer(
258
+ address sender,
259
+ address recipient,
260
+ uint256 amount
261
+ )
262
+ external
263
+ override
264
+ whenNotPaused
265
+ onlySuperAdminOrTransferRouter
266
+ returns (bool)
267
+ {
268
+ require(
269
+ kyc.kycsLevel(sender) >= acceptedKYCLevel,
270
+ "KAP20: only internal purpose"
271
+ );
272
+
273
+ _transfer(sender, recipient, amount);
274
+ return true;
275
+ }
276
+ }
@@ -0,0 +1,43 @@
1
+ //SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.9.0;
4
+
5
+ interface IKAP20 {
6
+ function totalSupply() external view returns (uint256);
7
+
8
+ function decimals() external view returns (uint8);
9
+
10
+ function symbol() external view returns (string memory);
11
+
12
+ function name() external view returns (string memory);
13
+
14
+ function balanceOf(address account) external view returns (uint256);
15
+
16
+ function transfer(address recipient, uint256 amount) external returns (bool);
17
+
18
+ function allowance(address owner, address spender) external view returns (uint256);
19
+
20
+ function approve(address spender, uint256 amount) external returns (bool);
21
+
22
+ function adminApprove(
23
+ address owner,
24
+ address spender,
25
+ uint256 amount
26
+ ) external returns (bool);
27
+
28
+ function transferFrom(
29
+ address sender,
30
+ address recipient,
31
+ uint256 amount
32
+ ) external returns (bool);
33
+
34
+ function adminTransfer(
35
+ address sender,
36
+ address recipient,
37
+ uint256 amount
38
+ ) external returns (bool);
39
+
40
+ event Transfer(address indexed from, address indexed to, uint256 value);
41
+
42
+ event Approval(address indexed owner, address indexed spender, uint256 value);
43
+ }
@@ -0,0 +1,17 @@
1
+ //SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.9.0;
4
+
5
+ interface IKToken {
6
+ function internalTransfer(
7
+ address sender,
8
+ address recipient,
9
+ uint256 amount
10
+ ) external returns (bool);
11
+
12
+ function externalTransfer(
13
+ address sender,
14
+ address recipient,
15
+ uint256 amount
16
+ ) external returns (bool);
17
+ }
@@ -0,0 +1,358 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.8.0;
4
+
5
+ import {IKAP20} from "../KAP-20/interfaces/IKAP20.sol";
6
+ import {IKAP22} from "./interfaces/IKAP22.sol";
7
+ import {IKToken} from "../KAP-20/interfaces/IKToken.sol";
8
+ import {TransferRouter} from "./utils/TransferRouter.sol";
9
+ import {IKYCBitkubChain} from "../../interfaces/IKYCBitkubChain.sol";
10
+ import {KYCHandler} from "../../kyc/KYCHandler.sol";
11
+ import {AccessControllerKAP} from "../../access/AccessControllerKAP.sol";
12
+ import {WhitelistAddress} from "./utils/WhitelistAddress.sol";
13
+ import {EnumerableSetAddress} from "../../libs/EnumerableSetAddress.sol";
14
+
15
+ abstract contract KAP22 is IKAP20, IKAP22, IKToken, AccessControllerKAP {
16
+ mapping(address => mapping(uint256 => uint256)) private _balances; // owner => period => balance
17
+ mapping(address => uint256) private _unStampBalance; // owner => balance
18
+ mapping(address => mapping(address => uint256)) private _allowances; // owner => spender => amount
19
+
20
+ string public override name;
21
+ string public override symbol;
22
+ uint8 public immutable override decimals;
23
+ uint256 public override cap;
24
+ uint256 public override totalSupply;
25
+ uint256 public override currentIndex;
26
+ uint256 public override startTime;
27
+ uint256 public override periodLength;
28
+
29
+ modifier updatePeriod() {
30
+ _updatePeriod();
31
+ _;
32
+ }
33
+
34
+ // constructor(
35
+ // // string memory name_,
36
+ // // string memory symbol_,
37
+ // // uint8 decimals_,
38
+ // address owner_,
39
+ // address vault_,
40
+ // address minter_,
41
+ // address burner_,
42
+ // address transferRouter_,
43
+ // address kyc_,
44
+ // uint256 acceptedKycLevel_,
45
+ // uint256 startTime_,
46
+ // uint256 periodLength_
47
+ // ) AccessControllerKAP(owner_, vault_, minter_, burner_, transferRouter_, kyc_, acceptedKycLevel_) {
48
+ // name = "name_";
49
+ // symbol = "symbol_";
50
+ // decimals = 18;
51
+ // startTime = startTime_;
52
+ // periodLength = periodLength_;
53
+ // }
54
+
55
+ constructor(
56
+ string memory name_,
57
+ string memory symbol_,
58
+ address owner_,
59
+ uint256 startTime_,
60
+ uint256 periodLength_,
61
+ uint256 cap_
62
+ ) AccessControllerKAP(owner_, address(0), address(0), 4) {
63
+ name = name_;
64
+ symbol = symbol_;
65
+ decimals = 18;
66
+ startTime = startTime_;
67
+ periodLength = periodLength_;
68
+ cap = cap_;
69
+ }
70
+
71
+ function setCap(uint256 amount) external onlyOwner updatePeriod returns (bool) {
72
+ require(cap <= amount, "KAP22: can't set less than older cap");
73
+
74
+ cap = amount;
75
+
76
+ return true;
77
+ }
78
+
79
+ function adminApprove(
80
+ address _owner,
81
+ address _spender,
82
+ uint256 _amount
83
+ ) external override onlyOwner returns (bool) {
84
+ _approve(_owner, _spender, _amount);
85
+ return true;
86
+ }
87
+
88
+ function mint(uint256 amount, address receiver) external override onlyOwner updatePeriod returns (bool) {
89
+ require(totalSupply + amount <= cap, "KAP22: cap exceeded");
90
+
91
+ totalSupply += amount;
92
+
93
+ _balances[receiver][currentIndex + 1] += amount;
94
+
95
+ emit Mint(currentIndex, receiver, amount);
96
+
97
+ return true;
98
+ }
99
+
100
+ function burn(
101
+ uint256 period,
102
+ uint256 amount,
103
+ address _owner
104
+ ) external override onlyOwner updatePeriod returns (bool) {
105
+ // _totalSupply -= amount;
106
+
107
+ // _balances[_owner][period] -= amount;
108
+
109
+ // emit Burn(period, _owner, amount);
110
+
111
+ return true;
112
+ }
113
+
114
+ function currentPeriod() external view override returns (uint256) {
115
+ return _currentPeriod(currentIndex);
116
+ }
117
+
118
+ function balanceOf(address _owner) external view override returns (uint256 balance) {
119
+ uint256 tmpBalances;
120
+ uint256 tmpPeriod = _currentPeriod(currentIndex);
121
+
122
+ if (isWhitelistAddr(_owner)) {
123
+ for (uint256 i = 0; i <= tmpPeriod + 1; i++) {
124
+ tmpBalances += _balances[_owner][i];
125
+ }
126
+ } else {
127
+ tmpBalances += _balances[_owner][tmpPeriod];
128
+ tmpBalances += _balances[_owner][tmpPeriod + 1];
129
+ }
130
+
131
+ return tmpBalances;
132
+ }
133
+
134
+ function balanceOfCurrentPeriod(address _owner) external view returns (uint256 balance) {
135
+ uint256 tmpPeriod = _currentPeriod(currentIndex);
136
+
137
+ return _balances[_owner][tmpPeriod];
138
+ }
139
+
140
+ function balanceOfNextPeriod(address _owner) external view returns (uint256 balance) {
141
+ uint256 tmpPeriod = _currentPeriod(currentIndex);
142
+
143
+ return _balances[_owner][tmpPeriod + 1];
144
+ }
145
+
146
+ function balanceOfByPeriod(address _owner, uint256 _period) external view returns (uint256 balance) {
147
+ return _balances[_owner][_period];
148
+ }
149
+
150
+ function transfer(address _recipient, uint256 _amount) external override returns (bool) {
151
+ if ((isWhitelistAddr(msg.sender) || msg.sender == owner) && isWhitelistAddr(_recipient)) {
152
+ _transferPreviousPeriod(msg.sender, _recipient, _amount, 0);
153
+ } else if (msg.sender == owner) {
154
+ _transferSpecificPeriod(msg.sender, _recipient, _amount, currentIndex + 1);
155
+ } else {
156
+ _transfer(msg.sender, _recipient, _amount);
157
+ }
158
+
159
+ return true;
160
+ }
161
+
162
+ function allowance(address _owner, address _spender) external view override returns (uint256 remaining) {
163
+ return _allowances[_owner][_spender];
164
+ }
165
+
166
+ function approve(address _spender, uint256 _amount) external override returns (bool) {
167
+ _approve(msg.sender, _spender, _amount);
168
+ return true;
169
+ }
170
+
171
+ function transferFrom(
172
+ address _sender,
173
+ address _recipient,
174
+ uint256 _amount
175
+ ) external override returns (bool) {
176
+ uint256 currentAllowance = _allowances[_sender][msg.sender];
177
+ require(currentAllowance >= _amount, "KAP22: transfer amount exceeds allowance");
178
+ _approve(_sender, msg.sender, currentAllowance - _amount);
179
+
180
+ if ((isWhitelistAddr(msg.sender) || msg.sender == owner) && isWhitelistAddr(_recipient)) {
181
+ _transferPreviousPeriod(msg.sender, _recipient, _amount, 0);
182
+ } else if (msg.sender == owner) {
183
+ _transferSpecificPeriod(msg.sender, _recipient, _amount, currentIndex + 1);
184
+ } else {
185
+ _transfer(msg.sender, _recipient, _amount);
186
+ }
187
+
188
+ return true;
189
+ }
190
+
191
+ function increaseAllowance(address _spender, uint256 _addedValue) external returns (bool) {
192
+ _approve(msg.sender, _spender, _allowances[msg.sender][_spender] + _addedValue);
193
+ return true;
194
+ }
195
+
196
+ function decreaseAllowance(address _spender, uint256 _subtractedValue) external returns (bool) {
197
+ uint256 currentAllowance = _allowances[msg.sender][_spender];
198
+ require(currentAllowance >= _subtractedValue, "KAP22: decreased allowance below zero");
199
+ _approve(msg.sender, _spender, currentAllowance - _subtractedValue);
200
+
201
+ return true;
202
+ }
203
+
204
+ function adminTransfer(
205
+ address _sender,
206
+ address _recipient,
207
+ uint256 _amount
208
+ ) external override onlyOwner returns (bool) {
209
+ _transfer(_sender, _recipient, _amount);
210
+ return true;
211
+ }
212
+
213
+ function adminTransferPreviousPeriod(
214
+ address _sender,
215
+ address _recipient,
216
+ uint256 _amount,
217
+ uint256 _period
218
+ ) external onlyOwner returns (bool) {
219
+ _transferPreviousPeriod(_sender, _recipient, _amount, _period);
220
+ return true;
221
+ }
222
+
223
+ function internalTransfer(
224
+ address _sender,
225
+ address _recipient,
226
+ uint256 _amount
227
+ ) external override onlyTransferRouter returns (bool) {
228
+ require(_isBitkubNext(_sender) && _isBitkubNext(_recipient), "KAP22: only internal purpose");
229
+
230
+ _transfer(_sender, _recipient, _amount);
231
+ return true;
232
+ }
233
+
234
+ function externalTransfer(
235
+ address _sender,
236
+ address _recipient,
237
+ uint256 _amount
238
+ ) external override onlyTransferRouter returns (bool) {
239
+ require(_isBitkubNext(_sender), "KAP22: only internal purpose");
240
+
241
+ _transfer(_sender, _recipient, _amount);
242
+ return true;
243
+ }
244
+
245
+ function updatePeriodNow() external {
246
+ _updatePeriod();
247
+ }
248
+
249
+ function _updatePeriod() internal {
250
+ while (block.timestamp > (currentIndex * periodLength) + startTime) {
251
+ currentIndex++;
252
+ }
253
+ }
254
+
255
+ function _currentPeriod(uint256 _period) internal view returns (uint256) {
256
+ uint256 newCurrentPeriod = _period;
257
+
258
+ while (block.timestamp > (newCurrentPeriod * periodLength) + startTime) {
259
+ newCurrentPeriod++;
260
+ }
261
+
262
+ return newCurrentPeriod;
263
+ }
264
+
265
+ function _transfer(
266
+ address _from,
267
+ address _to,
268
+ uint256 _amount
269
+ ) internal updatePeriod {
270
+ require(_balances[_from][currentIndex] >= _amount, "KAP22: transfer amount exceeds balance");
271
+
272
+ _balances[_from][currentIndex] -= _amount;
273
+ _balances[_to][currentIndex] += _amount;
274
+
275
+ emit Transfer(_from, _to, _amount);
276
+ }
277
+
278
+ function _transferSpecificPeriod(
279
+ address _from,
280
+ address _to,
281
+ uint256 _amount,
282
+ uint256 _period
283
+ ) internal updatePeriod {
284
+ require(_balances[_from][_period] >= _amount, "KAP22: transfer amount exceeds balance");
285
+
286
+ _balances[_from][_period] -= _amount;
287
+ _balances[_to][_period] += _amount;
288
+
289
+ emit Transfer(_from, _to, _amount);
290
+ }
291
+
292
+ function _transferNextPeriod(
293
+ address _from,
294
+ address _to,
295
+ uint256 _amount
296
+ ) internal updatePeriod {
297
+ require(
298
+ _balances[_from][currentIndex] + _balances[_from][currentIndex + 1] >= _amount,
299
+ "KAP22: transfer amount exceeds balance"
300
+ );
301
+
302
+ if (_balances[_from][currentIndex] >= _amount) {
303
+ _balances[_from][currentIndex] -= _amount;
304
+ _balances[_to][currentIndex] += _amount;
305
+ } else {
306
+ uint256 balanceCurrentPeriod = _balances[_from][currentIndex];
307
+
308
+ _balances[_from][currentIndex] = 0;
309
+ _balances[_to][currentIndex] += balanceCurrentPeriod;
310
+
311
+ _balances[_from][currentIndex + 1] -= _amount - balanceCurrentPeriod;
312
+ _balances[_to][currentIndex + 1] += _amount - balanceCurrentPeriod;
313
+ }
314
+
315
+ emit Transfer(_from, _to, _amount);
316
+ }
317
+
318
+ function _transferPreviousPeriod(
319
+ address _from,
320
+ address _to,
321
+ uint256 _amount,
322
+ uint256 _period
323
+ ) internal updatePeriod {
324
+ uint256 remainingToTransfer = _amount;
325
+ for (uint256 i = _period; i <= currentIndex; i++) {
326
+ uint256 amountToTransfer = _balances[_from][i];
327
+ if (amountToTransfer > remainingToTransfer) {
328
+ amountToTransfer = remainingToTransfer;
329
+ }
330
+
331
+ _balances[_from][i] -= amountToTransfer;
332
+ _balances[_to][i] += amountToTransfer;
333
+
334
+ remainingToTransfer -= amountToTransfer;
335
+
336
+ if (remainingToTransfer == 0) {
337
+ break;
338
+ }
339
+ }
340
+
341
+ require(remainingToTransfer == 0, "KAP22: transfer amount exceeds balance");
342
+
343
+ emit Transfer(_from, _to, _amount);
344
+ }
345
+
346
+ function _approve(
347
+ address _owner,
348
+ address _spender,
349
+ uint256 _amount
350
+ ) internal updatePeriod {
351
+ require(_owner != address(0), "KAP22: approve from the zero address");
352
+ require(_spender != address(0), "KAP22: approve to the zero address");
353
+
354
+ _allowances[_owner][_spender] = _amount;
355
+
356
+ emit Approval(_owner, _spender, _amount);
357
+ }
358
+ }