Last active
June 7, 2022 22:21
-
-
Save merlox/9b79a13182745b546a940411c464cb04 to your computer and use it in GitHub Desktop.
The yield farming wallet with stETH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-2.0-or-later | |
pragma solidity 0.7.6; | |
pragma abicoder v2; | |
import "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol"; | |
interface IV3SwapRouter is IUniswapV3SwapCallback { | |
struct ExactInputSingleParams { | |
address tokenIn; | |
address tokenOut; | |
uint24 fee; | |
address recipient; | |
uint256 amountIn; | |
uint256 amountOutMinimum; | |
uint160 sqrtPriceLimitX96; | |
} | |
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut); | |
struct ExactInputParams { | |
bytes path; | |
address recipient; | |
uint256 amountIn; | |
uint256 amountOutMinimum; | |
} | |
function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut); | |
struct ExactOutputSingleParams { | |
address tokenIn; | |
address tokenOut; | |
uint24 fee; | |
address recipient; | |
uint256 amountOut; | |
uint256 amountInMaximum; | |
uint160 sqrtPriceLimitX96; | |
} | |
function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn); | |
struct ExactOutputParams { | |
bytes path; | |
address recipient; | |
uint256 amountOut; | |
uint256 amountInMaximum; | |
} | |
function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn); | |
} | |
interface IERC20 { | |
function totalSupply() external view returns (uint256); | |
function balanceOf(address account) external view returns (uint256); | |
function transfer(address recipient, uint256 amount) external returns (bool); | |
function allowance(address owner, address spender) external view returns (uint256); | |
function approve(address spender, uint256 amount) external returns (bool); | |
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); | |
} | |
interface IWETH { | |
function deposit() external payable; | |
function withdraw(uint wad) external; | |
} | |
// This contract will be executed when the user decides to stake his balance | |
contract YieldFarmingWallet { | |
address public stETH = 0x2B8b15f3A9B5a80f5D61D9309D6EBe9e423e2d24; | |
address public wETH = 0xc778417E063141139Fce010982780140Aa0cD5Ab; | |
address payable public owner; | |
IV3SwapRouter public uniswapRouter = IV3SwapRouter(0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45); | |
modifier onlyOwner { | |
require(msg.sender == owner, "Not owner"); | |
_; | |
} | |
receive () external payable { | |
convertToSTETH(); | |
} | |
constructor () payable { | |
owner = payable(msg.sender); | |
// Approve all tokens to the uniswap contract | |
IERC20(stETH).approve(address(uniswapRouter), ~uint256(0)); | |
IERC20(wETH).approve(address(uniswapRouter), ~uint256(0)); | |
// Convert all tokens to stETH | |
convertToSTETH(); | |
} | |
function convertToSTETH() public payable { | |
require(msg.value > 0, "Must pass non 0 ETH amount"); | |
address tokenIn = wETH; | |
address tokenOut = stETH; | |
uint24 fee = 3000; | |
address recipient = address(this); | |
uint256 amountIn = msg.value; | |
uint256 amountOutMinimum = 1; | |
uint160 sqrtPriceLimitX96 = 0; | |
IWETH(wETH).deposit{value: msg.value}(); | |
IV3SwapRouter.ExactInputSingleParams memory params = IV3SwapRouter.ExactInputSingleParams( | |
tokenIn, | |
tokenOut, | |
fee, | |
recipient, | |
amountIn, | |
amountOutMinimum, | |
sqrtPriceLimitX96 | |
); | |
uniswapRouter.exactInputSingle(params); | |
} | |
function convertToETH(uint256 _amount) public onlyOwner { | |
require(_amount > 0, "Must pass non 0 amount"); | |
IERC20(stETH).transferFrom(msg.sender, address(this), _amount); | |
IV3SwapRouter.ExactInputSingleParams memory params = | |
IV3SwapRouter.ExactInputSingleParams({ | |
tokenIn: stETH, | |
tokenOut: wETH, | |
fee: 3000, | |
recipient: msg.sender, | |
amountIn: _amount, | |
amountOutMinimum: 0, | |
sqrtPriceLimitX96: 0 | |
}); | |
// The call to `exactInputSingle` executes the swap. | |
uniswapRouter.exactInputSingle(params); | |
} | |
function transferFunds(address payable _to, uint256 _amount) external onlyOwner { | |
_to.transfer(_amount); | |
} | |
function transferTokens(address _token, address _to, uint256 _amount) external onlyOwner { | |
IERC20(_token).transfer(_to, _amount); | |
} | |
function extractFunds() external onlyOwner { | |
owner.transfer(address(this).balance); | |
} | |
function extractTokens(address _token) external onlyOwner { | |
uint256 balance = IERC20(_token).balanceOf(owner); | |
IERC20(_token).transfer(owner, balance); | |
} | |
function getBalance() public view returns(uint256) { | |
return address(this).balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment