@1inch/solidity-utils 2.0.6-alpha2 → 2.0.8
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.
- package/README.md +4 -3
- package/contracts/libraries/UniERC20.sol +64 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
<
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://github.com/1inch/farming/blob/master/.github/1inch_github_w.svg#gh-light-mode-only">
|
|
3
|
+
<img src="https://github.com/1inch/farming/blob/master/.github/1inch_github_b.svg#gh-dark-mode-only">
|
|
4
|
+
</div>
|
|
4
5
|
|
|
5
6
|
# Utils library for contracts and tests
|
|
6
7
|
|
|
@@ -7,6 +7,7 @@ import "@openzeppelin/contracts/utils/math/SafeMath.sol";
|
|
|
7
7
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
8
8
|
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
|
9
9
|
import "./RevertReasonParser.sol";
|
|
10
|
+
import "./StringUtil.sol";
|
|
10
11
|
|
|
11
12
|
library UniERC20 {
|
|
12
13
|
using SafeMath for uint256;
|
|
@@ -37,6 +38,30 @@ library UniERC20 {
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
function uniTransferFrom(IERC20 token, address payable from, address to, uint256 amount) internal {
|
|
42
|
+
if (amount > 0) {
|
|
43
|
+
if (isETH(token)) {
|
|
44
|
+
require(msg.value >= amount, "UniERC20: not enough value");
|
|
45
|
+
require(from == msg.sender, "from is not msg.sender");
|
|
46
|
+
require(to == address(this), "to is not this");
|
|
47
|
+
if (msg.value > amount) {
|
|
48
|
+
// Return remainder if exist
|
|
49
|
+
from.transfer(msg.value.sub(amount));
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
token.safeTransferFrom(from, to, amount);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function uniSymbol(IERC20 token) internal view returns(string memory) {
|
|
58
|
+
return _uniDecode(token, "symbol()", "SYBMOL()");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function uniName(IERC20 token) internal view returns(string memory) {
|
|
62
|
+
return _uniDecode(token, "name()", "NAME()");
|
|
63
|
+
}
|
|
64
|
+
|
|
40
65
|
function uniApprove(IERC20 token, address to, uint256 amount) internal {
|
|
41
66
|
require(!isETH(token), "Approve called on ETH");
|
|
42
67
|
|
|
@@ -49,6 +74,45 @@ library UniERC20 {
|
|
|
49
74
|
}
|
|
50
75
|
}
|
|
51
76
|
|
|
77
|
+
function _uniDecode(IERC20 token, string memory lowerCaseSignature, string memory upperCaseSignature) private view returns(string memory) {
|
|
78
|
+
if (isETH(token)) {
|
|
79
|
+
return "ETH";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
(bool success, bytes memory data) = address(token).staticcall{ gas: 20000 }(
|
|
83
|
+
abi.encodeWithSignature(lowerCaseSignature)
|
|
84
|
+
);
|
|
85
|
+
if (!success) {
|
|
86
|
+
(success, data) = address(token).staticcall{ gas: 20000 }(
|
|
87
|
+
abi.encodeWithSignature(upperCaseSignature)
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (success && data.length >= 96) {
|
|
92
|
+
(uint256 offset, uint256 len) = abi.decode(data, (uint256, uint256));
|
|
93
|
+
if (offset == 0x20 && len > 0 && len <= 256) {
|
|
94
|
+
return string(abi.decode(data, (bytes)));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (success && data.length == 32) {
|
|
99
|
+
uint len = 0;
|
|
100
|
+
while (len < data.length && data[len] >= 0x20 && data[len] <= 0x7E) {
|
|
101
|
+
len++;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (len > 0) {
|
|
105
|
+
bytes memory result = new bytes(len);
|
|
106
|
+
for (uint i = 0; i < len; i++) {
|
|
107
|
+
result[i] = data[i];
|
|
108
|
+
}
|
|
109
|
+
return string(result);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return StringUtil.toHex(address(token));
|
|
114
|
+
}
|
|
115
|
+
|
|
52
116
|
function _callOptionalReturn(IERC20 token, bytes memory data) private {
|
|
53
117
|
// solhint-disable-next-line avoid-low-level-calls
|
|
54
118
|
(bool success, bytes memory result) = address(token).call(data);
|