1
1
const router = require ( "express" ) . Router ( ) ;
2
2
const { Alchemy } = require ( 'alchemy-sdk' ) ;
3
+ const axios = require ( 'axios' )
4
+
5
+ const Key = process . env . ALCHEMY_API_KEY
3
6
4
7
// Configuration settings for each network
5
8
const configs = {
6
9
Eth : {
7
- apiKey : process . env . ALCHEMY_API_KEY ,
10
+ apiKey : Key ,
8
11
network : 'eth-mainnet' ,
9
12
} ,
10
13
Polygon : {
11
- apiKey : process . env . ALCHEMY_API_KEY ,
14
+ apiKey : Key ,
12
15
network : 'polygon-mainnet'
13
16
} ,
14
17
Aritrum : {
15
- apiKey : process . env . ALCHEMY_API_KEY ,
18
+ apiKey : Key ,
16
19
network : "arbitrum-mainnet"
17
20
} ,
18
21
Optimism : {
19
- apiKey : process . env . ALCHEMY_API_KEY ,
22
+ apiKey : Key ,
20
23
network : "optimism-mainnet"
21
24
}
22
25
} ;
23
26
27
+
24
28
// // testing
25
29
// router.get('/testing/:address', async (req, res) => {
26
30
// console.log('===test===')
@@ -81,27 +85,47 @@ router.get('/nft/wallet/:address', async (req, res) => {
81
85
console . log ( '==============/NFT/wallet==============' )
82
86
const address = req . params . address
83
87
88
+ // const fetchNFTperNetwork = async (net, config, address) => {
89
+ // const alchemy = new Alchemy(config);
90
+ // let options = {
91
+ // omitMetadata: true
92
+ // }
93
+ // try {
94
+ // // use interator version to get back all nfts with paging
95
+ // const nftsIterator = alchemy.nft.getNftsForOwnerIterator(address, options);
96
+ // let nfts = [];
97
+ // //for each entry push to array
98
+ // for await (const item of nftsIterator) {
99
+ // nfts.push(item);
100
+ // }
101
+
102
+ // console.log(`completed ${net}`)
103
+ // console.log(`total item in ${net}, ${nfts.length}`)
104
+ // return { [net]: nfts };
105
+ // } catch (err) {
106
+ // console.error(`Failed to fetch NFT for ${net} network`)
107
+ // return { [net]: { error: `Failed to fetch NFT for ${net} network`, details: err.message } }
108
+ // };
109
+ // };
110
+
84
111
const fetchNFTperNetwork = async ( net , config , address ) => {
85
112
const alchemy = new Alchemy ( config ) ;
86
- let options = {
87
- omitMetadata : true
88
- }
113
+ console . log ( config ) ;
89
114
try {
90
- // use interator version to get back all nfts with paging
91
- const nftsIterator = alchemy . nft . getNftsForOwnerIterator ( address , options ) ;
92
- let nfts = [ ] ;
93
- //for each page push to array
94
- for await ( const item of nftsIterator ) {
95
- nfts . push ( item ) ;
96
- }
97
-
98
- console . log ( `completed ${ net } ` )
99
- console . log ( `total item in ${ net } , ${ nfts . length } ` )
100
- return { [ net ] : nfts } ;
115
+ const nfts = await alchemy . nft . getNftsForOwner ( address ) ;
116
+ console . log ( `completed ${ net } , totalCount: ${ nfts . totalCount } ` ) ;
117
+ //returning an object for each network, including res, pulled out totalCount and pageKey for easier access
118
+ return {
119
+ [ net ] : {
120
+ nfts,
121
+ "totalCount" : nfts . totalCount ,
122
+ "pageKey" : nfts . pageKey
123
+ }
124
+ } ;
101
125
} catch ( err ) {
102
- console . error ( `Failed to fetch NFT for ${ net } network` )
103
- return { [ net ] : { error : `Failed to fetch NFT for ${ net } network` , details : err . message } }
104
- } ;
126
+ console . error ( `Failed to fetch NFT for ${ net } network` ) ;
127
+ return { [ net ] : { error : `Failed to fetch NFT for ${ net } network` , details : err . message } } ;
128
+ }
105
129
} ;
106
130
107
131
try {
@@ -113,6 +137,7 @@ router.get('/nft/wallet/:address', async (req, res) => {
113
137
) ;
114
138
// Combine the results into a single array
115
139
const combinedResults = results . reduce ( ( acc , result ) => ( { ...acc , ...result } ) , { } ) ;
140
+
116
141
res . json ( [ combinedResults ] ) ;
117
142
} catch ( err ) {
118
143
console . log ( err ) ;
@@ -122,41 +147,63 @@ router.get('/nft/wallet/:address', async (req, res) => {
122
147
123
148
124
149
125
- // router.get('nft/collection:contractAdd?:slug?', async (req, res) => {
126
- // console.log('==============/NFT/collection==============')
127
-
128
- // const contractAdd = req.params.contractAdd
129
- // const slug = req.params.slug
150
+ router . get ( '/nft/collection/:net' , async ( req , res ) => {
151
+ console . log ( '==============/NFT/collection==============' )
152
+ console . log ( ` network selcted: ${ req . params . net } ` )
130
153
131
- // const fetchNFTperNetwork = async (net, config, address) => {
132
- // const alchemy = new Alchemy(config);
133
- // try {
134
- // const nfts = await alchemy.nft.getNftsForOwner(address);
135
- // console.log(`completed ${net}`)
136
- // return { [net]: nfts };
137
- // } catch (err) {
138
- // console.error(`Failed to fetch NFT for ${net} network`)
139
- // return { [net]: { error: `Failed to fetch NFT for ${net} network`, details: err.message } }
140
- // };
141
- // };
142
-
143
- // // check to make sure that only 1 optional param is passed
144
- // //please see https://docs.alchemy.com/reference/getnftsforcollection-v3
145
- // //if both slug name and address is provided, return 400 error
146
- // if ((contractAdd && slug) || (!contractAdd) && (!slug)) {
147
- // return res.status(400).json({ err: 'Please provide only contract OR slug' })
148
- // }
149
-
150
- // try {
151
- // if (contractAdd) {
152
- // nfts = await alchemy.nft.get
153
- // }
154
- // }
154
+ //use query param to pass thru
155
+ // api/nft/collection/eth?contractAdd=x&slug=y
155
156
157
+ const input = {
158
+ contractAdd : req . query . contractAdd ,
159
+ slug : req . query . slug
160
+ }
161
+ console . log ( input )
162
+
163
+ const fetchNFTCollection = async ( net , configs , input ) => {
164
+ //using the network param passed to decide which setting to use
165
+ const config = { ...configs [ net ] }
166
+ console . log ( config )
167
+
168
+ let finalInput = input . contractAdd ?
169
+ `contractAddress=${ input . contractAdd } ` :
170
+ `collectionSlug=${ input . slug } ` ;
171
+
172
+ //sdk doesn't support slug name? using axios to fetch the collection endpoint which support slug names
173
+ const options = {
174
+ method : 'GET' ,
175
+ url : `https://${ config . network } .g.alchemy.com/nft/v3/${ Key } /getNFTsForContract?` +
176
+ `${ finalInput } ` + `&withMetadata=true` ,
177
+ headers : { accept : 'application/json' }
178
+ } ;
156
179
180
+ try {
181
+ console . log ( 'url' , options . url )
182
+ const nfts = await axios . request ( options ) ;
183
+ console . log ( `completed ${ net } ` )
184
+ console . log ( `total item in Collection - ${ finalInput } , ${ nfts . data . nfts . length } ` )
185
+ return { ...nfts . data }
186
+ } catch ( err ) {
187
+ console . error ( `Failed to fetch NFT for ${ finalInput } for ${ net } network` )
188
+ return { error : `Failed to fetch NFT for ${ net } network` , details : err . message }
189
+ } ;
190
+ } ;
191
+ //check to make sure that only 1 optional param is passed
192
+ //please see https://docs.alchemy.com/reference/getnftsforcollection-v3
193
+ //if both slug name and address is provided, return 400 error
194
+ if ( ( input . contractAdd && input . slug ) || ( ! input . contractAdd ) && ( ! input . slug ) ) {
195
+ return res . status ( 400 ) . json ( { err : 'Please provide only contract OR slug' } )
196
+ }
197
+ try {
198
+ const results = await fetchNFTCollection ( req . params . net , configs , input )
157
199
158
200
201
+ res . json ( results ) ;
202
+ } catch ( err ) {
203
+ console . log ( err ) ;
204
+ res . status ( 500 ) . json ( err ) ;
205
+ } ;
159
206
160
- // })
207
+ } )
161
208
162
209
module . exports = router ;
0 commit comments