From 72711efe664061f09141485e32ba130a82ee6730 Mon Sep 17 00:00:00 2001 From: Schlagonia Date: Thu, 6 Jun 2024 17:11:17 -0600 Subject: [PATCH 1/5] feat: track deposits --- src/L1YearnEscrow.sol | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/L1YearnEscrow.sol b/src/L1YearnEscrow.sol index 77d5358..d4daed1 100644 --- a/src/L1YearnEscrow.sol +++ b/src/L1YearnEscrow.sol @@ -37,6 +37,7 @@ contract L1YearnEscrow is L1Escrow { /// @custom:storage-location erc7201:yearn.storage.vault struct VaultStorage { IVault vaultAddress; + uint256 deployed; uint256 minimumBuffer; } @@ -55,6 +56,11 @@ contract L1YearnEscrow is L1Escrow { return address($.vaultAddress); } + function deployed() public view returns (uint256) { + VaultStorage storage $ = _getVaultStorage(); + return $.deployed; + } + function minimumBuffer() public view returns (uint256) { VaultStorage storage $ = _getVaultStorage(); return $.minimumBuffer; @@ -138,6 +144,9 @@ contract L1YearnEscrow is L1Escrow { } _vault.deposit(amount, address(this)); + unchecked { + $.deployed += amount; + } } /** @@ -161,7 +170,8 @@ contract L1YearnEscrow is L1Escrow { } // Check if the vault will allow for a full withdraw. - IVault _vault = _getVaultStorage().vaultAddress; + VaultStorage storage $ = _getVaultStorage(); + IVault _vault = $.vaultAddress; uint256 maxWithdraw = _vault.maxWithdraw(address(this)); // If liquidity will not allow for a full withdraw. if (amount > maxWithdraw) { @@ -181,6 +191,7 @@ contract L1YearnEscrow is L1Escrow { shares = _vault.convertToShares(amount - maxWithdraw); } _vault.transfer(destinationAddress, shares); + $.deployed -= (amount - maxWithdraw); if (maxWithdraw == 0) return; amount = maxWithdraw; } @@ -188,6 +199,9 @@ contract L1YearnEscrow is L1Escrow { // Withdraw from vault to receiver. _vault.withdraw(amount, destinationAddress, address(this)); + unchecked { + $.deployed -= amount; + } } // **************************** @@ -238,6 +252,7 @@ contract L1YearnEscrow is L1Escrow { // Withdraw the full balance of the current vault. if (balance != 0) { oldVault.redeem(balance, address(this), address(this)); + $.deployed = 0; } } @@ -255,6 +270,7 @@ contract L1YearnEscrow is L1Escrow { balance - _minimumBuffer, address(this) ); + $.deployed = balance - _minimumBuffer; } } } @@ -290,6 +306,7 @@ contract L1YearnEscrow is L1Escrow { // Deposit the difference. unchecked { $.vaultAddress.deposit(balance - _minimumBuffer, address(this)); + $.deployed += (balance - _minimumBuffer); } } else if (balance < _minimumBuffer) { // Withdraw the difference @@ -302,6 +319,7 @@ contract L1YearnEscrow is L1Escrow { // Withdraw the min between the difference or what is available. diff = diff > available ? available : diff; $.vaultAddress.withdraw(diff, address(this), address(this)); + $.deployed -= diff; } } } From d7a44f283fa08377c19f7f83077dbc6f29b2e97d Mon Sep 17 00:00:00 2001 From: Schlagonia Date: Fri, 14 Jun 2024 13:22:58 -0600 Subject: [PATCH 2/5] fix: deposited --- src/L1YearnEscrow.sol | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/L1YearnEscrow.sol b/src/L1YearnEscrow.sol index d4daed1..e314272 100644 --- a/src/L1YearnEscrow.sol +++ b/src/L1YearnEscrow.sol @@ -37,7 +37,7 @@ contract L1YearnEscrow is L1Escrow { /// @custom:storage-location erc7201:yearn.storage.vault struct VaultStorage { IVault vaultAddress; - uint256 deployed; + uint256 deposited; uint256 minimumBuffer; } @@ -56,9 +56,9 @@ contract L1YearnEscrow is L1Escrow { return address($.vaultAddress); } - function deployed() public view returns (uint256) { + function deposited() public view returns (uint256) { VaultStorage storage $ = _getVaultStorage(); - return $.deployed; + return $.deposited; } function minimumBuffer() public view returns (uint256) { @@ -145,7 +145,7 @@ contract L1YearnEscrow is L1Escrow { _vault.deposit(amount, address(this)); unchecked { - $.deployed += amount; + $.deposited += amount; } } @@ -186,12 +186,14 @@ contract L1YearnEscrow is L1Escrow { // Check again to account for if there was loose underlying if (amount > maxWithdraw) { // Send an equivalent amount of shares for the difference. - uint256 shares; + uint256 diff; unchecked { - shares = _vault.convertToShares(amount - maxWithdraw); + diff = amount - maxWithdraw; + $.deposited -= diff; } + uint256 shares = _vault.convertToShares(diff); _vault.transfer(destinationAddress, shares); - $.deployed -= (amount - maxWithdraw); + if (maxWithdraw == 0) return; amount = maxWithdraw; } @@ -200,7 +202,7 @@ contract L1YearnEscrow is L1Escrow { // Withdraw from vault to receiver. _vault.withdraw(amount, destinationAddress, address(this)); unchecked { - $.deployed -= amount; + $.deposited -= amount; } } @@ -252,7 +254,6 @@ contract L1YearnEscrow is L1Escrow { // Withdraw the full balance of the current vault. if (balance != 0) { oldVault.redeem(balance, address(this), address(this)); - $.deployed = 0; } } @@ -265,13 +266,11 @@ contract L1YearnEscrow is L1Escrow { uint256 balance = originToken.balanceOf(address(this)); uint256 _minimumBuffer = $.minimumBuffer; if (balance > _minimumBuffer) { + uint256 toDeposit; unchecked { - IVault(_vaultAddress).deposit( - balance - _minimumBuffer, - address(this) - ); - $.deployed = balance - _minimumBuffer; + toDeposit = balance - _minimumBuffer; } + IVault(_vaultAddress).deposit(toDeposit, address(this)); } } @@ -306,7 +305,6 @@ contract L1YearnEscrow is L1Escrow { // Deposit the difference. unchecked { $.vaultAddress.deposit(balance - _minimumBuffer, address(this)); - $.deployed += (balance - _minimumBuffer); } } else if (balance < _minimumBuffer) { // Withdraw the difference @@ -319,7 +317,6 @@ contract L1YearnEscrow is L1Escrow { // Withdraw the min between the difference or what is available. diff = diff > available ? available : diff; $.vaultAddress.withdraw(diff, address(this), address(this)); - $.deployed -= diff; } } } From c28fa568e0587a6a98980c8d95dd4e05b5a56a01 Mon Sep 17 00:00:00 2001 From: Schlagonia Date: Mon, 17 Jun 2024 14:10:37 -0600 Subject: [PATCH 3/5] fix: always update --- src/L1YearnEscrow.sol | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/L1YearnEscrow.sol b/src/L1YearnEscrow.sol index e314272..884ce7a 100644 --- a/src/L1YearnEscrow.sol +++ b/src/L1YearnEscrow.sol @@ -124,6 +124,10 @@ contract L1YearnEscrow is L1Escrow { originToken.safeTransferFrom(msg.sender, address(this), amount); VaultStorage storage $ = _getVaultStorage(); + unchecked { + $.deposited += amount; + } + uint256 _minimumBuffer = $.minimumBuffer; // Deposit to the vault if above buffer if (_minimumBuffer != 0) { @@ -144,9 +148,6 @@ contract L1YearnEscrow is L1Escrow { } _vault.deposit(amount, address(this)); - unchecked { - $.deposited += amount; - } } /** @@ -160,6 +161,10 @@ contract L1YearnEscrow is L1Escrow { uint256 amount ) internal virtual override whenNotPaused { IERC20 originToken = originTokenAddress(); + VaultStorage storage $ = _getVaultStorage(); + unchecked { + $.deposited -= amount; + } // Check if there is enough buffer. uint256 underlyingBalance = originToken.balanceOf(address(this)); @@ -170,7 +175,6 @@ contract L1YearnEscrow is L1Escrow { } // Check if the vault will allow for a full withdraw. - VaultStorage storage $ = _getVaultStorage(); IVault _vault = $.vaultAddress; uint256 maxWithdraw = _vault.maxWithdraw(address(this)); // If liquidity will not allow for a full withdraw. @@ -186,12 +190,10 @@ contract L1YearnEscrow is L1Escrow { // Check again to account for if there was loose underlying if (amount > maxWithdraw) { // Send an equivalent amount of shares for the difference. - uint256 diff; + uint256 shares; unchecked { - diff = amount - maxWithdraw; - $.deposited -= diff; + _vault.convertToShares(amount - maxWithdraw); } - uint256 shares = _vault.convertToShares(diff); _vault.transfer(destinationAddress, shares); if (maxWithdraw == 0) return; @@ -201,9 +203,6 @@ contract L1YearnEscrow is L1Escrow { // Withdraw from vault to receiver. _vault.withdraw(amount, destinationAddress, address(this)); - unchecked { - $.deposited -= amount; - } } // **************************** From 8012b65fd1448c6258a9bda20ce3202fe6c743fc Mon Sep 17 00:00:00 2001 From: Schlagonia Date: Mon, 17 Jun 2024 14:42:04 -0600 Subject: [PATCH 4/5] test: deposited --- src/L1YearnEscrow.sol | 2 +- test/L1Escrow.t.sol | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/L1YearnEscrow.sol b/src/L1YearnEscrow.sol index 884ce7a..d72a21b 100644 --- a/src/L1YearnEscrow.sol +++ b/src/L1YearnEscrow.sol @@ -192,7 +192,7 @@ contract L1YearnEscrow is L1Escrow { // Send an equivalent amount of shares for the difference. uint256 shares; unchecked { - _vault.convertToShares(amount - maxWithdraw); + shares = _vault.convertToShares(amount - maxWithdraw); } _vault.transfer(destinationAddress, shares); diff --git a/test/L1Escrow.t.sol b/test/L1Escrow.t.sol index e302e1b..2aad7c4 100644 --- a/test/L1Escrow.t.sol +++ b/test/L1Escrow.t.sol @@ -106,6 +106,7 @@ contract EscrowTest is Setup { mockEscrow.bridgeToken(user, _amount, true); assertEq(vault.totalAssets(), _amount); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), _amount); @@ -136,6 +137,7 @@ contract EscrowTest is Setup { mockEscrow.onMessageReceived(counterPart, l2RollupID, data); assertEq(vault.totalAssets(), _amount - toWithdraw); + assertEq(mockEscrow.deposited(), _amount - toWithdraw); assertEq(asset.balanceOf(user), toWithdraw); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), _amount - toWithdraw); @@ -146,6 +148,7 @@ contract EscrowTest is Setup { mockEscrow.onMessageReceived(counterPart, l2RollupID, data); assertEq(vault.totalAssets(), 0); + assertEq(mockEscrow.deposited(), 0); assertEq(asset.balanceOf(user), _amount); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), 0); @@ -185,6 +188,7 @@ contract EscrowTest is Setup { mockEscrow.bridgeToken(user, _amount, true); assertEq(vault.totalAssets(), 0); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), _amount); assertEq(vault.balanceOf(address(mockEscrow)), 0); @@ -201,6 +205,7 @@ contract EscrowTest is Setup { mockEscrow.bridgeToken(user, _amount, true); assertEq(vault.totalAssets(), _amount); + assertEq(mockEscrow.deposited(), _amount * 2); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), _amount); assertEq(vault.balanceOf(address(mockEscrow)), _amount); @@ -214,6 +219,7 @@ contract EscrowTest is Setup { mockEscrow.onMessageReceived(counterPart, l2RollupID, data); assertEq(vault.totalAssets(), _amount - 10); + assertEq(mockEscrow.deposited(), _amount * 2 - toWithdraw); assertEq(asset.balanceOf(user), toWithdraw); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), _amount - 10); @@ -247,6 +253,7 @@ contract EscrowTest is Setup { mintAndBridge(mockEscrow, user, _amount); assertEq(vault.totalAssets(), toDeposit); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), _amount - toDeposit); assertEq(vault.balanceOf(address(mockEscrow)), toDeposit); @@ -257,6 +264,7 @@ contract EscrowTest is Setup { mockEscrow.onMessageReceived(counterPart, l2RollupID, data); assertEq(vault.totalAssets(), 0); + assertEq(mockEscrow.deposited(), 0); assertEq(asset.balanceOf(user), _amount); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), 0); @@ -276,6 +284,7 @@ contract EscrowTest is Setup { mintAndBridge(mockEscrow, user, _amount); assertEq(vault.totalAssets(), _amount); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), _amount); @@ -288,6 +297,7 @@ contract EscrowTest is Setup { mockEscrow.rebalance(); assertEq(vault.totalAssets(), left); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), _amount - left); assertEq(vault.balanceOf(address(mockEscrow)), left); @@ -298,6 +308,7 @@ contract EscrowTest is Setup { mockEscrow.rebalance(); assertEq(vault.totalAssets(), _amount); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), _amount); @@ -316,6 +327,7 @@ contract EscrowTest is Setup { mintAndBridge(mockEscrow, user, _amount); assertEq(vault.totalAssets(), _amount); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), _amount); @@ -330,6 +342,7 @@ contract EscrowTest is Setup { mockEscrow.updateVault(address(newVault)); assertEq(vault.totalAssets(), 0); + assertEq(mockEscrow.deposited(), _amount); assertEq(vault.balanceOf(address(mockEscrow)), 0); assertEq(asset.allowance(address(mockEscrow), address(vault)), 0); assertEq(asset.balanceOf(address(mockEscrow)), 0); @@ -345,6 +358,7 @@ contract EscrowTest is Setup { assertEq(vault.totalAssets(), 0); assertEq(newVault.totalAssets(), _amount * 2); + assertEq(mockEscrow.deposited(), _amount * 2); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), 0); @@ -356,6 +370,7 @@ contract EscrowTest is Setup { mockEscrow.onMessageReceived(counterPart, l2RollupID, data); assertEq(newVault.totalAssets(), 0); + assertEq(mockEscrow.deposited(), 0); assertEq(asset.balanceOf(user), _amount * 2); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(newVault.balanceOf(address(mockEscrow)), 0); @@ -374,6 +389,7 @@ contract EscrowTest is Setup { mintAndBridge(mockEscrow, user, _amount); assertEq(vault.totalAssets(), _amount); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), _amount); @@ -390,6 +406,7 @@ contract EscrowTest is Setup { mockEscrow.withdraw(czar, _amount); assertEq(vault.totalAssets(), _amount); + assertEq(mockEscrow.deposited(), _amount); assertEq(vault.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(czar), _amount); assertEq(asset.balanceOf(address(mockEscrow)), 0); @@ -404,6 +421,7 @@ contract EscrowTest is Setup { mockEscrow.onMessageReceived(counterPart, l2RollupID, data); assertEq(vault.totalAssets(), 0); + assertEq(mockEscrow.deposited(), 0); assertEq(asset.balanceOf(user), _amount); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), 0); @@ -422,6 +440,7 @@ contract EscrowTest is Setup { mintAndBridge(mockEscrow, user, _amount); assertEq(vault.totalAssets(), _amount); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), 0); assertEq(vault.balanceOf(address(mockEscrow)), _amount); @@ -443,6 +462,7 @@ contract EscrowTest is Setup { // Should have sent the liquid balance and the rest in shares assertEq(vault.totalAssets(), toLock); + assertEq(mockEscrow.deposited(), 0); assertEq(asset.balanceOf(user), _amount - toLock); assertEq(vault.balanceOf(user), toLock); assertEq(asset.balanceOf(address(mockEscrow)), 0); @@ -469,6 +489,7 @@ contract EscrowTest is Setup { mintAndBridge(mockEscrow, user, _amount); assertEq(vault.totalAssets(), _amount - _minimumBuffer); + assertEq(mockEscrow.deposited(), _amount); assertEq(asset.balanceOf(user), 0); assertEq(asset.balanceOf(address(mockEscrow)), _minimumBuffer); assertEq( @@ -496,6 +517,7 @@ contract EscrowTest is Setup { // Should have sent the liquid balance and the rest in shares assertEq(vault.totalAssets(), toLock); + assertEq(mockEscrow.deposited(), 0); assertEq(asset.balanceOf(user), _amount - toLock); assertEq(vault.balanceOf(user), toLock); assertEq(asset.balanceOf(address(mockEscrow)), 0); From f3c48882f6ae5a0c52a0b2eb797fb55ece982bd6 Mon Sep 17 00:00:00 2001 From: Schlagonia Date: Mon, 17 Jun 2024 15:00:08 -0600 Subject: [PATCH 5/5] fix: undo change --- src/L1YearnEscrow.sol | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/L1YearnEscrow.sol b/src/L1YearnEscrow.sol index d72a21b..96985c8 100644 --- a/src/L1YearnEscrow.sol +++ b/src/L1YearnEscrow.sol @@ -265,11 +265,12 @@ contract L1YearnEscrow is L1Escrow { uint256 balance = originToken.balanceOf(address(this)); uint256 _minimumBuffer = $.minimumBuffer; if (balance > _minimumBuffer) { - uint256 toDeposit; unchecked { - toDeposit = balance - _minimumBuffer; + IVault(_vaultAddress).deposit( + balance - _minimumBuffer, + address(this) + ); } - IVault(_vaultAddress).deposit(toDeposit, address(this)); } }