Skip to content

Commit

Permalink
fixed: formatting of amount in schema used for returning staked GTC a…
Browse files Browse the repository at this point in the history
…mounts (#732)
  • Loading branch information
nutrina authored Nov 18, 2024
1 parent f014a95 commit 4c5b2ea
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
5 changes: 3 additions & 2 deletions api/stake/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import List

import api_logging as logging
from django.db.models import Q
from ninja_extra import NinjaExtraAPI

import api_logging as logging
from registry.api.utils import is_valid_address, with_read_db
from registry.exceptions import InvalidAddressException, StakingRequestError
from stake.models import Stake
from stake.schema import ErrorMessageResponse, StakeSchema, GetSchemaResponse
from stake.schema import ErrorMessageResponse, GetSchemaResponse, StakeSchema
from trusta_labs.api import CgrantsApiKey

secret_key = CgrantsApiKey()
Expand Down
5 changes: 5 additions & 0 deletions api/stake/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List

from ninja import Schema
from pydantic import field_serializer

from registry.api.schema import ErrorMessageResponse

Expand All @@ -17,6 +18,10 @@ class StakeSchema(Schema):
lock_time: str
last_updated_in_block: int

@field_serializer("amount")
def serialize_amount(self, amount: Decimal, _info):
return format(amount, ".18f")


class GetSchemaResponse(Schema):
items: List[StakeSchema]
56 changes: 56 additions & 0 deletions api/stake/test/test_get_stake.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,59 @@ def test_ceramic_cache_endpoint_failed_auth(self, mock_stakes):
)

assert response.status_code == 401

def test_serialising_scientific_decimals(
self, mocker, sample_token, sample_address
):
client = Client()

class QueryMock:
def filter(self, *args, **kwargs):
return [
Stake.objects.create(
chain="1",
staker=sample_address.lower(),
stakee=sample_address.lower(),
current_amount=Decimal("0E-18"),
last_updated_in_block=Decimal("10000000"),
unlock_time=unlock_time_0,
lock_time=lock_time_0,
),
Stake.objects.create(
chain="1",
staker=sample_address.lower(),
stakee=other_user_address.lower(),
current_amount=Decimal("2.0e+0"),
last_updated_in_block=Decimal("10000001"),
unlock_time=unlock_time_1,
lock_time=lock_time_1,
),
]

mocker.patch("stake.api.with_read_db", side_effect=[QueryMock()])
response = client.get(
"/ceramic-cache/stake/gtc",
HTTP_AUTHORIZATION="Bearer " + sample_token,
)

response_data = response.json()["items"]
assert len(response_data) == 2

assert response_data[0] == {
"staker": sample_address.lower(),
"chain": 1,
"amount": "0.000000000000000000",
"last_updated_in_block": 10000000,
"lock_time": lock_time_0.isoformat(),
"unlock_time": unlock_time_0.isoformat(),
"stakee": sample_address.lower(),
}
assert response_data[1] == {
"staker": sample_address.lower(),
"chain": 1,
"amount": "2.000000000000000000",
"last_updated_in_block": 10000001,
"lock_time": lock_time_1.isoformat(),
"unlock_time": unlock_time_1.isoformat(),
"stakee": other_user_address.lower(),
}

0 comments on commit 4c5b2ea

Please sign in to comment.