mezoAgent 0.3.0__py3-none-any.whl → 0.3.3__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.1
2
+ Name: mezoAgent
3
+ Version: 0.3.3
4
+ Summary: A Python package for Mezo Agent transactions with LangChain tools
5
+ Author: Dreadwulf, Duck, Digi
6
+ Requires-Dist: python-dotenv
7
+ Requires-Dist: web3
8
+ Requires-Dist: langchain
9
+ Requires-Dist: langchain_openai
@@ -0,0 +1,9 @@
1
+ mezo_agent/__init__.py,sha256=XOJWLvrxgH7jBkKdXFLVGLV2dmCyyRpo8VcjicWBD0I,133
2
+ mezo_agent/config.py,sha256=D5lNOK34HFi_ZT7rAKxbf15j3GG6K90dCKltKeklF00,2164
3
+ mezo_agent/parsing.py,sha256=rolPVE8r_RS6BqKe25r1nyZQ47rvTPG2Hv09Kxxfv4c,2821
4
+ mezo_agent/swap_musd_btc.py,sha256=Co-XcfK73spm94VC6qzGY0VYoGwsMDKKHM8ToGh2JxU,4289
5
+ mezo_agent/transaction.py,sha256=YpfWrkEaf0YGM_Kc4cFwlT9GmBGZkeJHWm0VGHs9Gks,4199
6
+ mezoAgent-0.3.3.dist-info/METADATA,sha256=dS7wLMopH2QBboolv6V_X1w8eJUY40l5d1Q9_OUZ9iA,273
7
+ mezoAgent-0.3.3.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
8
+ mezoAgent-0.3.3.dist-info/top_level.txt,sha256=rrAci_NyTR9z6w_BrQhQrAhzMW_A0NYhVa0x2USl0nQ,11
9
+ mezoAgent-0.3.3.dist-info/RECORD,,
mezo_agent/config.py CHANGED
@@ -6,14 +6,20 @@ from web3 import Web3
6
6
  # Load environment variables
7
7
  load_dotenv()
8
8
 
9
- # Check for keys
10
- OpenAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
- if not OpenAI_API_KEY:
12
- raise ValueError("OPENAI_API_KEY not found in environment variables!")
9
+ USER_PROJECT_DIR = os.getcwd()
10
+ USER_ENV_PATH = os.path.join(USER_PROJECT_DIR, ".env")
11
+
12
+ if os.path.exists(USER_ENV_PATH):
13
+ load_dotenv(USER_ENV_PATH)
14
+ else:
15
+ print("⚠️ Warning: No `.env` file found in your project directory! Transactions requiring signing may fail.")
13
16
 
14
17
  PRIVATE_KEY = os.getenv("PRIVATE_KEY")
18
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
19
+
15
20
  if not PRIVATE_KEY:
16
- raise ValueError("PRIVATE_KEY not found in environment variables!")
21
+ print("⚠️ Warning: PRIVATE_KEY not set. Please create a `.env` file in your project with your keys.")
22
+ PRIVATE_KEY = None # Allow package to be installed but prevent transactions
17
23
 
18
24
  # Mezo Testnet RPC and Web3 initialization
19
25
  RPC_URL = "https://rpc.test.mezo.org"
@@ -38,7 +44,8 @@ WRAPPED_BTC_ADDRESS = "0xA460F83cdd9584E4bD6a9838abb0baC58EAde999"
38
44
  ROUTER_ADDRESS = "0xC2E61936a542D78b9c3AA024fA141c4C632DF6c1"
39
45
 
40
46
  # Load router ABI from a JSON file packaged with mezoAgent
41
- router_abi_path = os.path.join(os.path.dirname(__file__), "new_router_abi.json")
47
+ router_abi_path = os.path.join(os.path.dirname(__file__), "data", "new_router_abi.json")
42
48
  with open(router_abi_path, "r") as f:
43
49
  router_abi = json.load(f)
50
+
44
51
  router_contract = web3_instance.eth.contract(address=ROUTER_ADDRESS, abi=router_abi)
@@ -0,0 +1,112 @@
1
+ import time
2
+ from langchain.tools import tool
3
+ from .config import (
4
+ web3_instance,
5
+ sender_address,
6
+ account,
7
+ PRIVATE_KEY,
8
+ musd_contract,
9
+ ROUTER_ADDRESS,
10
+ router_contract,
11
+ MUSD_ADDRESS,
12
+ WRAPPED_BTC_ADDRESS
13
+ )
14
+ from .parsing import extract_swap_details
15
+
16
+ def approve_if_needed(token_contract, amount_wei):
17
+ """
18
+ Checks if the router has enough allowance to spend tokens.
19
+ If not, sends an approval transaction.
20
+ """
21
+ current_allowance = token_contract.functions.allowance(sender_address, ROUTER_ADDRESS).call()
22
+ if current_allowance < amount_wei:
23
+ print(f"Current allowance ({current_allowance}) is less than required ({amount_wei}). Approving...")
24
+ nonce = web3_instance.eth.get_transaction_count(sender_address)
25
+ gas_price = web3_instance.eth.gas_price
26
+ approve_tx = token_contract.functions.approve(ROUTER_ADDRESS, amount_wei).build_transaction({
27
+ "from": sender_address,
28
+ "nonce": nonce,
29
+ "gas": 50000,
30
+ "gasPrice": gas_price,
31
+ })
32
+ signed_tx = web3_instance.eth.account.sign_transaction(approve_tx, PRIVATE_KEY)
33
+ tx_hash = web3_instance.eth.send_raw_transaction(signed_tx.raw_transaction)
34
+ receipt = web3_instance.eth.wait_for_transaction_receipt(tx_hash)
35
+ if receipt.status != 1:
36
+ raise Exception("Approval transaction failed.")
37
+ print(f"Approval successful. TX Hash: {tx_hash.hex()}")
38
+ else:
39
+ print("Sufficient allowance already set.")
40
+
41
+ @tool
42
+ def mezo_agent_swap_musd_btc(prompt: str) -> str:
43
+ """
44
+ Swaps mUSD for Wrapped BTC using the Dumpy Swap router.
45
+
46
+ This tool extracts swap details from the prompt, approves the router if needed,
47
+ and executes the swap transaction.
48
+
49
+ The prompt should specify the mUSD amount to swap.
50
+ """
51
+ swap_details = extract_swap_details(prompt)
52
+ if isinstance(swap_details, str):
53
+ return swap_details
54
+
55
+ try:
56
+ amount_musd = float(swap_details["amount"])
57
+ except KeyError as e:
58
+ return f"❌ Missing key in swap details: {str(e)}"
59
+
60
+ # For demonstration, we use a dummy minimum Wrapped BTC amount.
61
+ min_wrapped_btc = 0.000000000000001 # in BTC
62
+
63
+ # Convert amounts to Wei (assuming 18 decimals)
64
+ amount_musd_wei = int(amount_musd * 10**18)
65
+ min_wrapped_btc_wei = int(min_wrapped_btc * 10**18)
66
+ deadline = int(time.time()) + 600 # 10 minutes from now
67
+
68
+ # Approve the router to spend mUSD if needed
69
+ try:
70
+ approve_if_needed(musd_contract, amount_musd_wei)
71
+ except Exception as e:
72
+ return f"❌ Approval failed: {str(e)}"
73
+
74
+ # Define the swap path: mUSD -> Wrapped BTC
75
+ path = [MUSD_ADDRESS, WRAPPED_BTC_ADDRESS]
76
+
77
+ nonce = web3_instance.eth.get_transaction_count(sender_address)
78
+ gas_price = web3_instance.eth.gas_price
79
+
80
+ try:
81
+ swap_tx = router_contract.functions.swapExactTokensForTokens(
82
+ amount_musd_wei, # mUSD amount
83
+ min_wrapped_btc_wei, # Minimum Wrapped BTC to receive
84
+ path, # Swap path
85
+ sender_address, # Recipient address
86
+ deadline # Transaction deadline
87
+ ).build_transaction({
88
+ "from": sender_address,
89
+ "nonce": nonce,
90
+ "gasPrice": gas_price,
91
+ })
92
+ except Exception as e:
93
+ return f"❌ Failed to build swap transaction: {str(e)}"
94
+
95
+ # Estimate gas and add a buffer
96
+ try:
97
+ estimated_gas = web3_instance.eth.estimate_gas(swap_tx)
98
+ swap_tx["gas"] = estimated_gas + 10000
99
+ except Exception as e:
100
+ swap_tx["gas"] = 250000 # Fallback gas limit
101
+
102
+ try:
103
+ signed_swap_tx = web3_instance.eth.account.sign_transaction(swap_tx, PRIVATE_KEY)
104
+ tx_hash = web3_instance.eth.send_raw_transaction(signed_swap_tx.raw_transaction)
105
+ except Exception as e:
106
+ return f"❌ Swap transaction failed: {str(e)}"
107
+
108
+ receipt = web3_instance.eth.wait_for_transaction_receipt(tx_hash)
109
+ if receipt.status != 1:
110
+ return f"❌ Swap transaction failed. Receipt: {receipt}"
111
+
112
+ return f"✅ Swap Successful! TX Hash: {tx_hash.hex()}"
@@ -1,10 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: mezoAgent
3
- Version: 0.3.0
4
- Summary: A LangChain based tool kit for AI Agents to send BTC and mUSD transactions on Mezo Matsnet.
5
- Author: Dreadwulf, Duck, Digi
6
- Requires-Python: >=3.7
7
- Requires-Dist: web3
8
- Requires-Dist: langchain
9
- Requires-Dist: langchain-openai
10
- Requires-Dist: python-dotenv
@@ -1,9 +0,0 @@
1
- mezo_agent/__init__.py,sha256=XOJWLvrxgH7jBkKdXFLVGLV2dmCyyRpo8VcjicWBD0I,133
2
- mezo_agent/config.py,sha256=2nwNy9Qq1e2y8ISg4uoQUjTChUxfVSuY7CV7ipX8xrQ,1863
3
- mezo_agent/parsing.py,sha256=rolPVE8r_RS6BqKe25r1nyZQ47rvTPG2Hv09Kxxfv4c,2821
4
- mezo_agent/swap_musd_btc.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- mezo_agent/transaction.py,sha256=YpfWrkEaf0YGM_Kc4cFwlT9GmBGZkeJHWm0VGHs9Gks,4199
6
- mezoAgent-0.3.0.dist-info/METADATA,sha256=t5yFwxj1wnYTvmljOIB6Az02BAXDE0vjk1igQAQb0EU,323
7
- mezoAgent-0.3.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
8
- mezoAgent-0.3.0.dist-info/top_level.txt,sha256=rrAci_NyTR9z6w_BrQhQrAhzMW_A0NYhVa0x2USl0nQ,11
9
- mezoAgent-0.3.0.dist-info/RECORD,,