Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
38 changes: 25 additions & 13 deletions components/opensea/opensea.app.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import { axios } from "@pipedream/platform";
import Bottleneck from "bottleneck";
const limiter = new Bottleneck({
minTime: 200, // 5 requests per second
maxConcurrent: 1,
});
const axiosRateLimiter = limiter.wrap(axios);

export default {
type: "app",
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 axiosRateLimiter($, {
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,
});
},
},
Expand Down
8 changes: 3 additions & 5 deletions components/opensea/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
{
"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",
"bottleneck": "^2.19.5"
}
}
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,74 @@ export default {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
contractAddress: {
collectionSlug: {
type: "string",
label: "Contract Address",
description: "Collection contract address",
},
eventType: {
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");
_getNextCursor() {
return this.db.get("nextCursor");
},
setLastTimestamp(ts) {
this.db.set("lastTimestamp", ts);
_setNextCursor(nextCursor) {
this.db.set("nextCursor", nextCursor);
},
},
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),
});
async getPaginatedCollectionEvents() {
const args = {
collectionSlug: this.collectionSlug,
params: {
limit: 100,
next: this._getNextCursor(),
},
};

let lastNextCursor, total = 0;
const results = [];

do {
const {
listings, next,
} = await this.opensea.retrieveEvents(args);
results.push(...listings);
total = listings?.length;
lastNextCursor = args.params.next;
args.params.next = next;
} while (total && args.params?.next);

this._setNextCursor(lastNextCursor);
return results;
},
emitEvents(items) {
items.forEach((item) => {
const meta = this.generateMeta(item);
this.$emit(item, meta);
});
if (!cursor && resp.asset_events.length > 0) {
const ts = Math.floor(new Date(resp.asset_events[0].created_date).getTime() / 1000);
this.setLastTimestamp(ts);
},
generateMeta(item) {
return {
id: item.order_hash,
summary: `New ${item.type} ${item.chain} Listing`,
ts: item.protocol_data.parameters.startTime,
};
},
async processEvent(max) {
let items = await this.getPaginatedCollectionEvents();
if (!items?.length) {
return;
}
cursor = resp.next;
} while (lastTimestamp && cursor);
if (max) {
items = items.slice(-1 * max);
}
this.emitEvents(items);
},
},
hooks: {
async deploy() {
await this.processEvent(25);
},
},
async run() {
await this.processEvent();
},
};
25 changes: 14 additions & 11 deletions pnpm-lock.yaml

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

Loading