@@ -2,12 +2,11 @@ import opensea from "../../opensea.app.mjs";
22import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform" ;
33
44export default {
5- name : "New Collection Events" ,
6- version : "0.0.3" ,
75 key : "opensea-new-collection-events" ,
8- description :
9- "Emit new filtered events. [See docs](https://docs.opensea.io/reference/retrieving-asset-events)" ,
10- dedupe : "greatest" ,
6+ name : "New Collection Events" ,
7+ description : "Emit new listings for a collection. [See the documentation](https://docs.opensea.io/reference/get_all_listings_on_collection_v2)" ,
8+ version : "0.0.4" ,
9+ dedupe : "unique" ,
1110 type : "source" ,
1211 props : {
1312 opensea,
@@ -18,54 +17,62 @@ export default {
1817 intervalSeconds : DEFAULT_POLLING_SOURCE_TIMER_INTERVAL ,
1918 } ,
2019 } ,
21- contractAddress : {
20+ collectionSlug : {
2221 type : "string" ,
23- label : "Contract Address" ,
24- description : "Collection contract address" ,
25- } ,
26- eventType : {
27- type : "string" ,
28- options : [
29- "sales" ,
30- "listings" ,
31- ] ,
32- label : "Event Type" ,
33- description : "OpenSea event type" ,
22+ label : "Collection Slug" ,
23+ description : "Unique string to identify a collection on OpenSea. This can be found by visiting the collection on the OpenSea website and noting the last path parameter." ,
3424 } ,
3525 } ,
3626 methods : {
3727 getLastTimestamp ( ) {
38- return this . db . get ( "lastTimestamp" ) ;
28+ return this . db . get ( "lastTimestamp" ) || 0 ;
3929 } ,
4030 setLastTimestamp ( ts ) {
4131 this . db . set ( "lastTimestamp" , ts ) ;
4232 } ,
43- } ,
44- async run ( ) {
45- const eventType = this . eventType === "sales"
46- ? "successful"
47- : "created" ;
48- const lastTimestamp = this . getLastTimestamp ( ) ;
49- let cursor = null ;
50- do {
51- const resp = await this . opensea . retrieveEvents ( {
52- contract : this . contractAddress ,
53- eventType,
54- occurredAfter : lastTimestamp ,
55- cursor,
56- } ) ;
57- resp . asset_events . forEach ( ( event ) => {
58- this . $emit ( event , {
59- id : event . id ,
60- summary : `${ event . asset . name } ${ this . eventType } event` ,
61- ts : + new Date ( event . created_date ) ,
62- } ) ;
33+ generateMeta ( item ) {
34+ return {
35+ id : item . order_hash ,
36+ summary : `New ${ item . type } ${ item . chain } Listing` ,
37+ ts : item . protocol_data . parameters . startTime ,
38+ } ;
39+ } ,
40+ async processEvent ( max ) {
41+ const lastTimestamp = this . getLastTimestamp ( ) ;
42+
43+ const results = this . opensea . paginate ( {
44+ fn : this . opensea . retrieveEvents ,
45+ args : {
46+ collectionSlug : this . collectionSlug ,
47+ } ,
48+ resourceKey : "listings" ,
6349 } ) ;
64- if ( ! cursor && resp . asset_events . length > 0 ) {
65- const ts = Math . floor ( new Date ( resp . asset_events [ 0 ] . created_date ) . getTime ( ) / 1000 ) ;
66- this . setLastTimestamp ( ts ) ;
50+
51+ let items = [ ] ;
52+ for await ( const result of results ) {
53+ const ts = result . protocol_data . parameters . startTime ;
54+ if ( ts >= lastTimestamp ) {
55+ items . push ( result ) ;
56+ }
6757 }
68- cursor = resp . next ;
69- } while ( lastTimestamp && cursor ) ;
58+
59+ if ( max ) {
60+ items = items . slice ( - 1 * max ) ;
61+ }
62+ this . setLastTimestamp ( items [ items . length - 1 ] . protocol_data . parameters . startTime ) ;
63+
64+ items . forEach ( ( item ) => {
65+ const meta = this . generateMeta ( item ) ;
66+ this . $emit ( item , meta ) ;
67+ } ) ;
68+ } ,
69+ } ,
70+ hooks : {
71+ async deploy ( ) {
72+ await this . processEvent ( 25 ) ;
73+ } ,
74+ } ,
75+ async run ( ) {
76+ await this . processEvent ( ) ;
7077 } ,
7178} ;
0 commit comments