-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLibERC20Compat.sol
32 lines (29 loc) · 990 Bytes
/
LibERC20Compat.sol
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
// SPDX-License-Identifier: Beta Software
pragma solidity ^0.8;
import "../tokens/IERC20.sol";
// Compatibility helpers for ERC20s.
library LibERC20Compat {
error NotATokenError(IERC20 token);
error TokenTransferFailedError(IERC20 token, address to, uint256 amount);
// Perform an `IERC20.transfer()` handling non-compliant implementations.
function compatTransfer(IERC20 token, address to, uint256 amount)
internal
{
(bool s, bytes memory r) =
address(token).call(abi.encodeCall(IERC20.transfer, (to, amount)));
if (s) {
if (r.length == 0) {
uint256 cs;
assembly { cs := extcodesize(token) }
if (cs == 0) {
revert NotATokenError(token);
}
return;
}
if (abi.decode(r, (bool))) {
return;
}
}
revert TokenTransferFailedError(token, to, amount);
}
}