Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions components/opensea/opensea.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,48 @@ export default {
app: "opensea",
propDefinitions: {},
methods: {
async retrieveEvents({
$, contract, eventType, occurredAfter, cursor,
_baseUrl() {
return "https://api.opensea.io/api/v2";
},
_makeRequest({
$ = this,
path,
...otherOpts
}) {
const apiKey = this.$auth.api_key;
return axios($ ?? this, {
url: "https://api.opensea.io/api/v1/events",
params: {
only_opensea: false,
asset_contract_address: contract,
event_type: eventType,
occurred_after: occurredAfter,
cursor,
},
return axios($, {
url: `${this._baseUrl()}${path}`,
headers: {
"X-API-KEY": apiKey,
"X-API-KEY": this.$auth.api_key,
},
...otherOpts,
});
},
retrieveEvents({
collectionSlug, ...opts
}) {
return this._makeRequest({
path: `/listings/collection/${collectionSlug}/all`,
...opts,
});
},
async *paginate({
fn,
args = {},
resourceKey,
}) {
let total = 0;
do {
const response = await fn(args);
const items = response[resourceKey];
for (const item of items) {
yield item;
}
total = items?.length;
args.params = {
...args?.params,
next: response?.next,
};
} while (total && args.params?.next);
},
},
};
7 changes: 2 additions & 5 deletions components/opensea/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
{
"name": "@pipedream/opensea",
"version": "0.0.3",
"version": "0.0.4",
"description": "Pipedream Opensea Components",
"main": "opensea.app.mjs",
"keywords": [
"pipedream",
"opensea"
],
"files": [
"dist"
],
"homepage": "https://pipedream.com/apps/opensea",
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.2.0"
"@pipedream/platform": "^3.0.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import opensea from "../../opensea.app.mjs";
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";

export default {
name: "New Collection Events",
version: "0.0.3",
key: "opensea-new-collection-events",
description:
"Emit new filtered events. [See docs](https://docs.opensea.io/reference/retrieving-asset-events)",
dedupe: "greatest",
name: "New Collection Events",
description: "Emit new listings for a collection. [See the documentation](https://docs.opensea.io/reference/get_all_listings_on_collection_v2)",
version: "0.0.4",
dedupe: "unique",
type: "source",
props: {
opensea,
Expand All @@ -18,54 +17,66 @@ export default {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
contractAddress: {
type: "string",
label: "Contract Address",
description: "Collection contract address",
},
eventType: {
collectionSlug: {
type: "string",
options: [
"sales",
"listings",
],
label: "Event Type",
description: "OpenSea event type",
label: "Collection Slug",
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.",
},
},
methods: {
getLastTimestamp() {
return this.db.get("lastTimestamp");
return this.db.get("lastTimestamp") || 0;
},
setLastTimestamp(ts) {
this.db.set("lastTimestamp", ts);
},
},
async run() {
const eventType = this.eventType === "sales"
? "successful"
: "created";
const lastTimestamp = this.getLastTimestamp();
let cursor = null;
do {
const resp = await this.opensea.retrieveEvents({
contract: this.contractAddress,
eventType,
occurredAfter: lastTimestamp,
cursor,
});
resp.asset_events.forEach((event) => {
this.$emit(event, {
id: event.id,
summary: `${event.asset.name} ${this.eventType} event`,
ts: +new Date(event.created_date),
});
generateMeta(item) {
return {
id: item.order_hash,
summary: `New ${item.type} ${item.chain} Listing`,
ts: item.protocol_data.parameters.startTime,
};
},
async processEvent(max) {
const lastTimestamp = this.getLastTimestamp();

const results = this.opensea.paginate({
fn: this.opensea.retrieveEvents,
args: {
collectionSlug: this.collectionSlug,
},
resourceKey: "listings",
});
if (!cursor && resp.asset_events.length > 0) {
const ts = Math.floor(new Date(resp.asset_events[0].created_date).getTime() / 1000);
this.setLastTimestamp(ts);

let items = [];
for await (const result of results) {
const ts = result.protocol_data.parameters.startTime;
if (ts >= lastTimestamp) {
items.push(result);
}
}
cursor = resp.next;
} while (lastTimestamp && cursor);

if (!items?.length) {
return;
}

if (max) {
items = items.slice(-1 * max);
}
this.setLastTimestamp(items[items.length - 1].protocol_data.parameters.startTime);

items.forEach((item) => {
const meta = this.generateMeta(item);
this.$emit(item, meta);
});
},
},
hooks: {
async deploy() {
await this.processEvent(25);
},
},
async run() {
await this.processEvent();
},
};
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading