diff --git a/contracts/contracts/vault/OETHVaultCore.sol b/contracts/contracts/vault/OETHVaultCore.sol index 001a7251a0..839606f04a 100644 --- a/contracts/contracts/vault/OETHVaultCore.sol +++ b/contracts/contracts/vault/OETHVaultCore.sol @@ -398,40 +398,20 @@ contract OETHVaultCore is VaultCore { uint256 wethAvailableInVault = _wethAvailable(); // Nothing in vault to allocate if (wethAvailableInVault == 0) return; - uint256 strategiesValue = _totalValueInStrategies(); - // We have a method that does the same as this, gas optimisation - uint256 calculatedTotalValue = wethAvailableInVault + strategiesValue; - - // We want to maintain a buffer on the Vault so calculate a percentage - // modifier to multiply each amount being allocated by to enforce the - // vault buffer - uint256 vaultBufferModifier; - if (strategiesValue == 0) { - // Nothing in Strategies, allocate 100% minus the vault buffer to - // strategies - vaultBufferModifier = uint256(1e18) - vaultBuffer; - } else { - vaultBufferModifier = - (vaultBuffer * calculatedTotalValue) / - wethAvailableInVault; - if (1e18 > vaultBufferModifier) { - // E.g. 1e18 - (1e17 * 10e18)/5e18 = 8e17 - // (5e18 * 8e17) / 1e18 = 4e18 allocated from Vault - vaultBufferModifier = uint256(1e18) - vaultBufferModifier; - } else { - // We need to let the buffer fill - return; - } - } - if (vaultBufferModifier == 0) return; - uint256 allocateAmount = wethAvailableInVault.mulTruncate( - vaultBufferModifier - ); + // Calculate the target buffer for the vault using the total supply + uint256 totalSupply = oUSD.totalSupply(); + uint256 targetBuffer = totalSupply.mulTruncate(vaultBuffer); + + // If available WETH in the Vault is below the target buffer then there's nothing to allocate + if (wethAvailableInVault <= targetBuffer) return; + + // The amount of assets to allocate to the default strategy + uint256 allocateAmount = wethAvailableInVault - targetBuffer; address depositStrategyAddr = assetDefaultStrategies[weth]; - if (depositStrategyAddr != address(0) && allocateAmount > 0) { + if (depositStrategyAddr != address(0)) { IStrategy strategy = IStrategy(depositStrategyAddr); // Transfer asset to Strategy and call deposit method to // mint or take required action diff --git a/contracts/test/vault/oeth-vault.js b/contracts/test/vault/oeth-vault.js index f421f68337..bf7c992d30 100644 --- a/contracts/test/vault/oeth-vault.js +++ b/contracts/test/vault/oeth-vault.js @@ -1306,6 +1306,7 @@ describe("OETH Vault", function () { await oethVault.connect(governor).setVaultBuffer(oethUnits("0.01")); // Have 4 + 12 + 16 = 32 WETH outstanding requests + // So a total supply of 100 - 32 = 68 OETH await oethVault.connect(daniel).requestWithdrawal(oethUnits("4")); await oethVault.connect(josh).requestWithdrawal(oethUnits("12")); await oethVault.connect(matt).requestWithdrawal(oethUnits("16")); @@ -1498,7 +1499,7 @@ describe("OETH Vault", function () { await expect(tx).to.not.emit(oethVault, "AssetAllocated"); }); }); - describe("when mint more than covers outstanding requests and vault buffer (17 + 1 + 3 WETH)", () => { + describe("when mint more than covers outstanding requests and vault buffer (17 + 1 + 3 = 21 OETH)", () => { beforeEach(async () => { const { oethVault, daniel, weth } = fixture; await oethVault @@ -1545,15 +1546,20 @@ describe("OETH Vault", function () { const tx = await oethVault.connect(domen).allocate(); - await expect(tx).to.emit(oethVault, "AssetAllocated"); + // total supply is 68 starting + 21 minted = 89 OETH + // Vault buffer is 1% of 89 = 0.89 WETH + // WETH transfer amount = 4 WETH available in vault - 0.89 WETH buffer = 3.11 WETH + await expect(tx) + .to.emit(oethVault, "AssetAllocated") + .withArgs(weth.address, mockStrategy.address, oethUnits("3.11")); - expect( - await weth.balanceOf(oethVault.address) - ).to.approxEqualTolerance(vaultBalance.sub(oethUnits("3")), 5); + expect(await weth.balanceOf(oethVault.address)).to.eq( + vaultBalance.sub(oethUnits("3.11")) + ); - expect( - await weth.balanceOf(mockStrategy.address) - ).to.approxEqualTolerance(stratBalance.add(oethUnits("3")), 5); + expect(await weth.balanceOf(mockStrategy.address)).to.eq( + stratBalance.add(oethUnits("3.11")) + ); }); }); });