forked from serum-community/pyserum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add async support (serum-community#82)
* add async utils and connection * refactor market.py before adding async * add async open orders account * add async_market * add type hint * replace pytest-tornasync with pytest-asyncio * add async tests * add async tests * linting * add async exmplae to README * fix unit test selection * bump minor version number * chmod * fix keygen error when key already exists * use --cov-append * fix coverage for multi test * fix typo Co-authored-by: kevinheavey <[email protected]>
- Loading branch information
1 parent
a2f9bd6
commit 9547329
Showing
28 changed files
with
1,238 additions
and
482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import List | ||
import httpx | ||
from solana.rpc.async_api import AsyncClient as async_conn # pylint: disable=unused-import # noqa:F401 | ||
|
||
from .market.types import MarketInfo, TokenInfo | ||
from .connection import LIVE_MARKETS_URL, TOKEN_MINTS_URL, parse_live_markets, parse_token_mints | ||
|
||
|
||
async def get_live_markets(httpx_client: httpx.AsyncClient) -> List[MarketInfo]: | ||
resp = await httpx_client.get(LIVE_MARKETS_URL) | ||
return parse_live_markets(resp.json()) | ||
|
||
|
||
async def get_token_mints(httpx_client: httpx.AsyncClient) -> List[TokenInfo]: | ||
resp = await httpx_client.get(TOKEN_MINTS_URL) | ||
return parse_token_mints(resp.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
from solana.rpc.async_api import AsyncClient | ||
from solana.publickey import PublicKey | ||
from solana.rpc.types import Commitment | ||
from solana.rpc.commitment import Recent | ||
|
||
from .async_utils import load_bytes_data | ||
from .open_orders_account import _OpenOrdersAccountCore | ||
|
||
|
||
class AsyncOpenOrdersAccount(_OpenOrdersAccountCore): | ||
@classmethod | ||
async def find_for_market_and_owner( # pylint: disable=too-many-arguments | ||
cls, | ||
conn: AsyncClient, | ||
market: PublicKey, | ||
owner: PublicKey, | ||
program_id: PublicKey, | ||
commitment: Commitment = Recent, | ||
) -> List[AsyncOpenOrdersAccount]: | ||
args = cls._build_get_program_accounts_args( | ||
market=market, program_id=program_id, owner=owner, commitment=commitment | ||
) | ||
resp = await conn.get_program_accounts(*args) | ||
return cls._process_get_program_accounts_resp(resp) | ||
|
||
@classmethod | ||
async def load(cls, conn: AsyncClient, address: str) -> AsyncOpenOrdersAccount: | ||
addr_pub_key = PublicKey(address) | ||
bytes_data = await load_bytes_data(addr_pub_key, conn) | ||
return cls.from_bytes(addr_pub_key, bytes_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from solana.publickey import PublicKey | ||
from solana.rpc.async_api import AsyncClient | ||
from spl.token.constants import WRAPPED_SOL_MINT | ||
|
||
from pyserum.utils import parse_bytes_data, parse_mint_decimals | ||
|
||
|
||
async def load_bytes_data(addr: PublicKey, conn: AsyncClient) -> bytes: | ||
res = await conn.get_account_info(addr) | ||
return parse_bytes_data(res) | ||
|
||
|
||
async def get_mint_decimals(conn: AsyncClient, mint_pub_key: PublicKey) -> int: | ||
"""Get the mint decimals for a token mint""" | ||
if mint_pub_key == WRAPPED_SOL_MINT: | ||
return 9 | ||
|
||
bytes_data = await load_bytes_data(mint_pub_key, conn) | ||
return parse_mint_decimals(bytes_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
from typing import List | ||
from typing import List, Dict, Any | ||
|
||
from solana.rpc.api import Client as conn # pylint: disable=unused-import # noqa:F401 | ||
from solana.rpc.providers.http import requests | ||
import requests | ||
|
||
from solana.rpc.api import Client as conn # pylint: disable=unused-import # noqa:F401 | ||
from solana.publickey import PublicKey | ||
from .market.types import MarketInfo, TokenInfo | ||
|
||
LIVE_MARKETS_URL = "https://raw.githubusercontent.com/project-serum/serum-ts/master/packages/serum/src/markets.json" | ||
TOKEN_MINTS_URL = "https://raw.githubusercontent.com/project-serum/serum-ts/master/packages/serum/src/token-mints.json" | ||
|
||
def get_live_markets() -> List[MarketInfo]: | ||
url = "https://raw.githubusercontent.com/project-serum/serum-ts/master/packages/serum/src/markets.json" | ||
|
||
def parse_live_markets(data: List[Dict[str, Any]]) -> List[MarketInfo]: | ||
return [ | ||
MarketInfo(name=m["name"], address=m["address"], program_id=m["programId"]) | ||
for m in requests.get(url).json() | ||
if not m["deprecated"] | ||
MarketInfo(name=m["name"], address=m["address"], program_id=m["programId"]) for m in data if not m["deprecated"] | ||
] | ||
|
||
|
||
def parse_token_mints(data: List[Dict[str, str]]) -> List[TokenInfo]: | ||
return [TokenInfo(name=t["name"], address=PublicKey(t["address"])) for t in data] | ||
|
||
|
||
def get_live_markets() -> List[MarketInfo]: | ||
return parse_live_markets(requests.get(LIVE_MARKETS_URL).json()) | ||
|
||
|
||
def get_token_mints() -> List[TokenInfo]: | ||
url = "https://raw.githubusercontent.com/project-serum/serum-ts/master/packages/serum/src/token-mints.json" | ||
return [TokenInfo(**t) for t in requests.get(url).json()] | ||
return parse_token_mints(requests.get(TOKEN_MINTS_URL).json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .market import Market # noqa: F401 | ||
from .async_market import AsyncMarket # noqa: F401 | ||
from .orderbook import OrderBook # noqa: F401 | ||
from .state import MarketState as State # noqa: F401 |
Oops, something went wrong.