Compare commits
2 Commits
a481f7ecff
...
e9380a127a
Author | SHA1 | Date | |
---|---|---|---|
e9380a127a | |||
18635cd52e |
|
@ -2,8 +2,8 @@
|
|||
|
||||
import datetime
|
||||
import logging
|
||||
from decimal import Decimal
|
||||
from collections.abc import Iterator
|
||||
from decimal import Decimal
|
||||
|
||||
import fin_defs
|
||||
import krakenex
|
||||
|
@ -64,9 +64,8 @@ class KrakenDepoFetcher(DepoFetcher):
|
|||
updated_time=now,
|
||||
)
|
||||
|
||||
|
||||
def _get_withdrawals(self) -> list[WithdrawalDetails]:
|
||||
raise NotImplementedError("work in progress")
|
||||
raise NotImplementedError('work in progress')
|
||||
"""
|
||||
json = self.client.query_private('WithdrawStatus',data={'start':1})
|
||||
results = []
|
||||
|
@ -82,7 +81,7 @@ class KrakenDepoFetcher(DepoFetcher):
|
|||
"""
|
||||
|
||||
def _get_deposits(self) -> list[DepositDetails]:
|
||||
raise NotImplementedError("work in progress")
|
||||
raise NotImplementedError('work in progress')
|
||||
"""
|
||||
json = self.client.query_private('DepositStatus',data={'start':1})
|
||||
results = []
|
||||
|
@ -98,7 +97,7 @@ class KrakenDepoFetcher(DepoFetcher):
|
|||
"""
|
||||
|
||||
def _get_historic_spot_orders(self) -> Iterator[TradeOrderDetails]:
|
||||
raise NotImplementedError("work in progress")
|
||||
raise NotImplementedError('work in progress')
|
||||
"""
|
||||
json = self.client.query_private('ClosedOrders',data={'start':1})
|
||||
for order_id, v in json['result']['closed'].items():
|
||||
|
@ -118,16 +117,18 @@ class KrakenDepoFetcher(DepoFetcher):
|
|||
"""
|
||||
|
||||
def _get_double_registers(self) -> list[DoubleRegister]:
|
||||
json = self.client.query_private('Ledgers',data={'start':1})
|
||||
json = self.client.query_private('Ledgers', data={'start': 1})
|
||||
return list(parse_from_ledger(json['result']['ledger'].values()))
|
||||
|
||||
|
||||
def parse_asset_from_ticker(ticker: str) -> fin_defs.Asset:
|
||||
account = ticker.removesuffix('.HOLD')
|
||||
if account == 'ZEUR':
|
||||
return fin_defs.EUR
|
||||
return fin_defs.WELL_KNOWN_SYMBOLS[account]
|
||||
|
||||
def parse_from_ledger(ledger_items: list[dict[str,str]]) -> Iterator:
|
||||
|
||||
def parse_from_ledger(ledger_items: list[dict[str, str]]) -> Iterator:
|
||||
collected_items = {}
|
||||
for item in ledger_items:
|
||||
collected_items.setdefault(item['refid'], []).append(item)
|
||||
|
@ -139,19 +140,23 @@ def parse_from_ledger(ledger_items: list[dict[str,str]]) -> Iterator:
|
|||
assert len(items) == 1
|
||||
asset = parse_asset_from_ticker(items[0]['asset'])
|
||||
yield DepositDetails(
|
||||
deposit = fin_defs.AssetAmount(asset, Decimal(items[0]['amount'])),
|
||||
fee = fin_defs.AssetAmount(asset, Decimal(items[0]['fee'])),
|
||||
executed_time = datetime.datetime.fromtimestamp(items[0]['time'],tz=datetime.UTC),
|
||||
raw_details = items[0],
|
||||
deposit=fin_defs.AssetAmount(asset, Decimal(items[0]['amount'])),
|
||||
fee=fin_defs.AssetAmount(asset, Decimal(items[0]['fee'])),
|
||||
executed_time=datetime.datetime.fromtimestamp(
|
||||
items[0]['time'], tz=datetime.UTC,
|
||||
),
|
||||
raw_details=items[0],
|
||||
)
|
||||
elif items[0]['type'] == 'withdrawal':
|
||||
assert len(items) == 1
|
||||
asset = parse_asset_from_ticker(items[0]['asset'])
|
||||
yield WithdrawalDetails(
|
||||
withdrawn = -fin_defs.AssetAmount(asset, Decimal(items[0]['amount'])),
|
||||
fee = fin_defs.AssetAmount(asset, Decimal(items[0]['fee'])),
|
||||
executed_time = datetime.datetime.fromtimestamp(items[0]['time'],tz=datetime.UTC),
|
||||
raw_details = items[0],
|
||||
withdrawn=-fin_defs.AssetAmount(asset, Decimal(items[0]['amount'])),
|
||||
fee=fin_defs.AssetAmount(asset, Decimal(items[0]['fee'])),
|
||||
executed_time=datetime.datetime.fromtimestamp(
|
||||
items[0]['time'], tz=datetime.UTC,
|
||||
),
|
||||
raw_details=items[0],
|
||||
)
|
||||
else:
|
||||
assert len(items) == 2, items
|
||||
|
@ -160,18 +165,20 @@ def parse_from_ledger(ledger_items: list[dict[str,str]]) -> Iterator:
|
|||
assert float(items[0]['amount']) > 0
|
||||
assert float(items[1]['amount']) < 0
|
||||
asset_input = parse_asset_from_ticker(items[1]['asset'])
|
||||
asset_output = parse_asset_from_ticker(items[0]['asset'])
|
||||
asset_output = parse_asset_from_ticker(items[0]['asset'])
|
||||
|
||||
input = -fin_defs.AssetAmount(asset_input, Decimal(items[1]['amount']))
|
||||
output = fin_defs.AssetAmount(asset_output, Decimal(items[0]['amount']))
|
||||
fee_of_input = fin_defs.AssetAmount(asset_input, Decimal(items[1]['fee']))
|
||||
fee_of_output = fin_defs.AssetAmount(asset_output, Decimal(items[0]['fee']))
|
||||
details = TradeOrderDetails(
|
||||
input = input,
|
||||
output = output,
|
||||
fee = fee_of_input if fee_of_input.amount != 0 else fee_of_output,
|
||||
executed_time = datetime.datetime.fromtimestamp(items[1]['time'],tz=datetime.UTC),
|
||||
order_id = refid,
|
||||
raw_order_details = items[0],
|
||||
input=input,
|
||||
output=output,
|
||||
fee=fee_of_input if fee_of_input.amount != 0 else fee_of_output,
|
||||
executed_time=datetime.datetime.fromtimestamp(
|
||||
items[1]['time'], tz=datetime.UTC,
|
||||
),
|
||||
order_id=refid,
|
||||
raw_order_details=items[0],
|
||||
)
|
||||
yield details
|
||||
|
|
|
@ -244,7 +244,9 @@ class KucoinDepoFetcher(DepoFetcher):
|
|||
timestamp = int(end_time.timestamp() * 1000)
|
||||
page_index = 1
|
||||
while True:
|
||||
raw_details = self.kucoin_client.get_orders(end=timestamp, page=page_index)
|
||||
raw_details = self.kucoin_client.get_orders(
|
||||
end=timestamp, page=page_index,
|
||||
)
|
||||
yield from (order_from_json(item) for item in raw_details['items'])
|
||||
|
||||
page_index = raw_details['currentPage'] + 1
|
||||
|
|
|
@ -1,17 +1,10 @@
|
|||
"""See `PartisiaBlockchainAccountDepoFetcher` for documentation."""
|
||||
|
||||
import dataclasses
|
||||
import datetime
|
||||
import email.utils
|
||||
import json
|
||||
import logging
|
||||
from collections.abc import Mapping
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
|
||||
import fin_defs
|
||||
import pbc_client
|
||||
import requests
|
||||
from frozendict import frozendict
|
||||
|
||||
from .data import (
|
||||
DepoFetcher,
|
||||
|
@ -25,121 +18,6 @@ from .data import (
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# mainnet: https://reader.partisiablockchain.com
|
||||
# testnet: https://node1.testnet.partisiablockchain.com
|
||||
|
||||
|
||||
HOSTNAME = 'reader.partisiablockchain.com'
|
||||
|
||||
URL_ACCOUNT_PLUGIN = 'https://{hostname}/{shard}blockchain/accountPlugin/local'
|
||||
URL_ACCOUNT_PLUGIN_GLOBAL = 'https://{hostname}/{shard}blockchain/accountPlugin/global'
|
||||
URL_CONTRACT_STATE = 'https://{hostname}/{shard}blockchain/contracts/{address}?requireContractState=false'
|
||||
|
||||
|
||||
MPC_DECIMALS = 10000
|
||||
|
||||
|
||||
def shard_id_for_address(address: str) -> str:
|
||||
# Very rough implementation
|
||||
if address is None:
|
||||
msg = 'Address must not be None'
|
||||
raise TypeError(msg)
|
||||
if address.endswith('a'):
|
||||
return 'shards/Shard0/'
|
||||
if address.endswith('2'):
|
||||
return 'shards/Shard1/'
|
||||
return 'shards/Shard2/'
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Balances:
|
||||
update_time: datetime.datetime
|
||||
mpc: Decimal
|
||||
byoc: Mapping[str, Decimal]
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class PbcClient:
|
||||
session: requests.Session
|
||||
|
||||
def get_json(
|
||||
self,
|
||||
url: str,
|
||||
data: Mapping[str, Any] = frozendict(),
|
||||
method='POST',
|
||||
) -> tuple[Any, datetime.datetime]:
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
|
||||
response = self.session.request(
|
||||
method,
|
||||
url,
|
||||
headers=headers,
|
||||
data=json.dumps(data),
|
||||
)
|
||||
response.raise_for_status()
|
||||
date_text = response.headers.get('last-modified') or response.headers.get(
|
||||
'date',
|
||||
)
|
||||
date = email.utils.parsedate_to_datetime(date_text)
|
||||
json_data = response.json()
|
||||
if json_data is None:
|
||||
msg = 'No result data for ' + url
|
||||
raise Exception(msg)
|
||||
return (json_data, date)
|
||||
|
||||
def determine_coins(self) -> list[dict[str, Any]]:
|
||||
data: dict = {'path': []}
|
||||
|
||||
url: str = URL_ACCOUNT_PLUGIN_GLOBAL.format(
|
||||
hostname=HOSTNAME,
|
||||
shard='',
|
||||
)
|
||||
|
||||
json_data, date = self.get_json(url, data=data)
|
||||
return json_data['coins']['coins']
|
||||
|
||||
def get_account_balances(self, address: str) -> Balances:
|
||||
coins = self.determine_coins()
|
||||
|
||||
url = URL_ACCOUNT_PLUGIN.format(
|
||||
hostname=HOSTNAME,
|
||||
shard=shard_id_for_address(address),
|
||||
)
|
||||
|
||||
data: dict = {
|
||||
'path': [
|
||||
{'type': 'field', 'name': 'accounts'},
|
||||
{'type': 'avl', 'keyType': 'BLOCKCHAIN_ADDRESS', 'key': address},
|
||||
],
|
||||
}
|
||||
account_data, date = self.get_json(url, data=data)
|
||||
|
||||
byoc: dict[str, Decimal] = {}
|
||||
mpc = Decimal(account_data['mpcTokens']) / MPC_DECIMALS
|
||||
|
||||
for coin_idx, amount_data in enumerate(account_data['accountCoins']):
|
||||
coin_data = coins[coin_idx]
|
||||
byoc_balance = Decimal(amount_data['balance'])
|
||||
denominator = Decimal(coin_data['conversionRate']['denominator'])
|
||||
native_balance = byoc_balance / denominator
|
||||
byoc[coin_data['symbol']] = native_balance
|
||||
del coin_idx, coin_data
|
||||
|
||||
return Balances(date, mpc, byoc)
|
||||
|
||||
def get_contract_state(self, address: str) -> tuple[dict, datetime.datetime]:
|
||||
url = URL_CONTRACT_STATE.format(
|
||||
hostname=HOSTNAME,
|
||||
shard=shard_id_for_address(address),
|
||||
address=address,
|
||||
)
|
||||
data: dict = {'path': []}
|
||||
return self.get_json(url, data=data)
|
||||
|
||||
|
||||
BYOC_ASSETS = {
|
||||
'ETH': fin_defs.WELL_KNOWN_SYMBOLS['ETH'],
|
||||
'POLYGON_USDC': fin_defs.WELL_KNOWN_SYMBOLS['USDC'],
|
||||
|
@ -164,7 +42,7 @@ class PartisiaBlockchainAccountDepoFetcher(DepoFetcher):
|
|||
self.assert_param('session', requests.Session, session)
|
||||
self.assert_param('blockchain_address', str, blockchain_address)
|
||||
|
||||
self.client = PbcClient(session)
|
||||
self.client = pbc_client.PbcClient(session)
|
||||
self.blockchain_address = blockchain_address
|
||||
|
||||
def get_depo(self) -> DepoGroup:
|
||||
|
@ -188,10 +66,12 @@ class PartisiaBlockchainAccountDepoFetcher(DepoFetcher):
|
|||
)
|
||||
|
||||
def _get_withdrawals(self) -> list[WithdrawalDetails]:
|
||||
raise UnsupportedOperationException("TODO")
|
||||
msg = 'TODO'
|
||||
raise UnsupportedOperationException(msg)
|
||||
|
||||
def _get_deposits(self) -> list[DepositDetails]:
|
||||
raise UnsupportedOperationException("TODO")
|
||||
msg = 'TODO'
|
||||
raise UnsupportedOperationException(msg)
|
||||
|
||||
def _get_double_registers(self) -> list[DoubleRegister]:
|
||||
double_registers: list[DoubleRegister] = []
|
||||
|
|
|
@ -3,4 +3,5 @@ python-kucoin
|
|||
krakenex
|
||||
frozendict
|
||||
fin-defs @ git+https://gitfub.space/Jmaa/fin-defs.git
|
||||
pbc-client @ git+https://gitfub.space/Jmaa/pbc-client.git
|
||||
dataclassabc
|
||||
|
|
|
@ -9,6 +9,7 @@ needs_secrets = pytest.mark.skipif(
|
|||
reason='Secret kraken_USERNAME required',
|
||||
)
|
||||
|
||||
|
||||
def get_fetcher():
|
||||
return fin_depo.defi_kraken.KrakenDepoFetcher(
|
||||
secrets.KRAKEN_KEY,
|
||||
|
|
|
@ -32,7 +32,7 @@ def test_place_market_order_requires_allow_trades():
|
|||
|
||||
with pytest.raises(PermissionError) as m:
|
||||
fetcher.place_market_order(
|
||||
fin_defs.AssetAmount(fin_defs.MPC, Decimal(1)), fin_defs.USDT
|
||||
fin_defs.AssetAmount(fin_defs.MPC, Decimal(1)), fin_defs.USDT,
|
||||
)
|
||||
|
||||
assert 'KucoinDepoFetcher.allow_trades is not enabled: Cannot make trades' in str(m)
|
||||
|
|
Loading…
Reference in New Issue
Block a user