Created
January 28, 2020 11:01
-
-
Save vince0656/606bb06a74900864a89453b13618f80d to your computer and use it in GitHub Desktop.
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
| contract rDAIRelayHub is RelayHub { | |
| using SafeMath for uint256; | |
| address ETH_PROXY = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; | |
| function refuelFor(address target, IRToken rDai, IERC20 dai, KyberNetworkInterface kyber) external { | |
| // 1. Claim earned rDAI interest | |
| address self = address(this); | |
| bool interestClaimSuccess = rDai.payInterest(self); | |
| require(interestClaimSuccess, "Failed to claim interest earned for target"); | |
| // 2. Redeem underlying DAI from rDAI contract | |
| bool redeemSuccess = rDai.redeemAll(); | |
| require(redeemSuccess, "Failed to redeem underlying DAI from interest paid"); | |
| // 3. Swap DAI for ETH | |
| uint256 daiBalance = dai.balanceOf(self); | |
| dai.approve(address(kyber), daiBalance); | |
| (uint expectedDaiEthExhangeRate, ) = kyber.getExpectedRate(dai, IERC20(ETH_PROXY), daiBalance); | |
| uint256 ethReceivedFromDaiSale = kyber.tradeWithHint( | |
| self, | |
| dai, | |
| daiBalance, | |
| IERC20(ETH_PROXY), | |
| self, | |
| 2**256 - 1, | |
| expectedDaiEthExhangeRate, | |
| address(0), | |
| abi.encodePacked(uint256(0)) | |
| ); | |
| // 4. Update target contract's relay balance | |
| balances[target] = balances[target].add(ethReceivedFromDaiSale); | |
| emit Deposited(target, msg.sender, ethReceivedFromDaiSale); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment