@@ -33,7 +33,7 @@ import { PreciseUnitMath } from "@setprotocol/set-protocol-v2/contracts/lib/Prec
33
33
import { BaseExtension } from "../lib/BaseExtension.sol " ;
34
34
import { IAccountBalance } from "../interfaces/IAccountBalance.sol " ;
35
35
import { IBaseManager } from "../interfaces/IBaseManager.sol " ;
36
- import { IChainlinkAggregatorV3 } from "../interfaces/IChainlinkAggregatorV3 .sol " ;
36
+ import { IPriceFeed } from "../interfaces/IPriceFeed .sol " ;
37
37
import { IVault } from "../interfaces/IVault.sol " ;
38
38
39
39
import { StringArrayUtils } from "../lib/StringArrayUtils.sol " ;
@@ -70,12 +70,12 @@ contract PerpV2LeverageStrategyExtension is BaseExtension {
70
70
71
71
struct ActionInfo {
72
72
int256 baseBalance; // Balance of virtual base asset from Perp in precise units (10e18). E.g. vWBTC = 10e18
73
- int256 quoteBalance; // Balance of virtual quote asset from Perp in precise units (10e18). E.g. vUSDC = 10e18
73
+ int256 quoteBalance; // Balance of virtual quote asset from Perp in precise units (10e18). E.g. vUSD = 10e18
74
74
IPerpV2LeverageModule.AccountInfo accountInfo; // Info on perpetual account including, collateral balance, owedRealizedPnl and pendingFunding
75
75
int256 basePositionValue; // Valuation in USD adjusted for decimals in precise units (10e18)
76
76
int256 quoteValue; // Valuation in USD adjusted for decimals in precise units (10e18)
77
- int256 basePrice; // Price of base asset in precise units (10e18) from Chainlink
78
- int256 quotePrice; // Price of quote asset in precise units (10e18) from Chainlink
77
+ int256 basePrice; // Price of base asset in precise units (10e18) from PerpV2 Oracle
78
+ int256 quotePrice; // Price of quote asset in precise units (10e18) from PerpV2 Oracle
79
79
uint256 setTotalSupply; // Total supply of SetToken
80
80
}
81
81
@@ -90,8 +90,13 @@ contract PerpV2LeverageStrategyExtension is BaseExtension {
90
90
ISetToken setToken; // Instance of leverage token
91
91
IPerpV2LeverageModule perpV2LeverageModule; // Instance of Perp V2 leverage module
92
92
IAccountBalance perpV2AccountBalance; // Instance of Perp V2 AccountBalance contract used to fetch position balances
93
- IChainlinkAggregatorV3 basePriceOracle; // Chainlink oracle feed that returns prices in 8 decimals for base asset
94
- IChainlinkAggregatorV3 quotePriceOracle; // Chainlink oracle feed that returns prices in 8 decimals for quote asset
93
+ IPriceFeed baseUSDPriceOracle; // PerpV2 oracle that returns TWAP price for base asset in USD. IPriceFeed is a PerpV2 specific interface
94
+ // to interact with differnt oracle providers, e.g. Band Protocol and Chainlink, for different assets
95
+ // listed on PerpV2
96
+ uint256 twapInterval; // TWAP interval to be used to fetch base asset price in seconds
97
+ // PerpV2 uses a 15 min TWAP interval, i.e. twapInterval = 900
98
+ uint256 basePriceDecimalAdjustment; // Decimal adjustment for the price returned by the PerpV2 oracle for the base asset.
99
+ // Equal to vBaseAsset.decimals() - baseUSDPriceOracle.decimals()
95
100
address virtualBaseAddress; // Address of virtual base asset (e.g. vETH, vWBTC etc)
96
101
address virtualQuoteAddress; // Address of virtual USDC quote asset. The Perp V2 system uses USDC for all markets
97
102
}
@@ -747,23 +752,25 @@ contract PerpV2LeverageStrategyExtension is BaseExtension {
747
752
function _createActionInfo () internal view returns (ActionInfo memory ) {
748
753
ActionInfo memory rebalanceInfo;
749
754
750
- // Calculate prices from chainlink. Chainlink returns prices with 8 decimal places, so we need to adjust by 10 ** 10 .
751
- // In Perp v2, virtual tokens all have 18 decimals, therefore we do not need to make further adjustments to determine
752
- // base and quote valuation
753
- int256 rawBasePrice = strategy.basePriceOracle. latestAnswer ( );
754
- int256 rawQuotePrice = strategy.quotePriceOracle. latestAnswer ();
755
- rebalanceInfo.basePrice = rawBasePrice. mul ( 10 ** 10 );
756
- rebalanceInfo.quotePrice = rawQuotePrice. mul ( 10 ** 10 );
755
+ // Fetch base token prices from PerpV2 oracles and adjust them to 18 decimal places .
756
+ int256 rawBasePrice = strategy.baseUSDPriceOracle. getPrice (strategy.twapInterval). toInt256 ();
757
+ uint256 decimals = strategy.baseUSDPriceOracle. decimals ();
758
+ rebalanceInfo.basePrice = rawBasePrice. mul (( 10 ** strategy.basePriceDecimalAdjustment). toInt256 () );
759
+
760
+ // vUSD price is fixed to 1$
761
+ rebalanceInfo.quotePrice = PreciseUnitMath. preciseUnit (). toInt256 ( );
757
762
758
763
// Note: getTakerPositionSize returns zero if base balance is less than 10 wei
759
764
rebalanceInfo.baseBalance = strategy.perpV2AccountBalance.getTakerPositionSize (address (strategy.setToken), strategy.virtualBaseAddress);
760
765
761
766
// Note: Fetching quote balance associated with a single position and not the net quote balance
762
767
rebalanceInfo.quoteBalance = strategy.perpV2AccountBalance.getTakerOpenNotional (address (strategy.setToken), strategy.virtualBaseAddress);
768
+
763
769
rebalanceInfo.accountInfo = strategy.perpV2LeverageModule.getAccountInfo (strategy.setToken);
764
770
771
+ // In Perp v2, all virtual tokens have 18 decimals, therefore we do not need to make further adjustments to determine base valuation.
765
772
rebalanceInfo.basePositionValue = rebalanceInfo.basePrice.preciseMul (rebalanceInfo.baseBalance);
766
- rebalanceInfo.quoteValue = rebalanceInfo.quotePrice. preciseMul (rebalanceInfo. quoteBalance) ;
773
+ rebalanceInfo.quoteValue = rebalanceInfo.quoteBalance;
767
774
768
775
rebalanceInfo.setTotalSupply = strategy.setToken.totalSupply ();
769
776
0 commit comments