abstract-math 0.0.0.17__py3-none-any.whl → 0.0.0.19__py3-none-any.whl

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.

Potentially problematic release.


This version of abstract-math might be problematic. Click here for more details.

abstract_math/__init__.py CHANGED
@@ -1,2 +1,3 @@
1
1
  from .safe_math import *
2
2
  from .derive_tokens import *
3
+ from .solar_math import *
@@ -1,18 +1,19 @@
1
1
  from decimal import Decimal, getcontext
2
2
  from .safe_math import *
3
+ from .safe_math import exp_it, divide_it # if you need these
3
4
 
4
- # Set high precision for decimal operations
5
+ # High precision for decimal ops
5
6
  getcontext().prec = 50
6
7
 
7
- # Constants for SOL decimal places and lamports conversion
8
+ # SOL constants
8
9
  SOL_DECIMAL_PLACE = 9
9
- SOL_LAMPORTS = sol_lamports = int(exponential(1, SOL_DECIMAL_PLACE, 1))
10
+ # lamports per SOL = 10**9
11
+ SOL_LAMPORTS = sol_lamports = int(exponential(1, exp=SOL_DECIMAL_PLACE, num=1))
10
12
 
11
13
 
12
14
  def get_proper_args(strings, *args, **kwargs):
13
15
  """
14
- Extracts values from kwargs based on the provided keys (strings). If not present in kwargs,
15
- falls back to positional args in order.
16
+ Extract values for keys in `strings` from kwargs; if missing, pull from positional args in order.
16
17
  """
17
18
  properArgs = []
18
19
  for key in strings:
@@ -23,25 +24,26 @@ def get_proper_args(strings, *args, **kwargs):
23
24
  properArgs.append(kwarg)
24
25
  return properArgs
25
26
 
26
- # Lamports calculations
27
27
 
28
- def get_lamports(integer):
28
+ # ---------- Lamports helpers ----------
29
+ def get_lamports(integer: int):
29
30
  """
30
- Given an integer representing SOL, returns lamports (10^(digits_in_integer+1)).
31
+ Convert an integer N to 10^(len(str(N)) + 1).
32
+ NOTE: this is your original behavior, not 10**9 unless N is 1 with exp=9.
31
33
  """
32
34
  return exp_it(10, len(str(integer)) + 1, 1)
33
35
 
34
36
 
35
- def get_lamport_difference(lamports, virtual_lamports):
37
+ def get_lamport_difference(lamports: int, virtual_lamports: int):
36
38
  """
37
- Compares actual lamports to virtual lamports and returns the corresponding exponent-based difference.
39
+ Compare 'lamports' vs 'virtual_lamports' and return 10^(len(str(int(virtual/actual))))
38
40
  """
39
41
  integer = int(virtual_lamports / lamports)
40
42
  exp = len(str(integer))
41
43
  return int(exponential(1, exp, 1))
42
44
 
43
- # Virtual reserves and ratios
44
45
 
46
+ # ---------- Virtual reserves / ratios ----------
45
47
  def get_vitual_reserves(*args, **kwargs):
46
48
  return get_proper_args(["virtualSolReserves", "virtualTokenReserves"], *args, **kwargs)
47
49
 
@@ -50,8 +52,8 @@ def get_virtual_reserve_ratio(*args, **kwargs):
50
52
  sol_res, token_res = get_vitual_reserves(*args, **kwargs)
51
53
  return divide_it(sol_res, token_res)
52
54
 
53
- # SOL-specific calculations
54
55
 
56
+ # ---------- SOL-specific ----------
55
57
  def get_virtual_sol_reservs(*args, **kwargs):
56
58
  reserves = get_proper_args(["virtualSolReserves"], *args, **kwargs)
57
59
  return reserves[0] if reserves else None
@@ -66,7 +68,6 @@ def get_virtual_sol_lamp_difference(*args, **kwargs):
66
68
  v_lam = get_virtual_sol_lamports(*args, **kwargs)
67
69
  return get_lamport_difference(SOL_LAMPORTS, v_lam)
68
70
 
69
- # SOL amount conversions
70
71
 
71
72
  def get_sol_amount(*args, **kwargs):
72
73
  amounts = get_proper_args(["solAmount"], *args, **kwargs)
@@ -77,8 +78,8 @@ def getSolAmountUi(*args, **kwargs):
77
78
  sol_amt = get_sol_amount(*args, **kwargs)
78
79
  return exponential(sol_amt, SOL_DECIMAL_PLACE)
79
80
 
80
- # Token-specific calculations
81
81
 
82
+ # ---------- Token-specific ----------
82
83
  def get_virtual_token_reserves(*args, **kwargs):
83
84
  reserves = get_proper_args(["virtualTokenReserves"], *args, **kwargs)
84
85
  return reserves[0] if reserves else None
@@ -94,22 +95,10 @@ def get_token_amount(*args, **kwargs):
94
95
  return amounts[0] if amounts else None
95
96
 
96
97
 
97
- def get_token_amount_ui(*args, **kwargs):
98
- token_amt = get_token_amount(*args, **kwargs)
99
- token_decimals = derive_decimals_from_vars(*args, **kwargs)
100
- return exponential(token_amt, token_decimals)
101
-
102
-
103
- def derive_token_decimals_from_token_variables(**variables):
104
- variables["price"] = get_price(**variables)
105
- variables["tokenDecimals"] = derive_decimals_from_vars(**variables)
106
- return variables
107
-
108
-
109
- def get_derived_token_ratio(*args, **kwargs):
110
- derived_amt = derive_token_amount(*args, **kwargs)
111
- token_amt = get_token_amount(*args, **kwargs)
112
- return divide_it(derived_amt, token_amt)
98
+ def get_price(*args, **kwargs):
99
+ reserve_ratio = get_virtual_reserve_ratio(*args, **kwargs)
100
+ sol_diff = get_virtual_sol_lamp_difference(*args, **kwargs)
101
+ return divide_it(reserve_ratio, sol_diff)
113
102
 
114
103
 
115
104
  def derive_token_amount(*args, **kwargs):
@@ -118,22 +107,35 @@ def derive_token_amount(*args, **kwargs):
118
107
  return divide_it(token_res, price)
119
108
 
120
109
 
121
- def get_price(*args, **kwargs):
122
- reserve_ratio = get_virtual_reserve_ratio(*args, **kwargs)
123
- sol_diff = get_virtual_sol_lamp_difference(*args, **kwargs)
124
- return divide_it(reserve_ratio, sol_diff)
110
+ def get_derived_token_ratio(*args, **kwargs):
111
+ derived_amt = derive_token_amount(*args, **kwargs)
112
+ token_amt = get_token_amount(*args, **kwargs)
113
+ return divide_it(derived_amt, token_amt)
125
114
 
126
115
 
127
116
  def derive_decimals_from_vars(*args, **kwargs):
128
117
  ratio = get_derived_token_ratio(*args, **kwargs)
129
118
  decimals = -1
119
+ # Count how many decimal places are needed to make ratio an integer (<= 1e-9 tolerance)
130
120
  while abs(ratio - round(ratio)) > 1e-9:
131
121
  ratio *= 10
132
122
  decimals += 1
133
123
  return decimals
134
124
 
135
125
 
136
- def update_token_variables(variables):
126
+ def get_token_amount_ui(*args, **kwargs):
127
+ token_amt = get_token_amount(*args, **kwargs)
128
+ token_decimals = derive_decimals_from_vars(*args, **kwargs)
129
+ return exponential(token_amt, token_decimals)
130
+
131
+
132
+ def derive_token_decimals_from_token_variables(**variables):
133
+ variables["price"] = get_price(**variables)
134
+ variables["tokenDecimals"] = derive_decimals_from_vars(**variables)
135
+ return variables
136
+
137
+
138
+ def update_token_variables(variables: dict):
137
139
  variables['solAmountUi'] = getSolAmountUi(**variables)
138
140
  variables['solDecimals'] = SOL_DECIMAL_PLACE
139
141
  variables = derive_token_decimals_from_token_variables(**variables)
File without changes