iwa 0.1.3__py3-none-any.whl → 0.1.4__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.
iwa/web/dependencies.py CHANGED
@@ -9,8 +9,42 @@ from loguru import logger
9
9
 
10
10
  from iwa.core.wallet import Wallet
11
11
 
12
- # Singleton wallet instance for the web app
13
- wallet = Wallet()
12
+ # Singleton wallet instance (lazy-initialized or injected)
13
+ _wallet: Optional[Wallet] = None
14
+
15
+
16
+ def get_wallet() -> Wallet:
17
+ """Get the wallet instance (lazy initialization or injected).
18
+
19
+ Returns the injected wallet if set_wallet() was called,
20
+ otherwise creates a new Wallet on first access.
21
+ """
22
+ global _wallet
23
+ if _wallet is None:
24
+ _wallet = Wallet()
25
+ return _wallet
26
+
27
+
28
+ def set_wallet(wallet: Wallet) -> None:
29
+ """Inject an external wallet instance.
30
+
31
+ Call this BEFORE importing routers to share a wallet
32
+ with an external application (e.g., Triton).
33
+ """
34
+ global _wallet
35
+ _wallet = wallet
36
+
37
+
38
+ # Backwards compatibility: module-level wallet property
39
+ # Deprecated: use get_wallet() instead
40
+ class _WalletProxy:
41
+ """Proxy that redirects to get_wallet() for backwards compatibility."""
42
+
43
+ def __getattr__(self, name: str):
44
+ return getattr(get_wallet(), name)
45
+
46
+
47
+ wallet = _WalletProxy()
14
48
 
15
49
  # Authentication
16
50
  api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
iwa/web/server.py CHANGED
@@ -147,11 +147,28 @@ async def root():
147
147
 
148
148
 
149
149
  def run_server(host: str = "127.0.0.1", port: int = 8000):
150
- """Run the web server using uvicorn."""
150
+ """Run the web server using uvicorn (blocking)."""
151
151
  import uvicorn
152
152
 
153
153
  uvicorn.run(app, host=host, port=port)
154
154
 
155
155
 
156
+ async def run_server_async(host: str = "127.0.0.1", port: int = 8000):
157
+ """Run the web server in an async context (non-blocking).
158
+
159
+ Use this to embed the web server in another async application.
160
+ The server runs until cancelled.
161
+
162
+ Args:
163
+ host: Bind address. Default "127.0.0.1" (localhost only for security).
164
+ port: Port number. Default 8000.
165
+ """
166
+ import uvicorn
167
+
168
+ config = uvicorn.Config(app, host=host, port=port, log_level="warning")
169
+ server = uvicorn.Server(config)
170
+ await server.serve()
171
+
172
+
156
173
  if __name__ == "__main__":
157
174
  run_server()
@@ -0,0 +1,204 @@
1
+ """Tests for wallet injection in web dependencies."""
2
+
3
+ from unittest.mock import MagicMock, patch
4
+
5
+
6
+ class TestWalletInjection:
7
+ """Tests for get_wallet, set_wallet, and _WalletProxy."""
8
+
9
+ def setup_method(self):
10
+ """Reset the global wallet state before each test."""
11
+ # Import fresh to reset state
12
+ import iwa.web.dependencies as deps
13
+
14
+ deps._wallet = None
15
+
16
+ def test_get_wallet_lazy_initialization(self):
17
+ """get_wallet() creates a Wallet on first call if none injected."""
18
+ import iwa.web.dependencies as deps
19
+
20
+ deps._wallet = None
21
+
22
+ with patch.object(deps, "Wallet") as mock_wallet_cls:
23
+ mock_wallet_cls.return_value = MagicMock(name="LazyWallet")
24
+
25
+ result = deps.get_wallet()
26
+
27
+ mock_wallet_cls.assert_called_once()
28
+ assert result == mock_wallet_cls.return_value
29
+
30
+ def test_get_wallet_returns_same_instance(self):
31
+ """get_wallet() returns the same instance on subsequent calls."""
32
+ import iwa.web.dependencies as deps
33
+
34
+ deps._wallet = None
35
+
36
+ with patch.object(deps, "Wallet") as mock_wallet_cls:
37
+ mock_wallet_cls.return_value = MagicMock(name="SingletonWallet")
38
+
39
+ first = deps.get_wallet()
40
+ second = deps.get_wallet()
41
+
42
+ # Should only create once
43
+ mock_wallet_cls.assert_called_once()
44
+ assert first is second
45
+
46
+ def test_set_wallet_injects_external_wallet(self):
47
+ """set_wallet() allows injecting an external wallet instance."""
48
+ import iwa.web.dependencies as deps
49
+
50
+ deps._wallet = None
51
+
52
+ external_wallet = MagicMock(name="ExternalWallet")
53
+ deps.set_wallet(external_wallet)
54
+
55
+ result = deps.get_wallet()
56
+
57
+ assert result is external_wallet
58
+
59
+ def test_set_wallet_prevents_lazy_init(self):
60
+ """set_wallet() prevents Wallet() from being called."""
61
+ import iwa.web.dependencies as deps
62
+
63
+ deps._wallet = None
64
+
65
+ with patch.object(deps, "Wallet") as mock_wallet_cls:
66
+ external_wallet = MagicMock(name="InjectedWallet")
67
+ deps.set_wallet(external_wallet)
68
+
69
+ result = deps.get_wallet()
70
+
71
+ # Wallet() should NOT be called
72
+ mock_wallet_cls.assert_not_called()
73
+ assert result is external_wallet
74
+
75
+ def test_set_wallet_overrides_existing(self):
76
+ """set_wallet() can override a previously set wallet."""
77
+ import iwa.web.dependencies as deps
78
+
79
+ deps._wallet = None
80
+
81
+ wallet1 = MagicMock(name="Wallet1")
82
+ wallet2 = MagicMock(name="Wallet2")
83
+
84
+ deps.set_wallet(wallet1)
85
+ assert deps.get_wallet() is wallet1
86
+
87
+ deps.set_wallet(wallet2)
88
+ assert deps.get_wallet() is wallet2
89
+
90
+
91
+ class TestWalletProxy:
92
+ """Tests for _WalletProxy backwards compatibility."""
93
+
94
+ def setup_method(self):
95
+ """Reset the global wallet state before each test."""
96
+ import iwa.web.dependencies as deps
97
+
98
+ deps._wallet = None
99
+
100
+ def test_wallet_proxy_forwards_attribute_access(self):
101
+ """wallet.attribute forwards to get_wallet().attribute."""
102
+ import iwa.web.dependencies as deps
103
+
104
+ deps._wallet = None
105
+
106
+ mock_wallet = MagicMock()
107
+ mock_wallet.balance_service = MagicMock(name="BalanceService")
108
+ deps.set_wallet(mock_wallet)
109
+
110
+ # Access through the proxy
111
+ result = deps.wallet.balance_service
112
+
113
+ assert result is mock_wallet.balance_service
114
+
115
+ def test_wallet_proxy_forwards_method_calls(self):
116
+ """wallet.method() forwards to get_wallet().method()."""
117
+ import iwa.web.dependencies as deps
118
+
119
+ deps._wallet = None
120
+
121
+ mock_wallet = MagicMock()
122
+ mock_wallet.get_accounts_balances.return_value = ({"0x1": {}}, {"0x1": {}})
123
+ deps.set_wallet(mock_wallet)
124
+
125
+ # Call method through proxy
126
+ result = deps.wallet.get_accounts_balances("gnosis", ["native"])
127
+
128
+ mock_wallet.get_accounts_balances.assert_called_once_with("gnosis", ["native"])
129
+ assert result == ({"0x1": {}}, {"0x1": {}})
130
+
131
+ def test_wallet_proxy_triggers_lazy_init(self):
132
+ """Accessing wallet.attr when no wallet set triggers lazy init."""
133
+ import iwa.web.dependencies as deps
134
+
135
+ deps._wallet = None
136
+
137
+ with patch.object(deps, "Wallet") as mock_wallet_cls:
138
+ mock_instance = MagicMock()
139
+ mock_instance.key_storage = MagicMock(name="KeyStorage")
140
+ mock_wallet_cls.return_value = mock_instance
141
+
142
+ # Access through proxy should trigger lazy init
143
+ result = deps.wallet.key_storage
144
+
145
+ mock_wallet_cls.assert_called_once()
146
+ assert result is mock_instance.key_storage
147
+
148
+
149
+ class TestIntegrationWithRouters:
150
+ """Integration tests for wallet injection with routers."""
151
+
152
+ def setup_method(self):
153
+ """Reset the global wallet state before each test."""
154
+ import iwa.web.dependencies as deps
155
+
156
+ deps._wallet = None
157
+
158
+ def test_injected_wallet_used_by_routers(self):
159
+ """Routers using 'wallet' get the injected instance."""
160
+ import iwa.web.dependencies as deps
161
+
162
+ # Inject a mock wallet BEFORE routers access it
163
+ mock_wallet = MagicMock()
164
+ mock_wallet.key_storage = MagicMock()
165
+ mock_wallet.key_storage.accounts = {}
166
+ deps.set_wallet(mock_wallet)
167
+
168
+ # Simulate what a router does
169
+ from iwa.web.dependencies import wallet
170
+
171
+ # Access through the module-level wallet (proxy)
172
+ accounts = wallet.key_storage.accounts
173
+
174
+ assert accounts == {}
175
+ # Verify it's using our injected wallet
176
+ assert deps.get_wallet() is mock_wallet
177
+
178
+
179
+ class TestServerAsync:
180
+ """Tests for run_server_async function."""
181
+
182
+ def test_run_server_async_source_has_localhost_default(self):
183
+ """run_server_async defaults to localhost for security (source check)."""
184
+ import pathlib
185
+
186
+ # Read the source file directly to verify default
187
+ server_path = pathlib.Path(__file__).parent.parent / "server.py"
188
+ source = server_path.read_text()
189
+
190
+ # Verify the function signature has localhost default
191
+ assert 'async def run_server_async(host: str = "127.0.0.1"' in source
192
+
193
+ def test_run_server_source_has_async_function(self):
194
+ """run_server_async function exists in server.py source."""
195
+ import pathlib
196
+
197
+ server_path = pathlib.Path(__file__).parent.parent / "server.py"
198
+ source = server_path.read_text()
199
+
200
+ # Verify the async function exists
201
+ assert "async def run_server_async" in source
202
+ assert "uvicorn.Config" in source
203
+ assert "uvicorn.Server" in source
204
+ assert "await server.serve()" in source
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: iwa
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: A secure, modular, and plugin-based framework for crypto agents and ops
5
5
  Requires-Python: <4.0,>=3.12
6
6
  Description-Content-Type: text/markdown
@@ -146,9 +146,9 @@ iwa/tui/tests/test_wallets_refactor.py,sha256=71G3HLbhTtgDy3ffVbYv0MFYRgdYd-NWGB
146
146
  iwa/tui/tests/test_widgets.py,sha256=C9UgIGeWRaQ459JygFEQx-7hOi9mWrSUDDIMZH1ge50,3994
147
147
  iwa/tui/widgets/__init__.py,sha256=UzD6nJbwv9hOtkWl9I7faXm1a-rcu4xFRxrf4KBwwY4,161
148
148
  iwa/tui/widgets/base.py,sha256=Z8FigMhsfD76PkFVERqMaotd-xwXfuFZm_8TmCMOsl4,3381
149
- iwa/web/dependencies.py,sha256=0_dAJlRh6gKrUDRPKUe92eshFsg572yx_H0lQgSqGDA,2103
149
+ iwa/web/dependencies.py,sha256=iTvdCSuETFLeQPtFydi21s1EKA_a80rbA57s-994fMQ,2980
150
150
  iwa/web/models.py,sha256=MSD9WPy_Nz_amWgoo2KSDTn4ZLv_AV0o0amuNtSf-68,3035
151
- iwa/web/server.py,sha256=4ZLVFEKoGs_NoCcXMeyYzDNdxUXazjwHQaX7CR1pwHE,5239
151
+ iwa/web/server.py,sha256=BcaLtmyPSsVehUtbWf4fbEGJ4E0bDlY_PzVq479HoZM,5786
152
152
  iwa/web/routers/accounts.py,sha256=VhCHrwzRWqZcSW-tTEqFWT5hFl-IYEWpqXeuN8xM3-4,3922
153
153
  iwa/web/routers/state.py,sha256=wsBAOIWZeMWjMwLiiWVhuEXHAceI0IIq6CPpmh7SbIc,2469
154
154
  iwa/web/routers/swap.py,sha256=8xycAytquR29ELxW3vx428W8s9bI_w_x2kpRhhJ0KXY,22630
@@ -162,11 +162,12 @@ iwa/web/routers/olas/staking.py,sha256=jktJ2C1Q9X4aC0tWJByN3sHpEXY0EIvr3rr4N0MtX
162
162
  iwa/web/static/app.js,sha256=VCm9zPJERb9pX6zbFQ_7D47UnztGdibrkZ1dmwhsvdc,114949
163
163
  iwa/web/static/index.html,sha256=Qg06MFK__2KxwWTr8hhjX_GwsoN53QsLCTypu4fBR-Q,28516
164
164
  iwa/web/static/style.css,sha256=7i6T96pS7gXSLDZfyp_87gRlyB9rpsFWJEHJ-dRY1ug,24371
165
+ iwa/web/tests/test_wallet_injection.py,sha256=YjoQh1FMwroswF6O_XZOwYXzanv-JYElNTDNKxAS77g,6751
165
166
  iwa/web/tests/test_web_endpoints.py,sha256=vA25YghHNB23sbmhD4ciesn_f_okSq0tjlkrSiKZ0rs,24007
166
167
  iwa/web/tests/test_web_olas.py,sha256=GunKEAzcbzL7FoUGMtEl8wqiqwYwA5lB9sOhfCNj0TA,16312
167
168
  iwa/web/tests/test_web_swap.py,sha256=7A4gBJFL01kIXPtW1E1J17SCsVc_0DmUn-R8kKrnnVA,2974
168
169
  iwa/web/tests/test_web_swap_coverage.py,sha256=zGNrzlhZ_vWDCvWmLcoUwFgqxnrp_ACbo49AtWBS_Kw,5584
169
- iwa-0.1.3.dist-info/licenses/LICENSE,sha256=eIubm_IlBHPYRQlLNZKbBNKhJUUP3JH0A2miZUhAVfI,1078
170
+ iwa-0.1.4.dist-info/licenses/LICENSE,sha256=eIubm_IlBHPYRQlLNZKbBNKhJUUP3JH0A2miZUhAVfI,1078
170
171
  tests/legacy_cow.py,sha256=oOkZvIxL70ReEoD9oHQbOD5GpjIr6AGNHcOCgfPlerU,8389
171
172
  tests/legacy_safe.py,sha256=AssM2g13E74dNGODu_H0Q0y412lgqsrYnEzI97nm_Ts,2972
172
173
  tests/legacy_transaction_retry_logic.py,sha256=D9RqZ7DBu61Xr2djBAodU2p9UE939LL-DnQXswX5iQk,1497
@@ -224,8 +225,8 @@ tests/test_utils.py,sha256=vkP49rYNI8BRzLpWR3WnKdDr8upeZjZcs7Rx0pjbQMo,1292
224
225
  tests/test_workers.py,sha256=MInwdkFY5LdmFB3o1odIaSD7AQZb3263hNafO1De5PE,2793
225
226
  tools/create_and_stake_service.py,sha256=1xwy_bJQI1j9yIQ968Oc9Db_F6mk1659LuuZntTASDE,3742
226
227
  tools/verify_drain.py,sha256=PkMjblyOOAuQge88FwfEzRtCYeEtJxXhPBmtQYCoQ-8,6743
227
- iwa-0.1.3.dist-info/METADATA,sha256=uKemLrcWBfz_raFNxn3okTiQWiwF-lLQok838PpZXtI,7336
228
- iwa-0.1.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
229
- iwa-0.1.3.dist-info/entry_points.txt,sha256=nwB6kscrfA7M00pYmL2j-sBH6eF6h2ga9IK1BZxdiyQ,241
230
- iwa-0.1.3.dist-info/top_level.txt,sha256=kedS9cRUbm4JE2wYeabIXilhHjN8KCw0IGbqqqsw0Bs,16
231
- iwa-0.1.3.dist-info/RECORD,,
228
+ iwa-0.1.4.dist-info/METADATA,sha256=a0L36sHev8jqTBgB7MfGHSz46nAvG9J3OrkHNE1FpvM,7336
229
+ iwa-0.1.4.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
230
+ iwa-0.1.4.dist-info/entry_points.txt,sha256=nwB6kscrfA7M00pYmL2j-sBH6eF6h2ga9IK1BZxdiyQ,241
231
+ iwa-0.1.4.dist-info/top_level.txt,sha256=kedS9cRUbm4JE2wYeabIXilhHjN8KCw0IGbqqqsw0Bs,16
232
+ iwa-0.1.4.dist-info/RECORD,,
File without changes