@@ -9,11 +9,11 @@ const { random } = require('lodash')
99const BURN_ADDRESS = 'tz1burnburnburnburnburnburnburjAYjjX'
1010
1111require ( 'dotenv' ) . config ( )
12+ const { Semaphore } = require ( 'prex' )
1213
1314const reducer = ( accumulator , currentValue ) => parseInt ( accumulator ) + parseInt ( currentValue )
1415
1516const getIpfsHash = async ( ipfsHash ) => {
16-
1717 return await axios . get ( 'https://cloudflare-ipfs.com/ipfs/' + ipfsHash ) . then ( res => res . data )
1818 /* const nftDetailJson = await nftDetails.json();
1919
@@ -49,14 +49,14 @@ const owners = async (obj) => {
4949 var values_arr = ( _ . values ( owners ) )
5050 obj . total_amount = ( values_arr . map ( e => parseInt ( e ) ) ) . length > 0 ? values_arr . filter ( e => parseInt ( e ) > 0 ) . reduce ( reducer ) : 0
5151 obj . owners = owners
52- console . log ( obj )
52+ // console.log(obj)
5353 //obj.total_amount = (values_arr.map(e => parseInt(e))).reduce(reducer)
5454 return obj
5555}
5656
5757const totalAmountIntegral = async ( obj ) => {
5858 var owners = await axios . get ( 'https://api.better-call.dev/v1/contract/mainnet/KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton/tokens/holders?token_id=' + obj . token_id ) . then ( res => res . data )
59- console . log ( owners )
59+ // console.log(owners)
6060 var values_arr = ( _ . values ( owners ) )
6161 obj . total_amount = ( values_arr . map ( e => parseInt ( e ) ) ) . length > 0 ? ( values_arr . filter ( e => parseInt ( e ) ) ) : 0
6262
@@ -144,20 +144,19 @@ const randomFeed = async (counter, res) => {
144144 feed = await feed . map ( async e => {
145145 e . token_info = await getIpfsHash ( e . ipfsHash )
146146 e . token_id = parseInt ( e . objectId )
147- console . log ( e )
147+ // console.log(e)
148148 return e
149149 } )
150150 var promise = Promise . all ( feed . map ( e => e ) )
151151 promise . then ( async ( results ) => {
152152 var aux_arr = results . map ( e => e )
153153 //res.set('Cache-Control', `public, max-age=${cache_time}`)
154- console . log ( aux_arr )
154+ // console.log(aux_arr)
155155 res . json ( { result : aux_arr } )
156156 } )
157157}
158158
159- const getFeed = async ( counter , res , featured ) => {
160-
159+ const getFeed = async ( counter , featured ) => {
161160 /* const now_time = Date.now()
162161 const immutable = (typeof max_time !== 'undefined') && (max_time < now_time)
163162 max_time = (typeof max_time !== 'undefined') ? max_time : customFloor(now_time, ONE_MINUTE_MILLIS)
@@ -171,7 +170,7 @@ const getFeed = async (counter, res, featured) => {
171170 }
172171
173172 var feed = offset ( desc ( arr ) , counter )
174- console . log ( feed )
173+ // console.log(feed)
175174 feed = await feed . map ( async e => {
176175 e . token_info = await getIpfsHash ( e . ipfsHash )
177176 e . token_id = parseInt ( e . objectId )
@@ -187,13 +186,13 @@ const getFeed = async (counter, res, featured) => {
187186 cache_time = (int)(((max_time + ONE_MINUTE_MILLIS) - now_time) / 1000)
188187 } */
189188 var promise = Promise . all ( feed . map ( e => e ) )
190- promise . then ( async ( results ) => {
189+ return promise . then ( async ( results ) => {
191190 var aux_arr = results . map ( e => e )
192191
193192 //res.set('Cache-Control', `public, max-age=${cache_time}`)
194193
195- console . log ( aux_arr )
196- res . json ( { result : aux_arr } )
194+ // console.log(aux_arr)
195+ return aux_arr
197196 } )
198197}
199198
@@ -309,6 +308,50 @@ const hDAOFeed = async (counter, res) => {
309308 } )
310309}
311310
311+ // list of restricted addresses
312+ const restrictedAdddressesCacheTimeLimit = ONE_MINUTE_MILLIS // the blockchain updates about once a minute
313+ let restrictedAddressesCache = null
314+ const restrictedAddressesLock = new Semaphore ( 1 )
315+ const getRestrictedAddresses = async ( ) => {
316+ await restrictedAddressesLock . wait ( )
317+ if ( restrictedAddressesCache && Date . now ( ) - restrictedAddressesCache . expires < restrictedAdddressesCacheTimeLimit ) {
318+ restrictedAddressesLock . release ( )
319+ // console.log('ADDRESS restrictions from CACHE')
320+ return restrictedAddressesCache . data
321+ }
322+
323+ const list = await axios . get ( 'https://raw.githubusercontent.com/hicetnunc2000/hicetnunc/main/filters/w.json' ) . then ( res => res . data )
324+ restrictedAddressesCache = {
325+ expires : Date . now ( ) ,
326+ data : list
327+ }
328+ restrictedAddressesLock . release ( )
329+ // console.log('ADDRESS restrictions from NEW')
330+ return list
331+ }
332+
333+ // list of restricted objkts
334+ const restrictedObjectsCacheTimeLimit = ONE_MINUTE_MILLIS // the blockchain updates about once a minute
335+ let restrictedObjectsCache = null
336+ const restrictedObjectsLock = new Semaphore ( 1 )
337+ const getRestrictedObjkts = async ( ) => {
338+ await restrictedObjectsLock . wait ( )
339+ if ( restrictedObjectsCache && Date . now ( ) - restrictedObjectsCache . expires < restrictedObjectsCacheTimeLimit ) {
340+ restrictedObjectsLock . release ( )
341+ // console.log('OBJKT restrictions from CACHE')
342+ return restrictedObjectsCache . data
343+ }
344+
345+ const list = await axios . get ( 'https://raw.githubusercontent.com/hicetnunc2000/hicetnunc/main/filters/o.json' ) . then ( res => res . data )
346+ restrictedObjectsCache = {
347+ expires : Date . now ( ) ,
348+ data : list
349+ }
350+ restrictedObjectsLock . release ( )
351+ // console.log('OBJKT restrictions from NEW')
352+ return list
353+ }
354+
312355//getObjkts()
313356//testSwaps()
314357//getFeed(1)
@@ -322,28 +365,38 @@ const app = express()
322365app . use ( express . json ( ) )
323366app . use ( cors ( { origin : '*' } ) )
324367
325- app . post ( '/featured' , async ( req , res ) => {
326- /*
327- var counter = req.query.counter
328- var max_time = req.query.hasOwnProperty('time') ? customFloor(req.query.time, ONE_MINUTE_MILLIS) : null
329- const now_time_qt = customFloor(Date.now(), ONE_MINUTE_MILLIS)
330- if (max_time != null & max_time > now_time_qt) {
331- max_time = null
332- }
333- */
334- await getFeed ( req . body . counter , res , true )
335- } )
368+ // used for very simple caching of the feed
369+ const feedCacheTimeLimit = ONE_MINUTE_MILLIS // the blockchain updates about once a minute
370+ const feedCache = { }
371+ const feedLocks = { }
336372
337- app . post ( '/feed' , async ( req , res ) => {
338- /*
339- var counter = req.query.counter
340- var max_time = req.query.hasOwnProperty('time') ? customFloor(req.query.time, ONE_MINUTE_MILLIS) : null
341- const now_time_qt = customFloor(Date.now(), ONE_MINUTE_MILLIS)
342- if (max_time != null & max_time > now_time_qt) {
343- max_time = null
344- }
345- */
346- await getFeed ( req . body . counter , res , false )
373+ const getFeedLock = ( key ) => {
374+ if ( ! feedLocks [ key ] ) {
375+ feedLocks [ key ] = new Semaphore ( 1 )
376+ }
377+ return feedLocks [ key ]
378+ }
379+
380+ app . post ( '/feed|/featured' , async ( req , res ) => {
381+ const feedOffset = req . body . counter || 0
382+ const isFeatured = req . path === '/featured'
383+ const lockKey = `${ feedOffset } -${ isFeatured ? 'featured' : '' } `
384+
385+ await getFeedLock ( lockKey ) . wait ( )
386+ if ( feedCache [ lockKey ] && Date . now ( ) - feedCache [ lockKey ] . expires < feedCacheTimeLimit ) {
387+ getFeedLock ( lockKey ) . release ( )
388+ // console.log('Feed from CACHE')
389+ return res . json ( { result : feedCache [ lockKey ] . data } )
390+ }
391+
392+ const aux_arr = await getFeed ( feedOffset , isFeatured )
393+ feedCache [ lockKey ] = {
394+ expires : Date . now ( ) ,
395+ data : aux_arr
396+ }
397+ getFeedLock ( lockKey ) . release ( )
398+ // console.log('Feed from NEW')
399+ return res . json ( { result : aux_arr } )
347400} )
348401
349402app . post ( '/random' , async ( req , res ) => {
@@ -353,7 +406,7 @@ app.post('/random', async (req, res) => {
353406app . post ( '/tz' , async ( req , res ) => {
354407
355408 // list of restricted addresses
356- var list = await axios . get ( 'https://raw.githubusercontent.com/hicetnunc2000/hicetnunc/main/filters/w.json' ) . then ( res => res . data )
409+ var list = await getRestrictedAddresses ( )
357410
358411 list . includes ( req . body . tz )
359412 ?
@@ -366,7 +419,7 @@ app.post('/tz', async (req, res) => {
366419app . post ( '/objkt' , async ( req , res ) => {
367420
368421 // list of restricted objkts
369- var list = await axios . get ( 'https://raw.githubusercontent.com/hicetnunc2000/hicetnunc/main/filters/o.json' ) . then ( res => res . data )
422+ var list = await getRestrictedObjkts ( )
370423
371424 list . includes ( parseInt ( req . body . objkt_id ) )
372425 ?
@@ -384,9 +437,10 @@ app.post('/hdao', async (req, res) => {
384437 await hDAOFeed ( parseInt ( req . body . counter ) , res )
385438} )
386439
387- const testhdao = async ( ) => await hDAOFeed ( parseInt ( 0 ) )
440+ // const testhdao = async () => await hDAOFeed(parseInt(0))
388441//testhdao()
389442
390- //app.listen(3001)
391- module . exports . handler = serverless ( app )
443+ app . listen ( 3001 )
444+ console . log ( 'SERVER RUNNING ON localhost:3001' )
445+ // module.exports.handler = serverless(app)
392446
0 commit comments