feat: Integrated SafeERC20
This commit is contained in:
Submodule evm/lib/forge-std updated: f73c73d201...155d547c44
@@ -3,6 +3,7 @@ pragma solidity ^0.8.13;
|
|||||||
|
|
||||||
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
||||||
import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
|
import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
|
||||||
|
import {SafeERC20} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||||
|
|
||||||
/// @dev Integral submitted deadline of 3600 seconds (1 hour) to Paraswap, but it is not strictly necessary to be this long
|
/// @dev Integral submitted deadline of 3600 seconds (1 hour) to Paraswap, but it is not strictly necessary to be this long
|
||||||
/// as the contract allows less durations, we use 1000 seconds (15 minutes) as a deadline
|
/// as the contract allows less durations, we use 1000 seconds (15 minutes) as a deadline
|
||||||
@@ -10,6 +11,8 @@ uint256 constant SWAP_DEADLINE_SEC = 1000;
|
|||||||
|
|
||||||
/// @title Integral Swap Adapter
|
/// @title Integral Swap Adapter
|
||||||
contract IntegralSwapAdapter is ISwapAdapter {
|
contract IntegralSwapAdapter is ISwapAdapter {
|
||||||
|
using SafeERC20 for IERC20;
|
||||||
|
|
||||||
ITwapRelayer immutable relayer;
|
ITwapRelayer immutable relayer;
|
||||||
|
|
||||||
constructor(address relayer_) {
|
constructor(address relayer_) {
|
||||||
@@ -124,21 +127,19 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
IERC20 buyToken,
|
IERC20 buyToken,
|
||||||
uint256 amount
|
uint256 amount
|
||||||
) internal returns (uint256) {
|
) internal returns (uint256) {
|
||||||
address swapper = msg.sender;
|
|
||||||
|
|
||||||
uint256 amountOut = relayer.quoteSell(address(sellToken), address(buyToken), amount);
|
uint256 amountOut = relayer.quoteSell(address(sellToken), address(buyToken), amount);
|
||||||
if (amountOut == 0) {
|
if (amountOut == 0) {
|
||||||
revert Unavailable("AmountOut is zero!");
|
revert Unavailable("AmountOut is zero!");
|
||||||
}
|
}
|
||||||
|
|
||||||
sellToken.transferFrom(msg.sender, address(this), amount);
|
sellToken.safeTransferFrom(msg.sender, address(this), amount);
|
||||||
sellToken.approve(address(relayer), amount);
|
sellToken.safeIncreaseAllowance(address(relayer), amount);
|
||||||
|
|
||||||
relayer.sell(ITwapRelayer.SellParams({
|
relayer.sell(ITwapRelayer.SellParams({
|
||||||
tokenIn: address(sellToken),
|
tokenIn: address(sellToken),
|
||||||
tokenOut: address(buyToken),
|
tokenOut: address(buyToken),
|
||||||
wrapUnwrap: false,
|
wrapUnwrap: false,
|
||||||
to: swapper,
|
to: msg.sender,
|
||||||
submitDeadline: uint32(block.timestamp + SWAP_DEADLINE_SEC),
|
submitDeadline: uint32(block.timestamp + SWAP_DEADLINE_SEC),
|
||||||
amountIn: amount,
|
amountIn: amount,
|
||||||
amountOutMin: amountOut
|
amountOutMin: amountOut
|
||||||
@@ -157,21 +158,19 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
IERC20 buyToken,
|
IERC20 buyToken,
|
||||||
uint256 amountBought
|
uint256 amountBought
|
||||||
) internal returns (uint256) {
|
) internal returns (uint256) {
|
||||||
address swapper = msg.sender;
|
|
||||||
|
|
||||||
uint256 amountIn = relayer.quoteBuy(address(sellToken), address(buyToken), amountBought);
|
uint256 amountIn = relayer.quoteBuy(address(sellToken), address(buyToken), amountBought);
|
||||||
if (amountIn == 0) {
|
if (amountIn == 0) {
|
||||||
revert Unavailable("AmountIn is zero!");
|
revert Unavailable("AmountIn is zero!");
|
||||||
}
|
}
|
||||||
|
|
||||||
sellToken.transferFrom(msg.sender, address(this), amountIn);
|
sellToken.safeTransferFrom(msg.sender, address(this), amountIn);
|
||||||
sellToken.approve(address(relayer), amountIn);
|
sellToken.safeIncreaseAllowance(address(relayer), amountIn);
|
||||||
|
|
||||||
relayer.buy(ITwapRelayer.BuyParams({
|
relayer.buy(ITwapRelayer.BuyParams({
|
||||||
tokenIn: address(sellToken),
|
tokenIn: address(sellToken),
|
||||||
tokenOut: address(buyToken),
|
tokenOut: address(buyToken),
|
||||||
wrapUnwrap: false,
|
wrapUnwrap: false,
|
||||||
to: swapper,
|
to: msg.sender,
|
||||||
submitDeadline: uint32(block.timestamp + SWAP_DEADLINE_SEC),
|
submitDeadline: uint32(block.timestamp + SWAP_DEADLINE_SEC),
|
||||||
amountInMax: amountIn,
|
amountInMax: amountIn,
|
||||||
amountOut: amountBought
|
amountOut: amountBought
|
||||||
|
|||||||
Reference in New Issue
Block a user