Skip to content

Commit

Permalink
use currency library to combine sweepETH and sweepToken
Browse files Browse the repository at this point in the history
  • Loading branch information
tinaszheng committed Nov 21, 2023
1 parent 0152e5c commit e5fc9dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
21 changes: 8 additions & 13 deletions contracts/base/PeripheryPayments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,27 @@ pragma solidity ^0.8.19;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC20Minimal} from "@uniswap/v4-core/contracts/interfaces/external/IERC20Minimal.sol";

import {Currency, CurrencyLibrary} from "@uniswap/v4-core/contracts/types/Currency.sol";
import {IPeripheryPayments} from "../interfaces/IPeripheryPayments.sol";
import {TransferHelper} from "../libraries/TransferHelper.sol";

import "../libraries/TransferHelper.sol";

using CurrencyLibrary for Currency;
using TransferHelper for address;
using TransferHelper for IERC20Minimal;

error InsufficientToken();

abstract contract PeripheryPayments is IPeripheryPayments {
/// @inheritdoc IPeripheryPayments
function sweepToken(address token, uint256 amountMinimum, address recipient) public payable override {
uint256 balanceToken = IERC20(token).balanceOf(address(this));
if (balanceToken < amountMinimum) revert InsufficientToken();
function sweepToken(Currency currency, uint256 amountMinimum, address recipient) public payable override {
uint256 balanceCurrency = currency.balanceOfSelf();
if (balanceCurrency < amountMinimum) revert InsufficientToken();

if (balanceToken > 0) {
IERC20Minimal(token).safeTransfer(recipient, balanceToken);
if (balanceCurrency > 0) {
currency.transfer(recipient, balanceCurrency);
}
}

/// @inheritdoc IPeripheryPayments
function refundETH() external payable override {
if (address(this).balance > 0) msg.sender.safeTransferETH(address(this).balance);
}

/// @param token The token to pay
/// @param payer The entity that must pay
/// @param recipient The entity that will receive payment
Expand Down
11 changes: 4 additions & 7 deletions contracts/interfaces/IPeripheryPayments.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import {Currency} from "@uniswap/v4-core/contracts/types/Currency.sol";

/// @title Periphery Payments
/// @notice Functions to ease deposits and withdrawals of ETH
interface IPeripheryPayments {
// TODO: figure out if we still need unwrapWETH9 from v3?

/// @notice Refunds any ETH balance held by this contract to the `msg.sender`
/// @dev Useful for bundling with mint or increase liquidity that uses ether, or exact output swaps
/// that use ether for the input amount
function refundETH() external payable;

/// @notice Transfers the full amount of a token held by this contract to recipient
/// @dev The amountMinimum parameter prevents malicious contracts from stealing the token from users
/// @param token The contract address of the token which will be transferred to `recipient`
/// @param currency The contract address of the token which will be transferred to `recipient`
/// @param amountMinimum The minimum amount of token required for a transfer
/// @param recipient The destination address of the token
function sweepToken(address token, uint256 amountMinimum, address recipient) external payable;
function sweepToken(Currency currency, uint256 amountMinimum, address recipient) external payable;
}

0 comments on commit e5fc9dd

Please sign in to comment.