Skip to content

Commit

Permalink
Custom json decoder for bytearray serialization (#192)
Browse files Browse the repository at this point in the history
* Implement custom json decoder for bytearray serialization

* Update subaccount examples with custom JSON decoding

* Rename Node client custom encoder method
  • Loading branch information
samtin0x authored Jul 11, 2024
1 parent 1f549ce commit 307b487
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
43 changes: 42 additions & 1 deletion v4-client-py-v2/dydx_v4_client/node/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import base64
import json
from dataclasses import dataclass
from typing import Union, Dict, Any

import grpc
from google.protobuf.message import Message
from google._upb._message import Message
from google.protobuf.json_format import MessageToDict, MessageToJson
from typing_extensions import List, Optional, Self
from v4_proto.cosmos.auth.v1beta1 import query_pb2_grpc as auth
from v4_proto.cosmos.auth.v1beta1.auth_pb2 import BaseAccount
Expand Down Expand Up @@ -77,10 +81,47 @@
from dydx_v4_client.wallet import Wallet


class CustomJSONDecoder:
def __init__(self):
self.decoder = json.JSONDecoder(object_hook=self.decode_dict)

def decode(self, json_string):
return self.decoder.decode(json_string)

@staticmethod
def decode_base64(value):
if isinstance(value, str):
try:
return list(base64.b64decode(value))
except (base64.binascii.Error, ValueError):
return value
return value

def decode_dict(self, data):
if isinstance(data, dict):
return {k: self.decode_base64(v) for k, v in data.items()}
return data


@dataclass
class QueryNodeClient:
channel: grpc.Channel

@staticmethod
def transcode_response(response: Message) -> Union[Dict[str, Any], List[Any]]:
"""
Encodes the response using the custom JSON encoder.
Args:
response (Message): The response message to encode.
Returns:
Union[Dict[str, Any], List[Any]]: The encoded response.
"""
response_dict = MessageToDict(response)
json_string = json.dumps(response_dict)
return CustomJSONDecoder().decode(json_string)

async def get_account_balances(
self, address: str
) -> bank_query.QueryAllBalancesResponse:
Expand Down
6 changes: 4 additions & 2 deletions v4-client-py-v2/examples/validator_get_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ async def test():

try:
subaccount = await node_client.get_subaccount(TEST_ADDRESS, 0)
decoded = node_client.transcode_response(subaccount)
print("Get Subaccount:")
print(subaccount)
print(decoded)
except Exception as e:
print("Error in get_subaccount:")
print(f"Error: {e}")

try:
subaccounts = await node_client.get_subaccounts()
decoded = node_client.transcode_response(subaccounts)
print("Get Subaccounts:")
print(subaccounts)
print(decoded)
except Exception as e:
print("Error in get_subaccounts:")
print(f"Error: {e}")
Expand Down

0 comments on commit 307b487

Please sign in to comment.