This repository was archived by the owner on Dec 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeTRC20.sol
More file actions
59 lines (42 loc) · 2.3 KB
/
SafeTRC20.sol
File metadata and controls
59 lines (42 loc) · 2.3 KB
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
pragma solidity ^0.5.8;
import "./SafeMath.sol";
import "./Address.sol";
import "./ITRC20.sol";
library SafeTRC20 {
address constant USDTAddr = 0xa614f803B6FD780986A42c78Ec9c7f77e6DeD13C;
using SafeMath for uint256;
using Address for address;
function safeTransfer(ITRC20 token, address to, uint256 value) internal {
if (address(token) == USDTAddr) {
(bool success, bytes memory data) = address(token).call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(success, "SafeTRC20: low-level call failed");
} else {
callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
}
function safeTransferFrom(ITRC20 token, address from, address to, uint256 value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(ITRC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeTRC20: approve from non-zero to non-zero allowance"
);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(ITRC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(ITRC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeTRC20: decreased allowance below zero");
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function callOptionalReturn(ITRC20 token, bytes memory data) private {
require(address(token).isContract(), "SafeTRC20: call to non-contract");
(bool success, bytes memory returndata) = address(token).call(data);
require(success, "SafeTRC20: low-level call failed");
if (returndata.length > 0) { // Return data is optional
require(abi.decode(returndata, (bool)), "SafeTRC20: TRC20 operation did not succeed");
}
}
}