Skip to content

Commit

Permalink
fix: proper way of dealing with Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Nov 23, 2023
1 parent a651abe commit e640dc3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 41 deletions.
102 changes: 66 additions & 36 deletions bitsharesbase/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([])),
]
)
Expand Down Expand Up @@ -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([])),
]
)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit e640dc3

Please sign in to comment.