From e640dc35ffe86f04aee763cfe2891a7acaf29386 Mon Sep 17 00:00:00 2001 From: Fabian Schuh Date: Thu, 23 Nov 2023 22:21:09 +0100 Subject: [PATCH] fix: proper way of dealing with Optional --- bitsharesbase/operations.py | 102 +++++++++++++++++++++++------------- tests/test_transactions.py | 10 ++-- 2 files changed, 71 insertions(+), 41 deletions(-) diff --git a/bitsharesbase/operations.py b/bitsharesbase/operations.py index f18552d..89a62dd 100644 --- a/bitsharesbase/operations.py +++ b/bitsharesbase/operations.py @@ -1278,14 +1278,22 @@ def __init__(self, *args, **kwargs): else: if len(args) == 1 and len(kwargs) == 0: kwargs = args[0] + if "new_fee_rate" in kwargs: + new_fee_rate = Optional(Uint32(kwargs["new_fee_rate"])) + else: + new_fee_rate = Optional(None) + if "delta_amount" in kwargs: + delta_amount = Optional(Asset(kwargs["delta_amount"])) + else: + delta_amount = Optional(None) super().__init__( OrderedDict( [ ("fee", Asset(kwargs["fee"])), ("owner_account", ObjectId(kwargs["owner_account"], "account")), ("fund_id", ObjectId(kwargs["fund_id"], "samet_fund")), - ("delta_amount", Optional(Asset(kwargs["delta_amount"]))), - ("new_fee_rate", Optional(Uint32(kwargs["new_fee_rate"]))), + ("delta_amount", delta_amount), + ("new_fee_rate", new_fee_rate), ("extensions", Set([])), ] ) @@ -1405,46 +1413,68 @@ def __init__(self, *args, **kwargs): else: if len(args) == 1 and len(kwargs) == 0: kwargs = args[0] + if kwargs.get("min_deal_amount"): + min_deal_amount = Optional(Int64(kwargs["min_deal_amount"])) + else: + min_deal_amount = Optional(None) + if kwargs.get("enabled"): + enabled = Optional(Bool(kwargs["enabled"])) + else: + enabled = Optional(None) + if kwargs.get("auto_disable_time"): + auto_disable_time = Optional(PointInTime(kwargs["auto_disable_time"])) + else: + auto_disable_time = Optional(None) + if kwargs.get("acceptable_collateral"): + acceptable_collateral = Optional( + Map( + [ + [ObjectId(k[0], "asset"), Price(k[1])] + for k in kwargs["acceptable_collateral"] + ] + ) + ) + else: + acceptable_collateral = Optional(None) + if kwargs.get("acceptable_borrowers"): + acceptable_borrowers = Optional( + Map( + [ + [ObjectId(k[0], "account"), Int64(k[1])] + for k in kwargs["acceptable_borrowers"] + ] + ) + ) + else: + acceptable_borrowers = Optional(None) + if kwargs.get("max_duration_seconds"): + max_duration_seconds = Optional( + Uint32(kwargs.get("max_duration_seconds")) + ) + else: + max_duration_seconds = Optional(None) + if kwargs.get("fee_rate"): + fee_rate = Optional(Uint32(kwargs.get("fee_rate"))) + else: + fee_rate = Optional(None) + if kwargs.get("delta_amount"): + delta_amount = Optional(Asset(kwargs.get("delta_amount"))) + else: + delta_amount = Optional(None) super().__init__( OrderedDict( [ ("fee", Asset(kwargs["fee"])), ("owner_account", ObjectId(kwargs["owner_account"], "account")), ("offer_id", ObjectId(kwargs["offer_id"], "credit_offer")), - ("delta_amount", Optional(Asset(kwargs["fee"]))), - ("fee_rate", Optional(Uint32(kwargs["fee_rate"]))), - ( - "max_duration_seconds", - Optional(Uint32(kwargs["max_duration_seconds"])), - ), - ("min_deal_amount", Optional(Int64(kwargs["min_deal_amount"]))), - ("enabled", Optional(Bool(kwargs["enabled"]))), - ( - "auto_disable_time", - Optional(PointInTime(kwargs["auto_disable_time"])), - ), - ( - "acceptable_collateral", - Optional( - Map( - [ - [ObjectId(k[0], "asset"), Price(k[1])] - for k in kwargs["acceptable_collateral"] - ] - ) - ), - ), - ( - "acceptable_borrowers", - Optional( - Map( - [ - [ObjectId(k[0], "account"), Int64(k[1])] - for k in kwargs["acceptable_borrowers"] - ] - ) - ), - ), + ("delta_amount", delta_amount), + ("fee_rate", fee_rate), + ("max_duration_seconds", max_duration_seconds), + ("min_deal_amount", min_deal_amount), + ("enabled", enabled), + ("auto_disable_time", auto_disable_time), + ("acceptable_collateral", acceptable_collateral), + ("acceptable_borrowers", acceptable_borrowers), ("extensions", Set([])), ] ) diff --git a/tests/test_transactions.py b/tests/test_transactions.py index 6820f93..028ec4f 100644 --- a/tests/test_transactions.py +++ b/tests/test_transactions.py @@ -1232,13 +1232,13 @@ def test_credit_offer_update(self): } ) self.cm = ( - "f68585abf4dce7c8045701470000000000000000007b010100" + "f68585abf4dce7c8045701470000000000000000007b010101" "0000000000000000010100000001e8030000010a0000000000" "0000010101800092650101d52b01000000000000000090d003" - "0000000000d52b010164e8030000000000000000012064575b" - "5fd2347411a643c54a2aaf2550c0c5653334ec81ca28a1e68f" - "fd7e7fd07748121135e01947ae317e3b7ae9263149b96e9630" - "1b16571984b94f7c72ebd9" + "0000000000d52b010164e8030000000000000000011f08187d" + "ea87e4306cb8e6fc9ddeeb38053499a86f5c01d8bfa8b72257" + "564bd3250bd4868d5db692077526b901a019cb8e2049e5daf7" + "95a7ba17e6122fdb0fb20a" ) self.doit(1)