
    |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&++U8_-.778HS`7a<(..0
 55F(,,ZF	 9,667GR_6`	 "I-778HS`7a0#2237M-c2L!7 G22>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HDGGHTK(77N++'NN/*45H4IJ)#(# , ( '')'66M $$++ !,!<!<&"11-+%+>(+F"%i."%f+	 , B" +2299 %$7(99"%i.'*?'; : J } "!  , 9:9PGC$45V]^aVb	898O73q{{#34U\]`Ua%;Ic? K%%-cN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|\!RD!$X__!5D
  ".D!$X__!5D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 > ><(  ! FO6?  !fo"{:: "$"33 "&n+"="= nr(   