11import opensea from "../../opensea.app.mjs" ;
22import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform" ;
3+ import md5 from "md5" ;
34
45export default {
5- name : "New Collection Events" ,
6- version : "0.0.3" ,
76 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" ,
7+ name : "New Collection Events" ,
8+ description : "Emit new filtered events for a collection. [See the documentation](https://docs.opensea.io/reference/list_events_by_collection)" ,
9+ version : "0.0.4" ,
10+ dedupe : "unique" ,
1111 type : "source" ,
1212 props : {
1313 opensea,
@@ -18,54 +18,81 @@ export default {
1818 intervalSeconds : DEFAULT_POLLING_SOURCE_TIMER_INTERVAL ,
1919 } ,
2020 } ,
21- contractAddress : {
21+ collectionSlug : {
2222 type : "string" ,
23- label : "Contract Address " ,
24- description : "Collection contract address " ,
23+ label : "Collection Slug " ,
24+ 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. " ,
2525 } ,
2626 eventType : {
2727 type : "string" ,
2828 options : [
29- "sales" ,
30- "listings" ,
29+ "all" ,
30+ "cancel" ,
31+ "listing" ,
32+ "offer" ,
33+ "order" ,
34+ "redemption" ,
35+ "sale" ,
36+ "transfer" ,
3137 ] ,
3238 label : "Event Type" ,
33- description : "OpenSea event type" ,
39+ description : "The type of event to filter by" ,
40+ default : "all" ,
41+ optional : true ,
3442 } ,
3543 } ,
3644 methods : {
37- getLastTimestamp ( ) {
45+ _getLastTimestamp ( ) {
3846 return this . db . get ( "lastTimestamp" ) ;
3947 } ,
40- setLastTimestamp ( ts ) {
48+ _setLastTimestamp ( ts ) {
4149 this . db . set ( "lastTimestamp" , ts ) ;
4250 } ,
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 ) ,
51+ generateMeta ( event ) {
52+ return {
53+ id : md5 ( JSON . stringify ( event ) ) ,
54+ summary : `New ${ event . event_type } event` ,
55+ ts : event . event_timestamp ,
56+ } ;
57+ } ,
58+ async processEvent ( max ) {
59+ const lastTimestamp = this . _getLastTimestamp ( ) ;
60+ let next = null ;
61+ let events = [ ] ;
62+ do {
63+ const resp = await this . opensea . retrieveEvents ( {
64+ collectionSlug : this . collectionSlug ,
65+ params : {
66+ event_type : this . eventType ,
67+ after : lastTimestamp ,
68+ next ,
69+ } ,
6270 } ) ;
63- } ) ;
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 ) ;
71+ if ( ! resp ?. asset_events ) {
72+ break ;
73+ }
74+ events . push ( ...resp . asset_events ) ;
75+ next = resp . next ;
76+ } while ( lastTimestamp && next ) ;
77+
78+ if ( ! events . length ) {
79+ return ;
6780 }
68- cursor = resp . next ;
69- } while ( lastTimestamp && cursor ) ;
81+ this . _setLastTimestamp ( events [ 0 ] . event_timestamp ) ;
82+ if ( max ) {
83+ events = events . slice ( 0 , max ) ;
84+ }
85+ events . reverse ( ) . forEach ( ( event ) => {
86+ this . $emit ( event , this . generateMeta ( event ) ) ;
87+ } ) ;
88+ } ,
89+ } ,
90+ hooks : {
91+ async deploy ( ) {
92+ await this . processEvent ( 25 ) ;
93+ } ,
94+ } ,
95+ async run ( ) {
96+ await this . processEvent ( ) ;
7097 } ,
7198} ;
0 commit comments