-
Notifications
You must be signed in to change notification settings - Fork 62
[PT1-115] Added safe send for UniERC20 #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.0; | ||
|
|
||
| /** | ||
| * @title SafeSendLib | ||
| * @dev Library for safely sending Ether to a recipient address. | ||
| */ | ||
| library SafeSendLib { | ||
| /** | ||
| * @dev Sends `amount` wei to address `to` in constructor by calling selfdestruct. | ||
| * Even recipients that are contracts with revert in fallback() or receive() will receive the funds. | ||
| * @param to The address to send Ether to. | ||
| * @param amount The amount of wei to send. | ||
| */ | ||
| function safeSend(address to, uint256 amount) internal { | ||
| assembly ("memory-safe") { // solhint-disable-line no-inline-assembly | ||
| mstore(0, to) | ||
| mstore8(11, 0x73) // 0x73 = PUSH20 opcode | ||
| mstore8(32, 0xff) // 0xff = SELFDESTRUCT opcode | ||
| pop(create(amount, 11, 22)) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; | |
| import "../interfaces/IERC20MetadataUppercase.sol"; | ||
| import "./SafeERC20.sol"; | ||
| import "./StringUtil.sol"; | ||
| import "./SafeSendLib.sol"; | ||
|
|
||
| /** | ||
| * @title UniERC20 | ||
|
|
@@ -15,6 +16,7 @@ import "./StringUtil.sol"; | |
| */ | ||
| library UniERC20 { | ||
| using SafeERC20 for IERC20; | ||
| using SafeSendLib for address payable; | ||
|
|
||
| error InsufficientBalance(); | ||
| error ApproveCalledOnETH(); | ||
|
|
@@ -73,6 +75,29 @@ library UniERC20 { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Transfers a specified amount of the token to a given address. | ||
| * Note: Does nothing if the amount is zero. | ||
| * Note 2: Uses the SafeSendLib for safe Ether transfers. | ||
| * @param token The token to transfer. | ||
| * @param to The address to transfer the token to. | ||
| * @param amount The amount of the token to transfer. | ||
| */ | ||
| function uniSafeTransfer( | ||
| IERC20 token, | ||
| address payable to, | ||
| uint256 amount | ||
| ) internal { | ||
| if (amount > 0) { | ||
| if (isETH(token)) { | ||
| if (address(this).balance < amount) revert InsufficientBalance(); | ||
| to.safeSend(amount); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about check |
||
| } else { | ||
| token.safeTransfer(to, amount); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Transfers a specified amount of the token from one address to another. | ||
| * Note: Does nothing if the amount is zero. | ||
|
|
@@ -106,6 +131,38 @@ library UniERC20 { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Transfers a specified amount of the token from one address to another. | ||
| * Note: Does nothing if the amount is zero. | ||
| * Note 2: Uses the SafeSendLib for safe Ether transfers. | ||
| * @param token The token to transfer. | ||
| * @param from The address to transfer the token from. | ||
| * @param to The address to transfer the token to. | ||
| * @param amount The amount of the token to transfer. | ||
| */ | ||
| function uniSafeTransferFrom( | ||
| IERC20 token, | ||
| address payable from, | ||
| address to, | ||
| uint256 amount | ||
| ) internal { | ||
| if (amount > 0) { | ||
| if (isETH(token)) { | ||
| if (msg.value < amount) revert NotEnoughValue(); | ||
| if (from != msg.sender) revert FromIsNotSender(); | ||
| if (to != address(this)) revert ToIsNotThis(); | ||
| if (msg.value > amount) { | ||
| // Return remainder if exist | ||
| unchecked { | ||
| from.safeSend(msg.value - amount); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method sends ETH from |
||
| } | ||
| } | ||
| } else { | ||
| token.safeTransferFrom(from, to, amount); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Retrieves the symbol from ERC20 metadata of the specified token. | ||
| * @param token The token to retrieve the symbol of. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.