Skip to content
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
8 changes: 3 additions & 5 deletions src/driftpy/math/amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ def calculate_effective_leverage(

effective_gap = max(0, local_base_asset_value - net_base_asset_value)

effective_leverage = (
effective_gap / (max(0, total_fee_minus_distributions) + 1)
+ 1 / QUOTE_PRECISION
)
denom = max(1, total_fee_minus_distributions)
effective_leverage_qp = (effective_gap * QUOTE_PRECISION) // denom

return effective_leverage
return effective_leverage_qp


def calculate_reference_price_offset(
Expand Down
64 changes: 25 additions & 39 deletions src/driftpy/math/fuel.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
from typing import Final
from driftpy.types import SpotMarketAccount, PerpMarketAccount
from driftpy.constants.numeric_constants import QUOTE_PRECISION, FUEL_WINDOW

_SCALE_10: Final[int] = QUOTE_PRECISION // 10
_DENOM: Final[int] = FUEL_WINDOW * _SCALE_10

def _scaled_fuel(value_abs: int, fuel_bonus_numerator: int, boost: int) -> int:
if value_abs == 0 or fuel_bonus_numerator == 0 or boost == 0:
return 0

return (value_abs * fuel_bonus_numerator * boost) // _DENOM


def calculate_insurance_fuel_bonus(
spot_market: SpotMarketAccount, token_stake_amount: int, fuel_bonus_numerator: int
) -> int:
insurance_fund_fuel = (
abs(token_stake_amount) * fuel_bonus_numerator
) * spot_market.fuel_boost_insurance
insurace_fund_fuel_per_day = insurance_fund_fuel // FUEL_WINDOW
insurance_fund_fuel_scaled = insurace_fund_fuel_per_day // (QUOTE_PRECISION // 10)

return insurance_fund_fuel_scaled
value_abs = abs(token_stake_amount)
return _scaled_fuel(value_abs, fuel_bonus_numerator, spot_market.fuel_boost_insurance)


def calculate_spot_fuel_bonus(
spot_market: SpotMarketAccount, signed_token_value: int, fuel_bonus_numerator: int
) -> int:
spot_fuel_scaled: int

# dust
if abs(signed_token_value) <= QUOTE_PRECISION:
spot_fuel_scaled = 0
elif signed_token_value > 0:
deposit_fuel = (
abs(signed_token_value) * fuel_bonus_numerator
) * spot_market.fuel_boost_deposits
deposit_fuel_per_day = deposit_fuel // FUEL_WINDOW
spot_fuel_scaled = deposit_fuel_per_day // (QUOTE_PRECISION // 10)
else:
borrow_fuel = (
abs(signed_token_value) * fuel_bonus_numerator
) * spot_market.fuel_boost_borrows
borrow_fuel_per_day = borrow_fuel // FUEL_WINDOW
spot_fuel_scaled = borrow_fuel_per_day // (QUOTE_PRECISION // 10)

return spot_fuel_scaled
value_abs = abs(signed_token_value)

if value_abs <= QUOTE_PRECISION:
return 0

boost = spot_market.fuel_boost_deposits if signed_token_value > 0 else spot_market.fuel_boost_borrows
return _scaled_fuel(value_abs, fuel_bonus_numerator, boost)


def calculate_perp_fuel_bonus(
perp_market: PerpMarketAccount, base_asset_value: int, fuel_bonus_numerator: int
) -> int:
perp_fuel_scaled: int

# dust
if abs(base_asset_value) <= QUOTE_PRECISION:
perp_fuel_scaled = 0
else:
perp_fuel = (
abs(base_asset_value) * fuel_bonus_numerator
) * perp_market.fuel_boost_position
perp_fuel_per_day = perp_fuel // FUEL_WINDOW
perp_fuel_scaled = perp_fuel_per_day // (QUOTE_PRECISION // 10)

return perp_fuel_scaled
value_abs = abs(base_asset_value)

if value_abs <= QUOTE_PRECISION:
return 0

return _scaled_fuel(value_abs, fuel_bonus_numerator, perp_market.fuel_boost_position)
Loading