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

Fix floating-point arithmetic in share params validation #567

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions app/model/blockchain/tx_params/ibet_share.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SPDX-License-Identifier: Apache-2.0
"""
import math
from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, field_validator
Expand Down Expand Up @@ -54,8 +55,8 @@ class UpdateParams(BaseModel):
@classmethod
def dividends_13_decimal_places(cls, v):
if v is not None:
float_data = float(v * 10**13)
int_data = int(v * 10**13)
float_data = float(Decimal(str(v)) * 10**13)
int_data = int(Decimal(str(v)) * 10**13)
if not math.isclose(int_data, float_data):
raise ValueError("dividends must be rounded to 13 decimal places")
return v
Expand Down
4 changes: 2 additions & 2 deletions tests/test_app_routers_share_tokens_POST.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_normal_1_1(self, client, db):
"symbol": "symbol_test1",
"issue_price": 1000,
"total_supply": 10000,
"dividends": 123.45,
"dividends": 123.4567898765432,
"dividend_record_date": "20211231",
"dividend_payment_date": "20211231",
"cancellation_date": "20221231",
Expand All @@ -122,7 +122,7 @@ def test_normal_1_1(self, client, db):
"symbol_test1",
1000,
10000,
1234500000000000,
1234567898765432,
"20211231",
"20211231",
"20221231",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_normal_1(self, client, db):
# request target API
req_param = {
"cancellation_date": "20221231",
"dividends": 345.67,
"dividends": 345.57,
"dividend_record_date": "20211231",
"dividend_payment_date": "20211231",
"tradable_exchange_contract_address": "0xe883A6f441Ad5682d37DF31d34fc012bcB07A740",
Expand Down
Loading