Skip to content

Commit

Permalink
fix: get as much Dai as possible after removing liquidity (#375)
Browse files Browse the repository at this point in the history
* fix: get as much Dai as possible after removing liquidity

* fix: remove unused parameter
  • Loading branch information
alcueca authored Oct 22, 2020
1 parent 00fcf81 commit 259aab2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 9 additions & 11 deletions contracts/peripheral/YieldProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ contract YieldProxy is DecimalMath {
"YieldProxy: minimumFYDaiPrice not reached"
);
}
withdrawAssets(fyDai);
withdrawAssets();
}

/// @dev Burns tokens and repays debt with proceedings. Sells any excess fyDai for Dai, then returns all Dai, and if there is no debt in the Controller, all posted Chai.
Expand Down Expand Up @@ -273,7 +273,7 @@ contract YieldProxy is DecimalMath {
"YieldProxy: minimumFYDaiPrice not reached"
);
}
withdrawAssets(fyDai);
withdrawAssets();
}

/// @dev Burns tokens and repays fyDai debt after Maturity.
Expand All @@ -293,18 +293,16 @@ contract YieldProxy is DecimalMath {
if (daiObtained > 0 && controller.debtFYDai(CHAI, maturity, msg.sender) > 0) {
controller.repayDai(CHAI, maturity, address(this), msg.sender, daiObtained);
}
withdrawAssets(fyDai);
withdrawAssets();
}

/// @dev Return to caller all posted chai if there is no debt, converted to dai, plus any dai remaining in the contract.
function withdrawAssets(IFYDai fyDai) internal {
if (controller.debtFYDai(CHAI, fyDai.maturity(), msg.sender) == 0) {
uint256 posted = controller.posted(CHAI, msg.sender);
uint256 locked = controller.locked(CHAI, msg.sender);
require (posted >= locked, "YieldProxy: Undercollateralized");
controller.withdraw(CHAI, msg.sender, address(this), posted - locked);
chai.exit(address(this), chai.balanceOf(address(this)));
}
function withdrawAssets() internal {
uint256 posted = controller.posted(CHAI, msg.sender);
uint256 locked = controller.locked(CHAI, msg.sender);
require (posted >= locked, "YieldProxy: Undercollateralized");
controller.withdraw(CHAI, msg.sender, address(this), posted - locked);
chai.exit(address(this), chai.balanceOf(address(this)));
require(dai.transfer(msg.sender, dai.balanceOf(address(this))), "YieldProxy: Dai Transfer Failed");
}

Expand Down
4 changes: 2 additions & 2 deletions test/peripheral/551_liquidityProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ contract('YieldProxy - LiquidityProxy', async (accounts) => {
expect(await pool0.balanceOf(user2)).to.be.bignumber.eq(ZERO)
// Has less fyDai debt
expect(await controller.debtFYDai(CHAI, maturity0, user2)).to.be.bignumber.lt(debt)
// Doesn't have dai
expect(await dai.balanceOf(user2)).to.be.bignumber.eq(ZERO)
// Got some dai
expect(await dai.balanceOf(user2)).to.be.bignumber.gt(ZERO)
// Has the same fyDai
expect(await fyDai0.balanceOf(user2)).to.be.bignumber.eq(toBorrow)
// Proxy doesn't keep dai (beyond rounding)
Expand Down

0 comments on commit 259aab2

Please sign in to comment.