Skip to content

Commit

Permalink
Refactor hex decoding (#1557)
Browse files Browse the repository at this point in the history
* Factor out hex string decoding in the Python bindings

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Remove extra whitespace in pydocs

Co-authored-by: Thoralf-M <[email protected]>

---------

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>
Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
tadzik and Thoralf-M authored Nov 3, 2023
1 parent d6f6250 commit ff9afca
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions bindings/python/iota_sdk/types/balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import List, Optional
from dataclasses import dataclass, field
from dataclasses_json import config
from iota_sdk.types.common import HexStr, json
from iota_sdk.types.common import hex_str_decoder, HexStr, json


@json
Expand Down Expand Up @@ -64,11 +64,11 @@ class NativeTokensBalance:
token_id: HexStr
total: int = field(metadata=config(
encoder=hex,
decoder=lambda v: int(v, 16)
decoder=hex_str_decoder,
))
available: int = field(metadata=config(
encoder=hex,
decoder=lambda v: int(v, 16)
decoder=hex_str_decoder,
))
metadata: Optional[HexStr]

Expand Down
5 changes: 5 additions & 0 deletions bindings/python/iota_sdk/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def opt_int_encoder(value):
return None


def hex_str_decoder(value: str) -> int:
"""Parses a given string as a hexadecimal integer."""
return int(value, 16)


@json
@dataclass
class AddressAndAmount():
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/iota_sdk/types/native_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass, field
from dataclasses_json import config

from iota_sdk.types.common import HexStr, json
from iota_sdk.types.common import hex_str_decoder, HexStr, json


@json
Expand All @@ -19,5 +19,5 @@ class NativeToken():
id: HexStr
amount: int = field(metadata=config(
encoder=hex,
decoder=lambda v: int(v, 16)
decoder=hex_str_decoder,
))
6 changes: 3 additions & 3 deletions bindings/python/iota_sdk/types/send_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import dataclass, field
from typing import Optional, List
from dataclasses_json import config
from iota_sdk.types.common import HexStr, json
from iota_sdk.types.common import hex_str_decoder, HexStr, json
from iota_sdk.types.native_token import NativeToken


Expand Down Expand Up @@ -73,11 +73,11 @@ class CreateNativeTokenParams():
"""
circulating_supply: int = field(metadata=config(
encoder=hex,
decoder=lambda v: int(v, 16)
decoder=hex_str_decoder,
))
maximum_supply: int = field(metadata=config(
encoder=hex,
decoder=lambda v: int(v, 16)
decoder=hex_str_decoder,
))
foundry_metadata: Optional[str] = None
account_id: Optional[str] = None
Expand Down
8 changes: 4 additions & 4 deletions bindings/python/iota_sdk/types/token_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TypeAlias
from dataclasses import dataclass, field
from dataclasses_json import config
from iota_sdk.types.common import json
from iota_sdk.types.common import hex_str_decoder, json


@json
Expand All @@ -20,15 +20,15 @@ class SimpleTokenScheme:
"""
minted_tokens: int = field(metadata=config(
encoder=hex,
decoder=lambda v: int(v, 16)
decoder=hex_str_decoder,
))
melted_tokens: int = field(metadata=config(
encoder=hex,
decoder=lambda v: int(v, 16)
decoder=hex_str_decoder,
))
maximum_supply: int = field(metadata=config(
encoder=hex,
decoder=lambda v: int(v, 16)
decoder=hex_str_decoder,
))
type: int = field(default=0, init=False)

Expand Down

0 comments on commit ff9afca

Please sign in to comment.