Simplify get_live_markets

This commit is contained in:
michaelhly 2020-10-29 23:24:09 -05:00
parent da4d62ff43
commit 734b372c33
2 changed files with 7 additions and 27 deletions

View File

@ -22,7 +22,7 @@ print(get_live_markets())
```
The source of truth of the market address can be found
[here](https://github.com/project-serum/serum-js/blob/master/src/tokens_and_markets.ts).
[here](https://github.com/project-serum/serum-js/blob/master/src/markets.json).
### Get Orderbook

View File

@ -1,5 +1,3 @@
import json
from solana.rpc.api import Client as conn # pylint: disable=unused-import # noqa:F401
from solana.rpc.providers.http import requests
@ -7,27 +5,9 @@ from .market.types import MarketInfo
def get_live_markets():
url = "https://raw.githubusercontent.com/project-serum/serum-js/master/src/tokens_and_markets.ts"
resp = requests.get(url)
page = resp.text
# Turn this JS into json
data = page.split("MARKETS:")[1].split("}> = ")[1].split(";")[0]
data = data.replace(" new PublicKey(", "").replace(")", "")
for col in ["name", "address", "programId", "deprecated"]:
data = data.replace(col, '"{}"'.format(col))
data = data.replace("'", '"')
data = data.replace(" ", "")
data = data.replace("//NewUSDCmarkets", "")
data = data.replace("\n", "")
data = data.replace(",}", "}")
data = data.replace(",]", "]")
data = json.loads(data)
markets = []
for raw in data:
if raw["deprecated"]:
continue
markets.append(MarketInfo(name=raw["name"], address=raw["address"], program_id=raw["programId"]))
return markets
url = "https://raw.githubusercontent.com/project-serum/serum-js/master/src/markets.json"
return [
MarketInfo(name=m["name"], address=m["address"], program_id=m["programId"])
for m in requests.get(url).json()
if not m["deprecated"]
]