stability-nexus 1.0.0__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.
- stability_nexus/__init__.py +7 -0
- stability_nexus/gluon/__init__.py +7 -0
- stability_nexus/gluon/reactors/__init__.py +6 -0
- stability_nexus/gluon/reactors/gluon_reactor.py +75 -0
- stability_nexus/gluon/reactors/gluon_z_reactor.py +138 -0
- stability_nexus/gluon/reactors/types.py +60 -0
- stability_nexus/types.py +3 -0
- stability_nexus-1.0.0.dist-info/METADATA +99 -0
- stability_nexus-1.0.0.dist-info/RECORD +10 -0
- stability_nexus-1.0.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from typing import Generic
|
|
3
|
+
|
|
4
|
+
from .types import *
|
|
5
|
+
|
|
6
|
+
class GluonReactor(ABC, Generic[R]):
|
|
7
|
+
|
|
8
|
+
def __init__(self, state: GluonReactorState):
|
|
9
|
+
assert state.reserves > 0
|
|
10
|
+
assert state.neutron_circulating_supply > 0
|
|
11
|
+
assert state.proton_circulating_supply > 0
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def parameters(self) -> GluonReactorParameters:
|
|
16
|
+
...
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
@abstractmethod
|
|
20
|
+
def state(self) -> GluonReactorState:
|
|
21
|
+
...
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def neutron_ratio(self, neutron_target_price: BasecoinPerNeutron) -> float:
|
|
25
|
+
...
|
|
26
|
+
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def normalized_neutron_ratio(self, neutron_target_price: BasecoinPerNeutron) -> float:
|
|
29
|
+
...
|
|
30
|
+
|
|
31
|
+
@abstractmethod
|
|
32
|
+
def neutron_price(self, neutron_target_price: BasecoinPerNeutron) -> BasecoinPerNeutron:
|
|
33
|
+
...
|
|
34
|
+
|
|
35
|
+
@abstractmethod
|
|
36
|
+
def proton_price(self, neutron_target_price: BasecoinPerNeutron) -> BasecoinPerProton:
|
|
37
|
+
...
|
|
38
|
+
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def fission(self, basecoins: Basecoin) -> Tokeons:
|
|
41
|
+
...
|
|
42
|
+
|
|
43
|
+
@abstractmethod
|
|
44
|
+
def fusion(self, tokeons: Tokeons) -> Basecoin:
|
|
45
|
+
...
|
|
46
|
+
|
|
47
|
+
def neutron_volume(self, neutron_target_price: BasecoinPerNeutron, neutrons: Neutron) -> Basecoin:
|
|
48
|
+
return neutrons * self.neutron_price(neutron_target_price)
|
|
49
|
+
|
|
50
|
+
def proton_volume(self, neutron_target_price: BasecoinPerNeutron, protons: Proton) -> Basecoin:
|
|
51
|
+
return protons * self.proton_price(neutron_target_price)
|
|
52
|
+
|
|
53
|
+
@abstractmethod
|
|
54
|
+
def volume_delta(self, volume: Basecoin, reaction_time: float) -> Basecoin:
|
|
55
|
+
...
|
|
56
|
+
|
|
57
|
+
@abstractmethod
|
|
58
|
+
def beta_decay_plus_fee(self, reaction_time: float, volume: Basecoin) -> float:
|
|
59
|
+
...
|
|
60
|
+
|
|
61
|
+
@abstractmethod
|
|
62
|
+
def beta_decay_minus_fee(self, reaction_time: float, volume: Basecoin) -> float:
|
|
63
|
+
...
|
|
64
|
+
|
|
65
|
+
@abstractmethod
|
|
66
|
+
def beta_decay_plus(self, neutron_target_price: BasecoinPerNeutron, reaction_time: float, protons: Proton) -> Neutron:
|
|
67
|
+
...
|
|
68
|
+
|
|
69
|
+
@abstractmethod
|
|
70
|
+
def beta_decay_minus(self, neutron_target_price: BasecoinPerNeutron, reaction_time: float, neutrons: Neutron) -> Proton:
|
|
71
|
+
...
|
|
72
|
+
|
|
73
|
+
@abstractmethod
|
|
74
|
+
def execute(self, reaction: GluonReaction, balance: Basecoin | Tokeons | Proton | Neutron, neutron_target_price: BasecoinPerNeutron, reaction_time: float) -> GluonExecution[R]:
|
|
75
|
+
...
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from .gluon_reactor import GluonReactor
|
|
2
|
+
from .types import (
|
|
3
|
+
Basecoin,
|
|
4
|
+
BasecoinPerNeutron,
|
|
5
|
+
BasecoinPerProton,
|
|
6
|
+
GluonExecution,
|
|
7
|
+
GluonReaction,
|
|
8
|
+
GluonZReactorParameters,
|
|
9
|
+
GluonZReactorState,
|
|
10
|
+
Neutron,
|
|
11
|
+
Proton,
|
|
12
|
+
Tokeons,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
class GluonZReactor(GluonReactor[GluonZReactorState]):
|
|
16
|
+
|
|
17
|
+
def __init__(self, parameters: GluonZReactorParameters, state: GluonZReactorState):
|
|
18
|
+
super().__init__(state)
|
|
19
|
+
self._parameters = parameters
|
|
20
|
+
self._state = state
|
|
21
|
+
assert state.prev_reaction_time == 0
|
|
22
|
+
assert state.prev_volume_delta == 0
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def parameters(self) -> GluonZReactorParameters:
|
|
26
|
+
return self._parameters
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def state(self) -> GluonZReactorState:
|
|
30
|
+
return self._state
|
|
31
|
+
|
|
32
|
+
def neutron_ratio(self, neutron_target_price: float) -> float:
|
|
33
|
+
return (neutron_target_price * self._state.neutron_circulating_supply) / self._state.reserves
|
|
34
|
+
|
|
35
|
+
def normalized_neutron_ratio(self, neutron_target_price: BasecoinPerNeutron) -> float:
|
|
36
|
+
|
|
37
|
+
q = self.neutron_ratio(neutron_target_price)
|
|
38
|
+
q_star = self._parameters.critical_neutron_ratio
|
|
39
|
+
|
|
40
|
+
return min(q, q / (1 + (q - q_star)))
|
|
41
|
+
|
|
42
|
+
def neutron_price(self, neutron_target_price: BasecoinPerNeutron) -> BasecoinPerNeutron:
|
|
43
|
+
return self.normalized_neutron_ratio(neutron_target_price) * (self._state.reserves / self._state.neutron_circulating_supply)
|
|
44
|
+
|
|
45
|
+
def proton_price(self, neutron_target_price: BasecoinPerNeutron) -> BasecoinPerProton:
|
|
46
|
+
return (1 - self.normalized_neutron_ratio(neutron_target_price)) * (self._state.reserves / self._state.proton_circulating_supply)
|
|
47
|
+
|
|
48
|
+
def fission(self, basecoins: Basecoin) -> Tokeons:
|
|
49
|
+
fee = self._parameters.fission_fee
|
|
50
|
+
sn = self._state.neutron_circulating_supply
|
|
51
|
+
sp = self._state.proton_circulating_supply
|
|
52
|
+
r = self._state.reserves
|
|
53
|
+
|
|
54
|
+
neutrons = (1 - fee) * basecoins * (sn / r)
|
|
55
|
+
protons = (1 - fee) * basecoins * (sp / r)
|
|
56
|
+
|
|
57
|
+
return Tokeons(neutrons, protons)
|
|
58
|
+
|
|
59
|
+
def fusion(self, tokeons: Tokeons) -> Basecoin:
|
|
60
|
+
fee = self._parameters.fusion_fee
|
|
61
|
+
sn = self._state.neutron_circulating_supply
|
|
62
|
+
sp = self._state.proton_circulating_supply
|
|
63
|
+
r = self._state.reserves
|
|
64
|
+
|
|
65
|
+
m_n = tokeons.neutrons * (r / sn)
|
|
66
|
+
m_p = tokeons.protons * (r / sp)
|
|
67
|
+
assert(m_n == m_p)
|
|
68
|
+
|
|
69
|
+
return (1 - fee) * m_n
|
|
70
|
+
|
|
71
|
+
def volume_delta(self, volume: Basecoin, reaction_time: float) -> Basecoin:
|
|
72
|
+
assert reaction_time > 0
|
|
73
|
+
return (self._state.prev_volume_delta * (self._parameters.volume_decay_factor ** (reaction_time - self._state.prev_reaction_time))) + volume
|
|
74
|
+
|
|
75
|
+
def beta_decay_plus_fee(self, reaction_time: float, volume: Basecoin) -> float:
|
|
76
|
+
assert reaction_time > 0
|
|
77
|
+
v = self.volume_delta(volume, reaction_time)
|
|
78
|
+
return min(1, self._parameters.beta_decay_fee_intercept + self._parameters.beta_decay_fee_slope*(max(v, 0) / self._state.reserves))
|
|
79
|
+
|
|
80
|
+
def beta_decay_minus_fee(self, reaction_time: float, volume: Basecoin) -> float:
|
|
81
|
+
assert reaction_time > 0
|
|
82
|
+
v = self.volume_delta(volume, reaction_time)
|
|
83
|
+
return min(1, self._parameters.beta_decay_fee_intercept + self._parameters.beta_decay_fee_slope*(max(-1*v, 0) / self._state.reserves))
|
|
84
|
+
|
|
85
|
+
def beta_decay_plus(self, neutron_target_price: BasecoinPerNeutron, reaction_time: float, protons: Proton) -> Neutron:
|
|
86
|
+
assert reaction_time > 0
|
|
87
|
+
v = self.proton_volume(neutron_target_price, protons)
|
|
88
|
+
fee = self.beta_decay_plus_fee(reaction_time, v)
|
|
89
|
+
pn = self.neutron_price(neutron_target_price)
|
|
90
|
+
pp = self.proton_price(neutron_target_price)
|
|
91
|
+
|
|
92
|
+
return (1 - fee) * protons * (pp / pn)
|
|
93
|
+
|
|
94
|
+
def beta_decay_minus(self, neutron_target_price: BasecoinPerNeutron, reaction_time: float, neutrons: Neutron) -> Proton:
|
|
95
|
+
assert reaction_time > 0
|
|
96
|
+
v = self.neutron_volume(neutron_target_price, neutrons)
|
|
97
|
+
fee = self.beta_decay_minus_fee(reaction_time, v)
|
|
98
|
+
pn = self.neutron_price(neutron_target_price)
|
|
99
|
+
pp = self.proton_price(neutron_target_price)
|
|
100
|
+
|
|
101
|
+
return (1 - fee) * neutrons * (pn / pp)
|
|
102
|
+
|
|
103
|
+
def execute(self, reaction: GluonReaction, balance: Basecoin | Tokeons | Proton | Neutron, neutron_target_price: BasecoinPerNeutron, reaction_time: float) -> GluonExecution[GluonZReactorState]:
|
|
104
|
+
assert reaction_time > 0
|
|
105
|
+
|
|
106
|
+
match reaction:
|
|
107
|
+
|
|
108
|
+
case GluonReaction.FISSION:
|
|
109
|
+
tokeons = self.fission(balance)
|
|
110
|
+
self._state.reserves += balance
|
|
111
|
+
self._state.neutron_circulating_supply += tokeons.neutrons
|
|
112
|
+
self._state.proton_circulating_supply += tokeons.protons
|
|
113
|
+
return GluonExecution(tokeons, self.state)
|
|
114
|
+
|
|
115
|
+
case GluonReaction.FUSION:
|
|
116
|
+
basecoins = self.fusion(balance)
|
|
117
|
+
self._state.reserves -= basecoins
|
|
118
|
+
self._state.neutron_circulating_supply -= balance.neutrons
|
|
119
|
+
self._state.proton_circulating_supply -= balance.protons
|
|
120
|
+
return GluonExecution(basecoins, self.state)
|
|
121
|
+
|
|
122
|
+
case GluonReaction.BETA_DECAY_PLUS:
|
|
123
|
+
neutrons = self.beta_decay_plus(neutron_target_price, reaction_time, balance)
|
|
124
|
+
self._state.neutron_circulating_supply += neutrons
|
|
125
|
+
self._state.proton_circulating_supply -= balance
|
|
126
|
+
pv = self.proton_volume(neutron_target_price, balance)
|
|
127
|
+
self._state.prev_volume_delta = self.volume_delta(pv, reaction_time)
|
|
128
|
+
self._state.prev_reaction_time = reaction_time
|
|
129
|
+
return GluonExecution(neutrons, self.state)
|
|
130
|
+
|
|
131
|
+
case GluonReaction.BETA_DECAY_MINUS:
|
|
132
|
+
protons = self.beta_decay_minus(neutron_target_price, reaction_time, balance)
|
|
133
|
+
self._state.neutron_circulating_supply -= balance
|
|
134
|
+
self._state.proton_circulating_supply += protons
|
|
135
|
+
nv = -1 * self.neutron_volume(neutron_target_price, balance)
|
|
136
|
+
self._state.prev_volume_delta = self.volume_delta(nv, reaction_time)
|
|
137
|
+
self._state.prev_reaction_time = reaction_time
|
|
138
|
+
return GluonExecution(protons, self.state)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Sequence, Generic, TypeVar
|
|
3
|
+
from enum import Enum, unique
|
|
4
|
+
|
|
5
|
+
from stability_nexus.types import Basecoin
|
|
6
|
+
|
|
7
|
+
Neutron = float
|
|
8
|
+
Proton = float
|
|
9
|
+
BasecoinPerNeutron = float
|
|
10
|
+
BasecoinPerProton = float
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class Tokeons:
|
|
14
|
+
neutrons: Neutron
|
|
15
|
+
protons: Proton
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class GluonReactorParameters:
|
|
19
|
+
critical_neutron_ratio: float
|
|
20
|
+
fission_fee: float
|
|
21
|
+
fusion_fee: float
|
|
22
|
+
beta_decay_fee_slope: float
|
|
23
|
+
beta_decay_fee_intercept: float
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class GluonZReactorParameters(GluonReactorParameters):
|
|
27
|
+
volume_decay_factor: float
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class GluonReactorState:
|
|
31
|
+
reserves: Basecoin
|
|
32
|
+
neutron_circulating_supply: Neutron
|
|
33
|
+
proton_circulating_supply: Proton
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class GluonZReactorState(GluonReactorState):
|
|
37
|
+
prev_volume_delta: Basecoin
|
|
38
|
+
prev_reaction_time: float
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class GluonUserState:
|
|
42
|
+
basecoins: Basecoin
|
|
43
|
+
neutrons: Neutron
|
|
44
|
+
protons: Proton
|
|
45
|
+
|
|
46
|
+
@unique
|
|
47
|
+
class GluonReaction(Enum):
|
|
48
|
+
FISSION = 1
|
|
49
|
+
FUSION = 2
|
|
50
|
+
BETA_DECAY_PLUS = 3
|
|
51
|
+
BETA_DECAY_MINUS = 4
|
|
52
|
+
|
|
53
|
+
GluonReactionSequence = Sequence[GluonReaction]
|
|
54
|
+
|
|
55
|
+
R = TypeVar("R", bound=GluonReactorState)
|
|
56
|
+
|
|
57
|
+
@dataclass
|
|
58
|
+
class GluonExecution(Generic[R]):
|
|
59
|
+
reactor_output: Tokeons | Basecoin | Neutron | Proton
|
|
60
|
+
reactor_state: R
|
stability_nexus/types.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: stability-nexus
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Stability Nexus multi-protocol simulation library.
|
|
5
|
+
Project-URL: Homepage, https://stability.nexus
|
|
6
|
+
Author-email: Luca D'Angelo <ldgaetano@protonmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Science/Research
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# stability-nexus
|
|
17
|
+
|
|
18
|
+
Stability Nexus multi-protocol simulation library.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install stability-nexus
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from stability_nexus.gluon.reactors import GluonZReactor
|
|
30
|
+
from stability_nexus.gluon.reactors.types import (
|
|
31
|
+
GluonReaction,
|
|
32
|
+
GluonZReactorParameters,
|
|
33
|
+
GluonZReactorState,
|
|
34
|
+
Tokeons,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# Configure reactor parameters
|
|
38
|
+
params = GluonZReactorParameters(
|
|
39
|
+
critical_neutron_ratio=0.5,
|
|
40
|
+
fission_fee=0.01,
|
|
41
|
+
fusion_fee=0.01,
|
|
42
|
+
beta_decay_fee_slope=0.1,
|
|
43
|
+
beta_decay_fee_intercept=0.005,
|
|
44
|
+
volume_decay_factor=0.99,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# Initialize reactor state
|
|
48
|
+
state = GluonZReactorState(
|
|
49
|
+
reserves=1000.0,
|
|
50
|
+
neutron_circulating_supply=500.0,
|
|
51
|
+
proton_circulating_supply=500.0,
|
|
52
|
+
prev_volume_delta=0.0,
|
|
53
|
+
prev_reaction_time=0.0,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
# Create reactor instance
|
|
57
|
+
reactor = GluonZReactor(params, state)
|
|
58
|
+
|
|
59
|
+
# Execute a fission reaction (basecoins -> neutrons + protons)
|
|
60
|
+
result = reactor.execute(
|
|
61
|
+
GluonReaction.FISSION,
|
|
62
|
+
balance=100.0,
|
|
63
|
+
neutron_target_price=1.0,
|
|
64
|
+
reaction_time=1.0,
|
|
65
|
+
)
|
|
66
|
+
print(f"Received: {result.reactor_output}")
|
|
67
|
+
print(f"New reserves: {result.reactor_state.reserves}")
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Reactions
|
|
71
|
+
|
|
72
|
+
The Gluon protocol supports four reaction types:
|
|
73
|
+
|
|
74
|
+
- **FISSION**: Convert basecoins into neutrons and protons
|
|
75
|
+
- **FUSION**: Convert neutrons and protons back into basecoins
|
|
76
|
+
- **BETA_DECAY_PLUS**: Convert protons into neutrons
|
|
77
|
+
- **BETA_DECAY_MINUS**: Convert neutrons into protons
|
|
78
|
+
|
|
79
|
+
## Types
|
|
80
|
+
|
|
81
|
+
The library uses type aliases for documentation:
|
|
82
|
+
|
|
83
|
+
- `Basecoin` - Reserve currency amounts (`float`)
|
|
84
|
+
- `Neutron` - Neutron token amounts (`float`)
|
|
85
|
+
- `Proton` - Proton token amounts (`float`)
|
|
86
|
+
- `BasecoinPerNeutron` - Price ratio (`float`)
|
|
87
|
+
- `BasecoinPerProton` - Price ratio (`float`)
|
|
88
|
+
- `Tokeons` - A dataclass containing neutrons and protons
|
|
89
|
+
|
|
90
|
+
## Contributing
|
|
91
|
+
|
|
92
|
+
If you encounter bugs, need help, or have feature requests:
|
|
93
|
+
|
|
94
|
+
- Please open an issue in this repository providing detailed information.
|
|
95
|
+
- Describe the problem clearly and include any relevant logs or screenshots.
|
|
96
|
+
|
|
97
|
+
We appreciate your feedback and contributions!
|
|
98
|
+
|
|
99
|
+
© 2025 The Stable Order.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
stability_nexus/__init__.py,sha256=UZUJrT1QQX3c_Gy5PtqjuEvFsh69UipWEy1n7oC7pKM,149
|
|
2
|
+
stability_nexus/types.py,sha256=kcm6KEGVSTfi9cvrtXrWIQPDzIkJLTtoVcThAzbTPKg,78
|
|
3
|
+
stability_nexus/gluon/__init__.py,sha256=fC_R5ITjU9LSNL3GEDlYKTtUuf_1vQamtuyGw3uLOcU,166
|
|
4
|
+
stability_nexus/gluon/reactors/__init__.py,sha256=3yolOWJunu1xl4UQmxDrvyfXSpSr0l8q7_MXbOPaHfA,151
|
|
5
|
+
stability_nexus/gluon/reactors/gluon_reactor.py,sha256=xxjjtxERiIxCXscMNbe7kJetJwemNHlaMo3yINfGfzE,2477
|
|
6
|
+
stability_nexus/gluon/reactors/gluon_z_reactor.py,sha256=xNp-NwJKqOSM0CJtNVSEYZD3RNNClcvHv53OiHCa3s4,6406
|
|
7
|
+
stability_nexus/gluon/reactors/types.py,sha256=jfRbcAmfNeb714fgKpOdteyMDMZ4H40RCpkYxJmXoeU,1293
|
|
8
|
+
stability_nexus-1.0.0.dist-info/METADATA,sha256=kWI3CiY8y-syVlQL2_9UWmIVpSDbRgj3hSQen-cLLxg,2660
|
|
9
|
+
stability_nexus-1.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
10
|
+
stability_nexus-1.0.0.dist-info/RECORD,,
|