Skip to content

Commit

Permalink
fix: merge problems and fixing unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Nov 23, 2023
2 parents 20a41e9 + d3ed3bf commit de9c789
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 27 deletions.
43 changes: 42 additions & 1 deletion bitsharesbase/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,5 +454,46 @@ def __init__(self, *args, **kwargs):
elif id == 2:
data = Block_id_predicate(o[1])
else:
raise ValueError("Unknown {}".format(self.__class__.name))
raise ValueError("Unknown {}".format(self.__class__.__name__))
super().__init__(data, id)


class LimitOrderAutoAction(Static_variant):
def __init__(self, o):
class Create_take_profit_order_action(GrapheneObject):
def __init__(self, *args, **kwargs):
kwargs.update(args[0])
super().__init__(
OrderedDict(
[
("fee_asset_id", ObjectId(kwargs["fee_asset_id"], "asset")),
("spread_percent", Uint16(kwargs["spread_percent"])),
("size_percent", Uint16(kwargs["size_percent"])),
(
"expiration_seconds",
Uint32(kwargs["expiration_seconds"]),
),
("repeat", Bool(kwargs["repeat"])),
("extensions", Set([])),
]
)
)

id = o[0]
if id == 0:
data = Create_take_profit_order_action(o[1])
else:
raise ValueError("Unknown {}".format(self.__class__.__name__))
super().__init__(data, id)


class LimitOrderCreateExtensions(Extension):
def NestedLimitOrderAutoAction(value):
if value:
return Array([LimitOrderAutoAction(o) for o in value])
else:
return None

sorted_options = [
("on_fill", NestedLimitOrderAutoAction),
]
123 changes: 106 additions & 17 deletions bitsharesbase/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
Worker_initializer,
isArgsThisClass,
AssertPredicate,
LimitOrderAutoAction,
LimitOrderCreateExtensions,
)
from .operationids import operations

Expand All @@ -54,22 +56,6 @@
class_namemap = {}


class VirtualOperationException(Exception):
pass


class ChainParameters(NotImplementedError):
pass


class CustomRestriction(NotImplementedError):
pass


class VestingPolicy(NotImplementedError):
pass


def fill_classmaps():
for name, ind in operations.items():
classname = name[0:1].upper() + name[1:]
Expand Down Expand Up @@ -402,7 +388,10 @@ def __init__(self, *args, **kwargs):
("min_to_receive", Asset(kwargs["min_to_receive"])),
("expiration", PointInTime(kwargs["expiration"])),
("fill_or_kill", Bool(kwargs["fill_or_kill"])),
("extensions", Set([])),
(
"extensions",
LimitOrderCreateExtensions(kwargs["extensions"]),
),
]
)
)
Expand Down Expand Up @@ -1532,4 +1521,104 @@ def __init__(self, *args, **kwargs):
)


class Liquidity_pool_update(GrapheneObject):
def __init__(self, *args, **kwargs):
if isArgsThisClass(self, args):
self.data = args[0].data
else:
if len(args) == 1 and len(kwargs) == 0:
kwargs = args[0]

if kwargs.get("taker_fee_percent"):
taker_fee_percent = Optional(Uint16(kwargs["taker_fee_percent"]))
else:
taker_fee_percent = Optional(None)

if kwargs.get("withdrawal_fee_percent"):
withdrawal_fee_percent = Optional(
Uint16(kwargs["withdrawal_fee_percent"])
)
else:
withdrawal_fee_percent = Optional(None)

super().__init__(
OrderedDict(
[
("fee", Asset(kwargs["fee"])),
("account", ObjectId(kwargs["account"], "account")),
("pool", ObjectId(kwargs["pool"], "liquidity_pool")),
("taker_fee_percent", taker_fee_percent),
("withdrawal_fee_percent", withdrawal_fee_percent),
("extensions", Set([])),
]
)
)


class Credit_deal_update(GrapheneObject):
def __init__(self, *args, **kwargs):
if isArgsThisClass(self, args):
self.data = args[0].data
else:
if len(args) == 1 and len(kwargs) == 0:
kwargs = args[0]
super().__init__(
OrderedDict(
[
("fee", Asset(kwargs["fee"])),
("account", ObjectId(kwargs["account"], "account")),
("deal_id", ObjectId(kwargs["deal_id"], "credit_deal")),
("auto_repay", Uint8(kwargs["auto_repay"])),
("extensions", Set([])),
]
)
)


class Limit_order_update(GrapheneObject):
def __init__(self, *args, **kwargs):
if isArgsThisClass(self, args):
self.data = args[0].data
else:
if len(args) == 1 and len(kwargs) == 0:
kwargs = args[0]

if kwargs.get("new_price"):
new_price = Optional(Price(kwargs["new_price"]))
else:
new_price = Optional(None)

if kwargs.get("delta_amount_to_sell"):
delta_amount_to_sell = Optional(Asset(kwargs["delta_amount_to_sell"]))
else:
delta_amount_to_sell = Optional(None)

if kwargs.get("new_expiration"):
new_expiration = Optional(PointInTime(kwargs["new_expiration"]))
else:
new_expiration = Optional(None)

if kwargs.get("on_fill"):
on_fill = Optional(
Array([LimitOrderAutoAction(o) for o in kwargs["on_fill"]])
)
else:
on_fill = Optional(None)

super().__init__(
OrderedDict(
[
("fee", Asset(kwargs["fee"])),
("seller", ObjectId(kwargs["seller"], "account")),
("order", ObjectId(kwargs["order"], "limit_order")),
("new_price", new_price),
("delta_amount_to_sell", delta_amount_to_sell),
("new_expiration", new_expiration),
("on_fill", on_fill),
("extensions", Set([])),
]
)
)


fill_classmaps()
6 changes: 3 additions & 3 deletions tests/test_bitshares.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ def test_create_asset(self):
self.assertEqual(op["symbol"], symbol)
self.assertEqual(op["precision"], precision)
self.assertEqual(
op["common_options"]["max_supply"], int(max_supply * 10 ** precision)
op["common_options"]["max_supply"], int(max_supply * 10**precision)
)
self.assertEqual(
op["common_options"]["market_fee_percent"], int(market_fee_percent * 100)
)
self.assertEqual(
op["common_options"]["max_market_fee"],
int(max_market_fee * 10 ** precision),
int(max_market_fee * 10**precision),
)
self.assertEqual(op["common_options"]["description"], description)
self.assertEqual(
Expand Down Expand Up @@ -211,7 +211,7 @@ def test_allow(self):
op = tx["operations"][0][1]
self.assertIn("owner", op)
self.assertIn(
["BTS55VCzsb47NZwWe5F3qyQKedX9iHBHMVVFSc96PDvV7wuj7W86n", "1"],
["BTS55VCzsb47NZwWe5F3qyQKedX9iHBHMVVFSc96PDvV7wuj7W86n", 1],
op["owner"]["key_auths"],
)
self.assertEqual(op["owner"]["weight_threshold"], 1)
Expand Down
117 changes: 111 additions & 6 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,29 @@ def test_limit_order_create(self):
"min_to_receive": {"amount": 10000, "asset_id": "1.3.105"},
"expiration": "2016-05-18T09:22:05",
"fill_or_kill": False,
"extensions": [],
"extensions": {
"on_fill": [
[
0,
{
"fee_asset_id": "1.3.0",
"spread_percent": 100,
"size_percent": 1000,
"expiration_seconds": 3600,
"repeat": True,
"extensions": [],
},
]
]
},
}
)
self.cm = (
"f68585abf4dce7c8045701016400000000000000001da08601000"
"0000000001027000000000000693d343c57000000011f75cbfd49"
"ae8d9b04af76cc0a7de8b6e30b71167db7fe8e2197ef9d858df18"
"77043493bc24ffdaaffe592357831c978fd8a296b913979f106de"
"be940d60d77b50"
"f68585abf4dce7c8045701016400000000000000001da0860100000"
"00000001027000000000000693d343c570001000100006400e80310"
"0e0000010000011f5ddffd232fd713e106aec3068646f5a74ae145e"
"08e1e13f7464885a507e808e365594f5e7c14049d9432bcf1ca2330"
"a65d1b7ab88aa08b355970ca6f23e06aa0"
)
self.doit()

Expand Down Expand Up @@ -1292,6 +1306,97 @@ def test_credit_deal_expired(self):
)
self.doit()

def test_limit_order_update(self):
self.op = operations.Limit_order_update(
**{
"fee": {"amount": 0, "asset_id": "1.3.0"},
"seller": "1.2.4",
"order": "1.7.12535",
"extensions": [],
}
)
self.cm = (
"f68585abf4dce7c80457014d00000000000000000004f76100"
"0000000000011f06d0b4467a5916ffb3d8ef4261c0719b1fb0"
"964aa5d3fbecbfbcbc9fe1117bc20e8a2c3f87e7817b83446c"
"45deb2a0d3d5b8b0d3b22fc8076ffc8eeb1a95e928"
)
self.doit(0)

self.op = operations.Limit_order_update(
**{
"fee": {"amount": 0, "asset_id": "1.3.0"},
"seller": "1.2.4",
"order": "1.7.12535",
"new_price": {
"base": {"amount": 1123456, "asset_id": "1.3.0"},
"quote": {"amount": 78901122, "asset_id": "1.3.0"},
},
"delta_amount_to_sell": {"amount": 12562, "asset_id": "1.3.0"},
"new_expiration": "2023-12-18T09:22:05",
"on_fill": [
[
0,
{
"fee_asset_id": "1.3.0",
"spread_percent": 100,
"size_percent": 1000,
"expiration_seconds": 3600,
"repeat": True,
"extensions": [],
},
]
],
"extensions": [],
}
)
self.cm = (
"f68585abf4dce7c80457014d00000000000000000004f76101"
"80241100000000000082efb304000000000001123100000000"
"000000013d0f8065010100006400e803100e00000100000001"
"2051a24fb550e4a8ec890ad404ea0e3cf6ea449d6ba397d280"
"64f7129153dd013e27846eb6567a88d8eea7557f32ddc02cdc"
"a614c5a30130c83141c4050ffc50e2"
)
self.doit()

def test_liquidity_pool_update(self):
self.op = operations.Liquidity_pool_update(
**{
"fee": {"amount": 0, "asset_id": "1.3.0"},
"account": "1.2.4",
"pool": "1.19.13356",
"taker_fee_percent": 124,
"withdrawal_fee_percent": 125,
"extensions": [],
}
)
self.cm = (
"f68585abf4dce7c80457014b00000000000000000004ac6801"
"7c00017d00000001202e3f140515ce936020348f845a4c034b"
"164441049dfe0a59a6c0fe27cbef740c10fa0d315e14e77e62"
"d8dae4f45d95fef358ff79f5304a420d6fc13abfd9374b"
)
self.doit(0)

def test_credit_deal_update(self):
self.op = operations.Credit_deal_update(
**{
"fee": {"amount": 0, "asset_id": "1.3.0"},
"account": "1.2.4",
"deal_id": "1.22.2356",
"auto_repay": 24,
"extensions": [],
}
)
self.cm = (
"f68585abf4dce7c80457014c00000000000000000004b41218"
"0000012026b59b7796cb9ca40d92b1d339558a94aa9f70d2f6"
"d8a7b8b0155c943b89bd602a1ecc9df3143664f801f401a728"
"78cce9f064dbcfc1af65826ce68a2177a38d"
)
self.doit()

def compareConstructedTX(self):
self.maxDiff = None
self.op = operations.Call_order_update(
Expand Down

0 comments on commit de9c789

Please sign in to comment.