Last active
November 21, 2022 17:42
-
-
Save pythonpete32/51158058ec8880d2bfb610c40382b099 to your computer and use it in GitHub Desktop.
This file contains 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: MIT | |
pragma solidity 0.8.10; | |
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
import {PluginUUPSUpgradeable} from "aragon-plugin-base/contracts/lib/plugin/PluginUUPSUpgradeable.sol"; | |
import {IUniswapV2Router} from "./IUniswap.sol"; | |
import {IDAO} from "aragon-plugin-base/contracts/lib/interfaces/IDAO.sol"; | |
/// @title UniswapHelper | |
/// @notice A helper contract providing a swap function for the `SwapPluginv1` and `SwapPluginv2` example contracts. | |
contract UniswapHelper is PluginUUPSUpgradeable { | |
/// @notice Address of the Uniswap router. | |
address private constant UNISWAP_V2_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; | |
/// @notice Address of the WETH token. | |
address private constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; | |
/// @notice The selector of the approve function on a token. | |
bytes4 private constant APPROVE_SELECTOR = bytes4(keccak256("approve(address,uint256)")); | |
/// @notice The selector of the swap function on the Uniswap router. | |
bytes4 private constant SWAP_SELECTOR = | |
bytes4(keccak256("swapExactTokensForTokens(uint256,uint256,address[],address,uint256)")); | |
/// @notice Create the approve and swap `Actions` for a given token and amount. | |
/// @param _tokenIn The token to swap from. | |
/// @param _tokenOut The token to swap to. | |
/// @param _amountIn The amount of tokens to swap. | |
/// @param _amountOutMin The minimum amount of tokens to receive. | |
/// @param _dao The contract of the associated DAO. | |
function swap( | |
address _tokenIn, | |
address _tokenOut, | |
uint256 _amountIn, | |
uint256 _amountOutMin, | |
address _dao | |
) external view returns (IDAO.Action[] memory actions) { | |
actions = new IDAO.Action[](2); | |
// Approve the Uniswap router to spend the token | |
actions[0] = IDAO.Action({ | |
to: _tokenIn, | |
value: 0, | |
data: abi.encodeWithSelector(APPROVE_SELECTOR, UNISWAP_V2_ROUTER, _amountIn) | |
}); | |
// Create path | |
address[] memory path; | |
path = new address[](2); | |
path[0] = _tokenIn; | |
path[1] = _tokenOut; | |
// Swap the tokens | |
actions[1] = IDAO.Action({ | |
to: UNISWAP_V2_ROUTER, | |
value: 0, | |
data: abi.encodeWithSelector( | |
SWAP_SELECTOR, | |
_amountIn, | |
_amountOutMin, | |
path, | |
_dao, | |
block.timestamp | |
) | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment