Skip to content

Commit

Permalink
Merge pull request #13 from yearn/track
Browse files Browse the repository at this point in the history
feat: track deposits
  • Loading branch information
Schlagonia authored Jun 24, 2024
2 parents d8881fb + f3c4888 commit b0eda47
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/L1YearnEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ contract L1YearnEscrow is L1Escrow {
/// @custom:storage-location erc7201:yearn.storage.vault
struct VaultStorage {
IVault vaultAddress;
uint256 deposited;
uint256 minimumBuffer;
}

Expand All @@ -55,6 +56,11 @@ contract L1YearnEscrow is L1Escrow {
return address($.vaultAddress);
}

function deposited() public view returns (uint256) {
VaultStorage storage $ = _getVaultStorage();
return $.deposited;
}

function minimumBuffer() public view returns (uint256) {
VaultStorage storage $ = _getVaultStorage();
return $.minimumBuffer;
Expand Down Expand Up @@ -118,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) {
Expand Down Expand Up @@ -151,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));
Expand All @@ -161,7 +175,7 @@ contract L1YearnEscrow is L1Escrow {
}

// Check if the vault will allow for a full withdraw.
IVault _vault = _getVaultStorage().vaultAddress;
IVault _vault = $.vaultAddress;
uint256 maxWithdraw = _vault.maxWithdraw(address(this));
// If liquidity will not allow for a full withdraw.
if (amount > maxWithdraw) {
Expand All @@ -181,6 +195,7 @@ contract L1YearnEscrow is L1Escrow {
shares = _vault.convertToShares(amount - maxWithdraw);
}
_vault.transfer(destinationAddress, shares);

if (maxWithdraw == 0) return;
amount = maxWithdraw;
}
Expand Down
22 changes: 22 additions & 0 deletions test/L1Escrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b0eda47

Please sign in to comment.