ostium-python-sdk 0.2.101__tar.gz → 0.2.103__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/PKG-INFO +1 -1
  2. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/formulae.py +83 -144
  3. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/sdk.py +14 -14
  4. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/subgraph.py +0 -3
  5. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk.egg-info/PKG-INFO +1 -1
  6. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/setup.py +1 -1
  7. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/MANIFEST.in +0 -0
  8. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/README.md +0 -0
  9. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/__init__.py +0 -0
  10. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/abi/__init__.py +0 -0
  11. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/abi/abi.py +0 -0
  12. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/abi/faucet_abi.py +0 -0
  13. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/balance.py +0 -0
  14. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/config.py +0 -0
  15. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/constants.py +0 -0
  16. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/exceptions.py +0 -0
  17. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/faucet.py +0 -0
  18. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/formulae_wrapper.py +0 -0
  19. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/ostium.py +0 -0
  20. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/price.py +0 -0
  21. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk/utils.py +0 -0
  22. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk.egg-info/SOURCES.txt +0 -0
  23. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk.egg-info/dependency_links.txt +0 -0
  24. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk.egg-info/requires.txt +0 -0
  25. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/ostium_python_sdk.egg-info/top_level.txt +0 -0
  26. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/pyproject.toml +0 -0
  27. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/requirements-dev.txt +0 -0
  28. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/requirements.txt +0 -0
  29. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/setup.cfg +0 -0
  30. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/tests/__init__.py +0 -0
  31. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/tests/test_get_price_impact.py +0 -0
  32. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/tests/test_slippage.py +0 -0
  33. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/tests/test_trade_get_tp_price.py +0 -0
  34. {ostium_python_sdk-0.2.101 → ostium_python_sdk-0.2.103}/tests/test_trade_liquidation_price.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: ostium-python-sdk
3
- Version: 0.2.101
3
+ Version: 0.2.103
4
4
  Summary: A python based SDK developed for interacting with Ostium, a leveraged trading application for trading currencies, commodities, indices, crypto and more.
5
5
  Home-page: https://github.com/0xOstium/ostium-python-sdk
6
6
  Author: ami@ostium.io
@@ -1,7 +1,10 @@
1
- from decimal import Decimal
1
+ from decimal import Decimal, getcontext, ROUND_DOWN
2
2
  from .constants import MAX_PROFIT_P, MAX_STOP_LOSS_P, PRECISION_16, PRECISION_2, PRECISION_6, PRECISION_12, PRECISION_18, LIQ_THRESHOLD_P
3
3
  from typing import Dict
4
+ from .scscript.funding import getPendingAccFundingFees, getTargetFundingRate
4
5
 
6
+ quantization_6 = Decimal('0.000001')
7
+ quantization_18 = Decimal('0.000000000000000001')
5
8
  #
6
9
  # This is a copy-cat of formulae repo originally written in TypeScript
7
10
  #
@@ -343,70 +346,9 @@ def CurrentTotalProfitP(total_profit: str, collateral: str) -> str:
343
346
  raise Exception(
344
347
  f"Unable to compute Current Total Profit Percentage: {error}")
345
348
 
346
- # given desired TP percentage, like 35, 50, 75, 100, 500 and 900 which is max: gives you the TP price
347
-
348
-
349
- def get_target_funding_rate(
350
- normalized_oi_delta: Decimal,
351
- hill_inflection_point: Decimal,
352
- max_fr: Decimal,
353
- hill_pos_scale: Decimal,
354
- hill_neg_scale: Decimal,
355
- ) -> Decimal:
356
- a = Decimal('184')
357
- k = Decimal('16')
358
-
359
- x = a * normalized_oi_delta / PRECISION_2
360
- x2 = x * x * PRECISION_6 # convert to PRECISION_18
361
- hill = x2 * PRECISION_18 / (k * PRECISION_16 + x2)
362
-
363
- if normalized_oi_delta >= 0:
364
- target_fr = hill_pos_scale * hill / PRECISION_2 + hill_inflection_point
365
- else:
366
- target_fr = hill_neg_scale * \
367
- Decimal('-1') * hill / PRECISION_2 + hill_inflection_point
368
-
369
- if target_fr > PRECISION_18:
370
- target_fr = PRECISION_18
371
- elif target_fr < PRECISION_18 * Decimal('-1'):
372
- target_fr = PRECISION_18 * Decimal('-1')
373
-
374
- return target_fr * max_fr / PRECISION_18
375
-
376
-
377
- def exponential_approximation(x: Decimal) -> Decimal:
378
- approx_threshold = Decimal('793231258909201900')
379
-
380
- if abs(x) < approx_threshold:
381
- three_with_precision = PRECISION_18 * 3
382
- numerator = x + three_with_precision
383
- numerator = numerator * numerator / PRECISION_18 + three_with_precision
384
- denominator = x - three_with_precision
385
- denominator = denominator * denominator / PRECISION_18 + three_with_precision
386
-
387
- return numerator * PRECISION_18 / denominator
388
- else:
389
- k = [1648721, 1284025, 1133148, 1064494, 1031743,
390
- 1015748, 1007843, 1003915, 1001955, 1000977]
391
- integer_part = abs(x) // PRECISION_18
392
- decimal_part = abs(x) - (integer_part * PRECISION_18)
393
-
394
- approx = PRECISION_6
395
-
396
- for ki in k:
397
- decimal_part = decimal_part * 2
398
- if decimal_part >= PRECISION_18:
399
- approx = approx * Decimal(str(ki)) / PRECISION_6
400
- decimal_part = decimal_part - PRECISION_18
401
- if decimal_part == 0:
402
- break
403
-
404
- return (PRECISION_18 * PRECISION_18 /
405
- (Decimal('2') ** integer_part *
406
- (approx / Decimal('1000') * Decimal('1e15'))) /
407
- Decimal('1e15') * Decimal('1e15'))
408
-
409
349
 
350
+ # returns (acc_funding_long, acc_funding_short, latest_funding_rate, target_fr)
351
+ # latest_funding_rate and target_fr are in % per year - how much long pay per year
410
352
  def get_funding_rate(
411
353
  acc_per_oi_long: str,
412
354
  acc_per_oi_short: str,
@@ -424,89 +366,86 @@ def get_funding_rate(
424
366
  s_factor_up_scale_p: str,
425
367
  s_factor_down_scale_p: str,
426
368
  verbose: bool = False
427
- ) -> Dict[str, str]:
428
- # Convert string inputs to Decimal
429
- acc_per_oi_long_dec = Decimal(acc_per_oi_long)
430
- acc_per_oi_short_dec = Decimal(acc_per_oi_short)
431
- last_funding_rate_dec = Decimal(last_funding_rate)
432
- max_funding_fee_per_block_dec = Decimal(max_funding_fee_per_block)
433
- last_update_block_dec = Decimal(last_update_block)
434
- latest_block_dec = Decimal(latest_block)
369
+ ) -> tuple[int, int, int, int]:
370
+ """
371
+ Calculate funding rates and return as integers multiplied by PRECISION_18
372
+ Returns: (acc_funding_long, acc_funding_short, latest_funding_rate, target_fr)
373
+ """
374
+ def log(message):
375
+ if verbose:
376
+ print(message)
377
+
378
+ # Set decimal precision
379
+ getcontext().prec = 128
380
+ getcontext().rounding = ROUND_DOWN
381
+
382
+ # Convert all inputs to Decimal
435
383
  oi_long_dec = Decimal(oi_long)
436
384
  oi_short_dec = Decimal(oi_short)
437
385
  oi_cap_dec = Decimal(oi_cap)
438
- spring_factor_dec = Decimal(spring_factor)
439
- s_factor_up_scale_p_dec = Decimal(s_factor_up_scale_p)
440
- s_factor_down_scale_p_dec = Decimal(s_factor_down_scale_p)
441
- hill_inflection_point_dec = Decimal(hill_inflection_point)
442
- hill_pos_scale_dec = Decimal(hill_pos_scale)
443
- hill_neg_scale_dec = Decimal(hill_neg_scale)
444
-
445
- # Calculate open interest max
386
+
387
+ log(f"Input values:")
388
+ log(f"OI Long: {oi_long_dec}")
389
+ log(f"OI Short: {oi_short_dec}")
390
+ log(f"OI Cap: {oi_cap_dec}")
391
+ log(f"Last Funding Rate: {last_funding_rate}")
392
+ log(f"Last Update Block: {last_update_block}")
393
+ log(f"Latest Block: {latest_block}")
394
+
395
+ # Calculate normalized OI delta
446
396
  open_interest_max = max(oi_long_dec, oi_short_dec)
447
- denominator = max(oi_cap_dec, open_interest_max)
448
- oi_delta = (oi_long_dec - oi_short_dec) * PRECISION_6 / denominator
449
-
450
- if verbose:
451
- print(f"open_interest_max: {open_interest_max}")
452
- print(f"denominator: {denominator}")
453
-
454
- print(
455
- f"oi_long_dec: {oi_long_dec} (make sure this is in notional aka usd)")
456
- print(
457
- f"oi_short_dec: {oi_short_dec} (make sure this is in notional aka usd)")
458
- print(f"oi_cap_dec: {oi_cap_dec}")
459
- print(f"oi_delta: {oi_delta}")
460
-
461
- # Get target funding rate
462
- target_fr = get_target_funding_rate(
463
- oi_delta,
464
- hill_inflection_point_dec,
465
- max_funding_fee_per_block_dec,
466
- hill_pos_scale_dec,
467
- hill_neg_scale_dec,
397
+ normalized_oi_delta = ((oi_long_dec - oi_short_dec)
398
+ * PRECISION_6) / max(oi_cap_dec, open_interest_max)
399
+
400
+ log(f"\nCalculated values:")
401
+ log(f"Open Interest Max: {open_interest_max}")
402
+ log(f"Normalized OI Delta: {normalized_oi_delta}")
403
+
404
+ # Get funding values
405
+ acc_funding_long, acc_funding_short, latest_funding_rate, target_fr = getPendingAccFundingFees(
406
+ blockNumber=Decimal(latest_block),
407
+ lastUpdateBlock=Decimal(last_update_block),
408
+ valueLong=Decimal(acc_per_oi_long),
409
+ valueShort=Decimal(acc_per_oi_short),
410
+ openInterestUsdcLong=oi_long_dec,
411
+ openInterestUsdcShort=oi_short_dec,
412
+ OiCap=oi_cap_dec,
413
+ maxFundingFeePerBlock=Decimal(max_funding_fee_per_block),
414
+ lastFundingRate=Decimal(last_funding_rate),
415
+ hillInflectionPoint=Decimal(hill_inflection_point),
416
+ hillPosScale=Decimal(hill_pos_scale),
417
+ hillNegScale=Decimal(hill_neg_scale),
418
+ springFactor=Decimal(spring_factor),
419
+ sFactorUpScale=Decimal(s_factor_up_scale_p),
420
+ sFactorDownScaleP=Decimal(s_factor_down_scale_p),
468
421
  )
469
422
 
470
- if verbose:
471
- print(f"target_fr: {target_fr}")
472
-
473
- # Calculate spring factor
474
- s_factor = Decimal('0')
475
- if last_funding_rate_dec * target_fr >= 0:
476
- if abs(target_fr) > abs(last_funding_rate_dec):
477
- s_factor = spring_factor_dec
478
- else:
479
- s_factor = s_factor_down_scale_p_dec * \
480
- spring_factor_dec / Decimal('10000')
481
- else:
482
- s_factor = s_factor_up_scale_p_dec * \
483
- spring_factor_dec / Decimal('10000')
484
-
485
- # Calculate blocks to charge and exponential
486
- num_blocks_to_charge = latest_block_dec - last_update_block_dec
487
- exp = exponential_approximation(
488
- s_factor * num_blocks_to_charge * Decimal('-1'))
489
-
490
- # Calculate funding rates
491
- acc_funding_rate = (target_fr * num_blocks_to_charge +
492
- (PRECISION_18 - exp) * (last_funding_rate_dec - target_fr) / s_factor)
493
- fr = target_fr + (last_funding_rate_dec - target_fr) * exp / PRECISION_18
494
-
495
- # Update accumulations
496
- if acc_funding_rate > 0:
497
- if oi_long_dec > 0:
498
- acc_per_oi_long_dec += acc_funding_rate
499
- acc_per_oi_short_dec -= (acc_funding_rate * oi_long_dec / oi_short_dec
500
- if oi_short_dec > 0 else Decimal('0'))
501
- else:
502
- if oi_short_dec > 0:
503
- acc_per_oi_short_dec -= acc_funding_rate
504
- acc_per_oi_long_dec += (acc_funding_rate * oi_short_dec / oi_long_dec
505
- if oi_long_dec > 0 else Decimal('0'))
506
-
507
- return {
508
- 'accFundingLong': str(acc_per_oi_long_dec),
509
- 'accFundingShort': str(acc_per_oi_short_dec),
510
- 'latestFundingRate': str(fr),
511
- 'targetFr': str(target_fr)
512
- }
423
+ log(f"\nIntermediate results:")
424
+ log(f"Acc Funding Long (pre-conversion): {acc_funding_long}")
425
+ log(f"Acc Funding Short (pre-conversion): {acc_funding_short}")
426
+ log(f"Latest Funding Rate (pre-conversion): {latest_funding_rate}")
427
+ log(f"Target Funding Rate (pre-conversion): {target_fr}")
428
+
429
+ acc_funding_long_int = (
430
+ acc_funding_long / PRECISION_18).quantize(quantization_18, rounding=ROUND_DOWN)
431
+ acc_funding_short_int = (
432
+ acc_funding_short / PRECISION_18).quantize(quantization_18, rounding=ROUND_DOWN)
433
+ latest_funding_rate = (
434
+ latest_funding_rate / PRECISION_18).quantize(quantization_18, rounding=ROUND_DOWN)
435
+ target_fr = (target_fr / PRECISION_18).quantize(quantization_18,
436
+ rounding=ROUND_DOWN)
437
+
438
+ log(f"\nFinal results (multiplied by 10^18):")
439
+ log(f"Acc Funding Long: {acc_funding_long_int}")
440
+ log(f"Acc Funding Short: {acc_funding_short_int}")
441
+ log(f"Latest Funding Rate: {latest_funding_rate}")
442
+ log(f"Target Funding Rate: {target_fr}")
443
+
444
+ return (
445
+ acc_funding_long_int,
446
+ acc_funding_short_int,
447
+ ((latest_funding_rate * 10 / 3 * 60) * 60 * 24 * 365 *
448
+ 100).quantize(quantization_6, rounding=ROUND_DOWN),
449
+ ((target_fr * 10 / 3 * 60) * 60 * 24 * 365 *
450
+ 100).quantize(quantization_6, rounding=ROUND_DOWN),
451
+ )
@@ -144,30 +144,30 @@ class OstiumSDK:
144
144
  self.log(f"Block number: {block_number}")
145
145
 
146
146
  # Get current price
147
- price, _ = await self.price.get_price(
148
- pair_details['from'],
149
- pair_details['to']
150
- )
151
- self.log(f"Price: {price}")
147
+ last_trade_price = pair_details['lastTradePrice']
148
+ self.log(f"lastTradePrice: {last_trade_price}")
152
149
 
153
- notional_long_oi = int(
154
- (Decimal(pair_details['longOI']) / PRECISION_18) * Decimal(price) * PRECISION_6)
155
- notional_short_oi = int(
156
- (Decimal(pair_details['shortOI']) / PRECISION_18) * Decimal(price) * PRECISION_6)
150
+ long_oi = int(
151
+ (Decimal(pair_details['longOI']) *
152
+ Decimal(last_trade_price) / PRECISION_18 / PRECISION_12)
153
+ )
154
+ short_oi = int(
155
+ (Decimal(pair_details['shortOI']) *
156
+ Decimal(last_trade_price) / PRECISION_18 / PRECISION_12)
157
+ )
157
158
 
158
- self.log(f"notional_long_oi: {notional_long_oi}")
159
- self.log(f"notional_short_oi: {notional_short_oi}")
159
+ self.log(f"notional_long_oi: {long_oi}")
160
+ self.log(f"notional_short_oi: {short_oi}")
160
161
 
161
162
  ret = get_funding_rate(
162
163
  pair_details['curFundingLong'],
163
164
  pair_details['curFundingShort'],
164
165
  pair_details['lastFundingRate'],
165
- # pair_details['lastFundingVelocity'],
166
166
  pair_details['maxFundingFeePerBlock'],
167
167
  pair_details['lastFundingBlock'],
168
168
  block_number,
169
- notional_long_oi, # Needs to be in notional aka usd
170
- notional_short_oi, # Needs to be in notional aka usd
169
+ long_oi, # Needs to be in USD
170
+ short_oi, # Needs to be in USD
171
171
  pair_details['maxOI'],
172
172
  pair_details['hillInflectionPoint'],
173
173
  pair_details['hillPosScale'],
@@ -50,8 +50,6 @@ class SubgraphClient:
50
50
  maxOI
51
51
  makerFeeP
52
52
  takerFeeP
53
- usageFeeP
54
- utilizationThresholdP
55
53
  makerMaxLeverage
56
54
  curFundingLong
57
55
  curFundingShort
@@ -70,7 +68,6 @@ class SubgraphClient:
70
68
  hillInflectionPoint
71
69
  hillPosScale
72
70
  hillNegScale
73
- lastOiDelta
74
71
  springFactor
75
72
  sFactorUpScaleP
76
73
  sFactorDownScaleP
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: ostium-python-sdk
3
- Version: 0.2.101
3
+ Version: 0.2.103
4
4
  Summary: A python based SDK developed for interacting with Ostium, a leveraged trading application for trading currencies, commodities, indices, crypto and more.
5
5
  Home-page: https://github.com/0xOstium/ostium-python-sdk
6
6
  Author: ami@ostium.io
@@ -19,7 +19,7 @@ if changelog_path.exists():
19
19
 
20
20
  setup(
21
21
  name="ostium-python-sdk",
22
- version="0.2.101",
22
+ version="0.2.103",
23
23
  packages=find_packages(),
24
24
  install_requires=read_requirements('requirements.txt'),
25
25
  extras_require={