Skip to content

Commit

Permalink
Add start time to params (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahlitvin authored Jan 21, 2025
2 parents b628485 + 518a1d5 commit d4d323f
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 30 deletions.
7 changes: 5 additions & 2 deletions packages/protocol/cannonfile.base-beta.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ lowerBoundMultiplier = "0.5773502691896257" # sqrt(1/3)
upperBoundMultiplier = "1.7320508075688772" # sqrt(3)
duration = "2419200" # 28 day in seconds
totalVaults = "2"
startTime = "0"
# tokens
wstETH = "0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452"
USDC = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
Expand Down Expand Up @@ -133,7 +134,8 @@ target="VaultYin"
from = "<%= settings.deployer %>"
func="initializeFirstEpoch"
args=[
"<%= settings.startingSqrtPriceX96 %>"
"<%= settings.startingSqrtPriceX96 %>",
"<%= settings.startTime %>"
]
depends=["invoke.initializeMarketForVaultYin"]

Expand All @@ -142,6 +144,7 @@ target="VaultYang"
from = "<%= settings.deployer %>"
func="initializeFirstEpoch"
args=[
"<%= settings.startingSqrtPriceX96 %>"
"<%= settings.startingSqrtPriceX96 %>",
"<%= settings.startTime %>"
]
depends=["invoke.initializeMarketForVaultYang"]
6 changes: 4 additions & 2 deletions packages/protocol/cannonfile.sepolia.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ target="VaultYin"
from = "<%= settings.owner %>"
func="initializeFirstEpoch"
args=[
"<%= settings.startingSqrtPriceX96 %>"
"<%= settings.startingSqrtPriceX96 %>",
"0"
]
depends=["invoke.initializeMarketForVaultYin"]

Expand All @@ -143,6 +144,7 @@ target="VaultYang"
from = "<%= settings.owner %>"
func="initializeFirstEpoch"
args=[
"<%= settings.startingSqrtPriceX96 %>"
"<%= settings.startingSqrtPriceX96 %>",
"0"
]
depends=["invoke.initializeMarketForVaultYang"]
6 changes: 4 additions & 2 deletions packages/protocol/cannonfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ target="VaultYin"
from = "<%= settings.owner %>"
func="initializeFirstEpoch"
args=[
"<%= settings.startingSqrtPriceX96 %>"
"<%= settings.startingSqrtPriceX96 %>",
"0"
]
depends=["invoke.initializeMarketForVault1"]

Expand All @@ -154,7 +155,8 @@ target="VaultYang"
from = "<%= settings.owner %>"
func="initializeFirstEpoch"
args=[
"<%= settings.startingSqrtPriceX96 %>"
"<%= settings.startingSqrtPriceX96 %>",
"0"
]
depends=["invoke.initializeMarketForVault2"]

Expand Down
11 changes: 8 additions & 3 deletions packages/protocol/src/vault/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,17 @@ contract Vault is IVault, ERC20, ERC165, ReentrancyGuardUpgradeable {
/// @dev price is set to 1e18
/// @dev any pending deposits prior to the first epoch are used to create the initial liquidity position
function initializeFirstEpoch(
uint160 initialSqrtPriceX96
uint160 initialSqrtPriceX96,
uint256 initialStartTime
) external onlyInitializer {
require(!initialized, "Already Initialized");
require(address(market) != address(0), "Market address not set");

uint256 initialStartTime = block.timestamp + (vaultIndex * duration);
uint256 startTime = initialStartTime == 0
? block.timestamp
: initialStartTime;

uint256 epochStartTime = startTime + (vaultIndex * duration);
// set tick spacing in storage once to reuse
// for future epoch creations
tickSpacing = market.getMarketTickSpacing();
Expand All @@ -136,7 +141,7 @@ contract Vault is IVault, ERC20, ERC165, ReentrancyGuardUpgradeable {
epochSharePrices[0] = startingSharePrice;

_createEpochAndPosition(
initialStartTime,
epochStartTime,
initialSqrtPriceX96,
0 // collateral received, should be 0 for first epoch
);
Expand Down
6 changes: 5 additions & 1 deletion packages/protocol/src/vault/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,12 @@ interface IVault is
/**
* @notice Initialize the first epoch of the vault
* @param initialSqrtPriceX96 The initial sqrt price
* @param initialStartTime The initial start time of the epoch from which
*/
function initializeFirstEpoch(uint160 initialSqrtPriceX96) external;
function initializeFirstEpoch(
uint160 initialSqrtPriceX96,
uint256 initialStartTime
) external;

/**
* @notice Submit the market settlement price for an epoch
Expand Down
10 changes: 5 additions & 5 deletions packages/protocol/test/Vault/Vault.deposit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ contract VaultDepositTest is TestVault {

function test_depositReverts_whenDepositNotCollected() public {
vm.prank(vaultOwner);
vault.initializeFirstEpoch(initialSqrtPriceX96);
vault.initializeFirstEpoch(initialSqrtPriceX96, block.timestamp);

vm.startPrank(lp1);
vm.expectRevert("Previous deposit request is not in the same epoch");
Expand All @@ -176,7 +176,7 @@ contract VaultDepositTest is TestVault {
public
{
vm.prank(vaultOwner);
vault.initializeFirstEpoch(initialSqrtPriceX96);
vault.initializeFirstEpoch(initialSqrtPriceX96, block.timestamp);

vm.startPrank(lp1);
vm.expectRevert("Previous deposit request is not in the same epoch");
Expand All @@ -187,7 +187,7 @@ contract VaultDepositTest is TestVault {

function test_claimDeposit_whenEpochEnded() public {
vm.prank(vaultOwner);
vault.initializeFirstEpoch(initialSqrtPriceX96);
vault.initializeFirstEpoch(initialSqrtPriceX96, block.timestamp);

uint256 claimable = vault.claimableDepositRequest(lp1);
assertEq(
Expand All @@ -213,7 +213,7 @@ contract VaultDepositTest is TestVault {

function test_depositWorks_whenRequestPending() public {
vm.prank(vaultOwner);
vault.initializeFirstEpoch(initialSqrtPriceX96);
vault.initializeFirstEpoch(initialSqrtPriceX96, block.timestamp);

vm.prank(lp1);
vault.deposit(0, lp1);
Expand All @@ -227,7 +227,7 @@ contract VaultDepositTest is TestVault {

function test_requestDepositReverts_whenRedeemPending() public {
vm.prank(vaultOwner);
vault.initializeFirstEpoch(initialSqrtPriceX96);
vault.initializeFirstEpoch(initialSqrtPriceX96, block.timestamp);

vm.prank(lp1);
vault.deposit(0, lp1);
Expand Down
8 changes: 4 additions & 4 deletions packages/protocol/test/Vault/Vault.duration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ contract VaultDurationTest is TestVault {

vm.startPrank(vaultOwner);
vm.warp(block.timestamp);
initialStartTime = block.timestamp;
vault1.initializeFirstEpoch(initialSqrtPriceX96);
vault2.initializeFirstEpoch(initialSqrtPriceX96);
vault3.initializeFirstEpoch(initialSqrtPriceX96);
initialStartTime = block.timestamp + 1 days;
vault1.initializeFirstEpoch(initialSqrtPriceX96, initialStartTime);
vault2.initializeFirstEpoch(initialSqrtPriceX96, initialStartTime);
vault3.initializeFirstEpoch(initialSqrtPriceX96, initialStartTime);
vm.stopPrank();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract VaultFailureTest is TestVault {

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

vault.deposit(0, lp1);
vault.deposit(0, lp2);
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/Vault/Vault.halted.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract VaultFailureTest is TestVault {

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

vault.deposit(0, lp1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract VaultIntegrationTest is TestVault {

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

// collect deposits
vm.prank(lp1);
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/Vault/Vault.integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ contract VaultIntegrationTest is TestVault {

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

// collect deposits
vm.prank(lp1);
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/Vault/Vault.minCollReceived.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract VaultMinimumCollateralReceived is TestVault {

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

vault.deposit(0, lp1);

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/Vault/Vault.priceSubmitter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ contract VaultPriceSubmitterTest is TestVault {

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

vault.deposit(0, lp1);
vault.deposit(0, lp2);
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/Vault/Vault.redeem.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ contract VaultRedeemTest is TestVault {

// Initialize first epoch and mint shares
vm.prank(vaultOwner);
vault.initializeFirstEpoch(initialSqrtPriceX96);
vault.initializeFirstEpoch(initialSqrtPriceX96, block.timestamp);

vm.prank(lp1);
vault.deposit(0, lp1);
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/Vault/Vault.sharePriceZero.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract VaultSharePriceZero is TestVault {

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

vault.deposit(0, lp1);
vault.deposit(0, lp2);
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/test/Vault/Vault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ contract VaultTest is TestVault {

function test_revertsWhenInitializeNonOwner() public {
vm.expectRevert("Only vaultInitializer can call this function");
vault.initializeFirstEpoch(initialSqrtPriceX96);
vault.initializeFirstEpoch(initialSqrtPriceX96, block.timestamp);
}

function test_revertsWhenInitializeFirstEpochAgain() public {
Expand All @@ -69,7 +69,7 @@ contract VaultTest is TestVault {
vm.startPrank(vaultOwner);

vm.expectRevert("Already Initialized");
vault.initializeFirstEpoch(initialSqrtPriceX96);
vault.initializeFirstEpoch(initialSqrtPriceX96, block.timestamp);

vm.stopPrank();
}
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/test/helpers/TestVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ contract TestVault is TestTrade {

// Initialize Epoch (by owner, kicks the ball with the first epoch)
IVault(vm.getAddress("Vault")).initializeFirstEpoch(
_initialSqrtPriceX96
_initialSqrtPriceX96,
0
);

vm.stopPrank();
Expand Down

0 comments on commit d4d323f

Please sign in to comment.