abstract-math 0.0.0.21__py3-none-any.whl → 0.0.0.23__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 +2 -0
- abstract_math/safe_math.py +154 -110
- abstract_math/solar_math/src/imports.py +1 -1
- {abstract_math-0.0.0.21.dist-info → abstract_math-0.0.0.23.dist-info}/METADATA +1 -1
- {abstract_math-0.0.0.21.dist-info → abstract_math-0.0.0.23.dist-info}/RECORD +7 -7
- {abstract_math-0.0.0.21.dist-info → abstract_math-0.0.0.23.dist-info}/WHEEL +0 -0
- {abstract_math-0.0.0.21.dist-info → abstract_math-0.0.0.23.dist-info}/top_level.txt +0 -0
abstract_math/derive_tokens.py
CHANGED
abstract_math/safe_math.py
CHANGED
|
@@ -1,56 +1,44 @@
|
|
|
1
1
|
from abstract_utilities import *
|
|
2
2
|
import math
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if return_0(number_1,number_2)==float(0):
|
|
12
|
-
return float(0)
|
|
13
|
-
return float(number_1)-float(number_2)
|
|
14
|
-
|
|
15
|
-
def get_percentage(owner_balance,address_balance):
|
|
16
|
-
retained_div = divide_it(owner_balance,address_balance)
|
|
17
|
-
retained_mul = multiply_it(retained_div,100)
|
|
18
|
-
return round(retained_mul,2)
|
|
19
|
-
|
|
20
|
-
def return_0(*args):
|
|
21
|
-
"""Return True if *any* arg is “falsy” (None, non-number, or zero‐like)."""
|
|
22
|
-
for arg in args:
|
|
23
|
-
if arg is None or not is_number(arg) or str(arg).strip().lower() in ('0', '', 'null'):
|
|
24
|
-
return True
|
|
25
|
-
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'))
|
|
26
11
|
|
|
27
12
|
def gather_0(*args):
|
|
28
|
-
"""Convert any
|
|
29
|
-
|
|
30
|
-
for
|
|
31
|
-
if
|
|
32
|
-
|
|
33
|
-
else:
|
|
34
|
-
cleaned.append(float(arg))
|
|
35
|
-
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
|
|
36
18
|
|
|
19
|
+
# -------------------------------
|
|
20
|
+
# Basic ops (single definitions)
|
|
21
|
+
# -------------------------------
|
|
37
22
|
def add_it(*args):
|
|
38
|
-
"""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."""
|
|
39
28
|
nums = gather_0(*args)
|
|
40
|
-
|
|
29
|
+
if not nums:
|
|
30
|
+
return 0.0
|
|
31
|
+
return float(reduce(operator.sub, nums))
|
|
41
32
|
|
|
42
33
|
def multiply_it(*args):
|
|
43
|
-
"""Multiply
|
|
34
|
+
"""Multiply all args; if any becomes 0 (bad or literal zero), result is 0."""
|
|
44
35
|
nums = gather_0(*args)
|
|
45
36
|
if any(n == 0.0 for n in nums):
|
|
46
37
|
return 0.0
|
|
47
38
|
return float(reduce(operator.mul, nums, 1.0))
|
|
48
39
|
|
|
49
40
|
def divide_it(*args):
|
|
50
|
-
"""
|
|
51
|
-
Divide args[0] by args[1] by args[2]…
|
|
52
|
-
If any input is bad or you hit a zero‐division, return 0.
|
|
53
|
-
"""
|
|
41
|
+
"""a0 / a1 / a2 / ... ; bad => 0, safe against ZeroDivisionError (returns 0)."""
|
|
54
42
|
nums = gather_0(*args)
|
|
55
43
|
try:
|
|
56
44
|
return float(reduce(operator.truediv, nums))
|
|
@@ -58,106 +46,162 @@ def divide_it(*args):
|
|
|
58
46
|
return 0.0
|
|
59
47
|
|
|
60
48
|
def floor_divide_it(*args):
|
|
61
|
-
"""
|
|
62
|
-
Floor‐divide args[0] by args[1] by args[2]…
|
|
63
|
-
If any input is bad or you hit a zero‐division, return 0.
|
|
64
|
-
"""
|
|
49
|
+
"""a0 // a1 // a2 // ... ; bad => 0, safe against ZeroDivisionError (returns 0)."""
|
|
65
50
|
nums = gather_0(*args)
|
|
66
51
|
try:
|
|
67
52
|
return float(reduce(operator.floordiv, nums))
|
|
68
53
|
except ZeroDivisionError:
|
|
69
54
|
return 0.0
|
|
70
55
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
args[0] - args[1] - args[2] - …
|
|
75
|
-
Bad values become 0.
|
|
76
|
-
"""
|
|
77
|
-
nums = gather_0(*args)
|
|
78
|
-
if not nums:
|
|
79
|
-
return 0.0
|
|
80
|
-
return float(reduce(operator.sub, nums))
|
|
81
|
-
|
|
56
|
+
# -------------------------------
|
|
57
|
+
# Exponent helpers (deduplicated)
|
|
58
|
+
# -------------------------------
|
|
82
59
|
def exp_it(base, *factors):
|
|
83
60
|
"""
|
|
84
61
|
Raise `base` to the power of (product of all `factors`).
|
|
85
|
-
|
|
86
|
-
|
|
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).
|
|
87
64
|
"""
|
|
88
65
|
nums = gather_0(base, *factors)
|
|
89
66
|
b, *f = nums
|
|
90
|
-
if
|
|
67
|
+
if not f or any(n == 0.0 for n in nums):
|
|
91
68
|
return 0.0
|
|
92
|
-
exponent = reduce(operator.mul, f, 1.0)
|
|
69
|
+
exponent = float(reduce(operator.mul, f, 1.0))
|
|
93
70
|
return float(b) ** exponent
|
|
94
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^…”:
|
|
95
83
|
def exponential(value, *exp_factors):
|
|
96
84
|
"""
|
|
97
|
-
Multiply `value` by 10
|
|
98
|
-
|
|
99
|
-
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).
|
|
100
102
|
"""
|
|
101
|
-
|
|
102
|
-
return multiply_it(value, power)
|
|
103
|
+
return exponential(value, exp, num)
|
|
103
104
|
|
|
104
|
-
|
|
105
|
-
|
|
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)
|
|
106
115
|
for key in strings:
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
args
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
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
|
+
):
|
|
136
175
|
raise ValueError("All inputs must be positive.")
|
|
137
|
-
derived_token_amount =
|
|
138
|
-
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)
|
|
139
179
|
decimals = -1
|
|
140
180
|
while abs(ratio - round(ratio)) > 1e-9:
|
|
141
181
|
ratio *= 10
|
|
142
182
|
decimals += 1
|
|
143
183
|
return decimals
|
|
184
|
+
|
|
144
185
|
def derive_token_decimals_from_token_variables(variables):
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
+
|
|
157
202
|
def update_token_variables(variables):
|
|
158
203
|
variables['solAmountUi'] = getSolAmountUi(**variables)
|
|
159
|
-
variables['solDecimals'] =
|
|
204
|
+
variables['solDecimals'] = SOL_DECIMAL_PLACE
|
|
160
205
|
variables = derive_token_decimals_from_token_variables(variables)
|
|
161
|
-
variables['tokenAmountUi'] = exponential(variables['tokenAmount'],
|
|
206
|
+
variables['tokenAmountUi'] = exponential(variables['tokenAmount'], -variables["tokenDecimals"], 1)
|
|
162
207
|
return variables
|
|
163
|
-
|
|
@@ -1,13 +1,13 @@
|
|
|
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
|
|
7
7
|
abstract_math/solar_math/flask_utils.py,sha256=c82LrZ0dKC3Y8LxqOs1zDR6gRy8oXSx9balBD18gNB0,459
|
|
8
8
|
abstract_math/solar_math/main.py,sha256=EtcUmUWj27c7mcnm-hlfeCeYFuPytPevXx5vClVu6Q8,10000
|
|
9
9
|
abstract_math/solar_math/src/__init__.py,sha256=qeal73F-NLu6sO_YSmD_komVpe17f0OFy2IfBR2QU_s,70
|
|
10
|
-
abstract_math/solar_math/src/imports.py,sha256=
|
|
10
|
+
abstract_math/solar_math/src/imports.py,sha256=UZp2SkF8INamFVwLOIca6w9K-YKXsqhiqcf6bkWuLVo,181
|
|
11
11
|
abstract_math/solar_math/src/constants/__init__.py,sha256=HpRa4MWkJZu3Wcx6KchB8__RL4FfkVqRw78TAnjRRwo,691
|
|
12
12
|
abstract_math/solar_math/src/constants/distance_constants.py,sha256=stM3nWxpANQEtoxFcbxpO0ly_UM4JJ2aH0VK42fHw6s,1985
|
|
13
13
|
abstract_math/solar_math/src/constants/planet_constants.py,sha256=tt2nbFtRtYhUU9D918WK91D-S5Cv0HEJ-1j7f9Yb50w,6602
|
|
@@ -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.23.dist-info/METADATA,sha256=sE9PH3BoVK35D72oQm4Le2vgRqxmsYxYMcqKz9VuZOU,3081
|
|
20
|
+
abstract_math-0.0.0.23.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
21
|
+
abstract_math-0.0.0.23.dist-info/top_level.txt,sha256=b7jOgD9c0U-CGH-l7yxhMPukzD40kMEQkQRV_sGyVfE,14
|
|
22
|
+
abstract_math-0.0.0.23.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|