o
    ZhI                     @   sT   d dl Z d dlZd dlmZmZ d dlZd dlmZ d dlm	Z	 G dd de	Z
dS )    N)IteratorList)Document)
BaseLoaderc                   @   s   e Zd ZdZ							d#d	ed
ededededededefddZdee fddZ	de
e fddZde
e fddZde
e fddZde
e fddZde
e fddZde
e fd d!Zd"S )$EtherscanLoaderzLoad 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.
    	docs-demonormal_transaction   
   r   descaccount_addressapi_keyfilterpageoffsetstart_block	end_blocksortc	           	      C   s   || _ tjdp
|| _|| _|| _|| _|| _|| _	|| _
| js%tdtd| j s4td| j  |dvr?td| d S )NZETHERSCAN_API_KEYzEtherscan API key not providedz^0x[a-fA-F0-9]{40}$zInvalid contract address )r   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    r"   e/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/etherscan.py__init__   s   zEtherscanLoader.__init__returnc                 c   s    g }| j dkr|  }n9| j dkr|  }n/| j dkr!|  }n%| j dkr+|  }n| j dkr5|  }n| j dkr?|  }ntdt  |D ]}|V  qHdS )	zLazy load Documents from table.r   r   r   r   r   r   r   N)r   	getNormTxgetInternalTx
getERC20TxgetEthBalancegetERC721TxgetERC1155Txr   )r!   resultdocr"   r"   r#   	lazy_load6   s$   











zEtherscanLoader.lazy_loadc           	      C   s   d| j  d| j d| j d| j d| j d| j d| j }zt|}|	  W n tj
jyB } ztd| W Y d }~nd }~ww | d	 }g }t|d
krWtddgS |D ]}t|}|d |d |d d}|t||d qYtt| |S )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r9   Ztx_hashr;   r8   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contentr>   r"   r"   r#   r&   J   s>   

zEtherscanLoader.getNormTxc              
   C   sr   d| j  d| j }zt|}|  W n tjjy. } ztd| W Y d }~nd }~ww t|	 d dgS )NzChttps://api.etherscan.io/api?module=account&action=balance&address=z&tag=latest&apikey=r5   r,   r7   )
r   r   r@   r   rA   rB   rC   rD   r   rE   )r!   rJ   rK   rL   r"   r"   r#   r)   `   s   
zEtherscanLoader.getEthBalancec           	      C      d| j  d| j d| j d| j d| j d| j d| j }zt|}|	  W n tj
jyB } ztd| W Y d }~nd }~ww | d	 }g }t|d
krWtddgS |D ]}t|}|d |d |d d}|t||d qY|S )NzJhttps://api.etherscan.io/api?module=account&action=txlistinternal&address=r/   r0   r1   r2   r3   r4   r5   r,   r   r6   r7   r9   r:   r;   r<   r=   r?   rI   r"   r"   r#   r'   m   @   
zEtherscanLoader.getInternalTxc           	      C   rP   )NzChttps://api.etherscan.io/api?module=account&action=tokentx&address=r/   r0   r1   r2   r3   r4   r5   r,   r   r6   r7   r9   r:   r;   r<   r=   r?   rI   r"   r"   r#   r(      rQ   zEtherscanLoader.getERC20Txc           	      C   rP   )NzFhttps://api.etherscan.io/api?module=account&action=tokennfttx&address=r/   r0   r1   r2   r3   r4   r5   r,   r   r6   r7   r9   r:   r;   r<   r=   r?   rI   r"   r"   r#   r*      rQ   zEtherscanLoader.getERC721Txc           	      C   rP   )NzGhttps://api.etherscan.io/api?module=account&action=token1155tx&address=r/   r0   r1   r2   r3   r4   r5   r,   r   r6   r7   r9   r:   r;   r<   r=   r?   rI   r"   r"   r#   r+      rQ   zEtherscanLoader.getERC1155TxN)r   r   r	   r
   r   r   r   )__name__
__module____qualname____doc__rG   intr$   r   r   r.   r   r&   r)   r'   r(   r*   r+   r"   r"   r"   r#   r      sB    
	
#r   )r   r   typingr   r   r@   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r   r"   r"   r"   r#   <module>   s    