mezoAgent 0.3.0__tar.gz → 0.3.1__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {mezoagent-0.3.0 → mezoagent-0.3.1}/PKG-INFO +1 -1
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezoAgent.egg-info/PKG-INFO +1 -1
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezo_agent/config.py +11 -5
- mezoagent-0.3.1/mezo_agent/swap_musd_btc.py +112 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/setup.py +1 -1
- mezoagent-0.3.0/mezo_agent/swap_musd_btc.py +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/README.md +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezoAgent.egg-info/SOURCES.txt +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezoAgent.egg-info/dependency_links.txt +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezoAgent.egg-info/requires.txt +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezoAgent.egg-info/top_level.txt +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezo_agent/__init__.py +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezo_agent/parsing.py +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/mezo_agent/transaction.py +0 -0
- {mezoagent-0.3.0 → mezoagent-0.3.1}/setup.cfg +0 -0
@@ -6,14 +6,20 @@ from web3 import Web3
|
|
6
6
|
# Load environment variables
|
7
7
|
load_dotenv()
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
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"
|
@@ -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()}"
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|