Skip to content

Commit

Permalink
Don't do unnecessary crank simulations (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
ams9198 authored Dec 1, 2022
1 parent ac2de4b commit 7da4b73
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions contracts/controllers/WithdrawController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,12 @@ contract WithdrawController is IWithdrawController {
/**
* @dev Cranks a lender
*/
function crankLender(address addr) internal {
_withdrawState[addr] = _currentWithdrawState(addr);
function crankLender(address addr)
internal
returns (IPoolWithdrawState memory state)
{
state = _currentWithdrawState(addr);
_withdrawState[addr] = state;
}

/*//////////////////////////////////////////////////////////////
Expand All @@ -511,18 +515,17 @@ contract WithdrawController is IWithdrawController {
onlyPool
returns (uint256 assets)
{
crankLender(owner);
IPoolWithdrawState memory state = crankLender(owner);

// Calculate how many assets should be transferred
IPoolWithdrawState memory state = _currentWithdrawState(owner);
assets = PoolLib.calculateConversion(
shares,
state.withdrawableAssets,
state.redeemableShares,
false
);

_performWithdraw(owner, shares, assets);
_performWithdraw(owner, state, shares, assets);
}

/**
Expand All @@ -533,30 +536,28 @@ contract WithdrawController is IWithdrawController {
onlyPool
returns (uint256 shares)
{
crankLender(owner);
IPoolWithdrawState memory state = crankLender(owner);

// Calculate how many shares should be burned
IPoolWithdrawState memory state = _currentWithdrawState(owner);
shares = PoolLib.calculateConversion(
assets,
state.redeemableShares,
state.withdrawableAssets,
true
);

_performWithdraw(owner, shares, assets);
_performWithdraw(owner, state, shares, assets);
}

/**
* @dev Perform the state update for a withdraw
*/
function _performWithdraw(
address owner,
IPoolWithdrawState memory currentState,
uint256 shares,
uint256 assets
) internal {
IPoolWithdrawState memory currentState = _currentWithdrawState(owner);

require(
assets <= currentState.withdrawableAssets,
"Pool: InsufficientBalance"
Expand Down

0 comments on commit 7da4b73

Please sign in to comment.