Skip to content

Commit 86d19f1

Browse files
committed
update to v2
1 parent 306d2d8 commit 86d19f1

File tree

3 files changed

+90
-61
lines changed

3 files changed

+90
-61
lines changed

components/opensea/opensea.app.mjs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,48 @@ export default {
55
app: "opensea",
66
propDefinitions: {},
77
methods: {
8-
async retrieveEvents({
9-
$, contract, eventType, occurredAfter, cursor,
8+
_baseUrl() {
9+
return "https://api.opensea.io/api/v2";
10+
},
11+
_makeRequest({
12+
$ = this,
13+
path,
14+
...otherOpts
1015
}) {
11-
const apiKey = this.$auth.api_key;
12-
return axios($ ?? this, {
13-
url: "https://api.opensea.io/api/v1/events",
14-
params: {
15-
only_opensea: false,
16-
asset_contract_address: contract,
17-
event_type: eventType,
18-
occurred_after: occurredAfter,
19-
cursor,
20-
},
16+
return axios($, {
17+
url: `${this._baseUrl()}${path}`,
2118
headers: {
22-
"X-API-KEY": apiKey,
19+
"X-API-KEY": this.$auth.api_key,
2320
},
21+
...otherOpts,
2422
});
2523
},
24+
retrieveEvents({
25+
collectionSlug, ...opts
26+
}) {
27+
return this._makeRequest({
28+
path: `/listings/collection/${collectionSlug}/all`,
29+
...opts,
30+
});
31+
},
32+
async *paginate({
33+
fn,
34+
args = {},
35+
resourceKey,
36+
}) {
37+
let total = 0;
38+
do {
39+
const response = await fn(args);
40+
const items = response[resourceKey];
41+
for (const item of items) {
42+
yield item;
43+
}
44+
total = items?.length;
45+
args.params = {
46+
...args?.params,
47+
next: response?.next,
48+
};
49+
} while (total && args.params?.next);
50+
},
2651
},
2752
};

components/opensea/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
{
22
"name": "@pipedream/opensea",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Pipedream Opensea Components",
55
"main": "opensea.app.mjs",
66
"keywords": [
77
"pipedream",
88
"opensea"
99
],
10-
"files": [
11-
"dist"
12-
],
1310
"homepage": "https://pipedream.com/apps/opensea",
1411
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1512
"publishConfig": {
1613
"access": "public"
1714
},
1815
"dependencies": {
19-
"@pipedream/platform": "^1.2.0"
16+
"@pipedream/platform": "^3.0.3"
2017
}
2118
}

components/opensea/sources/new-collection-events/new-collection-events.mjs

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import opensea from "../../opensea.app.mjs";
22
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
33

44
export 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

Comments
 (0)