2024-04-16 21:23:27 +00:00
|
|
|
import dataclasses
|
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import re
|
2024-04-16 21:38:35 +00:00
|
|
|
import json
|
2024-04-16 21:23:27 +00:00
|
|
|
import secrets
|
2024-04-16 22:02:51 +00:00
|
|
|
from decimal import Decimal
|
2024-04-23 20:11:43 +00:00
|
|
|
import email.utils
|
2024-04-16 21:23:27 +00:00
|
|
|
|
|
|
|
import bs4
|
|
|
|
|
|
|
|
import personal_data.html_util
|
|
|
|
import personal_data.parse_util
|
|
|
|
from personal_data.data import DeduplicateMode, Scraper
|
2024-04-16 22:48:21 +00:00
|
|
|
from .. import secrets
|
2024-04-16 21:23:27 +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'
|
2024-04-16 21:38:35 +00:00
|
|
|
URL_ACCOUNT_PLUGIN_GLOBAL = 'https://{hostname}/{shard}blockchain/accountPlugin/global'
|
2024-04-16 21:23:27 +00:00
|
|
|
|
2024-04-23 16:04:12 +00:00
|
|
|
def shard_id_for_address(address: str) -> str:
|
|
|
|
return 'shards/Shard2/' # TODO
|
|
|
|
|
2024-04-16 21:23:27 +00:00
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
|
|
class MpcBalance(Scraper):
|
|
|
|
dataset_name = 'defi_mpc_balance'
|
2024-04-16 22:16:40 +00:00
|
|
|
deduplicate_mode = DeduplicateMode.ONLY_LATEST
|
|
|
|
deduplicate_ignore_columns = ['account.update_time']
|
2024-04-16 21:23:27 +00:00
|
|
|
|
2024-04-23 20:11:43 +00:00
|
|
|
def get_json(self, url: str, data: dict) -> tuple[dict, datetime.datetime]:
|
2024-04-16 22:02:51 +00:00
|
|
|
headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Accept': 'application/json',
|
|
|
|
}
|
|
|
|
|
|
|
|
response = self.session.post(url, headers = headers, data=json.dumps(data))
|
|
|
|
response.raise_for_status()
|
2024-04-23 20:11:43 +00:00
|
|
|
date_text = response.headers.get('last-modified') or response.headers.get('date')
|
|
|
|
date = email.utils.parsedate_to_datetime(date_text)
|
2024-04-17 22:24:19 +00:00
|
|
|
json_data = response.json()
|
|
|
|
if json_data is None:
|
|
|
|
msg = 'No result data for ' + url
|
|
|
|
raise Exception(msg)
|
|
|
|
return json_data, date
|
2024-04-16 22:02:51 +00:00
|
|
|
|
|
|
|
def determine_coins(self) -> list[dict]:
|
|
|
|
data: dict = {'path':[]}
|
|
|
|
|
|
|
|
url = URL_ACCOUNT_PLUGIN_GLOBAL.format(
|
|
|
|
hostname = HOSTNAME,
|
|
|
|
shard = '',
|
|
|
|
)
|
|
|
|
|
|
|
|
json_data, date = self.get_json(url, data=data)
|
|
|
|
return json_data['coins']['coins']
|
|
|
|
|
2024-04-16 21:23:27 +00:00
|
|
|
def scrape(self):
|
2024-04-16 22:48:21 +00:00
|
|
|
address = secrets.PBC_ACCOUNT_ADDRESS
|
2024-04-16 22:02:51 +00:00
|
|
|
coins = self.determine_coins()
|
2024-04-16 21:23:27 +00:00
|
|
|
|
|
|
|
url = URL_ACCOUNT_PLUGIN.format(
|
|
|
|
hostname = HOSTNAME,
|
2024-04-23 16:04:12 +00:00
|
|
|
shard = shard_id_for_address(address),
|
2024-04-16 21:23:27 +00:00
|
|
|
)
|
|
|
|
|
2024-04-16 22:02:51 +00:00
|
|
|
data: dict = {'path':[{'type':'field','name':'accounts'},{'type':'avl','keyType':'BLOCKCHAIN_ADDRESS','key':address}]}
|
|
|
|
account_data, date = self.get_json(url, data=data)
|
2024-04-16 21:23:27 +00:00
|
|
|
|
2024-04-16 21:38:35 +00:00
|
|
|
data_point = {
|
|
|
|
'account.address': address,
|
2024-04-16 22:02:51 +00:00
|
|
|
'account.update_time': date,
|
2024-04-16 21:38:35 +00:00
|
|
|
}
|
2024-04-16 21:23:27 +00:00
|
|
|
|
2024-04-16 22:16:40 +00:00
|
|
|
data_point['balance.MPC'] = str(Decimal(account_data['mpcTokens'])/1000)
|
2024-04-16 22:02:51 +00:00
|
|
|
|
|
|
|
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
|
2024-04-16 22:16:40 +00:00
|
|
|
data_point['balance.'+coin_data['symbol']] = str(native_balance)
|
2024-04-16 22:02:51 +00:00
|
|
|
del coin_idx, coin_data
|
2024-04-16 21:23:27 +00:00
|
|
|
|
2024-04-16 21:38:35 +00:00
|
|
|
yield data_point
|