2024-06-20 21:43:45 +00:00
|
|
|
"""See `PartisiaBlockchainAccountDepoFetcher` for documentation."""
|
|
|
|
|
2024-06-02 16:33:25 +00:00
|
|
|
import dataclasses
|
|
|
|
import datetime
|
|
|
|
import email.utils
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
from collections.abc import Mapping
|
|
|
|
from decimal import Decimal
|
2024-07-20 18:21:50 +00:00
|
|
|
from typing import Any
|
2024-06-02 16:33:25 +00:00
|
|
|
|
|
|
|
import fin_defs
|
|
|
|
import requests
|
|
|
|
from frozendict import frozendict
|
|
|
|
|
2024-07-18 22:22:54 +00:00
|
|
|
from .data import DepoFetcher, DepoGroup, DepoSingle
|
2024-06-02 16:33:25 +00:00
|
|
|
|
|
|
|
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:
|
2024-11-01 10:30:18 +00:00
|
|
|
# Very rough implementation
|
|
|
|
if address is None:
|
|
|
|
msg = 'Address must not be None'
|
|
|
|
raise TypeError(msg)
|
2024-06-02 16:33:25 +00:00
|
|
|
if address.endswith('a'):
|
2024-11-01 10:30:18 +00:00
|
|
|
return 'shards/Shard0/'
|
|
|
|
if address.endswith('2'):
|
|
|
|
return 'shards/Shard1/'
|
|
|
|
return 'shards/Shard2/'
|
2024-06-02 16:33:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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,
|
2024-07-20 18:21:50 +00:00
|
|
|
data: Mapping[str, Any] = frozendict(),
|
2024-06-02 16:33:25 +00:00
|
|
|
method='POST',
|
2024-07-20 18:21:50 +00:00
|
|
|
) -> tuple[Any, datetime.datetime]:
|
2024-06-02 16:33:25 +00:00
|
|
|
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)
|
|
|
|
|
2024-07-20 18:21:50 +00:00
|
|
|
def determine_coins(self) -> list[dict[str, Any]]:
|
2024-06-02 16:33:25 +00:00
|
|
|
data: dict = {'path': []}
|
|
|
|
|
2024-07-20 18:21:50 +00:00
|
|
|
url: str = URL_ACCOUNT_PLUGIN_GLOBAL.format(
|
2024-06-02 16:33:25 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2024-06-02 16:52:53 +00:00
|
|
|
BYOC_ASSETS = {
|
2024-06-04 19:30:42 +00:00
|
|
|
'ETH': fin_defs.WELL_KNOWN_SYMBOLS['ETH'],
|
|
|
|
'POLYGON_USDC': fin_defs.WELL_KNOWN_SYMBOLS['USDC'],
|
|
|
|
'BNB': fin_defs.WELL_KNOWN_SYMBOLS['BNB'],
|
|
|
|
'MATIC': fin_defs.WELL_KNOWN_SYMBOLS['MATIC'],
|
2024-06-02 16:52:53 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 19:30:42 +00:00
|
|
|
|
2024-07-18 22:22:29 +00:00
|
|
|
class PartisiaBlockchainAccountDepoFetcher(DepoFetcher):
|
2024-06-20 21:43:45 +00:00
|
|
|
"""Depository fetcher for individual [Partisia
|
|
|
|
Blockchain](https://partisiablockchain.com/) accounts, including [MPC](https://partisiablockchain.gitlab.io/documentation/pbc-fundamentals/mpc-token-model-and-account-elements.html) and
|
|
|
|
[Bring-Your-Own-Coin](https://partisiablockchain.gitlab.io/documentation/pbc-fundamentals/byoc/introduction-to-byoc.html).
|
|
|
|
|
|
|
|
Requirements for use:
|
|
|
|
- Account on Partisia Blockchain.
|
|
|
|
|
|
|
|
Depository structure: A nested depo containing both a `Native` and a `BYOC`
|
|
|
|
depo.
|
|
|
|
"""
|
|
|
|
|
2024-07-18 22:22:29 +00:00
|
|
|
def __init__(self, session: requests.Session, blockchain_address: str):
|
|
|
|
self.assert_param('session', requests.Session, session)
|
|
|
|
self.assert_param('blockchain_address', str, blockchain_address)
|
2024-06-02 16:52:53 +00:00
|
|
|
|
2024-06-02 16:33:25 +00:00
|
|
|
self.client = PbcClient(session)
|
|
|
|
self.blockchain_address = blockchain_address
|
|
|
|
|
|
|
|
def get_depo(self) -> DepoGroup:
|
|
|
|
balances = self.client.get_account_balances(self.blockchain_address)
|
|
|
|
|
2024-06-04 19:30:42 +00:00
|
|
|
depo_mpc = DepoSingle(
|
2024-06-04 20:15:21 +00:00
|
|
|
'Native',
|
|
|
|
balances.update_time,
|
|
|
|
{fin_defs.MPC: balances.mpc},
|
2024-06-04 19:30:42 +00:00
|
|
|
)
|
2024-06-02 16:33:25 +00:00
|
|
|
depo_byoc = DepoSingle(
|
2024-07-18 22:53:14 +00:00
|
|
|
'Bring Your Own Coin',
|
2024-06-02 16:33:25 +00:00
|
|
|
balances.update_time,
|
2024-06-02 16:52:53 +00:00
|
|
|
{BYOC_ASSETS[k]: v for k, v in balances.byoc.items()},
|
2024-06-02 16:33:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return DepoGroup(
|
|
|
|
'Partisia Blockchain',
|
|
|
|
balances.update_time,
|
|
|
|
[depo_mpc, depo_byoc],
|
|
|
|
)
|