Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't do unnecessary crank "simulations" #117

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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