[docs]defstr_to_felt(text:str)->int:""" Convert a string to a felt. WARNING : text is converting to uppercase """iftext.upper()!=text:text=text.upper()b_text=bytes(text,"utf-8")returnint.from_bytes(b_text,"big")
[docs]deffelt_to_str(felt:int)->str:""" Convert a felt to a string. """num_bytes=(felt.bit_length()+7)//8bytes_=felt.to_bytes(num_bytes,"big")returnbytes_.decode("utf-8")
[docs]defcurrency_pair_to_pair_id(base:str,quote:str)->str:""" Return a pair id from base and quote currencies. e.g currency_pair_to_pair_id("btc", "usd") -> "BTC/USD" :param base: Base currency :param quote: Quote currency :return: Pair id """returnf"{base}/{quote}".upper()
[docs]defget_cur_from_pair(asset:str)->List[str]:""" Get the currency from a pair. e.g get_cur_from_pair("BTC/USD") -> ["BTC", "USD"] :param asset: Asset pair :return: List of currencies """returnasset.split("/")
[docs]defadd_sync_methods(original_class:T)->T:""" Decorator for adding a synchronous version of a class. :param original_class: Input class :return: Input class with .sync property that contains synchronous version of this class """properties={**original_class.__dict__}forname,valueinproperties.items():sync_name=name+"_sync"# Handwritten implementation existsifsync_nameinproperties:continue# Make all callables synchronousifinspect.iscoroutinefunction(value):setattr(original_class,sync_name,make_sync(value))_set_sync_method_docstring(original_class,sync_name)elifisinstance(value,staticmethod)andinspect.iscoroutinefunction(value.__func__):setattr(original_class,sync_name,staticmethod(make_sync(value.__func__)))_set_sync_method_docstring(original_class,sync_name)elifisinstance(value,classmethod)andinspect.iscoroutinefunction(value.__func__):setattr(original_class,sync_name,classmethod(make_sync(value.__func__)))returnoriginal_class
def_set_sync_method_docstring(original_class:Any,sync_name:str)->None:sync_method=getattr(original_class,sync_name)sync_method.__doc__="Synchronous version of the method."