Ë
    Â|Si>   ã                  óp  — d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZmZ  ed«      Z ed«      Z ed«       ed«      dœZ ed«      Z ed«      Z G d„ dej<                  «      Z G d„ dej<                  «      Z  G d„ dej<                  «      Z!y)z4Serializers for payment deposit and withdrawal APIs.é    )Úannotations)ÚDecimalÚROUND_HALF_UP)Útransaction)Útimezone)Úserializers)ÚValidationError)ÚInsufficientBalanceError)ÚTransaction)Úget_wallet_serviceÚ
WITHDRAWALé   )ÚDepositÚPaymentAddressÚWithdrawalRequestz
0.00000001z1.00z3.00z2.00©ÚtronÚbnbz10.00z5.00c                  ó$   — e Zd ZdZ G d„ d«      Zy)ÚPaymentAddressSerializerz!Serializer for deposit addresses.c                  ó   — e Zd ZeZg d¢ZeZy)úPaymentAddressSerializer.Meta)ÚidÚ
blockchainÚassetÚaddressÚstatusÚlabelÚ
expires_atÚ
created_atN)Ú__name__Ú
__module__Ú__qualname__r   ÚmodelÚfieldsÚread_only_fields© ó    úJ/home/cursorai/projects/telegram-earn/backend/apps/payments/serializers.pyÚMetar       s   „ Øˆò	
ˆð "Ñr(   r*   N©r!   r"   r#   Ú__doc__r*   r'   r(   r)   r   r      s   „ Ù+÷"ò "r(   r   c                  ó$   — e Zd ZdZ G d„ d«      Zy)ÚDepositSerializerzSerializer for deposit history.c                  ó   — e Zd ZeZg d¢ZeZy)úDepositSerializer.Meta)	r   r   r   ÚamountÚtransaction_hashÚconfirmationsr   Údetected_atÚconfirmed_atN)r!   r"   r#   r   r$   r%   r&   r'   r(   r)   r*   r0   2   s   „ Øˆò

ˆð "Ñr(   r*   Nr+   r'   r(   r)   r.   r.   /   s   „ Ù)÷"ò "r(   r.   c                  ó¦   ‡ — e Zd ZdZ ej
                  dddd¬«      Z ej
                  dddd¬«      Z G d„ d	«      Zd
„ Z	d„ Z
d„ Zd„ Zˆ fd„Zˆ xZS )ÚWithdrawalRequestSerializerz/Serializer for withdrawal creation and history.é   é   TzCalculated withdrawal fee)Ú
max_digitsÚdecimal_placesÚ	read_onlyÚ	help_textz&Amount after fees (what user receives)c                  ó    — e Zd ZeZg d¢Zg d¢Zy)ú WithdrawalRequestSerializer.Meta)r   r   r   r1   Údestination_addressr   r2   Ú
fee_amountÚ
net_amountÚrequested_atÚprocessed_at)r   r   r2   rA   rB   rC   rD   N)r!   r"   r#   r   r$   r%   r&   r'   r(   r)   r*   r?   R   s   „ Ø!ˆò
ˆò
Ñr(   r*   c                ó<   — |t         k  rt        dt         › �«      ‚|S )z,Validate withdrawal amount is above minimum.zMinimum withdrawal amount is $)ÚMIN_WITHDRAWALr	   ©ÚselfÚvalues     r)   Úvalidate_amountz+WithdrawalRequestSerializer.validate_amountk   s)   € à”>Ò!Ü!Ø0´Ð0@ÐAóð ð ˆr(   c                ó\   — |j                  «       dvrt        d«      ‚|j                  «       S )z!Validate blockchain is supported.r   z,Only 'tron' and 'bnb' networks are supported)Úlowerr	   rG   s     r)   Úvalidate_blockchainz/WithdrawalRequestSerializer.validate_blockchains   s(   € à�;‰;‹= Ñ/Ü!Ð"PÓQÐQØ�{‰{‹}Ðr(   c                ó  — | j                   d   j                  }t        t        |d   «      «      j	                  t
        t        ¬«      }|d   j                  «       }|t        z  t        d«      z  }t        j                  |t        d«      «      }||z   j	                  t
        t        ¬«      }||z   j	                  t
        t        ¬«      }|j                  |k  r&|j                  d›}	|d›}
t        dd|	› d	|
› �gi«      ‚||d
<   ||d<   |S )z%Validate user has sufficient balance.Úrequestr1   ©Úroundingr   Ú100Ú0ú.8fú!Insufficient balance. Available: z, Required (including fees): Ú
_total_feeÚ_total_deduction)ÚcontextÚuserr   ÚstrÚquantizeÚDECIMAL_QUANTIZEr   rL   ÚWITHDRAWAL_FEE_PERCENTÚWITHDRAWAL_FEE_FIXEDÚgetÚcredit_balancer	   )rH   ÚattrsrY   r1   r   Úfee_percentÚ	fee_fixedÚ	total_feeÚtotal_deductionÚavailable_strÚrequired_strs              r)   Úvalidatez$WithdrawalRequestSerializer.validatey   s*  € à�|‰|˜IÑ&×+Ñ+ˆÜœ˜U 8™_Ó-Ó.×7Ñ7Ô8HÔS`Ð7ÓaˆØ˜<Ñ(×.Ñ.Ó0ˆ
ð Ô5Ñ5¼À»ÑFˆÜ(×,Ñ,¨Z¼À»ÓFˆ	Ø  9Ñ,×6Ñ6Ô7GÔR_Ð6Ó`ˆ	ð " IÑ-×7Ñ7Ô8HÔS`Ð7Óaˆà×Ñ Ò0Ø#×2Ñ2°3Ð7ˆMØ-¨cÐ2ˆLÜ!ØØ7¸°ð G2Ø2>°ðAðð#ó ð ð (ˆˆlÑØ$3ˆÐ Ñ!àˆr(   c                ó  — | j                   d   j                  }t        t        |d   «      «      j	                  t
        t        ¬«      }|d   }|d   }|j                  d«      }|j                  d«      }t        «       }t        j                  «       5  |j                  j                  j                  «       j                  |j                  ¬«      }	|	j                   }
	 |j#                  |	j$                  d	|d
|› �t&        d|	j$                  ¬«       |	j1                  «        |	j                   }t2        j                  j5                  |	t2        j6                  |t2        j8                  |
|||t        |«      t        |«      t        |«      dœ¬«      }t:        j                  j5                  |	||d||t:        j<                  t        |«      t        |«      dœ¬«      }|cddd«       S # t(        $ r…}|j*                  �t        t        |j*                  «      «      n
t        d«      }|j,                  �t        t        |j,                  «      «      n
t        d«      }t/        dd|d›d|d›�gi«      ‚d}~ww xY w# 1 sw Y   yxY w)z-Create withdrawal request and deduct balance.rO   r1   rP   r   r@   rV   rW   )Úpkr`   zWithdrawal request to F)Úuser_idÚ
point_typer1   ÚremarksÚ
trans_typeÚallow_negativeÚiidNrS   rU   rT   z, Required: )r@   Úrequested_amountrA   rB   )rY   Útransaction_typer1   r   Úbalance_beforeÚbalance_afterr   ÚdataÚUSDT)rA   re   )rY   r   r   r   r1   r@   r   Úmetadata)rX   rY   r   rZ   r[   r\   r   Úpopr   r   ÚatomicÚ	__class__ÚobjectsÚselect_for_updater_   rj   r`   Údeduct_pointr   r   r
   Ú	availableÚ	requestedr	   Úrefresh_from_dbr   ÚcreateÚTYPE_WITHDRAWALÚSTATUS_PENDINGr   ÚSTATUS_REQUESTED)rH   Úvalidated_datarY   r1   r   r@   rd   re   Úwallet_serviceÚuser_lockedrs   Úer~   Úrequiredrt   ÚtxÚ
withdrawals                    r)   r�   z"WithdrawalRequestSerializer.create˜   sb  € à�|‰|˜IÑ&×+Ñ+ˆÜœ˜^¨HÑ5Ó6Ó7×@Ñ@ÔAQÔ\iÐ@ÓjˆØ# LÑ1ˆ
Ø,Ð-BÑCÐà"×&Ñ& |Ó4ˆ	Ø(×,Ñ,Ð-?Ó@ˆä+Ó-ˆä×ÑÕ!àŸ.™.×0Ñ0×BÑBÓD×HÑHÈDÏGÉGÐHÓTˆKØ(×7Ñ7ˆNðØ×+Ñ+Ø'ŸN™NØ/Ø*Ø4Ð5HÐ4IÐJÜ)Ø#(Ø#Ÿ™ð ,ô ð( ×'Ñ'Ô)Ø'×6Ñ6ˆMô ×$Ñ$×+Ñ+Ø Ü!,×!<Ñ!<Ø&Ü"×1Ñ1Ø-Ø+Ø%à+>Ü(+¨F«Ü"% i£.Ü"% f£+ñ	ð ,ó ˆBô" +×2Ñ2×9Ñ9Ø ØØ%ØØØ$7Ü(×9Ñ9ä"% i£.Ü'*¨?Ó';ñð :ó ˆJð ÷} "Ñ!øô  ,ò Ø9:¿¹Ð9PœG¤C¨¯©Ó$4Ô5ÔV]Ð^aÓVb�	Ø89¿¹Ð8Oœ7¤3 q§{¡{Ó#3Ô4ÔU\Ð]`ÓUa�Ü%ØØ;¸IÀc¸?ð K%Ø%-¨c Nð4ðð'ó ð ûðú÷! "Ð!ús3   ÂAI8Ã 3G'ÄC
I8Ç'	I5Ç0B I0É0I5É5I8É8Jc                óê   •— t         ‰| �  |«      }|j                  r9|j                  j                  dd«      |d<   t	        |j
                  «      |d<   |S d|d<   t	        |j
                  «      |d<   |S )z.Add calculated fee and net amount to response.rA   z
0.00000000rB   )ÚsuperÚto_representationrw   r_   rZ   r1   )rH   Úinstanceru   rz   s      €r)   rŽ   z-WithdrawalRequestSerializer.to_representationä   sx   ø€ ä‰wÑ(¨Ó2ˆð ×ÒØ!)×!2Ñ!2×!6Ñ!6°|À\Ó!RˆD�ÑÜ!$ X§_¡_Ó!5ˆD�Ñð
 ˆð ".ˆD�ÑÜ!$ X§_¡_Ó!5ˆD�Ñàˆr(   )r!   r"   r#   r,   r   ÚDecimalFieldrA   rB   r*   rJ   rM   rh   r�   rŽ   Ú__classcell__)rz   s   @r)   r7   r7   B   sn   ø„ Ù9à)�×)Ñ)ØØØØ-ô	€Jð *�×)Ñ)ØØØØ:ô	€J÷
ñ 
ò2òòò>J÷Xð r(   r7   N)"r,   Ú
__future__r   Údecimalr   r   Ú	django.dbr   Údjango.utilsr   Úrest_frameworkr   Úrest_framework.exceptionsr	   Údjango_wallet_utils.exceptionsr
   Úapps.transactions.modelsr   Úutils.walletr   r   Úmodelsr   r   r   r\   r]   r^   rF   ÚMIN_DEPOSITÚModelSerializerr   r.   r7   r'   r(   r)   Ú<module>rž      s£   ðÙ :å "ç *Ý !Ý !Ý &Ý 5å CÝ 0ß 7ç >Ñ >á˜<Ó(Ð ñ ! ›Ð á�F‹OÙ�6‹?ñÐ ñ ˜Ó!€Ù�f‹o€ô"˜{×:Ñ:ô "ô$"˜×3Ñ3ô "ô&n +×"=Ñ"=õ nr(   