mech-client 0.9.0__py3-none-any.whl → 0.10.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.
mech_client/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  """Mech client."""
2
2
 
3
- __version__ = "0.9.0"
3
+ __version__ = "0.10.0"
@@ -303,7 +303,13 @@
303
303
  {
304
304
  "indexed": false,
305
305
  "internalType": "bytes",
306
- "name": "data",
306
+ "name": "requestData",
307
+ "type": "bytes"
308
+ },
309
+ {
310
+ "indexed": false,
311
+ "internalType": "bytes",
312
+ "name": "deliveryData",
307
313
  "type": "bytes"
308
314
  }
309
315
  ],
@@ -333,7 +339,7 @@
333
339
  "type": "address"
334
340
  },
335
341
  {
336
- "indexed": true,
342
+ "indexed": false,
337
343
  "internalType": "address[]",
338
344
  "name": "requesters",
339
345
  "type": "address[]"
@@ -442,6 +448,12 @@
442
448
  "internalType": "bytes32[]",
443
449
  "name": "requestIds",
444
450
  "type": "bytes32[]"
451
+ },
452
+ {
453
+ "indexed": false,
454
+ "internalType": "bytes[]",
455
+ "name": "requestDatas",
456
+ "type": "bytes[]"
445
457
  }
446
458
  ],
447
459
  "name": "MarketplaceRequest",
@@ -50,6 +50,7 @@ from mech_client.interact import (
50
50
  get_event_signatures,
51
51
  get_mech_config,
52
52
  )
53
+ from mech_client.mech_marketplace_tool_management import get_mech_tools
53
54
  from mech_client.prompt_to_ipfs import push_metadata_to_ipfs
54
55
  from mech_client.wss import (
55
56
  register_event_handlers,
@@ -254,6 +255,9 @@ def fetch_requester_nvm_subscription_balance(
254
255
  nvm_balance_tracker_contract = get_contract(
255
256
  contract_address=mech_payment_balance_tracker, abi=abi, ledger_api=ledger_api
256
257
  )
258
+ requester_balance_tracker_balance = (
259
+ nvm_balance_tracker_contract.functions.mapRequesterBalances(requester).call()
260
+ )
257
261
  subscription_nft_address = (
258
262
  nvm_balance_tracker_contract.functions.subscriptionNFT().call()
259
263
  )
@@ -271,7 +275,7 @@ def fetch_requester_nvm_subscription_balance(
271
275
  requester, subscription_id
272
276
  ).call()
273
277
 
274
- return requester_balance
278
+ return requester_balance_tracker_balance + requester_balance
275
279
 
276
280
 
277
281
  def send_marketplace_request( # pylint: disable=too-many-arguments,too-many-locals
@@ -587,6 +591,8 @@ def wait_for_offchain_marketplace_data(mech_offchain_url: str, request_id: str)
587
591
  ).json()
588
592
  if response:
589
593
  return response
594
+
595
+ time.sleep(WAIT_SLEEP)
590
596
  except Exception: # pylint: disable=broad-except
591
597
  time.sleep(WAIT_SLEEP)
592
598
 
@@ -640,6 +646,30 @@ def check_prepaid_balances(
640
646
  sys.exit(1)
641
647
 
642
648
 
649
+ def verify_tools(tools: tuple, service_id: int, chain_config: Optional[str]) -> None:
650
+ """
651
+ Verifies user supplied tool(s) with the mech's metadata
652
+
653
+ :param tools: The user supplied tools.
654
+ :type tools: tuple
655
+ :param service_id: Service id of the mech.
656
+ :type service_id: int
657
+ :param chain_config: Id of the mech's chain configuration (stored configs/mechs.json)
658
+ :type chain_config: str
659
+ :rtype: None
660
+ """
661
+ mech_tools_data = get_mech_tools(service_id=service_id, chain_config=chain_config)
662
+ if not mech_tools_data:
663
+ raise ValueError("Error while fetching mech tools data")
664
+
665
+ mech_tools = mech_tools_data.get("tools", [])
666
+ invalid_tools = [tool for tool in tools if tool not in mech_tools]
667
+ if invalid_tools:
668
+ raise ValueError(
669
+ f"Tool(s) {invalid_tools} not found in mech tools: {mech_tools}"
670
+ )
671
+
672
+
643
673
  def marketplace_interact( # pylint: disable=too-many-arguments, too-many-locals, too-many-statements, too-many-return-statements
644
674
  prompts: tuple,
645
675
  priority_mech: str,
@@ -742,7 +772,7 @@ def marketplace_interact( # pylint: disable=too-many-arguments, too-many-locals
742
772
  )
743
773
  (
744
774
  payment_type,
745
- _,
775
+ service_id,
746
776
  max_delivery_rate,
747
777
  mech_payment_balance_tracker,
748
778
  mech_contract,
@@ -754,6 +784,8 @@ def marketplace_interact( # pylint: disable=too-many-arguments, too-many-locals
754
784
  mech_marketplace_request_config.delivery_rate = max_delivery_rate
755
785
  mech_marketplace_request_config.payment_type = payment_type
756
786
 
787
+ verify_tools(tools, service_id, chain_config)
788
+
757
789
  with open(IMECH_ABI_PATH, encoding="utf-8") as f:
758
790
  abi = json.load(f)
759
791
 
@@ -802,22 +834,29 @@ def marketplace_interact( # pylint: disable=too-many-arguments, too-many-locals
802
834
  max_delivery_rate,
803
835
  )
804
836
 
805
- if payment_type in [PaymentType.NATIVE_NVM.value, PaymentType.TOKEN_NVM.value]:
837
+ is_nvm_mech = payment_type in [
838
+ PaymentType.NATIVE_NVM.value,
839
+ PaymentType.TOKEN_NVM.value,
840
+ ]
841
+ if is_nvm_mech:
806
842
  nvm_mech_type = PaymentType(payment_type).name.lower()
807
843
  print(
808
844
  f"{nvm_mech_type} Nevermined Mech detected, subscription credits to be used"
809
845
  )
810
846
  requester = crypto.address
811
- requester_balance = fetch_requester_nvm_subscription_balance(
847
+ requester_total_balance_before = fetch_requester_nvm_subscription_balance(
812
848
  requester, ledger_api, mech_payment_balance_tracker, payment_type
813
849
  )
814
- if requester_balance < price:
850
+ if requester_total_balance_before < price:
815
851
  print(
816
- f" - Sender Subscription balance low. Needed: {price}, Actual: {requester_balance}"
852
+ f" - Sender Subscription balance low. Needed: {price}, Actual: {requester_total_balance_before}"
817
853
  )
818
854
  print(f" - Sender Address: {requester}")
819
855
  sys.exit(1)
820
856
 
857
+ print(
858
+ f" - Sender Subscription balance before request: {requester_total_balance_before}"
859
+ )
821
860
  # set price 0 to not send any msg.value in request transaction for nvm type mech
822
861
  price = 0
823
862
 
@@ -878,6 +917,19 @@ def marketplace_interact( # pylint: disable=too-many-arguments, too-many-locals
878
917
  )
879
918
 
880
919
  if data_url:
920
+ if is_nvm_mech:
921
+ requester_total_balance_after = (
922
+ fetch_requester_nvm_subscription_balance(
923
+ requester,
924
+ ledger_api,
925
+ mech_payment_balance_tracker,
926
+ payment_type,
927
+ )
928
+ )
929
+ print(
930
+ f" - Sender Subscription balance after delivery: {requester_total_balance_after}"
931
+ )
932
+
881
933
  print(f" - Data arrived: {data_url}")
882
934
  data = requests.get(f"{data_url}/{request_id_int}", timeout=30).json()
883
935
  print(" - Data from agent:")
@@ -7,8 +7,9 @@ from typing import Any, Dict, List, Optional, Tuple
7
7
 
8
8
  import requests
9
9
  from aea_ledger_ethereum import EthereumApi
10
+ from web3.constants import ADDRESS_ZERO
10
11
 
11
- from mech_client.marketplace_interact import ADDRESS_ZERO, get_contract, get_mech_config
12
+ from mech_client.interact import get_contract, get_mech_config
12
13
 
13
14
 
14
15
  ABI_DIR_PATH = Path(__file__).parent / "abis"
@@ -58,7 +59,7 @@ def fetch_tools(
58
59
 
59
60
 
60
61
  def get_mech_tools(
61
- service_id: int, chain_config: str = DEFAULT_CONFIG
62
+ service_id: int, chain_config: Optional[str] = DEFAULT_CONFIG
62
63
  ) -> Optional[Dict[str, Any]]:
63
64
  """
64
65
  Fetch tools for a given mech's service ID.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mech-client
3
- Version: 0.9.0
3
+ Version: 0.10.0
4
4
  Summary: Basic client to interact with a mech
5
5
  License: Apache-2.0
6
6
  Author: David Minarsch
@@ -1,4 +1,4 @@
1
- mech_client/__init__.py,sha256=Ed2swOIv8MmttDlcU_lXntd4MDQZ75GbLzEVhuhi_3o,42
1
+ mech_client/__init__.py,sha256=zubatTm13iBIEOwOE74QFlqg6tpFL5v53x4lS6XYY0U,43
2
2
  mech_client/abis/AgentMech.json,sha256=IEbs_xBGunBu5h-uT5DvIty8Zw412QoPI46S_DUMYNw,18082
3
3
  mech_client/abis/AgentRegistry.json,sha256=2qmXeFINZWz9pyOma6Bq67kMDSUI1lD7WvgHLwuETD8,24723
4
4
  mech_client/abis/AgreementStoreManager.base.json,sha256=_ljdIZcfFGmFzBHUTfhA4X0382ZHHpkdr_CziTwUETo,34360
@@ -17,7 +17,7 @@ mech_client/abis/IMech.json,sha256=km0NMRyqBYh3jBQwPJCispsRfPwqNJ67kkZwYjuJci4,3
17
17
  mech_client/abis/IToken.json,sha256=VrzR6Rr1DmrUzy5DygN1rKm6df4ir2KGdWsunZnuRKo,19637
18
18
  mech_client/abis/LockPaymentCondition.base.json,sha256=mGOi_xYyH7gCzLyWu7nk_Ta46tpLNxoPAgbjQvKNQtA,43424
19
19
  mech_client/abis/LockPaymentCondition.gnosis.json,sha256=ofYQo6JYSE_7Q4LPHxoOZI2qFMddSWcfVoCW-6k9KnY,43424
20
- mech_client/abis/MechMarketplace.json,sha256=auKTxPTi07yD98Gz2RuwH1Gq5qRCyy8-C7QBNj3uto8,32104
20
+ mech_client/abis/MechMarketplace.json,sha256=KPnF-H_UATb3wdb_7o6ky_hSp5xwgvckD-QqylsWJLg,32468
21
21
  mech_client/abis/NFTSalesTemplate.base.json,sha256=blheE2NNxCt3ttklcsAF9WrIcnsvywAZ8jICjGVl4lQ,37995
22
22
  mech_client/abis/NFTSalesTemplate.gnosis.json,sha256=6fKoxvTSQWOGQq_Yik09R8JTMRxXIPgoOC_HSAf5A7Q,37995
23
23
  mech_client/abis/NeverminedConfig.base.json,sha256=0p_GV87fsBvlN6VNDm_T1ykjzD4yZKlR9-jDpo1_RRU,21221
@@ -62,8 +62,8 @@ mech_client/helpers/p2p_libp2p_client/__init__.py,sha256=-GOP3D_JnmXTDomrMLCbnRk
62
62
  mech_client/helpers/p2p_libp2p_client/connection.py,sha256=b5jfcUeSoNrUw8DOSTCbK4DTi-N8bf2_pdogUOz0ep0,28606
63
63
  mech_client/helpers/p2p_libp2p_client/connection.yaml,sha256=nMiHnU_dv9EFjVNqZ-0SAnoATfadJSad-JsbDvk97Mk,1790
64
64
  mech_client/interact.py,sha256=52UW5NysSTIC--APLpJde8VvrruWeYFCFzO02uRQpwc,21288
65
- mech_client/marketplace_interact.py,sha256=7K-xb4Bg9UrWs3U_xXIRibnfBjCaRG4XZyeAi-nO6BU,33591
66
- mech_client/mech_marketplace_tool_management.py,sha256=q_cXyJGI1rLXKB_Ds21eQLCzUhTYE9BHN48wqIw0w6g,7341
65
+ mech_client/marketplace_interact.py,sha256=24louKzqpgDVYmuOIwZQzIVnZwPLXhkF2LPSQVrEuvE,35554
66
+ mech_client/mech_marketplace_tool_management.py,sha256=G1O0ajbeltRM5FpqPfmn2C4QRrwqf5HfWKUH2VKn6UA,7365
67
67
  mech_client/mech_tool_management.py,sha256=NQFmVzzGZsIkeHokDPWXGHwa8u-pyQIMPR1Q5H81bKw,7806
68
68
  mech_client/prompt_to_ipfs.py,sha256=XqSIBko15MEkpWOQNT97fRI6jNxMF5EDBDEPOJFdhyk,2533
69
69
  mech_client/push_to_ipfs.py,sha256=IfvgaPU79N_ZmCPF9d7sPCYz2uduZH0KjT_HQ2LHXoQ,2059
@@ -92,8 +92,8 @@ scripts/nvm_subscription/manager.py,sha256=y0Qh0aVAmOPB4Ytt93alIarSvhrQpC-lRYNAY
92
92
  scripts/nvm_subscription/resources/networks.json,sha256=xH0P3YkgkMTkQdahVKO0kI9m6ybJ67iwHApstUlfRmw,2359
93
93
  scripts/utils.py,sha256=lXjY3s1HvNHT2fXm2fBpZtVvlQaqW288Y2S-s3rpSDM,3248
94
94
  scripts/whitelist.py,sha256=-TF4fcojBmF6a7fXleBk96DvJ-xWkNGoN0s_8r8J20Q,289
95
- mech_client-0.9.0.dist-info/LICENSE,sha256=mdBDB-mWKV5Cz4ejBzBiKqan6Z8zVLAh9xwM64O2FW4,11339
96
- mech_client-0.9.0.dist-info/METADATA,sha256=S5aSBEHrmFl-Az-VGwdqRQEtp2OQ2DN8jPPOXvYsYfM,25935
97
- mech_client-0.9.0.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
98
- mech_client-0.9.0.dist-info/entry_points.txt,sha256=SbRMRsayzD8XfNXhgwPuXEqQsdZ5Bw9XDPnUuaDExyY,45
99
- mech_client-0.9.0.dist-info/RECORD,,
95
+ mech_client-0.10.0.dist-info/LICENSE,sha256=mdBDB-mWKV5Cz4ejBzBiKqan6Z8zVLAh9xwM64O2FW4,11339
96
+ mech_client-0.10.0.dist-info/METADATA,sha256=wXMRgH_HN3Kg0yt4k9rsfySvc6XL-TmQkyqKQ-3rtto,25936
97
+ mech_client-0.10.0.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
98
+ mech_client-0.10.0.dist-info/entry_points.txt,sha256=SbRMRsayzD8XfNXhgwPuXEqQsdZ5Bw9XDPnUuaDExyY,45
99
+ mech_client-0.10.0.dist-info/RECORD,,