Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Calc actual oSqthToMint instead of estimating #979

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# CallbackValidation
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/FlashSwap.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/FlashSwap.sol)

Provides validation for callbacks from Uniswap V3 Pools

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# FlashSwap
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/FlashSwap.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/FlashSwap.sol)

**Inherits:**
IUniswapV3SwapCallback
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# PoolAddress
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/FlashSwap.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/FlashSwap.sol)


## State Variables
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# NettingLib
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/NettingLib.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/NettingLib.sol)


## Functions
Expand Down Expand Up @@ -171,13 +171,13 @@ function getZenBullPrice(
|`_ethUsdcPrice`|`uint256`|ETH/USDC price|


### calcOsqthToMintAndEthIntoCrab
### estimateOsqthToMintAndEthIntoCrab

calculate oSQTH to mint and amount of eth to deposit into Crab v2 based on amount of crab token
estimate oSQTH to mint and amount of eth to deposit into Crab v2 based on amount of crab token


```solidity
function calcOsqthToMintAndEthIntoCrab(address _crab, address _zenBull, uint256 _crabAmount)
function estimateOsqthToMintAndEthIntoCrab(address _crab, address _zenBull, uint256 _crabAmount)
external
view
returns (uint256, uint256);
Expand All @@ -191,6 +191,35 @@ function calcOsqthToMintAndEthIntoCrab(address _crab, address _zenBull, uint256
|`_crabAmount`|`uint256`|amount of crab token|


### calcOsqthToMint

calculate oSQTH to mint based on amount _ethIntoCrab to deposit into Crab v2


```solidity
function calcOsqthToMint(
address _oracle,
address _ethSqueethPool,
address _oSqth,
address _weth,
address _zenBull,
uint256 _ethIntoCrab,
uint32 _auctionTwapPeriod
) external view returns (uint256);
```
**Parameters**

|Name|Type|Description|
|----|----|-----------|
|`_oracle`|`address`|oracle address|
|`_ethSqueethPool`|`address`|wPowerPerp/ETH uni v3 pool address|
|`_oSqth`|`address`|oSQTH address|
|`_weth`|`address`|WETH address|
|`_zenBull`|`address`|ZenBull strategy address|
|`_ethIntoCrab`|`uint256`|amount of ETH to deposit into Crab V2|
|`_auctionTwapPeriod`|`uint32`|auction TWAP|


### calcWethToLendAndUsdcToBorrow

calculate amount of WETH to lend in and USDC to borrow from Euler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ZenBullNetting
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/ZenBullNetting.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/ZenBullNetting.sol)

**Inherits:**
Ownable, EIP712, [FlashSwap](/src/FlashSwap.sol/contract.FlashSwap.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# IController
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/interface/IController.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/interface/IController.sol)


## Functions
Expand Down Expand Up @@ -45,3 +45,10 @@ function wPowerPerpPool() external view returns (address);
function oracle() external view returns (address);
```

### feeRate


```solidity
function feeRate() external view returns (uint256);
```

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ICrabStrategyV2
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/interface/ICrabStrategyV2.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/interface/ICrabStrategyV2.sol)

**Inherits:**
IERC20
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# IEulerSimpleLens
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/interface/IEulerSimpleLens.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/interface/IEulerSimpleLens.sol)


## Functions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# IFlashZen
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/interface/IFlashZen.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/interface/IFlashZen.sol)


## Functions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# IOracle
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/interface/IOracle.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/interface/IOracle.sol)


## Functions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# IWETH
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/interface/IWETH.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/interface/IWETH.sol)

**Inherits:**
IERC20
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# IZenBullStrategy
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/334783aa87db73939fb00d5b133216b0033dfece/src/interface/IZenBullStrategy.sol)
[Git Source](https://github.com/opynfinance/squeeth-monorepo/blob/d9f476e77fa42301e16041672bb68b167162f81f/src/interface/IZenBullStrategy.sol)

**Inherits:**
IERC20
Expand Down
2 changes: 0 additions & 2 deletions packages/zen-bull-netting/script/DeployScript.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ contract DeployScript is Script {
vm.stopBroadcast();

require(zenBullNetting.owner() == ownerAddress);
require(zenBullNetting.MAX_OTC_PRICE_TOLERANCE() == 2e17);
require(zenBullNetting.MIN_AUCTION_TWAP() == 180);
require(zenBullNetting.otcPriceTolerance() == 5e16);
require(zenBullNetting.auctionTwapPeriod() == 420);
require(
Expand Down
48 changes: 41 additions & 7 deletions packages/zen-bull-netting/src/NettingLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IERC20 } from "openzeppelin/interfaces/IERC20.sol";
import { IZenBullStrategy } from "./interface/IZenBullStrategy.sol";
import { IOracle } from "./interface/IOracle.sol";
import { IEulerSimpleLens } from "./interface/IEulerSimpleLens.sol";
import { IController } from "./interface/IController.sol";

library NettingLib {
event TransferWethFromMarketMakers(
Expand Down Expand Up @@ -115,7 +116,7 @@ library NettingLib {
address _trader,
uint256 _remainingOsqthToPull,
uint256 _quantity
) internal returns (uint256) {
) external returns (uint256) {
uint256 oSqthRemaining;
if (_quantity < _remainingOsqthToPull) {
IERC20(_oSqth).transferFrom(_trader, address(this), _quantity);
Expand Down Expand Up @@ -233,22 +234,55 @@ library NettingLib {
}

/**
* @notice calculate oSQTH to mint and amount of eth to deposit into Crab v2 based on amount of crab token
* @notice estimate amount of eth to deposit into Crab v2 based on amount of crab token
* @param _crab crab strategy address
* @param _zenBull ZenBull strategy address
* @param _crabAmount amount of crab token
* @return amount of ETH to deposit into Crab V2
*/
function calcOsqthToMintAndEthIntoCrab(address _crab, address _zenBull, uint256 _crabAmount)
function estimateEthIntoCrab(address _crab, address _zenBull, uint256 _crabAmount)
external
view
returns (uint256, uint256)
returns (uint256)
{
uint256 crabTotalSupply = IERC20(_crab).totalSupply();
(uint256 crabEth, uint256 crabDebt) = IZenBullStrategy(_zenBull).getCrabVaultDetails();
uint256 _oSqthToMint = _crabAmount * crabDebt / crabTotalSupply;
(uint256 crabEth,) = IZenBullStrategy(_zenBull).getCrabVaultDetails();
uint256 ethIntoCrab = _crabAmount * crabEth / crabTotalSupply;

return (_oSqthToMint, ethIntoCrab);
return ethIntoCrab;
}

/**
* @notice calculate oSQTH to mint based on amount _ethIntoCrab to deposit into Crab v2
* @param _oracle oracle address
* @param _ethSqueethPool wPowerPerp/ETH uni v3 pool address
* @param _oSqth oSQTH address
* @param _weth WETH address
* @param _zenBull ZenBull strategy address
* @param _ethIntoCrab amount of ETH to deposit into Crab V2
* @param _auctionTwapPeriod auction TWAP
*/
function calcOsqthToMint(
address _oracle,
address _ethSqueethPool,
address _oSqth,
address _weth,
address _zenBull,
uint256 _ethIntoCrab,
uint32 _auctionTwapPeriod
) external view returns (uint256) {
uint256 squeethEthPrice =
IOracle(_oracle).getTwap(_ethSqueethPool, _oSqth, _weth, _auctionTwapPeriod, false);
uint256 feeRate = IController(IZenBullStrategy(_zenBull).powerTokenController()).feeRate();
uint256 feeAdjustment = div(mul(squeethEthPrice, feeRate), 10000);

(uint256 crabCollateral, uint256 crabDebt) =
IZenBullStrategy(_zenBull).getCrabVaultDetails();
// _ethIntoCrab.wmul(crabDebt).wdiv(crabCollateral.add(crabDebt.wmul(feeAdjustment)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delet

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept it as a reference because the actual implementation is quite long with parentheses, so I'll just keep it

uint256 oSqthToMint =
div(mul(_ethIntoCrab, crabDebt), (crabCollateral + (mul(crabDebt, feeAdjustment))));

return oSqthToMint;
}

/**
Expand Down
Loading