Skip to content

Commit

Permalink
Add settlement price submitter to Vault (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyvempati authored Jan 17, 2025
2 parents 65ed1aa + 74c9e47 commit 32fc06b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/protocol/cannonfile.sepolia.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ highlight=true
highlight=true
artifact="Vault"
from="<%= settings.owner %>"
args=[ "<%= settings.vault1Name %>", "<%= settings.vault1Symbol %>", "<%= FoilYin.address %>", "<%= imports.wstETH.contracts.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "0", "<%= settings.totalVaults %>" ]
args=[ "<%= settings.vault1Name %>", "<%= settings.vault1Symbol %>", "<%= FoilYin.address %>", "<%= imports.wstETH.contracts.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "0", "<%= settings.totalVaults %>", "<%= settings.owner %>" ]
depends = ['router.FoilYin', 'pull.wstETH', 'var.settings']

[invoke.initializeMarketForVaultYin]
Expand All @@ -110,7 +110,7 @@ depends=["deploy.VaultYin"]
highlight=true
artifact="Vault"
from="<%= settings.owner %>"
args=[ "<%= settings.vault2Name %>", "<%= settings.vault2Symbol %>", "<%= FoilYang.address %>", "<%= imports.wstETH.contracts.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "1", "<%= settings.totalVaults %>" ]
args=[ "<%= settings.vault2Name %>", "<%= settings.vault2Symbol %>", "<%= FoilYang.address %>", "<%= imports.wstETH.contracts.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "1", "<%= settings.totalVaults %>", "<%= settings.owner %>" ]
depends = ['router.FoilYang', 'pull.wstETH', 'var.settings']

[invoke.initializeMarketForVaultYang]
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/cannonfile.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ highlight=true
[deploy.Vault]
artifact="Vault"
from="<%= settings.owner2 %>"
args=[ "<%= settings.vaultName %>", "<%= settings.vaultSymbol %>", "<%= Foil.address %>", "<%= CollateralAsset.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "0", "<%= settings.totalVaults %>" ]
args=[ "<%= settings.vaultName %>", "<%= settings.vaultSymbol %>", "<%= Foil.address %>", "<%= CollateralAsset.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "0", "<%= settings.totalVaults %>", "<%= settings.owner2 %>" ]
depends = ['router.Foil', 'clone.CollateralAsset', 'var.settings']
4 changes: 2 additions & 2 deletions packages/protocol/cannonfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ highlight=true
highlight=true
artifact="Vault"
from="<%= settings.owner %>"
args=[ "<%= settings.vault1Name %>", "<%= settings.vault1Symbol %>", "<%= FoilYin.address %>", "<%= CollateralAsset.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "0", "<%= settings.totalVaults %>" ]
args=[ "<%= settings.vault1Name %>", "<%= settings.vault1Symbol %>", "<%= FoilYin.address %>", "<%= CollateralAsset.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "0", "<%= settings.totalVaults %>", "<%= settings.owner %>" ]
depends = ['router.FoilYin', 'clone.CollateralAsset', 'var.settings']

[invoke.initializeMarketForVault1]
Expand All @@ -122,7 +122,7 @@ depends=["deploy.VaultYin", "clone.BondCurrency"]
highlight=true
artifact="Vault"
from="<%= settings.owner %>"
args=[ "<%= settings.vault2Name %>", "<%= settings.vault2Symbol %>", "<%= FoilYang.address %>", "<%= CollateralAsset.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "1", "<%= settings.totalVaults %>" ]
args=[ "<%= settings.vault2Name %>", "<%= settings.vault2Symbol %>", "<%= FoilYang.address %>", "<%= CollateralAsset.Token.address %>", "<%= parseEther(settings.lowerBoundMultiplier) %>", "<%= parseEther(settings.upperBoundMultiplier) %>", "<%= settings.duration %>", "1", "<%= settings.totalVaults %>", "<%= settings.owner %>" ]
depends = ['router.FoilYang', 'clone.CollateralAsset', 'var.settings']

[invoke.initializeMarketForVault2]
Expand Down
11 changes: 10 additions & 1 deletion packages/protocol/src/vault/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ contract Vault is IVault, ERC20, ERC165, ReentrancyGuardUpgradeable {

bool __VAULT_HALTED;

/**
* only address that has permission to submit market settlement prices
*/
address public settlementPriceSubmitter;

constructor(
string memory _name,
string memory _symbol,
Expand All @@ -97,7 +102,8 @@ contract Vault is IVault, ERC20, ERC165, ReentrancyGuardUpgradeable {
uint256 _upperBoundMultiplier,
uint256 _duration,
uint256 _vaultIndex,
uint256 _totalVaults
uint256 _totalVaults,
address _settlementPriceSubmitter
) ERC20(_name, _symbol) {
market = IFoil(_marketAddress);
collateralAsset = IERC20(_collateralAssetAddress);
Expand All @@ -108,6 +114,7 @@ contract Vault is IVault, ERC20, ERC165, ReentrancyGuardUpgradeable {
lowerBoundMultiplier = _lowerBoundMultiplier;
upperBoundMultiplier = _upperBoundMultiplier;
vaultInitializer = msg.sender;
settlementPriceSubmitter = _settlementPriceSubmitter;
}

/// @notice initializes the first epoch
Expand Down Expand Up @@ -142,6 +149,8 @@ contract Vault is IVault, ERC20, ERC165, ReentrancyGuardUpgradeable {
uint256 epochId,
uint160 price
) external returns (bytes32 assertionId) {
require(msg.sender == settlementPriceSubmitter, "Not authorized");

(, , , , IFoilStructs.MarketParams memory marketParams) = market
.getMarket();
IERC20 bondCurrency = IERC20(marketParams.bondCurrency);
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/test/Vault/Vault.duration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ contract VaultDurationTest is TestVault {
1732050807568877200, //sqrt(3)
duration,
vaultIndex,
totalVaults
totalVaults,
vaultOwner
)
);
vm.stopPrank();
Expand Down
90 changes: 90 additions & 0 deletions packages/protocol/test/Vault/Vault.priceSubmitter.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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 {IUMASettlementModule} from "../../src/market/interfaces/IUMASettlementModule.sol";
import {TestVault} from "../helpers/TestVault.sol";
import {TestUser} from "../helpers/TestUser.sol";
import {IVault} from "../../src/vault/interfaces/IVault.sol";
import {IFoilStructs} from "../../src/market/interfaces/IFoilStructs.sol";

contract VaultPriceSubmitterTest is TestVault {
using Cannon for Vm;

IFoil foil;
IVault vault;
IMintableToken collateralAsset;

uint160 initialSqrtPriceX96 = 250541448375047946302209916928; // 10
uint256 epochStartTime;

address lp1;
address lp2;
function setUp() public {
lp1 = TestUser.createUser("LP1", 100_000 ether);
lp2 = TestUser.createUser("LP2", 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(1 ether);

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

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

vm.prank(lp1);
vault.requestRedeem(5 ether);

vm.warp(epochStartTime + 30 days);
}

function test_reverts_when_not_settlement_price_submitter() public {
(, , , , IFoilStructs.MarketParams memory marketParams) = foil
.getMarket();

IMintableToken bondCurrency = IMintableToken(
vm.getAddress("BondCurrency.Token")
);
bondCurrency.mint(marketParams.bondAmount * 2, lp1);

bondCurrency.approve(address(vault), marketParams.bondAmount);
vm.expectRevert("Not authorized");
vm.prank(lp1);
vault.submitMarketSettlementPrice(1, initialSqrtPriceX96);
}

function test_works_when_settlement_price_submitter() public {
(, , , , IFoilStructs.MarketParams memory marketParams) = foil
.getMarket();

IMintableToken bondCurrency = IMintableToken(
vm.getAddress("BondCurrency.Token")
);
bondCurrency.mint(marketParams.bondAmount * 2, vaultOwner);
vm.startPrank(vaultOwner);
bondCurrency.approve(address(vault), marketParams.bondAmount);
vm.expectEmit(true, true, true, true, address(foil));
emit IUMASettlementModule.SettlementSubmitted(
1,
vaultOwner,
initialSqrtPriceX96,
block.timestamp
);
vault.submitMarketSettlementPrice(1, initialSqrtPriceX96);
vm.stopPrank();
}
}

0 comments on commit 32fc06b

Please sign in to comment.