Add balance and assets retreival methods, cache assets on startup
Pass config dict into client and assign to `.conf`.kraken_ws_orders
parent
b96b7a8b9c
commit
54008a1976
|
@ -29,8 +29,7 @@ from typing import (
|
||||||
)
|
)
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# import trio
|
from bidict import bidict
|
||||||
# import tractor
|
|
||||||
import pendulum
|
import pendulum
|
||||||
import asks
|
import asks
|
||||||
from fuzzywuzzy import process as fuzzy
|
from fuzzywuzzy import process as fuzzy
|
||||||
|
@ -40,6 +39,7 @@ import urllib.parse
|
||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
import base64
|
import base64
|
||||||
|
import trio
|
||||||
|
|
||||||
from piker import config
|
from piker import config
|
||||||
from piker.brokers._util import (
|
from piker.brokers._util import (
|
||||||
|
@ -143,9 +143,11 @@ class Client:
|
||||||
|
|
||||||
# global symbol normalization table
|
# global symbol normalization table
|
||||||
_ntable: dict[str, str] = {}
|
_ntable: dict[str, str] = {}
|
||||||
|
_atable: bidict[str, str] = bidict()
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
config: dict[str, str],
|
||||||
name: str = '',
|
name: str = '',
|
||||||
api_key: str = '',
|
api_key: str = '',
|
||||||
secret: str = ''
|
secret: str = ''
|
||||||
|
@ -156,6 +158,7 @@ class Client:
|
||||||
'User-Agent':
|
'User-Agent':
|
||||||
'krakenex/2.1.0 (+https://github.com/veox/python3-krakenex)'
|
'krakenex/2.1.0 (+https://github.com/veox/python3-krakenex)'
|
||||||
})
|
})
|
||||||
|
self.conf: dict[str, str] = config
|
||||||
self._pairs: list[str] = []
|
self._pairs: list[str] = []
|
||||||
self._name = name
|
self._name = name
|
||||||
self._api_key = api_key
|
self._api_key = api_key
|
||||||
|
@ -215,8 +218,36 @@ class Client:
|
||||||
data['nonce'] = str(int(1000*time.time()))
|
data['nonce'] = str(int(1000*time.time()))
|
||||||
return await self._private(method, data, uri_path)
|
return await self._private(method, data, uri_path)
|
||||||
|
|
||||||
|
async def get_balances(
|
||||||
|
self,
|
||||||
|
) -> dict[str, float]:
|
||||||
|
'''
|
||||||
|
Return the set of asset balances for this account
|
||||||
|
by symbol.
|
||||||
|
|
||||||
|
'''
|
||||||
|
resp = await self.endpoint(
|
||||||
|
'Balance',
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
by_bsuid = resp['result']
|
||||||
|
return {
|
||||||
|
self._atable[sym].lower(): float(bal)
|
||||||
|
for sym, bal in by_bsuid.items()
|
||||||
|
}
|
||||||
|
|
||||||
|
async def get_assets(self) -> dict[str, dict]:
|
||||||
|
resp = await self._public('Assets', {})
|
||||||
|
return resp['result']
|
||||||
|
|
||||||
|
async def cache_assets(self) -> None:
|
||||||
|
assets = self.assets = await self.get_assets()
|
||||||
|
for bsuid, info in assets.items():
|
||||||
|
self._atable[bsuid] = info['altname']
|
||||||
|
|
||||||
async def get_trades(
|
async def get_trades(
|
||||||
self,
|
self,
|
||||||
|
fetch_limit: int = 10,
|
||||||
|
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
'''
|
'''
|
||||||
|
@ -228,6 +259,8 @@ class Client:
|
||||||
trades_by_id: dict[str, Any] = {}
|
trades_by_id: dict[str, Any] = {}
|
||||||
|
|
||||||
for i in itertools.count():
|
for i in itertools.count():
|
||||||
|
if i >= fetch_limit:
|
||||||
|
break
|
||||||
|
|
||||||
# increment 'ofs' pagination offset
|
# increment 'ofs' pagination offset
|
||||||
ofs = i*50
|
ofs = i*50
|
||||||
|
@ -459,17 +492,21 @@ class Client:
|
||||||
@acm
|
@acm
|
||||||
async def get_client() -> Client:
|
async def get_client() -> Client:
|
||||||
|
|
||||||
section = get_config()
|
conf = get_config()
|
||||||
if section:
|
if conf:
|
||||||
client = Client(
|
client = Client(
|
||||||
name=section['key_descr'],
|
conf,
|
||||||
api_key=section['api_key'],
|
name=conf['key_descr'],
|
||||||
secret=section['secret']
|
api_key=conf['api_key'],
|
||||||
|
secret=conf['secret']
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
client = Client()
|
client = Client({})
|
||||||
|
|
||||||
# at startup, load all symbols locally for fast search
|
# at startup, load all symbols, and asset info in
|
||||||
await client.cache_symbols()
|
# batch requests.
|
||||||
|
async with trio.open_nursery() as nurse:
|
||||||
|
nurse.start_soon(client.cache_assets)
|
||||||
|
await client.cache_symbols()
|
||||||
|
|
||||||
yield client
|
yield client
|
||||||
|
|
Loading…
Reference in New Issue