abstract-math 0.0.0.20__py3-none-any.whl → 0.0.0.22__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/derive_tokens.py +3 -1
- abstract_math/safe_math.py +154 -112
- {abstract_math-0.0.0.20.dist-info → abstract_math-0.0.0.22.dist-info}/METADATA +1 -1
- {abstract_math-0.0.0.20.dist-info → abstract_math-0.0.0.22.dist-info}/RECORD +6 -6
- {abstract_math-0.0.0.20.dist-info → abstract_math-0.0.0.22.dist-info}/WHEEL +0 -0
- {abstract_math-0.0.0.20.dist-info → abstract_math-0.0.0.22.dist-info}/top_level.txt +0 -0
abstract_math/derive_tokens.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
from decimal import Decimal, getcontext
|
|
2
2
|
from .safe_math import *
|
|
3
|
+
def exponentials(value,exp=9,num=-1):
|
|
4
|
+
return multiply_it(value,exp_it(10,exp,num))
|
|
3
5
|
|
|
4
6
|
# High precision for decimal ops
|
|
5
7
|
getcontext().prec = 50
|
|
@@ -7,7 +9,7 @@ getcontext().prec = 50
|
|
|
7
9
|
# SOL constants
|
|
8
10
|
SOL_DECIMAL_PLACE = 9
|
|
9
11
|
# lamports per SOL = 10**9
|
|
10
|
-
SOL_LAMPORTS = sol_lamports = int(
|
|
12
|
+
SOL_LAMPORTS = sol_lamports = int(exponentials(1, exp=SOL_DECIMAL_PLACE, num=1))
|
|
11
13
|
|
|
12
14
|
|
|
13
15
|
def get_proper_args(strings, *args, **kwargs):
|
abstract_math/safe_math.py
CHANGED
|
@@ -1,58 +1,44 @@
|
|
|
1
1
|
from abstract_utilities import *
|
|
2
2
|
import math
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
def subtract_it(number_1,number_2):
|
|
13
|
-
if return_0(number_1,number_2)==float(0):
|
|
14
|
-
return float(0)
|
|
15
|
-
return float(number_1)-float(number_2)
|
|
16
|
-
|
|
17
|
-
def get_percentage(owner_balance,address_balance):
|
|
18
|
-
retained_div = divide_it(owner_balance,address_balance)
|
|
19
|
-
retained_mul = multiply_it(retained_div,100)
|
|
20
|
-
return round(retained_mul,2)
|
|
21
|
-
|
|
22
|
-
def return_0(*args):
|
|
23
|
-
"""Return True if *any* arg is “falsy” (None, non-number, or zero‐like)."""
|
|
24
|
-
for arg in args:
|
|
25
|
-
if arg is None or not is_number(arg) or str(arg).strip().lower() in ('0', '', 'null'):
|
|
26
|
-
return True
|
|
27
|
-
return False
|
|
3
|
+
import operator
|
|
4
|
+
from functools import reduce
|
|
5
|
+
|
|
6
|
+
# -------------------------------
|
|
7
|
+
# Core numeric hygiene
|
|
8
|
+
# -------------------------------
|
|
9
|
+
def _is_bad(x):
|
|
10
|
+
return (x is None) or (not is_number(x)) or (str(x).strip().lower() in ('', 'null'))
|
|
28
11
|
|
|
29
12
|
def gather_0(*args):
|
|
30
|
-
"""Convert any
|
|
31
|
-
|
|
32
|
-
for
|
|
33
|
-
if
|
|
34
|
-
|
|
35
|
-
else:
|
|
36
|
-
cleaned.append(float(arg))
|
|
37
|
-
return cleaned
|
|
13
|
+
"""Convert any bad / empty / 'null' to 0.0, keep others as float."""
|
|
14
|
+
out = []
|
|
15
|
+
for a in args:
|
|
16
|
+
out.append(0.0 if _is_bad(a) else float(a))
|
|
17
|
+
return out
|
|
38
18
|
|
|
19
|
+
# -------------------------------
|
|
20
|
+
# Basic ops (single definitions)
|
|
21
|
+
# -------------------------------
|
|
39
22
|
def add_it(*args):
|
|
40
|
-
"""Sum
|
|
23
|
+
"""Sum all args; bad values become 0."""
|
|
24
|
+
return float(sum(gather_0(*args)))
|
|
25
|
+
|
|
26
|
+
def subtract_it(*args):
|
|
27
|
+
"""a0 - a1 - a2 - ... ; bad values become 0."""
|
|
41
28
|
nums = gather_0(*args)
|
|
42
|
-
|
|
29
|
+
if not nums:
|
|
30
|
+
return 0.0
|
|
31
|
+
return float(reduce(operator.sub, nums))
|
|
43
32
|
|
|
44
33
|
def multiply_it(*args):
|
|
45
|
-
"""Multiply
|
|
34
|
+
"""Multiply all args; if any becomes 0 (bad or literal zero), result is 0."""
|
|
46
35
|
nums = gather_0(*args)
|
|
47
36
|
if any(n == 0.0 for n in nums):
|
|
48
37
|
return 0.0
|
|
49
38
|
return float(reduce(operator.mul, nums, 1.0))
|
|
50
39
|
|
|
51
40
|
def divide_it(*args):
|
|
52
|
-
"""
|
|
53
|
-
Divide args[0] by args[1] by args[2]…
|
|
54
|
-
If any input is bad or you hit a zero‐division, return 0.
|
|
55
|
-
"""
|
|
41
|
+
"""a0 / a1 / a2 / ... ; bad => 0, safe against ZeroDivisionError (returns 0)."""
|
|
56
42
|
nums = gather_0(*args)
|
|
57
43
|
try:
|
|
58
44
|
return float(reduce(operator.truediv, nums))
|
|
@@ -60,106 +46,162 @@ def divide_it(*args):
|
|
|
60
46
|
return 0.0
|
|
61
47
|
|
|
62
48
|
def floor_divide_it(*args):
|
|
63
|
-
"""
|
|
64
|
-
Floor‐divide args[0] by args[1] by args[2]…
|
|
65
|
-
If any input is bad or you hit a zero‐division, return 0.
|
|
66
|
-
"""
|
|
49
|
+
"""a0 // a1 // a2 // ... ; bad => 0, safe against ZeroDivisionError (returns 0)."""
|
|
67
50
|
nums = gather_0(*args)
|
|
68
51
|
try:
|
|
69
52
|
return float(reduce(operator.floordiv, nums))
|
|
70
53
|
except ZeroDivisionError:
|
|
71
54
|
return 0.0
|
|
72
55
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
args[0] - args[1] - args[2] - …
|
|
77
|
-
Bad values become 0.
|
|
78
|
-
"""
|
|
79
|
-
nums = gather_0(*args)
|
|
80
|
-
if not nums:
|
|
81
|
-
return 0.0
|
|
82
|
-
return float(reduce(operator.sub, nums))
|
|
83
|
-
|
|
56
|
+
# -------------------------------
|
|
57
|
+
# Exponent helpers (deduplicated)
|
|
58
|
+
# -------------------------------
|
|
84
59
|
def exp_it(base, *factors):
|
|
85
60
|
"""
|
|
86
61
|
Raise `base` to the power of (product of all `factors`).
|
|
87
|
-
|
|
88
|
-
|
|
62
|
+
exp_it(10, 2, 3) -> 10 ** (2*3) == 10**6
|
|
63
|
+
If any input is bad or no factors given, returns 0.0 (consistent with other helpers).
|
|
89
64
|
"""
|
|
90
65
|
nums = gather_0(base, *factors)
|
|
91
66
|
b, *f = nums
|
|
92
|
-
if
|
|
67
|
+
if not f or any(n == 0.0 for n in nums):
|
|
93
68
|
return 0.0
|
|
94
|
-
exponent = reduce(operator.mul, f, 1.0)
|
|
69
|
+
exponent = float(reduce(operator.mul, f, 1.0))
|
|
95
70
|
return float(b) ** exponent
|
|
96
71
|
|
|
72
|
+
def pow10(exp):
|
|
73
|
+
"""10**exp with float output; bad exp -> 0.0^? -> return 1.0 (neutral for scaling)."""
|
|
74
|
+
if _is_bad(exp):
|
|
75
|
+
return 1.0
|
|
76
|
+
return 10.0 ** float(exp)
|
|
77
|
+
|
|
78
|
+
def scale_pow10(value, exp):
|
|
79
|
+
"""Scale value by 10**exp. Neutral if exp is bad."""
|
|
80
|
+
return multiply_it(value, pow10(exp))
|
|
81
|
+
|
|
82
|
+
# Canonical public API for “multiply by 10^…”:
|
|
97
83
|
def exponential(value, *exp_factors):
|
|
98
84
|
"""
|
|
99
|
-
Multiply `value` by 10
|
|
100
|
-
|
|
101
|
-
If
|
|
85
|
+
Multiply `value` by 10 ** (product(exp_factors)).
|
|
86
|
+
exponential(5, 2, 3) -> 5 * 10**(2*3) == 5e6
|
|
87
|
+
If no exp_factors are given, returns value.
|
|
88
|
+
"""
|
|
89
|
+
if not exp_factors:
|
|
90
|
+
return float(value) if not _is_bad(value) else 0.0
|
|
91
|
+
exp_prod = multiply_it(*exp_factors)
|
|
92
|
+
if exp_prod == 0.0 and all(not _is_bad(x) for x in exp_factors):
|
|
93
|
+
# valid inputs produced 0 exponent => scale by 1
|
|
94
|
+
return float(value) if not _is_bad(value) else 0.0
|
|
95
|
+
return scale_pow10(value, exp_prod)
|
|
96
|
+
|
|
97
|
+
# Back-compat alias for legacy callers:
|
|
98
|
+
def exponentials(value, exp=9, num=1):
|
|
99
|
+
"""
|
|
100
|
+
Legacy alias: multiply `value` by 10 ** (exp * num).
|
|
101
|
+
Prefer: exponential(value, exp, num) or scale_pow10(value, exp*num).
|
|
102
102
|
"""
|
|
103
|
-
|
|
104
|
-
return multiply_it(value, power)
|
|
103
|
+
return exponential(value, exp, num)
|
|
105
104
|
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
# -------------------------------
|
|
106
|
+
# Argument picker
|
|
107
|
+
# -------------------------------
|
|
108
|
+
def get_proper_args(strings, *args, **kwargs):
|
|
109
|
+
"""
|
|
110
|
+
Extract values by key order from kwargs first; if missing, consume positional args in order.
|
|
111
|
+
Example: get_proper_args(["a","b"], 1, b=3) -> [1, 3]
|
|
112
|
+
"""
|
|
113
|
+
proper = []
|
|
114
|
+
args = list(args)
|
|
108
115
|
for key in strings:
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
args
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
116
|
+
if key in kwargs:
|
|
117
|
+
proper.append(kwargs[key])
|
|
118
|
+
elif args:
|
|
119
|
+
proper.append(args.pop(0))
|
|
120
|
+
else:
|
|
121
|
+
proper.append(None)
|
|
122
|
+
return proper
|
|
123
|
+
|
|
124
|
+
# -------------------------------
|
|
125
|
+
# Your SOL / token helpers (using the unified expo)
|
|
126
|
+
# -------------------------------
|
|
127
|
+
SOL_DECIMAL_PLACE = 9
|
|
128
|
+
SOL_LAMPORTS = int(pow10(SOL_DECIMAL_PLACE)) # 10**9
|
|
129
|
+
|
|
130
|
+
def get_lamp_difference(*args, **kwargs):
|
|
131
|
+
"""
|
|
132
|
+
Keep behavior: compute a lamport “scale” based on digit length of virtualSolReserves.
|
|
133
|
+
"""
|
|
134
|
+
sol_lamports = SOL_LAMPORTS
|
|
135
|
+
[virtualSolReserves] = get_proper_args(["virtualSolReserves"], *args, **kwargs)
|
|
136
|
+
if _is_bad(virtualSolReserves):
|
|
137
|
+
return 0
|
|
138
|
+
virtual_len = len(str(int(float(virtualSolReserves))))
|
|
139
|
+
virtual_sol_lamports = int(pow10(virtual_len)) # 10**virtual_len
|
|
140
|
+
scale_len = len(str(int(virtual_sol_lamports / sol_lamports))) if sol_lamports else 0
|
|
141
|
+
return int(pow10(scale_len))
|
|
142
|
+
|
|
143
|
+
def get_price(*args, **kwargs):
|
|
144
|
+
"""
|
|
145
|
+
price = virtualSolReserves / virtualTokenReserves / lamp_difference
|
|
146
|
+
"""
|
|
147
|
+
virtualSolReserves, virtualTokenReserves = get_proper_args(
|
|
148
|
+
["virtualSolReserves", "virtualTokenReserves"], *args, **kwargs
|
|
149
|
+
)
|
|
150
|
+
base = divide_it(virtualSolReserves, virtualTokenReserves)
|
|
151
|
+
return divide_it(base, get_lamp_difference(*args, **kwargs))
|
|
152
|
+
|
|
153
|
+
def get_amount_price(*args, **kwargs):
|
|
154
|
+
solAmount, tokenAmount = get_proper_args(["solAmount", "tokenAmount"], *args, **kwargs)
|
|
155
|
+
return divide_it(solAmount, tokenAmount)
|
|
156
|
+
|
|
157
|
+
def getSolAmountUi(*args, **kwargs):
|
|
158
|
+
[solAmount] = get_proper_args(["solAmount"], *args, **kwargs)
|
|
159
|
+
# scale by 10**9
|
|
160
|
+
return exponential(solAmount, SOL_DECIMAL_PLACE)
|
|
161
|
+
|
|
162
|
+
def getTokenAmountUi(*args, **kwargs):
|
|
163
|
+
solAmountUi = getSolAmountUi(*args, **kwargs)
|
|
164
|
+
price = get_price(*args, **kwargs)
|
|
165
|
+
return divide_it(solAmountUi, price)
|
|
166
|
+
|
|
167
|
+
def derive_token_decimals(*args, **kwargs):
|
|
168
|
+
virtualTokenReserves, tokenAmount = get_proper_args(
|
|
169
|
+
["virtualTokenReserves", "tokenAmount"], *args, **kwargs
|
|
170
|
+
)
|
|
171
|
+
price = get_price(*args, **kwargs)
|
|
172
|
+
if not (virtualTokenReserves and tokenAmount and price) or any(
|
|
173
|
+
float(x) <= 0 for x in [virtualTokenReserves, tokenAmount, price]
|
|
174
|
+
):
|
|
138
175
|
raise ValueError("All inputs must be positive.")
|
|
139
|
-
derived_token_amount =
|
|
140
|
-
ratio = derived_token_amount
|
|
176
|
+
derived_token_amount = divide_it(virtualTokenReserves, price)
|
|
177
|
+
ratio = divide_it(derived_token_amount, tokenAmount)
|
|
178
|
+
# count decimal places needed to make ratio an integer (tolerant to fp noise)
|
|
141
179
|
decimals = -1
|
|
142
180
|
while abs(ratio - round(ratio)) > 1e-9:
|
|
143
181
|
ratio *= 10
|
|
144
182
|
decimals += 1
|
|
145
183
|
return decimals
|
|
184
|
+
|
|
146
185
|
def derive_token_decimals_from_token_variables(variables):
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
186
|
+
variables["price"] = get_price(**variables)
|
|
187
|
+
derived_token_amount = divide_it(variables["virtualTokenReserves"], variables["price"])
|
|
188
|
+
ratio = divide_it(derived_token_amount, variables["tokenAmount"])
|
|
189
|
+
decimals = -1
|
|
190
|
+
while abs(ratio - round(ratio)) > 1e-9:
|
|
191
|
+
ratio *= 10
|
|
192
|
+
decimals += 1
|
|
193
|
+
variables["tokenDecimals"] = decimals
|
|
194
|
+
return variables
|
|
195
|
+
|
|
196
|
+
def get_token_amount_ui(*args, **kwargs):
|
|
197
|
+
[tokenAmount] = get_proper_args(["tokenAmount"], *args, **kwargs)
|
|
198
|
+
# scale by 10**(-token_decimals)
|
|
199
|
+
dec = derive_token_decimals(*args, **kwargs)
|
|
200
|
+
return exponential(tokenAmount, -dec, 1)
|
|
201
|
+
|
|
159
202
|
def update_token_variables(variables):
|
|
160
203
|
variables['solAmountUi'] = getSolAmountUi(**variables)
|
|
161
|
-
variables['solDecimals'] =
|
|
204
|
+
variables['solDecimals'] = SOL_DECIMAL_PLACE
|
|
162
205
|
variables = derive_token_decimals_from_token_variables(variables)
|
|
163
|
-
variables['tokenAmountUi'] = exponential(variables['tokenAmount'],
|
|
206
|
+
variables['tokenAmountUi'] = exponential(variables['tokenAmount'], -variables["tokenDecimals"], 1)
|
|
164
207
|
return variables
|
|
165
|
-
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
abstract_math/__init__.py,sha256=wr-kwaB8micSsR1nCHuDXRlsL59DWlqZQFN7M_rfZys,80
|
|
2
|
-
abstract_math/derive_tokens.py,sha256=
|
|
3
|
-
abstract_math/safe_math.py,sha256=
|
|
2
|
+
abstract_math/derive_tokens.py,sha256=mVzwkLv1Jo76R-SGR0hZbO5MPOr7vZpDC44eyxUvfzU,4585
|
|
3
|
+
abstract_math/safe_math.py,sha256=9T1-n6dcMYzJtem8xEIGEKOLR5X4DOIj_h9aotvyzdE,7347
|
|
4
4
|
abstract_math/flask_scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
5
|
abstract_math/flask_scripts/flask_utils.py,sha256=z6gpPshiOeqNANBQkZMZPkegCzj-GK3OpSOi1lN19Sc,28483
|
|
6
6
|
abstract_math/solar_math/__init__.py,sha256=FfPkmWi3cQ3BLvUUUf6k22rGBE8scDL1EoM5l24biVo,39
|
|
@@ -16,7 +16,7 @@ abstract_math/solar_math/src/utils/__init__.py,sha256=Vb2bfx1p9YSmhnbqXBjGVPt1OQ
|
|
|
16
16
|
abstract_math/solar_math/src/utils/escape_velocity.py,sha256=9oixNxWyY4pxa7e1INwbreD8GJEw2BZIgV_IqXiDd9Y,2971
|
|
17
17
|
abstract_math/solar_math/src/utils/geometry_utils.py,sha256=Ij4mASNFp2-CWy425fxMTEuMPuAZHs-E0qvi3BjaNkk,4169
|
|
18
18
|
abstract_math/solar_math/src/utils/velocity_utils.py,sha256=_SyOscVvYsvbm1T1Rh4uqegXdf61mHll5s3UfQcdUnU,1162
|
|
19
|
-
abstract_math-0.0.0.
|
|
20
|
-
abstract_math-0.0.0.
|
|
21
|
-
abstract_math-0.0.0.
|
|
22
|
-
abstract_math-0.0.0.
|
|
19
|
+
abstract_math-0.0.0.22.dist-info/METADATA,sha256=FLjb2zRsmO08zh8nWsJXjGBJ7VI8mqxWLbn7Syfw5WA,3081
|
|
20
|
+
abstract_math-0.0.0.22.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
21
|
+
abstract_math-0.0.0.22.dist-info/top_level.txt,sha256=b7jOgD9c0U-CGH-l7yxhMPukzD40kMEQkQRV_sGyVfE,14
|
|
22
|
+
abstract_math-0.0.0.22.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|