
    dhI                     Z    S SK r S SKrS SKJrJr  S SKrS SKJr  S SKJ	r	   " S S\	5      r
g)    N)IteratorList)Document)
BaseLoaderc                       \ rS rSrSr       SS\S\S\S\S\S\S	\S
\4S jjrS\\	   4S jr
S\\	   4S jrS\\	   4S jrS\\	   4S jrS\\	   4S jrS\\	   4S jrS\\	   4S jrSrg)EtherscanLoader   zLoad transactions from `Ethereum` mainnet.

The Loader use Etherscan API to interact with Ethereum mainnet.

ETHERSCAN_API_KEY environment variable must be set use this loader.
account_addressapi_keyfilterpageoffsetstart_block	end_blocksortc	                    Xl         [        R                  R                  S5      =(       d    UU l        X0l        X@l        XPl        X`l        Xpl	        Xl
        U R                  (       d  [        S5      e[        R                  " SU R                   5      (       d  [        SU R                    35      eUS;  a  [        SU 35      eg )NETHERSCAN_API_KEYzEtherscan API key not providedz^0x[a-fA-F0-9]{40}$zInvalid contract address )normal_transactioninternal_transactionerc20_transactioneth_balanceerc721_transactionerc1155_transactionInvalid filter )r
   osenvirongetr   r   r   r   r   r   r   
ValueErrorrematch)	selfr
   r   r   r   r   r   r   r   s	            f/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/etherscan.py__init__EtherscanLoader.__init__   s      /zz~~&9:Eg	&"	||=>>xx.0D0DEE89M9M8NOPP 
 
 vh788
    returnc              #     #    / nU R                   S:X  a  U R                  5       nOU R                   S:X  a  U R                  5       nOU R                   S:X  a  U R                  5       nOuU R                   S:X  a  U R	                  5       nOTU R                   S:X  a  U R                  5       nO3U R                   S:X  a  U R                  5       nO[        S[          35      eU H  nUv   M	     g7f)	zLazy load Documents from table.r   r   r   r   r   r   r   N)r   	getNormTxgetInternalTx
getERC20TxgetEthBalancegetERC721TxgetERC1155Txr   )r!   resultdocs      r"   	lazy_loadEtherscanLoader.lazy_load6   s     ;;..^^%F[[22'')F[[//__&F[[M)'')F[[00%%'F[[11&&(Fvh788CI s   C)C+c                 v   SU R                    SU R                   SU R                   SU R                   SU R                   SU R
                   SU R                   3n [        R                  " U5      nUR                  5         WR                  5       S	   n/ n[        U5      S
:X  a
  [        SS9/$ U H5  n[!        U5      nUS   US   US   S.nUR#                  [        XxS95        M7     [        [        U5      5        U$ ! [        R                  R                   a  n[        SU5         S nANS nAff = f)NzBhttps://api.etherscan.io/api?module=account&action=txlist&address=&startblock=
&endblock=&page=&offset=&sort=&apikey=(Error occurred while making the request:r.   r    page_contentfromhashtor=   tx_hashr?   r<   metadatar
   r   r   r   r   r   r   requestsr   raise_for_status
exceptionsRequestExceptionprintjsonlenr   strappend	r!   urlresponseeitemsr.   itemcontentrC   s	            r"   r(   EtherscanLoader.getNormTxJ   s,   PQUQeQePf4++,Jt~~6FfTYYKt{{m6$))HT\\NL 	
	A||C(H%%' )u:?"-..D$iG $VfTRVZXHMM(KL  	c&k ""33 	A<a@@	As   &D D8"D33D8c                 ,   SU R                    SU R                   3n [        R                  " U5      nUR	                  5         [        WR                  5       S   S9/$ ! [        R
                  R                   a  n[        SU5         S nANJS nAff = f)NzChttps://api.etherscan.io/api?module=account&action=balance&address=z&tag=latest&apikey=r9   r.   r;   )
r
   r   rE   r   rF   rG   rH   rI   r   rJ   )r!   rO   rP   rQ   s       r"   r+   EtherscanLoader.getEthBalance`   s    ,,--@P 	
	A||C(H%%' hmmoh&?@AA ""33 	A<a@@	As   &A B=BBc                 N   SU R                    SU R                   SU R                   SU R                   SU R                   SU R
                   SU R                   3n [        R                  " U5      nUR                  5         WR                  5       S	   n/ n[        U5      S
:X  a
  [        SS9/$ U H5  n[!        U5      nUS   US   US   S.nUR#                  [        XxS95        M7     U$ ! [        R                  R                   a  n[        SU5         S nANS nAff = f)NzJhttps://api.etherscan.io/api?module=account&action=txlistinternal&address=r3   r4   r5   r6   r7   r8   r9   r.   r   r:   r;   r=   r>   r?   r@   rB   rD   rN   s	            r"   r)   EtherscanLoader.getInternalTxm   !   ,,-\$:J:J9K(tyyk$++TYYKx~7 		A||C(H%%' )u:?"-..D$iG $VfTRVZXHMM(KL   ""33 	A<a@@	A   &C0 0D$DD$c                 N   SU R                    SU R                   SU R                   SU R                   SU R                   SU R
                   SU R                   3n [        R                  " U5      nUR                  5         WR                  5       S	   n/ n[        U5      S
:X  a
  [        SS9/$ U H5  n[!        U5      nUS   US   US   S.nUR#                  [        XxS95        M7     U$ ! [        R                  R                   a  n[        SU5         S nANS nAff = f)NzChttps://api.etherscan.io/api?module=account&action=tokentx&address=r3   r4   r5   r6   r7   r8   r9   r.   r   r:   r;   r=   r>   r?   r@   rB   rD   rN   s	            r"   r*   EtherscanLoader.getERC20Tx   rZ   r[   c                 N   SU R                    SU R                   SU R                   SU R                   SU R                   SU R
                   SU R                   3n [        R                  " U5      nUR                  5         WR                  5       S	   n/ n[        U5      S
:X  a
  [        SS9/$ U H5  n[!        U5      nUS   US   US   S.nUR#                  [        XxS95        M7     U$ ! [        R                  R                   a  n[        SU5         S nANS nAff = f)NzFhttps://api.etherscan.io/api?module=account&action=tokennfttx&address=r3   r4   r5   r6   r7   r8   r9   r.   r   r:   r;   r=   r>   r?   r@   rB   rD   rN   s	            r"   r,   EtherscanLoader.getERC721Tx   rZ   r[   c                 N   SU R                    SU R                   SU R                   SU R                   SU R                   SU R
                   SU R                   3n [        R                  " U5      nUR                  5         WR                  5       S	   n/ n[        U5      S
:X  a
  [        SS9/$ U H5  n[!        U5      nUS   US   US   S.nUR#                  [        XxS95        M7     U$ ! [        R                  R                   a  n[        SU5         S nANS nAff = f)NzGhttps://api.etherscan.io/api?module=account&action=token1155tx&address=r3   r4   r5   r6   r7   r8   r9   r.   r   r:   r;   r=   r>   r?   r@   rB   rD   rN   s	            r"   r-   EtherscanLoader.getERC1155Tx   rZ   r[   )r
   r   r   r   r   r   r   r   N)z	docs-demor      
   r   idesc)__name__
__module____qualname____firstlineno____doc__rL   intr#   r   r   r0   r   r(   r+   r)   r*   r,   r-   __static_attributes__ r%   r"   r   r      s     #*!!9!9 !9 	!9
 !9 !9 !9 !9 !9F8H- (4> ,BtH~ BtH~ .DN .T(^ .d8n r%   r   )r   r   typingr   r   rE   langchain_core.documentsr   )langchain_community.document_loaders.baser   r   rl   r%   r"   <module>rp      s%    	 	 !  - @|j |r%   