Skip to content

Commit

Permalink
chore: fix convention
Browse files Browse the repository at this point in the history
  • Loading branch information
huyhuynh3103 committed Jan 2, 2024
1 parent 301210e commit 7768fd1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 88 deletions.
16 changes: 8 additions & 8 deletions src/legacy/transfers/RONTransferHelper.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/Strings.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";

/**
* @title RONTransferHelper
Expand All @@ -10,17 +10,17 @@ library RONTransferHelper {
/**
* @dev Transfers RON and wraps result for the method caller to a recipient.
*/
function safeTransfer(address payable _to, uint256 _value) internal {
bool _success = send(_to, _value);
function safeTransfer(address payable to, uint256 value) internal {
bool success = send(to, value);

if (!_success) {
if (!success) {
revert(
string(
abi.encodePacked(
"TransferHelper: could not transfer RON to ",
Strings.toHexString(uint160(address(_to)), 20),
Strings.toHexString(uint160(address(to)), 20),
" value ",
Strings.toHexString(_value)
Strings.toHexString(value)
)
)
);
Expand All @@ -31,7 +31,7 @@ library RONTransferHelper {
* @dev Returns whether the call was success.
* Note: this function should use with the `ReentrancyGuard`.
*/
function send(address payable _to, uint256 _value) internal returns (bool _success) {
(_success,) = _to.call{ value: _value }(new bytes(0));
function send(address payable to, uint256 value) internal returns (bool success) {
(success,) = to.call{ value: value }(new bytes(0));
}
}
110 changes: 53 additions & 57 deletions src/legacy/transfers/RONTransferHelperExtended.sol
Original file line number Diff line number Diff line change
@@ -1,122 +1,118 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../../interfaces/IWRON.sol";
import "../../interfaces/IWRONHelper.sol";
import "./RONTransferHelper.sol";
import "./TransferFromHelper.sol";
import "./TransferHelper.sol";
import { IWRON } from "../../interfaces/IWRON.sol";
import { IWRONHelper } from "../../interfaces/IWRONHelper.sol";
import { RONTransferHelper } from "./RONTransferHelper.sol";
import { TransferFromHelper } from "./TransferFromHelper.sol";
import { TransferHelper } from "./TransferHelper.sol";

library RONTransferHelperExtended {
/**
* @dev Safely unwraps tokens if it is WRON token and transfers them to a specified recipient.
* @param _wron Address of the WRON contract.
* @param _token Address of the ERC20 token to unwrap and transfer.
* @param _to Address of the recipient to transfer the tokens to.
* @param _amount Amount of tokens to transfer.
* @param wron Address of the WRON contract.
* @param token Address of the ERC20 token to unwrap and transfer.
* @param to Address of the recipient to transfer the tokens to.
* @param amount Amount of tokens to transfer.
*
* Note: This function may cause a revert if the consumer contract is a proxy, consider using the method
* `safeUnwrapTokenAndTransferWithHelper` instead.
*/
function safeUnwrapTokenAndTransfer(IWRON _wron, address _token, address payable _to, uint256 _amount) internal {
if (_token == address(_wron)) {
function safeUnwrapTokenAndTransfer(IWRON wron, address token, address payable to, uint256 amount) internal {
if (token == address(wron)) {
// Check whether the recipient receives RON
if (RONTransferHelper.send(_to, 0)) {
_wron.withdraw(_amount);
RONTransferHelper.safeTransfer(_to, _amount);
if (RONTransferHelper.send(to, 0)) {
wron.withdraw(amount);
RONTransferHelper.safeTransfer(to, amount);
return;
}
}

TransferHelper.safeTransfer(_token, _to, _amount);
TransferHelper.safeTransfer(token, to, amount);
}

/**
* @dev See `safeUnwrapTokenAndTransfer`.
*
* Requirements:
* - The consumer contract must approve the contract `_wronHelper`.
* - The consumer contract must approve the contract `wronHelper`.
*
* Note: This function supports the use of a proxy contract by using the WRONHelper contract to unwrap WRON and
* transfer RON.
*/
function safeUnwrapTokenAndTransferWithHelper(
IWRON _wron,
IWRONHelper _wronHelper,
address _token,
address payable _to,
uint256 _amount
IWRON wron,
IWRONHelper wronHelper,
address token,
address payable to,
uint256 amount
) internal {
if (_token == address(_wron)) {
if (token == address(wron)) {
// Check whether the recipient receives RON
if (RONTransferHelper.send(_to, 0)) {
_wron.approve(address(_wronHelper), _amount);
_wronHelper.withdrawTo(_to, _amount);
if (RONTransferHelper.send(to, 0)) {
wron.approve(address(wronHelper), amount);
wronHelper.withdrawTo(to, amount);
return;
}
}

TransferHelper.safeTransfer(_token, _to, _amount);
TransferHelper.safeTransfer(token, to, amount);
}

/**
* @dev Safely unwraps tokens if it is WRON token from specified sender, and transfers them to a specified recipient.
* @param _wron Address of the WRON contract.
* @param _token Address of the ERC20 token to unwrap and transfer.
* @param _from Address of the sender on whose behalf the tokens will be unwrapped and transferred.
* @param _to Address of the recipient to transfer the tokens to.
* @param _amount Amount of tokens to transfer.
* @param wron Address of the WRON contract.
* @param token Address of the ERC20 token to unwrap and transfer.
* @param from Address of the sender on whose behalf the tokens will be unwrapped and transferred.
* @param to Address of the recipient to transfer the tokens to.
* @param amount Amount of tokens to transfer.
*
* Note: This function may cause a revert if the consumer contract is a proxy, consider using the method
* `safeUnwrapTokenAndTransferFromWithHelper` instead.
*/
function safeUnwrapTokenAndTransferFrom(
IWRON _wron,
address _token,
address _from,
address payable _to,
uint256 _amount
) internal {
if (_token == address(_wron)) {
function safeUnwrapTokenAndTransferFrom(IWRON wron, address token, address from, address payable to, uint256 amount)
internal
{
if (token == address(wron)) {
// Check whether the recipient receives RON
if (RONTransferHelper.send(_to, 0)) {
TransferFromHelper.safeTransferFrom(_token, _from, address(this), _amount);
IWRON(_wron).withdraw(_amount);
RONTransferHelper.safeTransfer(_to, _amount);
if (RONTransferHelper.send(to, 0)) {
TransferFromHelper.safeTransferFrom(token, from, address(this), amount);
IWRON(wron).withdraw(amount);
RONTransferHelper.safeTransfer(to, amount);
return;
}
}

TransferFromHelper.safeTransferFrom(_token, _from, _to, _amount);
TransferFromHelper.safeTransferFrom(token, from, to, amount);
}

/**
* @dev See `safeUnwrapTokenAndTransfer`.
*
* Requirements:
* - The consumer contract must approve the contract `_wronHelper`.
* - The consumer contract must approve the contract `wronHelper`.
*
* Note: This function supports the use of a proxy contract by using the WRONHelper contract to unwrap WRON and
* transfer RON.
*/
function safeUnwrapTokenAndTransferFromWithHelper(
IWRON _wron,
IWRONHelper _wronHelper,
address _token,
address _from,
address payable _to,
uint256 _amount
IWRON wron,
IWRONHelper wronHelper,
address token,
address from,
address payable to,
uint256 amount
) internal {
if (_token == address(_wron)) {
if (token == address(wron)) {
// Check whether the recipient receives RON
if (RONTransferHelper.send(_to, 0)) {
TransferFromHelper.safeTransferFrom(_token, _from, address(this), _amount);
_wron.approve(address(_wronHelper), _amount);
_wronHelper.withdrawTo(_to, _amount);
if (RONTransferHelper.send(to, 0)) {
TransferFromHelper.safeTransferFrom(token, from, address(this), amount);
wron.approve(address(wronHelper), amount);
wronHelper.withdrawTo(to, amount);
return;
}
}

TransferFromHelper.safeTransferFrom(_token, _from, _to, _amount);
TransferFromHelper.safeTransferFrom(token, from, to, amount);
}
}
25 changes: 10 additions & 15 deletions src/legacy/transfers/TransferFromHelper.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/Strings.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";

/**
* @title TransferFromHelper
Expand All @@ -11,18 +11,13 @@ library TransferFromHelper {
/**
* @dev Transfers token and wraps result for the input address to a recipient.
*/
function safeTransferFrom(
address _token,
address _from,
address _to,
uint256 _value
) internal {
(bool success, bytes memory data) = _token.call(
function safeTransferFrom(address token, address from, address to, uint256 value) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(
0x23b872dd, // IERC20.transferFrom.selector
_from,
_to,
_value
from,
to,
value
)
);

Expand All @@ -31,13 +26,13 @@ library TransferFromHelper {
string(
abi.encodePacked(
"TransferFromHelper: could not transfer token ",
Strings.toHexString(uint160(_token), 20),
Strings.toHexString(uint160(token), 20),
" from ",
Strings.toHexString(uint160(_from), 20),
Strings.toHexString(uint160(from), 20),
" to ",
Strings.toHexString(uint160(_to), 20),
Strings.toHexString(uint160(to), 20),
" value ",
Strings.toHexString(_value)
Strings.toHexString(value)
)
)
);
Expand Down
16 changes: 8 additions & 8 deletions src/legacy/transfers/TransferHelper.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/Strings.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";

/**
* @title TransferHelper
Expand All @@ -11,12 +11,12 @@ library TransferHelper {
/**
* @dev Transfers token and wraps result for the method caller to a recipient.
*/
function safeTransfer(address _token, address _to, uint256 _value) internal {
(bool success, bytes memory data) = _token.call(
function safeTransfer(address token, address to, uint256 value) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(
0xa9059cbb, // IERC20.transfer.selector
_to,
_value
to,
value
)
);

Expand All @@ -25,11 +25,11 @@ library TransferHelper {
string(
abi.encodePacked(
"TransferHelper: could not transfer token ",
Strings.toHexString(uint160(_token), 20),
Strings.toHexString(uint160(token), 20),
" to ",
Strings.toHexString(uint160(_to), 20),
Strings.toHexString(uint160(to), 20),
" value ",
Strings.toHexString(_value)
Strings.toHexString(value)
)
)
);
Expand Down

0 comments on commit 7768fd1

Please sign in to comment.