From a3d16848192c48dffd1505a438080b87fed6792f Mon Sep 17 00:00:00 2001 From: 0xNineteen <0x39015319@gmail.com> Date: Tue, 15 Nov 2022 16:09:15 -0500 Subject: [PATCH] update fcn naming --- examples/view.py | 36 ++++++++- src/driftpy/clearing_house_user.py | 2 +- src/driftpy/math/market.py | 2 +- src/driftpy/math/positions.py | 120 ++++++++++++++--------------- 4 files changed, 96 insertions(+), 64 deletions(-) diff --git a/examples/view.py b/examples/view.py index e8d0eb49..4312752c 100644 --- a/examples/view.py +++ b/examples/view.py @@ -31,9 +31,41 @@ async def main( chu = ClearingHouseUser(ch, authority=authority, subaccount_id=subaccount, use_cache=True) await chu.set_cache() + user = await chu.get_user() + print('subaccount name:', bytes(user.name)) + + from driftpy.constants.numeric_constants import QUOTE_PRECISION + spot_collateral = await chu.get_spot_market_asset_value( + None, + include_open_orders=True, + ) + print('spot collat:', spot_collateral/QUOTE_PRECISION) + + pnl = await chu.get_unrealized_pnl(False) + print('pnl:', pnl/QUOTE_PRECISION) + total_collateral = await chu.get_total_collateral() print('total collateral:', total_collateral) - print('leverage:', await chu.get_leverage()) + + perp_liability = await chu.get_total_perp_positon( + None, 0, True + ) + spot_liability = await chu.get_spot_market_liability( + None, None, 0, True + ) + print( + 'perp_liability', perp_liability, + 'spot_liability', spot_liability + ) + + total_liability = await chu.get_margin_requirement(None) + total_asset_value = await chu.get_total_collateral() + print( + 'total_liab', total_liability, + 'total_asset', total_asset_value + ) + print('leverage:', (await chu.get_leverage()) / 10_000) + user = await chu.get_user() print('perp positions:') @@ -41,7 +73,7 @@ async def main( if not is_available(position): print('>', position) - print(time.time() - s) + print('time taken:', time.time() - s) print('done! :)') if __name__ == '__main__': diff --git a/src/driftpy/clearing_house_user.py b/src/driftpy/clearing_house_user.py index e356f8ce..e6f83fae 100644 --- a/src/driftpy/clearing_house_user.py +++ b/src/driftpy/clearing_house_user.py @@ -387,7 +387,7 @@ async def get_unrealized_pnl( market = await self.get_perp_market(position.market_index) oracle_data = await self.get_perp_oracle_data(market) - position_unrealized_pnl = calculate_position_pnl( + position_unrealized_pnl = calculate_position_pnl_with_oracle( market, position, oracle_data, with_funding ) diff --git a/src/driftpy/math/market.py b/src/driftpy/math/market.py index 583a7d6a..5b203732 100644 --- a/src/driftpy/math/market.py +++ b/src/driftpy/math/market.py @@ -9,7 +9,7 @@ import copy from solana.publickey import PublicKey import numpy as np -from driftpy.math.positions import calculate_base_asset_value, calculate_position_pnl +# from driftpy.math.positions import calculate_base_asset_value, calculate_position_pnl from driftpy.constants.numeric_constants import ( AMM_RESERVE_PRECISION, AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO, diff --git a/src/driftpy/math/positions.py b/src/driftpy/math/positions.py index 2fe711ed..d93440be 100644 --- a/src/driftpy/math/positions.py +++ b/src/driftpy/math/positions.py @@ -61,7 +61,7 @@ def calculate_position_funding_pnl(market: PerpMarket, perp_position: PerpPositi return funding_rate_pnl -def calculate_position_pnl( +def calculate_position_pnl_with_oracle( market: PerpMarket, perp_position: PerpPosition, oracle_data, @@ -103,83 +103,83 @@ def is_available(position: PerpPosition): ) -# def calculate_base_asset_value(market: PerpMarket, user_position: PerpPosition) -> int: -# if user_position.base_asset_amount == 0: -# return 0 +def calculate_base_asset_value(market: PerpMarket, user_position: PerpPosition) -> int: + if user_position.base_asset_amount == 0: + return 0 -# direction_to_close = ( -# PositionDirection.SHORT -# if user_position.base_asset_amount > 0 -# else PositionDirection.LONG -# ) + direction_to_close = ( + PositionDirection.SHORT + if user_position.base_asset_amount > 0 + else PositionDirection.LONG + ) -# new_quote_asset_reserve, _ = calculate_amm_reserves_after_swap( -# market.amm, -# AssetType.BASE, -# abs(user_position.base_asset_amount), -# get_swap_direction(AssetType.BASE, direction_to_close), -# ) + new_quote_asset_reserve, _ = calculate_amm_reserves_after_swap( + market.amm, + AssetType.BASE, + abs(user_position.base_asset_amount), + get_swap_direction(AssetType.BASE, direction_to_close), + ) -# result = None -# if direction_to_close == PositionDirection.SHORT: -# result = ( -# (market.amm.quote_asset_reserve - new_quote_asset_reserve) -# * market.amm.peg_multiplier -# ) / AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO -# else: -# # PositionDirection.LONG: -# result = ( -# ( -# (new_quote_asset_reserve - market.amm.quote_asset_reserve) -# * market.amm.peg_multiplier -# ) -# / AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO -# ) + 1.0 + result = None + if direction_to_close == PositionDirection.SHORT: + result = ( + (market.amm.quote_asset_reserve - new_quote_asset_reserve) + * market.amm.peg_multiplier + ) / AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO + else: + # PositionDirection.LONG: + result = ( + ( + (new_quote_asset_reserve - market.amm.quote_asset_reserve) + * market.amm.peg_multiplier + ) + / AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO + ) + 1.0 -# return result + return result -# def calculate_position_pnl( -# market: PerpMarket, market_position: PerpPosition, with_funding=False -# ): -# pnl = 0.0 +def calculate_position_pnl( + market: PerpMarket, market_position: PerpPosition, with_funding=False +): + pnl = 0.0 -# if market_position.base_asset_amount == 0: -# return pnl + if market_position.base_asset_amount == 0: + return pnl -# base_asset_value = calculate_base_asset_value(market, market_position) + base_asset_value = calculate_base_asset_value(market, market_position) -# if market_position.base_asset_amount > 0: -# pnl = base_asset_value - market_position.quote_asset_amount -# else: -# pnl = market_position.quote_asset_amount - base_asset_value + if market_position.base_asset_amount > 0: + pnl = base_asset_value - market_position.quote_asset_amount + else: + pnl = market_position.quote_asset_amount - base_asset_value -# if with_funding: -# funding_rate_pnl = 0.0 -# pnl += funding_rate_pnl / float(PRICE_TO_QUOTE_PRECISION_RATIO) + if with_funding: + funding_rate_pnl = 0.0 + pnl += funding_rate_pnl / float(PRICE_TO_QUOTE_PRECISION_RATIO) -# return pnl + return pnl -# def calculate_position_funding_pnl(market: PerpMarket, market_position: PerpPosition): -# funding_pnl = 0.0 +def calculate_position_funding_pnl(market: PerpMarket, market_position: PerpPosition): + funding_pnl = 0.0 -# if market_position.base_asset_amount == 0: -# return funding_pnl + if market_position.base_asset_amount == 0: + return funding_pnl -# amm_cum_funding_rate = ( -# market.amm.cumulative_funding_rate_long -# if market_position.base_asset_amount > 0 -# else market.amm.cumulative_funding_rate_short -# ) + amm_cum_funding_rate = ( + market.amm.cumulative_funding_rate_long + if market_position.base_asset_amount > 0 + else market.amm.cumulative_funding_rate_short + ) -# funding_pnl = ( -# market_position.last_cumulative_funding_rate - amm_cum_funding_rate -# ) * market_position.base_asset_amount + funding_pnl = ( + market_position.last_cumulative_funding_rate - amm_cum_funding_rate + ) * market_position.base_asset_amount -# funding_pnl /= float(AMM_RESERVE_PRECISION * FUNDING_RATE_BUFFER) + funding_pnl /= float(AMM_RESERVE_PRECISION * FUNDING_RATE_BUFFER) -# return funding_pnl + return funding_pnl def calculate_entry_price(market_position: PerpPosition):