|
| 1 | + |
| 2 | +""" DESCRIPTION |
| 3 | +The code below makes use of the requests library to perform API requests and retrieve data from the UniSwap protocol. Here are the comments explaining each part of the code: |
| 4 | +
|
| 5 | + The code starts by setting the parameters for the UniSwap transaction request. The uniSwap_api_url variable contains the API endpoint URL, and the uniSwap_query variable stores the GraphQL query string to retrieve the desired transactions. |
| 6 | +
|
| 7 | + The requests.post function is used to send a POST request to the UniSwap API endpoint, passing the GraphQL query as the request payload. The response is then stored in the uniSwap_response variable. |
| 8 | +
|
| 9 | + The JSON response data is extracted using the json() method and accessed to retrieve the swaps data. |
| 10 | +
|
| 11 | + The UniSwap transactions are then printed by iterating over the uniSwap_data list and printing each swap object. |
| 12 | +
|
| 13 | + After that, the code sets the parameters for the pending UniSwap transactions request. The uniSwap_query_pending variable contains the GraphQL query string to retrieve pending transactions. |
| 14 | +
|
| 15 | + The pending UniSwap transactions request is executed similar to the previous request, and the response is stored in the uniSwap_response_pending variable. |
| 16 | +
|
| 17 | + The JSON response data is extracted and accessed to retrieve the transactions data. |
| 18 | +
|
| 19 | + Finally, the pending UniSwap transactions are printed by iterating over the uniSwap_data_pending list and printing each transaction object. |
| 20 | +
|
| 21 | +""" |
| 22 | +import requests |
| 23 | +# Setting parameters for UniSwap transaction request |
| 24 | +uniSwap_api_url = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2' |
| 25 | +uniSwap_query = """ |
| 26 | +query { |
| 27 | + swaps(first: 10, orderBy: timestamp, orderDirection: desc, where: { pair: "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599" }) { |
| 28 | + transaction { |
| 29 | + id |
| 30 | + timestamp |
| 31 | + gasPrice |
| 32 | + } |
| 33 | + amount0In |
| 34 | + amount0Out |
| 35 | + amount1In |
| 36 | + amount1Out |
| 37 | + amountUSD |
| 38 | + pair { |
| 39 | + token0 { |
| 40 | + symbol |
| 41 | + } |
| 42 | + token1 { |
| 43 | + symbol |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +""" |
| 49 | + |
| 50 | +# Executing the UniSwap transaction request |
| 51 | +uniSwap_response = requests.post(uniSwap_api_url, json={'query': uniSwap_query}) |
| 52 | +uniSwap_data = uniSwap_response.json()['data']['swaps'] |
| 53 | + |
| 54 | +# Printing UniSwap transactions |
| 55 | +print('UniSwap Transactions:') |
| 56 | +for swap in uniSwap_data: |
| 57 | + print(swap) |
| 58 | + print('-------------------------------------------') |
| 59 | + |
| 60 | +# Setting parameters for pending UniSwap transactions request |
| 61 | +uniSwap_query_pending = """ |
| 62 | +query { |
| 63 | + transactions(first: 10, orderBy: timestamp, orderDirection: desc, where: {status: "pending"}) { |
| 64 | + id |
| 65 | + from |
| 66 | + to |
| 67 | + gasPrice |
| 68 | + gas |
| 69 | + value |
| 70 | + input |
| 71 | + timestamp |
| 72 | + } |
| 73 | +} |
| 74 | +""" |
| 75 | + |
| 76 | +# Executing the pending UniSwap transactions request |
| 77 | +uniSwap_response_pending = requests.post(uniSwap_api_url, json={'query': uniSwap_query_pending}) |
| 78 | +uniSwap_data_pending = uniSwap_response_pending.json()['data']['transactions'] |
| 79 | + |
| 80 | +# Printing pending UniSwap transactions |
| 81 | +print('Pending UniSwap Transactions:') |
| 82 | +for tx in uniSwap_data_pending: |
| 83 | + print(tx) |
| 84 | + print('-------------------------------------------') |
| 85 | + |
0 commit comments