
    Ng                         d dl Z d dlZd dlZd dlm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e          Z G d d	e          ZdS )
    N)Enum)ListOptional)Document)
BaseLoaderc                   ^    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdS )BlockchainTypez(Enumerator of the supported blockchains.zeth-mainnetz
eth-goerlizeth-sepoliazeth-holeskyzpolygon-mainnetzpolygon-mumbaizpolygon-amoyzarb-mainnetzarb-sepoliazopt-mainnetzopt-sepoliazbase-mainnetzbase-sepoliazblast-mainnetzblast-sepoliazzksync-mainnetzzksync-sepoliazzora-mainnetzzora-sepoliaN)__name__
__module____qualname____doc__ETH_MAINNET
ETH_GOERLIETH_SEPOLIAETH_HOLESKYPOLYGON_MAINNETPOLYGON_MUMBAIPOLYGON_AMOYARB_MAINNETARB_SEPOLIA
OP_MAINNET
OP_SEPOLIABASE_MAINNETBASE_SEPOLIABLAST_MAINNETBLAST_SEPOLIAZKSYNC_MAINNETZKSYNC_SEPOLIAZORA_MAINNETZORA_SEPOLIA     k/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/blockchain.pyr	   r	      ss        22KJKK'O%N!LKKJJ!L!L#M#M%N%N!L!LLLr"   r	   c                       e Zd ZdZej        ddddfdededed	ed
edee	         fdZ
dee         fdZdedefdZededefd            ZdS )BlockchainDocumentLoadera9  Load elements from a blockchain smart contract.

    See supported blockchains here: https://python.langchain.com/v0.2/api_reference/community/document_loaders/langchain_community.document_loaders.blockchain.BlockchainType.html

    If no BlockchainType is specified, the default is Ethereum mainnet.

    The Loader uses the Alchemy API to interact with the blockchain.
    ALCHEMY_API_KEY environment variable must be set to use this loader.

    The API returns 100 NFTs per request and can be paginated using the
    startToken parameter.

    If get_all_tokens is set to True, the loader will get all tokens
    on the contract.  Note that for contracts with a large number of tokens,
    this may take a long time (e.g. 10k tokens is 100 requests).
    Default value is false for this reason.

    The max_execution_time (sec) can be set to limit the execution time
    of the loader.

    Future versions of this loader can:
        - Support additional Alchemy APIs (e.g. getTransactions, etc.)
        - Support additional blockchain APIs (e.g. Infura, Opensea, etc.)
    z	docs-demo FNcontract_addressblockchainTypeapi_key
startTokenget_all_tokensmax_execution_timec                 0   || _         |j        | _        t          j                            d          p|| _        || _        || _        || _	        | j        st          d          t          j        d| j                   st          d| j                    dS )au  

        Args:
            contract_address: The address of the smart contract.
            blockchainType: The blockchain type.
            api_key: The Alchemy API key.
            startToken: The start token for pagination.
            get_all_tokens: Whether to get all tokens on the contract.
            max_execution_time: The maximum execution time (sec).
        ALCHEMY_API_KEYzAlchemy API key not provided.z^0x[a-fA-F0-9]{40}$zInvalid contract address N)r'   valuer(   osenvirongetr)   r*   r+   r,   
ValueErrorrematch)selfr'   r(   r)   r*   r+   r,   s          r#   __init__z!BlockchainDocumentLoader.__init__?   s    & !1,2z~~&788CG$,"4| 	><===x.0EFF 	RP9NPPQQQ	R 	Rr"   returnc                    g }| j         }t          j                    }	 d| j         d| j         d| j         d| }t          j        |          }|j        dk    rt          d|j                   |	                                d         }|sn|D ]S}t          |          }|d	         d
         }	| j        | j        |	d}
|                    t          ||
                     T| j        sn]|                     |d         j        d
                   }| j        .t          j                    |z
  | j        k    rt#          d          +|st          d| j                   |S )NTzhttps://z.g.alchemy.com/nft/v2/z8/getNFTsForCollection?withMetadata=True&contractAddress=z&startToken=   z Request failed with status code nftsidtokenId)source
blockchainr=   )page_contentmetadataz/Execution time exceeded the allowed time limit.z#No NFTs found for contract address )r*   timer(   r)   r'   requestsr2   status_coder3   jsonstrappendr   r+   _get_next_tokenIdrA   r,   RuntimeError)r6   resultcurrent_start_token
start_timeurlresponseitemsitemcontentr=   rA   s              r#   loadzBlockchainDocumentLoader.load_   s   "oY[[
)	V54. 5 5<5 5(,(=5 5  35 5   |C((H#s** Mx7KMM   MMOOF+E  Q Qd))t*Y/"3"&"5& 
 hGhOOOPPPP &  #'"8"89LY9W"X"X '3Y[[:-1HHH"#TUUUS)	VV  	Md6KMM   r"   r=   c           
         |                      |          }|dk    rt          |d          }n.|dk    rt          |dd          d          }nt          |          }|dz   }|dk    r6dt          |dt          t	          |          dz
            z   dz             z   S |dk    r6d	t          |dt          t	          |          d
z
            z   dz             z   S t          |          S )Nhex_0x   hex_0xbf      0x0x0xbf   )_detect_value_typeintformatrG   len)r6   r=   
value_type	value_intrK   s        r#   rI   z*BlockchainDocumentLoader._get_next_tokenId   s    ,,W55
!!GR((II:%%GABBK,,IIGIQ!!&s3w<<!3C/D/D)Ds)JKKKK:%%F63S\\A5E1F1F+F+LMMMMv;;r"   c                     t          | t                    rdS |                     d          rdS |                     d          rdS dS )Nr`   rZ   rU   r]   rW   )
isinstancer`   
startswith)r=   s    r#   r_   z+BlockchainDocumentLoader._detect_value_type   sT    gs## 	5%% 	8'' 	::r"   )r
   r   r   r   r	   r   rG   boolr   r`   r7   r   r   rS   rI   staticmethodr_   r!   r"   r#   r%   r%   %   s        8 *8)C"$,0R RR 'R 	R
 R R %SMR R R R@7d8n 7 7 7 7t     ( C C    \  r"   r%   )r0   r4   rC   enumr   typingr   r   rD   langchain_core.documentsr   )langchain_community.document_loaders.baser   r	   r%   r!   r"   r#   <module>rn      s    				 				        ! ! ! ! ! ! ! !  - - - - - - @ @ @ @ @ @" " " " "T " " "0Q Q Q Q Qz Q Q Q Q Qr"   