I'm working on a DeFi price bot for python practice. I thought my code had been bugged, when it calculated the price of WETH to ADS.
After auditing my code and realizing it was calculating the correct way, I went further up in the chain and got that the data coming from Uniswap's Graph QL is actually where the error is. Here is the info being given to me by GraphQL, relating to the price of ADS/WETH
{
"id": "0xb9044f46dcdea7ecebbd918a9659ba8239bd9f37",
"totalValueLockedETH": "9904.271380202576136314291146113488",
"token0Price": "0.001320257312519524061776624436378727",
"token1Price": "757.4281092915453379114232503540107",
"feeTier": "10000",
"token0": {
"id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"symbol": "WETH",
"name": "Wrapped Ether",
"decimals": "18"
},
"token1": {
"id": "0xcfcecfe2bd2fed07a9145222e8a7ad9cf1ccd22a",
"symbol": "ADS",
"name": "Adshares",
"decimals": "11"
}
},
As you can see in token0Price, we are being given "0.0013..." for the price.
However, manually selecting ADS -> WETH in Uniswap's dapp, gives the price: "0.00078".
Am I using GraphQL wrong? Or is this an issue on the Uniswap side?
Related
I need to create a "bundled" item that points to items I reference by item_id. In other terms, this is an 'un-stockable' item that is not tracked by inventory rather when it is sold it deducts the inventory of only the linked item/variations. I cannot figure this out using Square's Endpoint API. So far I have:
result = client.catalog.upsert_catalog_object(
body={
"idempotency_key": str(idempotency_key),
"object": {
"type": "ITEM",
"id": "#Cocoa_bundle",
"item_data": {
"name": "Cocoa Bundle",
"description": "Hot Chocolate Bundle of Small and Large sizes",
"abbreviation": "CB",
"item_variation_ids": ["H6EK5FZ4MMJB56FFASFNYZXC", "NZGVV7TXJHVUMNQAY743FRQX"],
"track_inventory": False,
}
}
}
)
And I recieve:
"category": "INVALID_REQUEST_ERROR",
"code": "BAD_REQUEST",
"detail": "Item with name Cocoa Bundle and token #Cocoa_bundle must have at least one variation."
However, this will create a new "variation" of an item rather than creating a bundle. It needs to be an item linked to the variations: 'item_variation_ids'. Is it possible to create a bundle as such?
I'm trying to read from a JSON file, but I get this error: TypeError: unhashable type: 'dict'
import pandas as pd
osp_json_path = 'Data/syllabi.sample.json'
df_osp_data = pd.read_json(osp_json_path)
print(df.head())
Here is some of the sample of the JSON file:
[
{
"id": 17308718202881,
"syllabus_probability": 0.6546372771263123,
"date": {
"year": 2016,
"term": null
},
"field": {
"code": "11",
"name": "Computer Science",
"label_precision": 0.7397769689559937,
"label_recall": 0.8223140239715576,
"label_f1": 0.7788649797439575
},
"institution": {
"id": 3994319585322,
"grid_id": "grid.7112.5",
"wikidata_id": "Q1783765",
"unitid": null,
"name": "Mendel University Brno",
"url": "http:\/\/mendelu.cz\/en\/",
"lat": 49.210208892822266
},
"extracted_metadata": {
"code": {
"text": "VT1",
"clean_text": "VT1",
"mean_p": 0.9811509251594543
},
"title": {
"text": "Computer Technology I",
"clean_text": "Computer Technology I",
"mean_p": 0.9993407726287842
},
"date": {
"text": "SS 2016\/2017",
"clean_text": "SS 2016\/2017",
"mean_p": 0.9996379613876343
},
"description": [
{
"text": "The aim of this course is to introduce students into the subject of computer science and data processing and to explain basic principles of computer operations. The part relating to operating systems is centered on operations with computer files and processes in systems Windows and Unix\/Linux.",
"clean_text": "The aim of this course is to introduce students into the subject of computer science and data processing and to explain basic principles of computer operations. The part relating to operating systems is centered on operations with computer files and processes in systems Windows and Unix\/Linux.",
"mean_p": 0.9988000392913818
}
]
},
"text_md5": "18443cb0375d7ba646c5ac203aac7380",
"mime_type": "text\/xml",
"anonymized_text": "Department of Informatics (FBE) Time allowance: full-time, period A\u00a02\/0, period B 2\/2 (hours of lectures per week \/ hours of seminars per week) Prerequisites for registration: not Computer Technology and Algorithms I and not Final Bachelor Exam Type of study: consulting Form of teaching: lecture, seminar Mode of completion and credits: Fulfillment of requirements (2 credits) \u00a0 Course objective: The aim of this course is to introduce students into the subject of computer science and data processing and to explain basic principles of computer operations. The part relating to operating systems is centered on operations with computer files and processes in systems Windows and Unix\/Linux. \u00a0 Course content: 1. Introduction to computer science (allowance 2\/0) \u00a0 a. Basic concepts"
}
Is there a way to avoid this TypeError? Also, not all fields are necessary, only "id" and "anonymized_text" are going to be used, so filtering some keys is also a possibility.
I'm having something strange happen with the Robinhood API. Specifically with getting all of the options instruments (just data about the options). The code below is part of my program
def get_options_instruments(self):
params = {
"chain_symbol" : "AMD",
"chain_id" : "e66ce029-db96-4572-87a0-b144613c08bf",
"type": "call",
"state": "active",
"tradability": "tradable",
"strike_price" : "16.0000",
"expiration_date": "2018-10-19"
}
#API_URLS['option-instrument']= "https://api.robinhood.com/options/instruments/"
response = self.login_session.get(API_URLS['option-instrument'],params = params)
response = response.json()["results"]
print(json.dumps(response,indent = 4,separators=(',', ': ')))#'option-instrument' : "https://api.robinhood.com/options/instruments/",
All parameters seem to have an effect when receiving the option instruments EXCEPT expiration date (which is the most one of the important parameters that I need to use). Below is a sample response of an option instrument.
{
"issue_date": "1987-01-12",
"strike_price": "16.0000",
"url": "https://api.robinhood.com/options/instruments/3cb75cca-0987-46d7-bff1-20cadfb74a83/",
"expiration_date": "2018-07-20",
"tradability": "tradable",
"chain_id": "e66ce029-db96-4572-87a0-b144613c08bf",
"updated_at": "2018-06-03T00:16:56.985489Z",
"min_ticks": {
"cutoff_price": "3.00",
"below_tick": "0.01",
"above_tick": "0.05"
},
"state": "active",
"id": "3cb75cca-0987-46d7-bff1-20cadfb74a83",
"chain_symbol": "AMD",
"type": "call",
"created_at": "2017-11-18T04:15:17.795113Z"
}
I'm just wondering if anybody has any idea why something like this might happen? Could it perhaps be something on the API's side and not mine? Thank you.
Use "expiration_dates" instead and it will work.
I encountered this issue recently as well, where specifying parameters for the "expiration_date" field yielded nothing.
I have a response that I receive from foursquare in the form of json. I have tried to access the certain parts of the object but have had no success. How would I access say the address of the object? Here is my code that I have tried.
url = 'https://api.foursquare.com/v2/venues/explore'
params = dict(client_id=foursquare_client_id,
client_secret=foursquare_client_secret,
v='20170801', ll=''+lat+','+long+'',
query=mealType, limit=100)
resp = requests.get(url=url, params=params)
data = json.loads(resp.text)
msg = '{} {}'.format("Restaurant Address: ",
data['response']['groups'][0]['items'][0]['venue']['location']['address'])
print(msg)
Here is an example of json response:
"items": [
{
"reasons": {
"count": 0,
"items": [
{
"summary": "This spot is popular",
"type": "general",
"reasonName": "globalInteractionReason"
}
]
},
"venue": {
"id": "412d2800f964a520df0c1fe3",
"name": "Central Park",
"contact": {
"phone": "2123106600",
"formattedPhone": "(212) 310-6600",
"twitter": "centralparknyc",
"instagram": "centralparknyc",
"facebook": "37965424481",
"facebookUsername": "centralparknyc",
"facebookName": "Central Park"
},
"location": {
"address": "59th St to 110th St",
"crossStreet": "5th Ave to Central Park West",
"lat": 40.78408342593807,
"lng": -73.96485328674316,
"labeledLatLngs": [
{
"label": "display",
"lat": 40.78408342593807,
"lng": -73.96485328674316
}
],
the full response can be found here
Like so
addrs=data['items'][2]['location']['address']
Your code (at least as far as loading and accessing the object) looks correct to me. I loaded the json from a file (since I don't have your foursquare id) and it worked fine. You are correctly using object/dictionary keys and array positions to navigate to what you want. However, you mispelled "address" in the line where you drill down to the data. Adding the missing 'a' made it work. I'm also correcting the typo in the URL you posted.
I answered this assuming that the example JSON you linked to is what is stored in data. If that isn't the case, a relatively easy way to see exact what python has stored in data is to import pprint and use it like so: pprint.pprint(data).
You could also start an interactive python shell by running the program with the -i switch and examine the variable yourself.
data["items"][2]["location"]["address"]
This will access the address for you.
You can go to any level of nesting by using integer index in case of an array and string index in case of a dict.
Like in your case items is an array
#items[int index]
items[0]
Now items[0] is a dictionary so we access by string indexes
item[0]['location']
Now again its an object s we use string index
item[0]['location']['address]
I have a small API that i'm working on, everything works ok, all my requests do what they are supposed to but when I try to filter results through the URL query for some reason it works for id but not for device field.
def on_get(self, req, resp):
"""Handles GET requests"""
if req.get_param("id"):
result = {'location': r.db(PROJECT_DB).table(PROJECT_TABLE).get(req.get_param("id")).run(db_connection)}
elif req.get_param("device"):
result = {'location': r.db(PROJECT_DB).table(PROJECT_TABLE).get(req.get_param("device")).run(db_connection)}
else:
location = r.db(PROJECT_DB).table(PROJECT_TABLE).run(db_connection)
result = {'locations': [i for i in location]}
resp.body = json.dumps(result)
example http://localhost:8000/location?id=(some random id) this will work
but if i do http://localhost:8000/location?device=(some device) this will not work, returns null
So could anyone tell me what am I doing wrong? or better yet if anyone knows a better way to filter using the URL?
Note: I am using rethinkdb
EDIT:
This is what I have normally:
{
"locations": [
{
"id": "4bf4b94f-747a-42db-9d54-a8399d995025",
"location": "gps coords",
"device": "Device 2"
},
{
"id": "b5cce561-37d2-42e7-86e4-a31c008b0af2",
"location": "gps coords",
"device": "Device 1"
},
{
"id": "bebba7cf-710c-4ee8-ad69-2d58174d4e02",
"location": "gps coords",
"device": "Device 1"
},
{
"id": "e928f84b-60ff-40f3-b839-920bc99e5480",
"location": "gps coords",
"device": "Device1"
}
]
}
Filtering by id works ok, but not by device which is weird
I found the answer to this problem, the reason why it did not war was because rethinkdb only gets via primary key on the get query
result = {'location': r.db(PROJECT_DB).table(PROJECT_TABLE).get(req.get_param("device")).run(db_connection)}
so what I should have done was to filter the results by what I wanted like this and it would have worked
result = {'location': list(r.db(PROJECT_DB).table(PROJECT_TABLE).filter({'device': param}).run(db_connection))}
Thanks for the help everyone and hope this answer helps.