Skip to content

Commit

Permalink
improve deposit() ux
Browse files Browse the repository at this point in the history
  • Loading branch information
1kresh committed Aug 1, 2024
1 parent 91cb633 commit 788f2d7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
19 changes: 11 additions & 8 deletions src/contracts/vault/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, Reen
/**
* @inheritdoc IVault
*/
function deposit(address onBehalfOf, uint256 amount) external nonReentrant returns (uint256 shares) {
function deposit(
address onBehalfOf,
uint256 amount
) external nonReentrant returns (uint256 depositedAmount, uint256 mintedShares) {
if (onBehalfOf == address(0)) {
revert InvalidOnBehalfOf();
}
Expand All @@ -89,22 +92,22 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, Reen

uint256 balanceBefore = IERC20(collateral).balanceOf(address(this));
IERC20(collateral).safeTransferFrom(msg.sender, address(this), amount);
amount = IERC20(collateral).balanceOf(address(this)) - balanceBefore;
depositedAmount = IERC20(collateral).balanceOf(address(this)) - balanceBefore;

if (amount == 0) {
if (depositedAmount == 0) {
revert InsufficientDeposit();
}

uint256 activeStake_ = activeStake();
uint256 activeShares_ = activeShares();

shares = ERC4626Math.previewDeposit(amount, activeShares_, activeStake_);
mintedShares = ERC4626Math.previewDeposit(depositedAmount, activeShares_, activeStake_);

_activeStake.push(Time.timestamp(), activeStake_ + amount);
_activeShares.push(Time.timestamp(), activeShares_ + shares);
_activeSharesOf[onBehalfOf].push(Time.timestamp(), activeSharesOf(onBehalfOf) + shares);
_activeStake.push(Time.timestamp(), activeStake_ + depositedAmount);
_activeShares.push(Time.timestamp(), activeShares_ + mintedShares);
_activeSharesOf[onBehalfOf].push(Time.timestamp(), activeSharesOf(onBehalfOf) + mintedShares);

emit Deposit(msg.sender, onBehalfOf, amount, shares);
emit Deposit(msg.sender, onBehalfOf, depositedAmount, mintedShares);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/interfaces/vault/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,13 @@ interface IVault is IVaultStorage {
* @notice Deposit collateral into the vault.
* @param onBehalfOf account the deposit is made on behalf of
* @param amount amount of the collateral to deposit
* @return shares amount of the active shares minted
* @return depositedAmount amount of the collateral deposited
* @return mintedShares amount of the active shares minted
*/
function deposit(address onBehalfOf, uint256 amount) external returns (uint256 shares);
function deposit(
address onBehalfOf,
uint256 amount
) external returns (uint256 depositedAmount, uint256 mintedShares);

/**
* @notice Withdraw collateral from the vault (it will be claimable after the next epoch).
Expand Down
4 changes: 2 additions & 2 deletions test/delegator/FullRestakeDelegator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1725,11 +1725,11 @@ contract FullRestakeDelegatorTest is Test {
vm.stopPrank();
}

function _deposit(address user, uint256 amount) internal returns (uint256 shares) {
function _deposit(address user, uint256 amount) internal returns (uint256 depositedAmount, uint256 mintedShares) {
collateral.transfer(user, amount);
vm.startPrank(user);
collateral.approve(address(vault), amount);
shares = vault.deposit(user, amount);
(depositedAmount, mintedShares) = vault.deposit(user, amount);
vm.stopPrank();
}

Expand Down
4 changes: 2 additions & 2 deletions test/delegator/NetworkRestakeDelegator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1996,11 +1996,11 @@ contract NetworkRestakeDelegatorTest is Test {
vm.stopPrank();
}

function _deposit(address user, uint256 amount) internal returns (uint256 shares) {
function _deposit(address user, uint256 amount) internal returns (uint256 depositedAmount, uint256 mintedShares) {
collateral.transfer(user, amount);
vm.startPrank(user);
collateral.approve(address(vault), amount);
shares = vault.deposit(user, amount);
(depositedAmount, mintedShares) = vault.deposit(user, amount);
vm.stopPrank();
}

Expand Down
4 changes: 2 additions & 2 deletions test/slasher/Slasher.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1416,11 +1416,11 @@ contract SlasherTest is Test {
vm.stopPrank();
}

function _deposit(address user, uint256 amount) internal returns (uint256 shares) {
function _deposit(address user, uint256 amount) internal returns (uint256 depositedAmount, uint256 mintedShares) {
collateral.transfer(user, amount);
vm.startPrank(user);
collateral.approve(address(vault), amount);
shares = vault.deposit(user, amount);
(depositedAmount, mintedShares) = vault.deposit(user, amount);
vm.stopPrank();
}

Expand Down
4 changes: 2 additions & 2 deletions test/slasher/VetoSlasher.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1946,11 +1946,11 @@ contract VetoSlasherTest is Test {
vm.stopPrank();
}

function _deposit(address user, uint256 amount) internal returns (uint256 shares) {
function _deposit(address user, uint256 amount) internal returns (uint256 depositedAmount, uint256 mintedShares) {
collateral.transfer(user, amount);
vm.startPrank(user);
collateral.approve(address(vault), amount);
shares = vault.deposit(user, amount);
(depositedAmount, mintedShares) = vault.deposit(user, amount);
vm.stopPrank();
}

Expand Down
42 changes: 33 additions & 9 deletions test/vault/Vault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,11 @@ contract VaultTest is Test {

uint256 tokensBefore = collateral.balanceOf(address(vault));
uint256 shares1 = amount1 * 10 ** 0;
assertEq(_deposit(alice, amount1), shares1);
{
(uint256 depositedAmount, uint256 mintedShares) = _deposit(alice, amount1);
assertEq(depositedAmount, amount1);
assertEq(mintedShares, shares1);
}
assertEq(collateral.balanceOf(address(vault)) - tokensBefore, amount1);

assertEq(vault.totalStake(), amount1);
Expand All @@ -501,7 +505,11 @@ contract VaultTest is Test {
vm.warp(blockTimestamp);

uint256 shares2 = amount2 * (shares1 + 10 ** 0) / (amount1 + 1);
assertEq(_deposit(alice, amount2), shares2);
{
(uint256 depositedAmount, uint256 mintedShares) = _deposit(alice, amount2);
assertEq(depositedAmount, amount2);
assertEq(mintedShares, shares2);
}

assertEq(vault.totalStake(), amount1 + amount2);
assertEq(vault.activeSharesAt(uint48(blockTimestamp - 1), ""), shares1);
Expand Down Expand Up @@ -674,7 +682,11 @@ contract VaultTest is Test {
feeOnTransferCollateral.transfer(alice, amount1 + 1);
vm.startPrank(alice);
feeOnTransferCollateral.approve(address(vault), amount1);
assertEq(vault.deposit(alice, amount1), shares1);
{
(uint256 depositedAmount, uint256 mintedShares) = vault.deposit(alice, amount1);
assertEq(depositedAmount, amount1 - 1);
assertEq(mintedShares, shares1);
}
vm.stopPrank();
assertEq(feeOnTransferCollateral.balanceOf(address(vault)) - tokensBefore, amount1 - 1);

Expand All @@ -700,7 +712,11 @@ contract VaultTest is Test {
feeOnTransferCollateral.transfer(alice, amount2 + 1);
vm.startPrank(alice);
feeOnTransferCollateral.approve(address(vault), amount2);
assertEq(vault.deposit(alice, amount2), shares2);
{
(uint256 depositedAmount, uint256 mintedShares) = vault.deposit(alice, amount2);
assertEq(depositedAmount, amount2 - 1);
assertEq(mintedShares, shares2);
}
vm.stopPrank();

assertEq(vault.totalStake(), amount1 - 1 + amount2 - 1);
Expand Down Expand Up @@ -831,13 +847,21 @@ contract VaultTest is Test {
vault = _getVault(epochDuration);

uint256 shares1 = amount1 * 10 ** 0;
assertEq(_deposit(alice, amount1), shares1);
{
(uint256 depositedAmount, uint256 mintedShares) = _deposit(alice, amount1);
assertEq(depositedAmount, amount1);
assertEq(mintedShares, shares1);
}

blockTimestamp = blockTimestamp + 1;
vm.warp(blockTimestamp);

uint256 shares2 = amount2 * (shares1 + 10 ** 0) / (amount1 + 1);
assertEq(_deposit(bob, amount2), shares2);
{
(uint256 depositedAmount, uint256 mintedShares) = _deposit(bob, amount2);
assertEq(depositedAmount, amount2);
assertEq(mintedShares, shares2);
}

assertEq(vault.totalStake(), amount1 + amount2);
assertEq(vault.activeSharesAt(uint48(blockTimestamp - 1), ""), shares1);
Expand Down Expand Up @@ -897,7 +921,7 @@ contract VaultTest is Test {
// uint48 epochDuration = 1;
vault = _getVault(1);

uint256 shares = _deposit(alice, amount1);
(, uint256 shares) = _deposit(alice, amount1);

blockTimestamp = blockTimestamp + 1;
vm.warp(blockTimestamp);
Expand Down Expand Up @@ -2013,11 +2037,11 @@ contract VaultTest is Test {
vm.stopPrank();
}

function _deposit(address user, uint256 amount) internal returns (uint256 shares) {
function _deposit(address user, uint256 amount) internal returns (uint256 depositedAmount, uint256 mintedShares) {
collateral.transfer(user, amount);
vm.startPrank(user);
collateral.approve(address(vault), amount);
shares = vault.deposit(user, amount);
(depositedAmount, mintedShares) = vault.deposit(user, amount);
vm.stopPrank();
}

Expand Down

0 comments on commit 788f2d7

Please sign in to comment.