Skip to content

Instantly share code, notes, and snippets.

@tienshaoku
Last active May 19, 2020 15:06
Show Gist options
  • Save tienshaoku/ac10fc1bfad1d255745a5b5431e0c373 to your computer and use it in GitHub Desktop.
Save tienshaoku/ac10fc1bfad1d255745a5b5431e0c373 to your computer and use it in GitHub Desktop.
// contract instance
CErc20 immutable cDai;
// constructor() {
cDaiAddress = 0x6D7F0754FFeb405d23C51CE938289d4835bE3b14;
cDai = CErc20(cDaiAddress);
// }
function swapETHToDaiToCompound() public payable returns(bool){
address[] memory _paths = new address[](2);
_paths[0] = WETHAddress;
_paths[1] = DaiAddress;
uint[] memory _result = uniswapV2Router01.swapExactETHForTokens{value: msg.value}(0, _paths, address(this), now + 120);
uint _DaiAmount = _result[1];
Dai.approve(cDaiAddress, _DaiAmount);
assert(cDai.mint(_DaiAmount) == 0);
return true;
}
function redeemFromCompoundAndReturnETH() public returns(uint[] memory){
assert(cDai.redeem(cDai.balanceOf(address(this))) == 0);
address[] memory _paths = new address[](2);
_paths[0] = DaiAddress;
_paths[1] = WETHAddress;
uint _DaiAmount = Dai.balanceOf(address(this));
Dai.approve(address(uniswapV2Router01), _DaiAmount);
return uniswapV2Router01.swapExactTokensForETH(_DaiAmount, 0, _paths, msg.sender, now + 120);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment