// SPDX-License-Identifier: GPL-2.0-or-later pragma solidity >=0.7.5; import '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import '../interfaces/IPeripheryPayments.sol'; import '../interfaces/external/ISAMB.sol'; import '../libraries/TransferHelper.sol'; import './PeripheryImmutableState.sol'; abstract contract PeripheryPayments is IPeripheryPayments, PeripheryImmutableState { receive() external payable { require(msg.sender == SAMB, 'Not SAMB'); } /// @inheritdoc IPeripheryPayments function unwrapSAMB(uint256 amountMinimum, address recipient) public payable override { uint256 balanceSAMB = ISAMB(SAMB).balanceOf(address(this)); require(balanceSAMB >= amountMinimum, 'Insufficient SAMB'); if (balanceSAMB > 0) { ISAMB(SAMB).withdraw(balanceSAMB); TransferHelper.safeTransferAMB(recipient, balanceSAMB); } } /// @inheritdoc IPeripheryPayments function sweepToken(address token, uint256 amountMinimum, address recipient) public payable override { uint256 balanceToken = IERC20(token).balanceOf(address(this)); require(balanceToken >= amountMinimum, 'Insufficient token'); if (balanceToken > 0) { TransferHelper.safeTransfer(token, recipient, balanceToken); } } /// @inheritdoc IPeripheryPayments function refundAMB() external payable override { if (address(this).balance > 0) TransferHelper.safeTransferAMB(msg.sender, address(this).balance); } /// @param token The token to pay /// @param payer The entity that must pay /// @param recipient The entity that will receive payment /// @param value The amount to pay function pay(address token, address payer, address recipient, uint256 value) internal { if (token == SAMB && address(this).balance >= value) { // pay with SAMB ISAMB(SAMB).deposit{value: value}(); // wrap only what is needed to pay ISAMB(SAMB).transfer(recipient, value); } else if (payer == address(this)) { // pay with tokens already in the contract (for the exact input multihop case) TransferHelper.safeTransfer(token, recipient, value); } else { // pull payment TransferHelper.safeTransferFrom(token, payer, recipient, value); } } }