@1inch/solidity-utils 2.0.6-alpha1 → 2.0.7

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.
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1inch/solidity-utils",
3
- "version": "2.0.6-alpha1",
3
+ "version": "2.0.7",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "repository": {
@@ -31,6 +31,7 @@
31
31
  "chai": "4.3.6",
32
32
  "chai-as-promised": "7.1.1",
33
33
  "ethereumjs-wallet": "1.0.2",
34
+ "hardhat": "2.8.4",
34
35
  "web3-utils": "1.7.0"
35
36
  },
36
37
  "devDependencies": {
@@ -56,7 +57,6 @@
56
57
  "eslint-plugin-promise": "6.0.0",
57
58
  "eslint-plugin-standard": "5.0.0",
58
59
  "eslint-plugin-typescript": "0.14.0",
59
- "hardhat": "2.8.4",
60
60
  "hardhat-deploy": "0.10.5",
61
61
  "hardhat-gas-reporter": "1.0.8",
62
62
  "rimraf": "3.0.2",