Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented data serialization/deserialization with marshmallow #21

Merged
merged 10 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ psycopg2-binary>=2.9.3
SQLAlchemy==1.4.41
pandas==1.5.0
click==8.1.3
marshmallow==3.18.0
25 changes: 16 additions & 9 deletions src/missing_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
from dune_client.client import DuneClient
from dune_client.query import Query
from dune_client.types import DuneRecord
from marshmallow import fields

from duneapi.api import DuneAPI
from duneapi.types import Address, DuneQuery, Network
from duneapi.util import open_query

from src.utils import TokenSchema, CoinSchema, CoinsSchema

DuneTokenPriceRow = tuple[str, str, str, str, int]


# TODO - remove the Anys here: https://github.com/cowprotocol/data-misc/issues/20
gentrexha marked this conversation as resolved.
Show resolved Hide resolved
def load_coins() -> dict[str, dict[str, Any]]:
""" "
"""
Loads and returns coin dictionaries from Coin Paprika via their API.
Excludes, inactive, new and non "token" types
"""
Expand Down Expand Up @@ -48,7 +51,10 @@ def load_coins() -> dict[str, dict[str, Any]]:
# print(f"Error with {err}, excluding entry {entry}")

print(f"Excluded address for {missed} entries out of {len(entries)}")
return coin_dict
# return coin_dict
return CoinsSchema(keys=fields.Str(), values=fields.Nested(CoinSchema)).load(
coin_dict
)


def write_results(results: list[DuneTokenPriceRow], path: str, filename: str) -> None:
Expand Down Expand Up @@ -103,7 +109,8 @@ def as_dune_repr(self, coin_id: str) -> dict[str, Any]:

def load_tokens(dune: DuneClient) -> list[DuneRecord]:
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
"""Loads Tokens with missing prices from Dune"""
return dune.refresh(Query(query_id=1317238, name="Tokens with Missing Prices"))
results = dune.refresh(Query(query_id=1317238, name="Tokens with Missing Prices"))
return [TokenSchema().load(r) for r in results]


def fetch_tokens_without_prices(dune: DuneAPI) -> list[CoinPaprikaToken]:
Expand All @@ -128,14 +135,14 @@ def run_missing_prices() -> None:
print(f"Fetched {len(tokens)} traded tokens from Dune without prices")
found, res = 0, []
for token in tokens:
if token["address"].lower() in coins:
paprika_data = coins[token["address"].lower()]
if token["address"] in coins:
gentrexha marked this conversation as resolved.
Show resolved Hide resolved
paprika_data = coins[token["address"]]
dune_row = (
str(paprika_data["id"]),
paprika_data["id"],
"ethereum",
str(paprika_data["symbol"]),
str(paprika_data["address"].lower()),
int(token["decimals"]),
paprika_data["symbol"],
paprika_data["address"],
token["decimals"],
gentrexha marked this conversation as resolved.
Show resolved Hide resolved
)
res.append(dune_row)
found += 1
Expand Down
56 changes: 55 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import os
from datetime import datetime
from enum import Enum
from typing import Any
from typing import Any, Mapping

from duneapi.types import Network as LegacyDuneNetwork
from marshmallow import fields, Schema


def partition_array(arr: list[Any], size: int) -> list[list[Any]]:
Expand Down Expand Up @@ -79,3 +80,56 @@ def chain_id(self) -> int:
Aligned with https://chainlist.org/
"""
return {Network.MAINNET: 1, Network.GNOSIS: 100}[self]


class LoweredString(fields.String):
gentrexha marked this conversation as resolved.
Show resolved Hide resolved
"""Custom marshmallow String field for lowered string"""

def _deserialize(self, value, *args, **kwargs):
if hasattr(value, "lower"):
value = value.lower()
return super()._deserialize(value, *args, **kwargs)

def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return ""
return str(value).lower()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In both of these methods I see several unused attributes... (attr, obj, args, kwargs) are these required by the Schema interface? How might one go about handling the lint/type errors that arise from this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were part of the suggested way to implement custom Fields in marshmallow: https://marshmallow.readthedocs.io/en/stable/custom_fields.html#creating-a-field-class

That's a good question. Not sure how this is possible. I'll try removing them and see if it still works.



class TokenSchema(Schema):
"""TokenSchema CoinSchema for serializing/deserializing token data"""

address = LoweredString(required=True)
decimals = fields.Int(required=True)
popularity = fields.Int()
symbol = fields.String()


class CoinSchema(Schema):
"""CoinSchema for serializing/deserializing coin data"""

id = fields.String(required=True)
name = fields.String()
symbol = fields.String(required=True)
rank = fields.Int()
is_new = fields.Bool()
is_active = fields.Bool()
type = fields.String()
address = LoweredString(required=True)


class CoinsSchema(fields.Dict):
"""CoinsSchema for containing multiple coinschema-s"""

@staticmethod
def _get_obj(obj, _attr, _default):
"""Accessor for the dump method"""
return obj

def dump(self, obj: Any):
"""Serializes data"""
return self.serialize("", obj, accessor=self._get_obj)

def load(self, data: Mapping[str, Any]):
gentrexha marked this conversation as resolved.
Show resolved Hide resolved
"""Loads data into mapping"""
return self.deserialize(data)