olas-operate-middleware 0.1.0rc59__py3-none-any.whl → 0.13.2__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.
Files changed (98) hide show
  1. olas_operate_middleware-0.13.2.dist-info/METADATA +75 -0
  2. olas_operate_middleware-0.13.2.dist-info/RECORD +101 -0
  3. {olas_operate_middleware-0.1.0rc59.dist-info → olas_operate_middleware-0.13.2.dist-info}/WHEEL +1 -1
  4. operate/__init__.py +17 -0
  5. operate/account/user.py +35 -9
  6. operate/bridge/bridge_manager.py +470 -0
  7. operate/bridge/providers/lifi_provider.py +377 -0
  8. operate/bridge/providers/native_bridge_provider.py +677 -0
  9. operate/bridge/providers/provider.py +469 -0
  10. operate/bridge/providers/relay_provider.py +457 -0
  11. operate/cli.py +1565 -417
  12. operate/constants.py +60 -12
  13. operate/data/README.md +19 -0
  14. operate/data/contracts/{service_staking_token → dual_staking_token}/__init__.py +2 -2
  15. operate/data/contracts/dual_staking_token/build/DualStakingToken.json +443 -0
  16. operate/data/contracts/dual_staking_token/contract.py +132 -0
  17. operate/data/contracts/dual_staking_token/contract.yaml +23 -0
  18. operate/{ledger/base.py → data/contracts/foreign_omnibridge/__init__.py} +2 -19
  19. operate/data/contracts/foreign_omnibridge/build/ForeignOmnibridge.json +1372 -0
  20. operate/data/contracts/foreign_omnibridge/contract.py +130 -0
  21. operate/data/contracts/foreign_omnibridge/contract.yaml +23 -0
  22. operate/{ledger/solana.py → data/contracts/home_omnibridge/__init__.py} +2 -20
  23. operate/data/contracts/home_omnibridge/build/HomeOmnibridge.json +1421 -0
  24. operate/data/contracts/home_omnibridge/contract.py +80 -0
  25. operate/data/contracts/home_omnibridge/contract.yaml +23 -0
  26. operate/data/contracts/l1_standard_bridge/__init__.py +20 -0
  27. operate/data/contracts/l1_standard_bridge/build/L1StandardBridge.json +831 -0
  28. operate/data/contracts/l1_standard_bridge/contract.py +158 -0
  29. operate/data/contracts/l1_standard_bridge/contract.yaml +23 -0
  30. operate/data/contracts/l2_standard_bridge/__init__.py +20 -0
  31. operate/data/contracts/l2_standard_bridge/build/L2StandardBridge.json +626 -0
  32. operate/data/contracts/l2_standard_bridge/contract.py +130 -0
  33. operate/data/contracts/l2_standard_bridge/contract.yaml +23 -0
  34. operate/data/contracts/mech_activity/__init__.py +20 -0
  35. operate/data/contracts/mech_activity/build/MechActivity.json +111 -0
  36. operate/data/contracts/mech_activity/contract.py +44 -0
  37. operate/data/contracts/mech_activity/contract.yaml +23 -0
  38. operate/data/contracts/optimism_mintable_erc20/__init__.py +20 -0
  39. operate/data/contracts/optimism_mintable_erc20/build/OptimismMintableERC20.json +491 -0
  40. operate/data/contracts/optimism_mintable_erc20/contract.py +45 -0
  41. operate/data/contracts/optimism_mintable_erc20/contract.yaml +23 -0
  42. operate/data/contracts/recovery_module/__init__.py +20 -0
  43. operate/data/contracts/recovery_module/build/RecoveryModule.json +811 -0
  44. operate/data/contracts/recovery_module/contract.py +61 -0
  45. operate/data/contracts/recovery_module/contract.yaml +23 -0
  46. operate/data/contracts/requester_activity_checker/__init__.py +20 -0
  47. operate/data/contracts/requester_activity_checker/build/RequesterActivityChecker.json +111 -0
  48. operate/data/contracts/requester_activity_checker/contract.py +33 -0
  49. operate/data/contracts/requester_activity_checker/contract.yaml +23 -0
  50. operate/data/contracts/staking_token/__init__.py +20 -0
  51. operate/data/contracts/staking_token/build/StakingToken.json +1336 -0
  52. operate/data/contracts/{service_staking_token → staking_token}/contract.py +27 -13
  53. operate/data/contracts/staking_token/contract.yaml +23 -0
  54. operate/data/contracts/uniswap_v2_erc20/contract.yaml +3 -1
  55. operate/data/contracts/uniswap_v2_erc20/tests/__init__.py +20 -0
  56. operate/data/contracts/uniswap_v2_erc20/tests/test_contract.py +363 -0
  57. operate/keys.py +118 -33
  58. operate/ledger/__init__.py +159 -56
  59. operate/ledger/profiles.py +321 -18
  60. operate/migration.py +555 -0
  61. operate/{http → operate_http}/__init__.py +3 -2
  62. operate/{http → operate_http}/exceptions.py +6 -4
  63. operate/operate_types.py +544 -0
  64. operate/pearl.py +13 -1
  65. operate/quickstart/analyse_logs.py +118 -0
  66. operate/quickstart/claim_staking_rewards.py +104 -0
  67. operate/quickstart/reset_configs.py +106 -0
  68. operate/quickstart/reset_password.py +70 -0
  69. operate/quickstart/reset_staking.py +145 -0
  70. operate/quickstart/run_service.py +726 -0
  71. operate/quickstart/stop_service.py +72 -0
  72. operate/quickstart/terminate_on_chain_service.py +83 -0
  73. operate/quickstart/utils.py +298 -0
  74. operate/resource.py +62 -3
  75. operate/services/agent_runner.py +202 -0
  76. operate/services/deployment_runner.py +868 -0
  77. operate/services/funding_manager.py +929 -0
  78. operate/services/health_checker.py +280 -0
  79. operate/services/manage.py +2356 -620
  80. operate/services/protocol.py +1246 -340
  81. operate/services/service.py +756 -391
  82. operate/services/utils/mech.py +103 -0
  83. operate/services/utils/tendermint.py +86 -12
  84. operate/settings.py +70 -0
  85. operate/utils/__init__.py +135 -0
  86. operate/utils/gnosis.py +407 -80
  87. operate/utils/single_instance.py +226 -0
  88. operate/utils/ssl.py +133 -0
  89. operate/wallet/master.py +708 -123
  90. operate/wallet/wallet_recovery_manager.py +507 -0
  91. olas_operate_middleware-0.1.0rc59.dist-info/METADATA +0 -304
  92. olas_operate_middleware-0.1.0rc59.dist-info/RECORD +0 -41
  93. operate/data/contracts/service_staking_token/build/ServiceStakingToken.json +0 -1273
  94. operate/data/contracts/service_staking_token/contract.yaml +0 -23
  95. operate/ledger/ethereum.py +0 -48
  96. operate/types.py +0 -260
  97. {olas_operate_middleware-0.1.0rc59.dist-info → olas_operate_middleware-0.13.2.dist-info}/entry_points.txt +0 -0
  98. {olas_operate_middleware-0.1.0rc59.dist-info → olas_operate_middleware-0.13.2.dist-info/licenses}/LICENSE +0 -0
@@ -0,0 +1,377 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # ------------------------------------------------------------------------------
4
+ #
5
+ # Copyright 2024 Valory AG
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ # ------------------------------------------------------------------------------
20
+ """LI.FI provider."""
21
+
22
+
23
+ import enum
24
+ import time
25
+ import typing as t
26
+ from http import HTTPStatus
27
+ from urllib.parse import urlencode
28
+
29
+ import requests
30
+ from autonomy.chain.base import registry_contracts
31
+
32
+ from operate.bridge.providers.provider import (
33
+ DEFAULT_MAX_QUOTE_RETRIES,
34
+ MESSAGE_QUOTE_ZERO,
35
+ Provider,
36
+ ProviderRequest,
37
+ ProviderRequestStatus,
38
+ QuoteData,
39
+ )
40
+ from operate.constants import ZERO_ADDRESS
41
+ from operate.ledger import update_tx_with_gas_estimate, update_tx_with_gas_pricing
42
+ from operate.operate_types import Chain
43
+
44
+
45
+ LIFI_DEFAULT_ETA = 5 * 60
46
+
47
+
48
+ class LiFiTransactionStatus(str, enum.Enum):
49
+ """LI.FI transaction status."""
50
+
51
+ NOT_FOUND = "NOT_FOUND"
52
+ INVALID = "INVALID"
53
+ PENDING = "PENDING"
54
+ DONE = "DONE"
55
+ FAILED = "FAILED"
56
+ UNKNOWN = "UNKNOWN"
57
+
58
+ def __str__(self) -> str:
59
+ """__str__"""
60
+ return self.value
61
+
62
+
63
+ class LiFiProvider(Provider):
64
+ """LI.FI provider."""
65
+
66
+ def description(self) -> str:
67
+ """Get a human-readable description of the provider."""
68
+ return "LI.FI Bridge & DEX Aggregation Protocol https://li.fi/"
69
+
70
+ def quote(self, provider_request: ProviderRequest) -> None:
71
+ """Update the request with the quote."""
72
+ self._validate(provider_request)
73
+
74
+ if provider_request.status not in (
75
+ ProviderRequestStatus.CREATED,
76
+ ProviderRequestStatus.QUOTE_DONE,
77
+ ProviderRequestStatus.QUOTE_FAILED,
78
+ ):
79
+ raise RuntimeError(
80
+ f"Cannot quote request {provider_request.id} with status {provider_request.status}."
81
+ )
82
+
83
+ if provider_request.execution_data:
84
+ raise RuntimeError(
85
+ f"Cannot quote request {provider_request.id}: execution already present."
86
+ )
87
+
88
+ from_chain = provider_request.params["from"]["chain"]
89
+ from_address = provider_request.params["from"]["address"]
90
+ from_token = provider_request.params["from"]["token"]
91
+ to_chain = provider_request.params["to"]["chain"]
92
+ to_address = provider_request.params["to"]["address"]
93
+ to_token = provider_request.params["to"]["token"]
94
+ to_amount = provider_request.params["to"]["amount"]
95
+
96
+ if to_amount == 0:
97
+ self.logger.info(f"[LI.FI PROVIDER] {MESSAGE_QUOTE_ZERO}")
98
+ quote_data = QuoteData(
99
+ eta=0,
100
+ elapsed_time=0,
101
+ message=MESSAGE_QUOTE_ZERO,
102
+ provider_data=None,
103
+ timestamp=int(time.time()),
104
+ )
105
+ provider_request.quote_data = quote_data
106
+ provider_request.status = ProviderRequestStatus.QUOTE_DONE
107
+ return
108
+
109
+ url = "https://li.quest/v1/quote/toAmount"
110
+ headers = {"accept": "application/json"}
111
+ params = {
112
+ "fromChain": Chain(from_chain).id,
113
+ "fromAddress": from_address,
114
+ "fromToken": from_token,
115
+ "toChain": Chain(to_chain).id,
116
+ "toAddress": to_address,
117
+ "toToken": to_token,
118
+ "toAmount": to_amount,
119
+ "maxPriceImpact": 0.50, # TODO determine correct value
120
+ }
121
+ for attempt in range(1, DEFAULT_MAX_QUOTE_RETRIES + 1):
122
+ start = time.time()
123
+ try:
124
+ self.logger.info(f"[LI.FI PROVIDER] GET {url}?{urlencode(params)}")
125
+ response = requests.get(
126
+ url=url, headers=headers, params=params, timeout=30
127
+ )
128
+ response.raise_for_status()
129
+ response_json = response.json()
130
+ quote_data = QuoteData(
131
+ eta=LIFI_DEFAULT_ETA,
132
+ elapsed_time=time.time() - start,
133
+ message=None,
134
+ provider_data={
135
+ "attempts": attempt,
136
+ "response": response_json,
137
+ "response_status": response.status_code,
138
+ },
139
+ timestamp=int(time.time()),
140
+ )
141
+ provider_request.quote_data = quote_data
142
+ provider_request.status = ProviderRequestStatus.QUOTE_DONE
143
+ return
144
+ except requests.Timeout as e:
145
+ self.logger.warning(
146
+ f"[LI.FI PROVIDER] Timeout request on attempt {attempt}/{DEFAULT_MAX_QUOTE_RETRIES}: {e}."
147
+ )
148
+ quote_data = QuoteData(
149
+ eta=None,
150
+ elapsed_time=time.time() - start,
151
+ message=str(e),
152
+ provider_data={
153
+ "attempts": attempt,
154
+ "response": None,
155
+ "response_status": HTTPStatus.GATEWAY_TIMEOUT,
156
+ },
157
+ timestamp=int(time.time()),
158
+ )
159
+ except requests.RequestException as e:
160
+ self.logger.warning(
161
+ f"[LI.FI PROVIDER] Request failed on attempt {attempt}/{DEFAULT_MAX_QUOTE_RETRIES}: {e}."
162
+ )
163
+ response_json = response.json()
164
+ quote_data = QuoteData(
165
+ eta=None,
166
+ elapsed_time=time.time() - start,
167
+ message=response_json.get("message") or str(e),
168
+ provider_data={
169
+ "attempts": attempt,
170
+ "response": response_json,
171
+ "response_status": getattr(
172
+ response, "status_code", HTTPStatus.BAD_GATEWAY
173
+ ),
174
+ },
175
+ timestamp=int(time.time()),
176
+ )
177
+ except Exception as e: # pylint:disable=broad-except
178
+ self.logger.warning(
179
+ f"[LI.FI PROVIDER] Request failed on attempt {attempt}/{DEFAULT_MAX_QUOTE_RETRIES}: {e}."
180
+ )
181
+ quote_data = QuoteData(
182
+ eta=None,
183
+ elapsed_time=time.time() - start,
184
+ message=str(e),
185
+ provider_data={
186
+ "attempts": attempt,
187
+ "response": None,
188
+ "response_status": HTTPStatus.INTERNAL_SERVER_ERROR,
189
+ },
190
+ timestamp=int(time.time()),
191
+ )
192
+ if attempt >= DEFAULT_MAX_QUOTE_RETRIES:
193
+ self.logger.error(
194
+ f"[LI.FI PROVIDER] Request failed after {DEFAULT_MAX_QUOTE_RETRIES} attempts."
195
+ )
196
+ provider_request.quote_data = quote_data
197
+ provider_request.status = ProviderRequestStatus.QUOTE_FAILED
198
+ return
199
+
200
+ time.sleep(2)
201
+
202
+ def _get_approve_tx(self, provider_request: ProviderRequest) -> t.Optional[t.Dict]:
203
+ """Get the approve transaction."""
204
+ self.logger.info(
205
+ f"[LI.FI PROVIDER] Get appprove transaction for request {provider_request.id}."
206
+ )
207
+
208
+ if provider_request.params["to"]["amount"] == 0:
209
+ return None
210
+
211
+ quote_data = provider_request.quote_data
212
+ if not quote_data:
213
+ return None
214
+
215
+ if not quote_data.provider_data:
216
+ return None
217
+
218
+ quote = quote_data.provider_data.get("response")
219
+ if not quote:
220
+ return None
221
+
222
+ if "action" not in quote:
223
+ return None
224
+
225
+ from_token = quote["action"]["fromToken"]["address"]
226
+ if from_token == ZERO_ADDRESS:
227
+ return None
228
+
229
+ transaction_request = quote.get("transactionRequest")
230
+ if not transaction_request:
231
+ return None
232
+
233
+ from_amount = int(quote["action"]["fromAmount"])
234
+ from_ledger_api = self._from_ledger_api(provider_request)
235
+
236
+ approve_tx = registry_contracts.erc20.get_approve_tx(
237
+ ledger_api=from_ledger_api,
238
+ contract_address=from_token,
239
+ spender=transaction_request["to"],
240
+ sender=transaction_request["from"],
241
+ amount=from_amount,
242
+ )
243
+ approve_tx["gas"] = 200_000 # TODO backport to ERC20 contract as default
244
+ update_tx_with_gas_pricing(approve_tx, from_ledger_api)
245
+ update_tx_with_gas_estimate(approve_tx, from_ledger_api)
246
+ return approve_tx
247
+
248
+ def _get_bridge_tx(self, provider_request: ProviderRequest) -> t.Optional[t.Dict]:
249
+ """Get the bridge transaction."""
250
+ self.logger.info(
251
+ f"[LI.FI PROVIDER] Get bridge transaction for request {provider_request.id}."
252
+ )
253
+
254
+ if provider_request.params["to"]["amount"] == 0:
255
+ return None
256
+
257
+ quote_data = provider_request.quote_data
258
+ if not quote_data:
259
+ return None
260
+
261
+ if not quote_data.provider_data:
262
+ return None
263
+
264
+ quote = quote_data.provider_data.get("response")
265
+ if not quote:
266
+ return None
267
+
268
+ if "action" not in quote:
269
+ return None
270
+
271
+ transaction_request = quote.get("transactionRequest")
272
+ if not transaction_request:
273
+ return None
274
+
275
+ from_ledger_api = self._from_ledger_api(provider_request)
276
+
277
+ bridge_tx = {
278
+ "value": int(transaction_request["value"], 16),
279
+ "to": transaction_request["to"],
280
+ "data": transaction_request["data"], # TODO remove bytes?
281
+ "from": transaction_request["from"],
282
+ "chainId": transaction_request["chainId"],
283
+ "gasPrice": int(transaction_request["gasPrice"], 16),
284
+ "gas": int(transaction_request["gasLimit"], 16),
285
+ "nonce": from_ledger_api.api.eth.get_transaction_count(
286
+ transaction_request["from"]
287
+ ),
288
+ }
289
+ update_tx_with_gas_pricing(bridge_tx, from_ledger_api)
290
+ update_tx_with_gas_estimate(bridge_tx, from_ledger_api)
291
+ return bridge_tx
292
+
293
+ def _get_txs(
294
+ self, provider_request: ProviderRequest, *args: t.Any, **kwargs: t.Any
295
+ ) -> t.List[t.Tuple[str, t.Dict]]:
296
+ """Get the sorted list of transactions to execute the quote."""
297
+ txs = []
298
+ approve_tx = self._get_approve_tx(provider_request)
299
+ if approve_tx:
300
+ txs.append(("approve_tx", approve_tx))
301
+ bridge_tx = self._get_bridge_tx(provider_request)
302
+ if bridge_tx:
303
+ txs.append(("bridge_tx", bridge_tx))
304
+ return txs
305
+
306
+ def _update_execution_status(self, provider_request: ProviderRequest) -> None:
307
+ """Update the execution status."""
308
+
309
+ if provider_request.status not in (
310
+ ProviderRequestStatus.EXECUTION_PENDING,
311
+ ProviderRequestStatus.EXECUTION_UNKNOWN,
312
+ ):
313
+ return
314
+
315
+ execution_data = provider_request.execution_data
316
+ if not execution_data:
317
+ raise RuntimeError(
318
+ f"Cannot update request {provider_request.id}: execution data not present."
319
+ )
320
+
321
+ from_tx_hash = execution_data.from_tx_hash
322
+ if not from_tx_hash:
323
+ return
324
+
325
+ lifi_status = LiFiTransactionStatus.UNKNOWN
326
+ url = "https://li.quest/v1/status"
327
+ headers = {"accept": "application/json"}
328
+ params = {
329
+ "txHash": from_tx_hash,
330
+ }
331
+
332
+ try:
333
+ self.logger.info(f"[LI.FI PROVIDER] GET {url}?{urlencode(params)}")
334
+ response = requests.get(url=url, headers=headers, params=params, timeout=30)
335
+ response_json = response.json()
336
+ lifi_status = response_json.get(
337
+ "status", str(LiFiTransactionStatus.UNKNOWN)
338
+ )
339
+ execution_data.message = response_json.get(
340
+ "substatusMessage", response_json.get("message")
341
+ )
342
+ response.raise_for_status()
343
+ except Exception as e:
344
+ self.logger.error(
345
+ f"[LI.FI PROVIDER] Failed to update status for request {provider_request.id}: {e}"
346
+ )
347
+
348
+ if lifi_status == LiFiTransactionStatus.DONE:
349
+ self.logger.info(
350
+ f"[LI.FI PROVIDER] Execution done for {provider_request.id}."
351
+ )
352
+ from_ledger_api = self._from_ledger_api(provider_request)
353
+ to_ledger_api = self._to_ledger_api(provider_request)
354
+ to_tx_hash = response_json.get("receiving", {}).get("txHash")
355
+ execution_data.message = None
356
+ execution_data.to_tx_hash = to_tx_hash
357
+ execution_data.elapsed_time = Provider._tx_timestamp(
358
+ to_tx_hash, to_ledger_api
359
+ ) - Provider._tx_timestamp(from_tx_hash, from_ledger_api)
360
+ provider_request.status = ProviderRequestStatus.EXECUTION_DONE
361
+ elif lifi_status == LiFiTransactionStatus.FAILED:
362
+ provider_request.status = ProviderRequestStatus.EXECUTION_FAILED
363
+ elif lifi_status == LiFiTransactionStatus.PENDING:
364
+ provider_request.status = ProviderRequestStatus.EXECUTION_PENDING
365
+ else:
366
+ provider_request.status = ProviderRequestStatus.EXECUTION_UNKNOWN
367
+
368
+ def _get_explorer_link(self, provider_request: ProviderRequest) -> t.Optional[str]:
369
+ """Get the explorer link for a transaction."""
370
+ if not provider_request.execution_data:
371
+ return None
372
+
373
+ tx_hash = provider_request.execution_data.from_tx_hash
374
+ if not tx_hash:
375
+ return None
376
+
377
+ return f"https://scan.li.fi/tx/{tx_hash}"