Skip to content

Commit

Permalink
fix(orders): improve lisiblity of serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenepix committed Jan 18, 2024
1 parent 6b94f59 commit f34cfbc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
73 changes: 59 additions & 14 deletions django_napse/api/orders/serializers/order_serializer.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,82 @@
from rest_framework import serializers

from django_napse.core.models import Order
from django_napse.core.models.bots.controller import Controller
from django_napse.utils.constants import SIDES


class OrderSerializer(serializers.ModelSerializer):
ticker = serializers.SerializerMethodField(read_only=True)
amount_spent = serializers.SerializerMethodField(read_only=True)
amount_received = serializers.SerializerMethodField(read_only=True)
fees = serializers.SerializerMethodField(read_only=True)
""".
{
"side": "BUY",
"completed": true,
"spent": {
"ticker": "BTC",
"amount": 0.1,
"price": 10000,
"value": 1000,
},
"received": {
"ticker": "USDT",
"amount": 1000,
"price": 1,
"value": 1000,
},
"fees": {
"ticker": "BTC",
"amount": 0.01,
"price": 10000,
"value": 100,
},
"created_at": "2021-09-01T12:00:00Z",
}
"""

spent = serializers.SerializerMethodField()
received = serializers.SerializerMethodField()
fees = serializers.SerializerMethodField()

class Meta:
model = Order
fields = [
"side",
"ticker",
"price",
"completed",
"amount_spent",
"amount_received",
"spent",
"received",
"fees",
"created_at",
]
read_only_fields = fields

def get_ticker(self, instance):
return instance.fee_ticker
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.exchange_account = self.instance.connection.space.exchange_account

def get_amount_spent(self, instance):
def get_spent(self, instance):
"""Return spend informations."""
exit_amount = instance.exit_amount_quote if instance.side == SIDES.BUY else instance.exit_amount_base
return instance.debited_amount - exit_amount
amount = instance.debited_amount - exit_amount
ticker = instance.tickers_info().get("spent_ticker")
price = Controller.get_asset_price(exchange_account=self.exchange_account, base=ticker)
return {
"ticker": ticker,
"amount": amount,
"price": price,
"value": amount * price,
}

def get_amount_received(self, instance):
return instance.exit_amount_base if instance.side == SIDES.BUY else instance.exit_amount_quote
def get_receive(self, instance):
"""Rerturn receive informations."""
amount = instance.exit_amount_base if instance.side == SIDES.BUY else instance.exit_amount_quote
ticker = instance.tickers_info().get("received_ticker")
price = Controller.get_asset_price(exchange_account=self.exchange_account, base=ticker)
return {
"ticker": ticker,
"amount": amount,
"price": price,
"value": amount * price,
}

def get_fees(self, instance):
return {
Expand Down
16 changes: 15 additions & 1 deletion django_napse/core/models/orders/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django_napse.core.models.transactions.credit import Credit
from django_napse.core.models.transactions.debit import Debit
from django_napse.core.models.transactions.transaction import Transaction
from django_napse.utils.constants import MODIFICATION_STATUS, ORDER_STATUS, SIDES, TRANSACTION_TYPES
from django_napse.utils.constants import EXCHANGE_PAIRS, MODIFICATION_STATUS, ORDER_STATUS, SIDES, TRANSACTION_TYPES
from django_napse.utils.errors import OrderError


Expand Down Expand Up @@ -251,3 +251,17 @@ def process_payout(self):
ticker=self.batch.controller.quote,
transaction_type=TRANSACTION_TYPES.ORDER_REFUND,
)

def tickers_info(self) -> dict[str, str]:
"""Give informations about received, spent & fee tickers."""
spent_ticker = EXCHANGE_PAIRS[self.connection.space.exchange_account.exchange.name][self.pair]["base" if self.side == SIDES.SELL else "quote"]
received_ticker = EXCHANGE_PAIRS[self.connection.space.exchange_account.exchange.name][self.pair][
"quote" if self.side == SIDES.SELL else "base"
]
fee_ticker = self.fee_ticker

return {
"spent_ticker": spent_ticker,
"received_ticker": received_ticker,
"fee_ticker": fee_ticker,
}

0 comments on commit f34cfbc

Please sign in to comment.