From 6dbb9cd4e1360a70d593167dc7d371b686612b93 Mon Sep 17 00:00:00 2001 From: Domen Grabec Date: Thu, 7 Dec 2023 00:44:48 +0100 Subject: [PATCH] rename variable, add test for double withdrawal. Gas optimisation when withdrawingAll with no BPT --- .../balancer/BalancerMetaPoolStrategy.sol | 16 +++++++++++----- .../balancerMetaStablePool.fork-test.js | 9 +++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/contracts/contracts/strategies/balancer/BalancerMetaPoolStrategy.sol b/contracts/contracts/strategies/balancer/BalancerMetaPoolStrategy.sol index 82e81ab883..9e7ec13847 100644 --- a/contracts/contracts/strategies/balancer/BalancerMetaPoolStrategy.sol +++ b/contracts/contracts/strategies/balancer/BalancerMetaPoolStrategy.sol @@ -174,11 +174,11 @@ contract BalancerMetaPoolStrategy is BaseAuraStrategy { } } - uint256 minBPT = _getBPTExpected( + uint256 minBpt = _getBPTExpected( strategyAssetsToPoolAssets, strategyAssetAmountsToPoolAssetAmounts ); - uint256 minBPTwDeviation = minBPT.mulTruncate( + uint256 minBptWDeviation = minBpt.mulTruncate( 1e18 - maxDepositDeviation ); @@ -192,7 +192,7 @@ contract BalancerMetaPoolStrategy is BaseAuraStrategy { bytes memory userData = abi.encode( IBalancerVault.MetaStablePoolJoinKind.EXACT_TOKENS_IN_FOR_BPT_OUT, _getUserDataEncodedAmounts(amountsIn), - minBPTwDeviation + minBptWDeviation ); IBalancerVault.JoinPoolRequest memory request = IBalancerVault @@ -471,9 +471,15 @@ contract BalancerMetaPoolStrategy is BaseAuraStrategy { // STEP 1 - Withdraw all Balancer Pool Tokens (BPT) from Aura to this strategy contract _lpWithdrawAll(); // Get the BPTs withdrawn from Aura plus any that were already in this strategy contract - uint256 BPTtoWithdraw = IERC20(platformAddress).balanceOf( + uint256 bptToWithdraw = IERC20(platformAddress).balanceOf( address(this) ); + + // Do not proceed with withdrawal if there is nothing to withdraw + if (bptToWithdraw == 0) { + return; + } + uint256[] memory minAmountsOut = new uint256[](_poolAssets.length); // STEP 2 - Withdraw the Balancer pool assets from the pool @@ -495,7 +501,7 @@ contract BalancerMetaPoolStrategy is BaseAuraStrategy { isRecoveryModeWithdrawal ? RECOVERY_MODE_EXIT_KIND : _exactBptInTokensOutIndex(), - BPTtoWithdraw + bptToWithdraw ); if (isRecoveryModeWithdrawal) { diff --git a/contracts/test/strategies/balancerMetaStablePool.fork-test.js b/contracts/test/strategies/balancerMetaStablePool.fork-test.js index b7451ea6bc..93d13d7298 100644 --- a/contracts/test/strategies/balancerMetaStablePool.fork-test.js +++ b/contracts/test/strategies/balancerMetaStablePool.fork-test.js @@ -1055,6 +1055,15 @@ describe("ForkTest: Balancer MetaStablePool wstETH/WETH Strategy", function () { expect(wethBalanceDiff).to.be.gte(await units("15", weth), 1); expect(stEthBalanceDiff).to.be.gte(await units("15", stETH), 1); }); + + it("Should be able to call withdrawAll twice (second time musn't revert)", async function () { + const { oethVault, balancerWstEthStrategy } = fixture; + + const oethVaultSigner = await impersonateAndFund(oethVault.address); + + await balancerWstEthStrategy.connect(oethVaultSigner).withdrawAll(); + await balancerWstEthStrategy.connect(oethVaultSigner).withdrawAll(); + }); }); describe("Harvest rewards", function () {