File size: 3,791 Bytes
cfd3735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "vm8vn9t8DvC_"
   },
   "source": [
    "# Blockchain"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5WjXERXzFEhg"
   },
   "source": [
    "## Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "juAmbgoWD17u"
   },
   "source": [
    "The intention of this notebook is to provide a means of testing functionality in the Langchain Document Loader for Blockchain.\n",
    "\n",
    "Initially this Loader supports:\n",
    "\n",
    "*   Loading NFTs as Documents from NFT Smart Contracts (ERC721 and ERC1155)\n",
    "*   Ethereum Maninnet, Ethereum Testnet, Polgyon Mainnet, Polygon Testnet (default is eth-mainnet)\n",
    "*   Alchemy's getNFTsForCollection API\n",
    "\n",
    "It can be extended if the community finds value in this loader.  Specifically:\n",
    "\n",
    "*   Additional APIs can be added (e.g. Tranction-related APIs)\n",
    "\n",
    "This Document Loader Requires:\n",
    "\n",
    "*   A free [Alchemy API Key](https://www.alchemy.com/)\n",
    "\n",
    "The output takes the following format:\n",
    "\n",
    "- pageContent= Individual NFT\n",
    "- metadata={'source': '0x1a92f7381b9f03921564a437210bb9396471050c', 'blockchain': 'eth-mainnet', 'tokenId': '0x15'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load NFTs into Document Loader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get ALCHEMY_API_KEY from https://www.alchemy.com/ \n",
    "\n",
    "alchemyApiKey = \"...\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Option 1: Ethereum Mainnet (default BlockchainType)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "J3LWHARC-Kn0"
   },
   "outputs": [],
   "source": [
    "from langchain.document_loaders.blockchain import BlockchainDocumentLoader, BlockchainType\n",
    "contractAddress = \"0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d\" # Bored Ape Yacht Club contract address\n",
    "\n",
    "blockchainType = BlockchainType.ETH_MAINNET  #default value, optional parameter\n",
    "\n",
    "blockchainLoader = BlockchainDocumentLoader(contract_address=contractAddress,\n",
    "                                            api_key=alchemyApiKey)\n",
    "\n",
    "nfts = blockchainLoader.load()\n",
    "\n",
    "nfts[:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Option 2: Polygon Mainnet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "contractAddress = \"0x448676ffCd0aDf2D85C1f0565e8dde6924A9A7D9\" # Polygon Mainnet contract address\n",
    "\n",
    "blockchainType = BlockchainType.POLYGON_MAINNET \n",
    "\n",
    "blockchainLoader = BlockchainDocumentLoader(contract_address=contractAddress, \n",
    "                                            blockchainType=blockchainType, \n",
    "                                            api_key=alchemyApiKey)\n",
    "\n",
    "nfts = blockchainLoader.load()\n",
    "\n",
    "nfts[:2]"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [
    "5WjXERXzFEhg"
   ],
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}