Skip to content
Merged
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
4 changes: 2 additions & 2 deletions contracts/interfaces/external/perp-v2/IAccountBalance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ interface IAccountBalance {
function hasOrder(address trader) external view returns (bool);
function getMarginRequirementForLiquidation(address trader) external view returns (int256);
function getTotalDebtValue(address trader) external view returns (uint256);
function getOwedAndUnrealizedPnl(address trader) external view returns (int256, int256);
function getPnlAndPendingFee(address trader) external view returns (int256,int256,uint256);
function getBase(address trader, address baseToken) external view returns (int256);
function getQuote(address trader, address baseToken) external view returns (int256);
function getNetQuoteBalance(address trader) external view returns (int256);
function getNetQuoteBalanceAndPendingFee(address trader) external view returns (int256, uint256);
function getPositionSize(address trader, address baseToken) external view returns (int256);
function getPositionValue(address trader, address baseToken) external view returns (int256);
function getTotalAbsPositionValue(address trader) external view returns (uint256);
Expand Down
18 changes: 10 additions & 8 deletions contracts/protocol/modules/PerpV2LeverageModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import "hardhat/console.sol";
* represented as a positive equity external position whose value is the net Perp account value denominated in the collateral token
* deposited into the Perp Protocol. This module only allows Perp positions to be collateralized by one asset, USDC, set on deployment of
* this contract (see collateralToken) however it can take positions simultaneuosly in multiple base assets.
*
*
* Upon issuance and redemption positions are not EXACTLY replicated like for other position types since a trade is necessary to enter/exit
* the position on behalf of the issuer/redeemer. Any cost of entering/exiting the position (slippage) is carried by the issuer/redeemer.
* Any pending funding costs or PnL is carried by the current token holders. To be used safely this module MUST issue using the
Expand Down Expand Up @@ -520,7 +520,7 @@ contract PerpV2LeverageModule is ModuleBase, ReentrancyGuard, Ownable, IModuleIs
uint256 _setTokenQuantity,
IERC20 _component,
bool _isEquity
)
)
external
override
onlyModule(_setToken)
Expand Down Expand Up @@ -665,7 +665,8 @@ contract PerpV2LeverageModule is ModuleBase, ReentrancyGuard, Ownable, IModuleIs
* + pending funding payments (10**18)
*/
function getAccountInfo(ISetToken _setToken) public view returns (AccountInfo memory accountInfo) {
(int256 owedRealizedPnl, ) = perpAccountBalance.getOwedAndUnrealizedPnl(address(_setToken));
(int256 owedRealizedPnl,, ) = perpAccountBalance.getPnlAndPendingFee(address(_setToken));
(int256 netQuoteBalance, ) = perpAccountBalance.getNetQuoteBalanceAndPendingFee(address(_setToken));

// NOTE: pendingFundingPayments are represented as in the Perp system as "funding owed"
// e.g a positive number is a debt which gets subtracted from owedRealizedPnl on settlement.
Expand All @@ -674,7 +675,7 @@ contract PerpV2LeverageModule is ModuleBase, ReentrancyGuard, Ownable, IModuleIs
collateralBalance: _getCollateralBalance(_setToken),
owedRealizedPnl: owedRealizedPnl,
pendingFundingPayments: perpExchange.getAllPendingFundingPayment(address(_setToken)).mul(-1),
netQuoteBalance: perpAccountBalance.getNetQuoteBalance(address(_setToken))
netQuoteBalance: netQuoteBalance
});
}

Expand Down Expand Up @@ -989,7 +990,7 @@ contract PerpV2LeverageModule is ModuleBase, ReentrancyGuard, Ownable, IModuleIs
}

/**
* @dev Construct the ActionInfo struct for trading. This method takes POSITION UNIT amounts and passes to
* @dev Construct the ActionInfo struct for trading. This method takes POSITION UNIT amounts and passes to
* _createAndValidateActionInfoNotional to create the struct. If the _baseTokenQuantity is zero then revert.
*
* @param _setToken Instance of the SetToken
Expand Down Expand Up @@ -1115,9 +1116,10 @@ contract PerpV2LeverageModule is ModuleBase, ReentrancyGuard, Ownable, IModuleIs
// account leverage = vAssets / (vAssets - vDebt + collateral)
// vAsset value is postive when long, negative when short
// vQuote balance is negative when long, positive when short
(int256 netQuoteBalance, ) = perpAccountBalance.getNetQuoteBalanceAndPendingFee(address(_setToken));
int256 currentAccountLeverage = totalPositionAbsoluteValue.preciseDiv(
totalPositionNetValue
.add(perpAccountBalance.getNetQuoteBalance(address(_setToken)))
.add(netQuoteBalance)
.add(_getCollateralBalance(_setToken))
);

Expand Down Expand Up @@ -1188,7 +1190,7 @@ contract PerpV2LeverageModule is ModuleBase, ReentrancyGuard, Ownable, IModuleIs
view
returns (int256)
{
(int256 owedRealizedPnl, ) = perpAccountBalance.getOwedAndUnrealizedPnl(address(_setToken));
(int256 owedRealizedPnl,,) = perpAccountBalance.getPnlAndPendingFee(address(_setToken));
int256 pendingFundingPayments = perpExchange.getAllPendingFundingPayment(address(_setToken));

// We subtract funding here since a positive value from Perp means funding is owed and the issuer should not pay
Expand Down Expand Up @@ -1267,7 +1269,7 @@ contract PerpV2LeverageModule is ModuleBase, ReentrancyGuard, Ownable, IModuleIs
}

return (equityAdjustments, debtAdjustments);
}
}

/**
* @dev Converts a UniswapV3 sqrtPriceX96 value to a priceX96 value. This method is borrowed from
Expand Down
117 changes: 44 additions & 73 deletions external/abi/perp/PerpV2AccountBalance.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions external/abi/perp/PerpV2BaseToken.json

Large diffs are not rendered by default.

74 changes: 71 additions & 3 deletions external/abi/perp/PerpV2ClearingHouse.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions external/abi/perp/PerpV2ClearingHouseConfig.json

Large diffs are not rendered by default.

186 changes: 57 additions & 129 deletions external/abi/perp/PerpV2Exchange.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions external/abi/perp/PerpV2InsuranceFund.json

Large diffs are not rendered by default.

Loading