-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
189 lines (158 loc) · 6.83 KB
/
test.py
File metadata and controls
189 lines (158 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import requests
import time
from decimal import Decimal
import json
import yfinance as yf
from colorama import Fore
from mcp.server.fastmcp import FastMCP
import pandas as pd
from datetime import datetime, timedelta
import time
import requests
from decimal import Decimal
import json
import mcp
def condition_Token_swap(tokenInaddress, tokenOutaddress, minPrice, swapamount, signerAddress, targetChain, tokenInDemical, slippage):
# Configuration
AggregatorDomain = "https://aggregator-api.kyberswap.com"
gettargetPath = f"/{targetChain}/api/v1/routes"
posttargetPath = f"/{targetChain}/api/v1/route/build"
# Swap parameters
POLL_INTERVAL = 2 # seconds between checks
max_attempts = 100 # max attempts before giving up (30 attempts = ~1 minute)
# Calculate amount in smallest units
amount_in = int(swapamount * (10 ** tokenInDemical))
# Monitoring loop
for attempt in range(1, max_attempts + 1):
try:
# Get current price quote
response = requests.get(
f"{AggregatorDomain}{gettargetPath}",
params={
"tokenIn": tokenInaddress,
"tokenOut": tokenOutaddress,
"amountIn": str(amount_in)
},
timeout=10
)
response.raise_for_status()
data = response.json()
# Check response structure
if not data.get('data', {}).get('routeSummary'):
print(f"[Attempt {attempt}] Unexpected response format")
continue
current_price = int(data['data']['routeSummary']['amountOut'])
print(f"[Attempt {attempt}] Current output: {current_price}, Target: {minPrice}")
# Check if price meets our condition
if current_price >= minPrice:
print("Price condition met! Building swap transaction...")
# Build swap transaction
swap_response = requests.post(
f"{AggregatorDomain}{posttargetPath}",
json={
"routeSummary": data['data']['routeSummary'],
"sender": signerAddress,
"recipient": signerAddress,
"slippageTolerance": slippage
},
timeout=10
)
swap_response.raise_for_status()
swap_data = swap_response.json()
print("Swap transaction built successfully!")
return swap_data['data']
except requests.exceptions.RequestException as error:
print(f"Attempt {attempt} failed:", str(error))
if hasattr(error, 'response') and error.response:
print("Error details:", error.response.text)
time.sleep(POLL_INTERVAL)
print(f"Failed after {max_attempts} attempts. Price condition not met.")
return None
def token_swap(tokenInaddress, tokenOutaddress, swapamount, signerAddress, targetChain, tokenInDemical, slippage):
"""This tool executes token swaps without regard to price"""
AggregatorDomain = "https://aggregator-api.kyberswap.com"
gettargetPath = f"/{targetChain}/api/v1/routes"
posttargetPath = f"/{targetChain}/api/v1/route/build"
amount_in = int(swapamount * (10 ** tokenInDemical))
try:
response = requests.get(
f"{AggregatorDomain}{gettargetPath}",
params={
"tokenIn": tokenInaddress,
"tokenOut": tokenOutaddress,
"amountIn": str(amount_in)
},
timeout=10
)
response.raise_for_status()
data = response.json()
swap_response = requests.post(
f"{AggregatorDomain}{posttargetPath}",
json={
"routeSummary": data['data']['routeSummary'],
"sender": signerAddress,
"recipient": signerAddress,
"slippageTolerance": slippage
},
timeout=10
)
swap_response.raise_for_status()
swap_data = swap_response.json()
print("Swap transaction built successfully!")
return swap_data['data']
except requests.exceptions.RequestException as error:
print(str(error))
def limit_order(tokenInaddress, tokenOutaddress, AmountIn, AmountOut, ChainID):
SignerAddress = "0x42c0682214BF0FdCac4FB29fc509FB636537396E"
AmountIn = Decimal(AmountIn)
AmountOut = Decimal(AmountOut)
makingAmount = str(int(AmountIn * (10 ** 18)))
takingAmount = str(int(AmountOut * (10 ** 6)))
try:
# First request to get signature data
response = requests.post(
"https://limit-order.kyberswap.com/write/api/v1/orders/sign-message",
headers={"Content-Type":"application/json"},
data=json.dumps({
"chainId": str(ChainID),
"makerAsset": tokenInaddress,
"takerAsset": tokenOutaddress,
"maker": SignerAddress,
"makingAmount": makingAmount,
"takingAmount": takingAmount,
"expiredAt": int(time.time()) + 3600}) # Expires in 1 hour
)
response.raise_for_status()
signdata = response.json()
print('Response:', signdata)
"""
# SIGN the response with wallet (commented out as in original)
# This would require web3.py implementation
from web3 import Web3
from eth_account.messages import encode_structured_data
w3 = Web3(Web3.HTTPProvider('YOUR_PROVIDER_URL'))
signed_message = w3.eth.account.sign_typed_data(
private_key='YOUR_PRIVATE_KEY',
domain_data=signdata['domain'],
message_types=signdata['types'],
message_data=signdata['message']
)
# Second request to submit the signed order
order_response = requests.post(
'https://limit-order.kyberswap.com/write/api/v1/orders',
headers={'Content-Type': 'application/json'},
data=json.dumps({
**signdata,
"signature": signed_message.signature.hex()
})
)
print("Order created:", order_response.json())
"""
except requests.exceptions.RequestException as error:
print('Error:', error.response.text if error.response else str(error))
# Example usage
if __name__ == "__main__":
tokenInaddress = "0x4200000000000000000000000000000000000006"
tokenOutaddress = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
TargetChain =8453
result = limit_order(tokenInaddress,tokenOutaddress, '0.001', '1.7', TargetChain)