Skip to content

Instantly share code, notes, and snippets.

@dawksh
Created March 1, 2022 19:29
Show Gist options
  • Save dawksh/4bb15f926c9c369b12f3184ed52db95c to your computer and use it in GitHub Desktop.
Save dawksh/4bb15f926c9c369b12f3184ed52db95c to your computer and use it in GitHub Desktop.
should swap using v3
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
pragma abicoder v2;
import "https://github.com/Uniswap/uniswap-v3-periphery/blob/main/contracts/interfaces/ISwapRouter.sol";
import "https://github.com/Uniswap/uniswap-v3-periphery/blob/main/contracts/interfaces/IQuoter.sol";
import "https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/TransferHelper.sol";
interface IUniswapRouter is ISwapRouter {
function refundETH() external payable;
}
contract Recipient {
IUniswapRouter public immutable uniswapRouter;
IQuoter public constant quoter = IQuoter(0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6);
address private constant multiDaiKovan = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
address private constant WETH9 = 0xd0A1E359811322d97991E03f863a0C30C2cF029C;
constructor(IUniswapRouter _uniswapRouter) {
uniswapRouter = _uniswapRouter;
}
function convertEthToExactDai(uint256 daiAmount) external payable {
TransferHelper.safeTransferFrom(WETH9, msg.sender, address(this), msg.value);
TransferHelper.safeApprove(WETH9, address(uniswapRouter), msg.value);
uint256 deadline = block.timestamp + 20; // using 'now' for convenience, for mainnet pass deadline from frontend!
address tokenIn = WETH9;
address tokenOut = multiDaiKovan;
uint24 fee = 3000;
address recipient = msg.sender;
uint256 amountOut = daiAmount;
uint160 sqrtPriceLimitX96 = 0;
ISwapRouter.ExactOutputSingleParams memory params = ISwapRouter.ExactOutputSingleParams(
tokenIn,
tokenOut,
fee,
recipient,
deadline,
amountOut,
0,
sqrtPriceLimitX96
);
uniswapRouter.exactOutputSingle(params);
uniswapRouter.refundETH();
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment