Skip to content

Commit

Permalink
Merge branch 'main' into only-submitter-market-price
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyvempati authored Jan 17, 2025
2 parents 71bda25 + 65ed1aa commit 74c9e47
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/protocol/src/vault/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ contract Vault is IVault, ERC20, ERC165, ReentrancyGuardUpgradeable {
? collateralReceived.mulDiv(1e18, totalShares)
: 1e18;

if (sharePrice == 0) {
revert("Share price cannot be 0");
}

// Store the share price for the current epoch
epochSharePrices[currentEpochId] = sharePrice;

Expand Down
80 changes: 80 additions & 0 deletions packages/protocol/test/Vault/Vault.sharePriceZero.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.2 <0.9.0;

import "forge-std/Test.sol";
import "cannon-std/Cannon.sol";
import {IFoil} from "../../src/market/interfaces/IFoil.sol";
import {IMintableToken} from "../../src/market/external/IMintableToken.sol";
import {TestVault} from "../helpers/TestVault.sol";
import {TestUser} from "../helpers/TestUser.sol";
import {ISettlementModule} from "../../src/market/interfaces/ISettlementModule.sol";
import {IVault} from "../../src/vault/interfaces/IVault.sol";
import {IFoilStructs} from "../../src/market/interfaces/IFoilStructs.sol";

contract VaultSharePriceZero is TestVault {
using Cannon for Vm;

IFoil foil;
IVault vault;
IMintableToken collateralAsset;

uint160 initialSqrtPriceX96 = 250541448375047946302209916928; // 10
uint256 epochStartTime;

address lp1;
address lp2;
address lp3;

address trader1;

function setUp() public {
lp1 = TestUser.createUser("LP1", 100_000 ether);
lp2 = TestUser.createUser("LP2", 100_000 ether);
lp3 = TestUser.createUser("LP3", 100_000 ether);

(foil, vault, collateralAsset) = initializeVault(
new address[](0),
100 ether,
10_000
);

vm.prank(lp1);
vault.requestDeposit(10 ether);

vm.prank(lp2);
vault.requestDeposit(15 ether);

epochStartTime = block.timestamp + 60;
vm.prank(vaultOwner);
vault.initializeFirstEpoch(initialSqrtPriceX96);

vault.deposit(0, lp1);
vault.deposit(0, lp2);

vm.mockCall(
address(foil),
abi.encodeWithSelector(ISettlementModule.settlePosition.selector),
abi.encode(10)
);

// since we mock call, send collateral to vault for redemption
collateralAsset.mint(10, address(vault));

vm.prank(lp3);
vault.requestDeposit(10 ether);

vm.warp(epochStartTime + 30 days);
settleEpoch(1, initialSqrtPriceX96, address(vault));
}

function test_epochStaysSame_whenSharePriceIsZero() public view {
assertEq(vault.getCurrentEpoch().epochId, 1);
}

function test_canCancelDeposit_whenSharePriceIsZero() public {
vm.prank(lp3);
vault.withdrawRequestDeposit(10 ether);

assertEq(vault.balanceOf(lp3), 0);
}
}

0 comments on commit 74c9e47

Please sign in to comment.