Skip to content

Commit

Permalink
Tuple single value compatibility PolkadotJS
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanz committed Jul 18, 2024
1 parent 92472bd commit 6a7cc67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions scalecodec/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,17 @@ def decode(self, data: ScaleBytes) -> tuple:
scale_obj = scale_def.new()

scale_obj.decode(data)

if len(self.values) == 1:
return scale_obj

value += (scale_obj,)

return value

def serialize(self, value: tuple) -> tuple:
if len(value) == 1:
return value[0].value
def serialize(self, value: Union[tuple, ScaleType]) -> tuple:
if issubclass(value.__class__, ScaleType):
return value.value

return tuple((i.value for i in value))

Expand Down
7 changes: 7 additions & 0 deletions test/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def test_tuple_deserialize(self):
obj.deserialize((3, 2))
self.assertEqual(obj.value, (3, 2))

def test_tuple_single_value(self):
# PolkadotJS compatilibity
obj = Tuple(U8).new()
obj.decode(ScaleBytes('0x03'))
self.assertEqual(obj.value, 3)
self.assertEqual(obj.value_object, U8.new(value=3))


if __name__ == '__main__':
unittest.main()

0 comments on commit 6a7cc67

Please sign in to comment.