dydx-v3-rust 0.1.2

Rust SDK for dYdX v3 API
Documentation
a

��tb��@s4ddlZddlZddlmZddlmZddlmZddlm	Z	ddlm
Z
ddlmZdd	d
Zd
ed>Z
ejejd�Zejejd�Zejejejejejgd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z d d!�Z!d"d#�Z"d$d%�Z#d&d'�Z$d(d)�Z%d*d+�Z&d,d-�Z'd.d/�Z(dS)0�N)�Web3)�ASSET_RESOLUTION)�ORDER_FIELD_BIT_LENGTHS)�get_random_private_key)�&private_key_to_ec_point_on_stark_curve)�private_to_stark_key�����nonce)�rounding)ZtrapscCs|�d�r|dd�S|S)N�0xr)�
startswith)�input�r�s/Users/okudajunta/Library/Mobile Documents/com~apple~CloudDocs/solidity/dydx-v3-rust/./src/stark/starkex/helpers.py�strip_hex_prefix s
rcCst|��d�S)zConvert a bytestring to an int.�)�int�hex)�xrrr�bytes_to_int&srcCs0t|�dd��dd�}t|�dkr,td��|S)z4Normalize to a 32-byte hex string without 0x prefix.rN�@�0zInput does not fit in 32 bytes)r�rjust�len�
ValueError)rZ
padded_hexrrr�
int_to_hex_32+srcCst|�t|�S)z>Convert a signature from an r, s pair to a 32-byte hex string.)r)�r�srrr�serialize_signature3sr cCs8t|�dkrtd��t|dd�d�t|dd�d�fS)z>Convert a signature from a 32-byte hex string to an r, s pair.�z?Invalid serialized signature, expected hex string of length 128Nrr)rrr)�	signaturerrr�deserialize_signature8s
�r#cCst||t�S)z�Convert a human-readable amount to an integer amount of quantums.

    If the provided human_amount is not a multiple of the quantum size,
    an exception will be raised.
    )�_to_quantums_helper�DECIMAL_CTX_EXACT��human_amount�assetrrr�to_quantums_exactAsr)cCst||t�S)z�Convert a human-readable amount to an integer amount of quantums.

    If the provided human_amount is not a multiple of the quantum size,
    the result will be rounded down to the nearest integer.
    )r$�DECIMAL_CTX_ROUND_DOWNr&rrr�to_quantums_round_downJsr+cCst||t�S)z�Convert a human-readable amount to an integer amount of quantums.

    If the provided human_amount is not a multiple of the quantum size,
    the result will be rounded up to the nearest integer.
    )r$�DECIMAL_CTX_ROUND_UPr&rrr�to_quantums_round_upSsr-cCsfz,|�|�}|�t|�}||j|d�}Wn0tjy\td�|dtt|����Yn0t|�S)N)�contextz2Amount {} is not a multiple of the quantum size {}r
)	�create_decimalr�to_integral_exact�decimal�Inexactr�format�floatr)r'r(�ctxZ
amount_decZresolution_decZquantumsrrrr$\s
��
r$cCs,t��}|�|���t|����d�tS)z<Generate a nonce deterministically from an arbitrary string.r)�hashlib�sha256�update�encoder�digestr�NONCE_UPPER_BOUND_EXCLUSIVE)�	client_id�messagerrr�nonce_from_client_idksr>cCsLt|�d|}|��s(td�||���t�gd�|t|�||g�}t|�S)N�
z3Amount {} has more precision than token decimals {})�address�uint256r@rA)r4�
is_integerrr3rZsolidityKeccakr�bytes)Z	recipientZtoken_decimalsr'Z
token_addressZsaltZtoken_amountZ	hex_bytesrrr�get_transfer_erc20_factrs"����rDcCs<t|t�std��t�t|��|}tt�|���d�t	@S)zAGenerate the condition, signed as part of a conditional transfer.zfact must be a byte-stringr)
�
isinstancerCr�fromhexrrr�keccakr�BIT_MASK_250)Zfact_registry_addressZfact�datarrr�fact_to_condition�s
rJcCs,t��}|�|���t|����d�d?S)z;Generate a hash deterministically from an arbitrary string.r�)r6r7r8r9rr:r)Zmessage_stringr=rrr�message_to_hash�srLcCs
tt��S)z<Generate a STARK key using the Python builtin random module.)rrrrrr�generate_private_key_hex_unsafe�srMcCs.t|t�std��ttt�|���d�d?�S)z8Generate a STARK key deterministically from binary data.zInput must be a byte-stringrrK)rErCrrrrrG)rIrrr�private_key_from_bytes�s
rNcCst|d�}tt|��S)zEGiven private key as hex string, return the public key as hex string.r)rrr)�private_key_hex�private_key_intrrr�private_key_to_public_hex�s
rQcCs&t|d�}t|�\}}t|�t|�gS)zDGiven private key as hex string, return the public x, y pair as hex.r)rrr)rOrPr�yrrr�"private_key_to_public_key_pair_hex�s
rS))r1r6Zweb3r�dydx_constantsr�starkex.constantsrZ*starkex.starkex_resources.python_signaturerrrrHr;�Context�
ROUND_DOWNr*�ROUND_UPr,r2�DivisionByZero�InvalidOperation�Overflowr%rrrr r#r)r+r-r$r>rDrJrLrMrNrQrSrrrr�<module>sF��