-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from milktoastlab/fix-magic-eden-sales-tracking
Add support for MagicEden V2
Showing
11 changed files
with
279 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { NotificationType } from "lib/notifier"; | ||
import { MagicEdenConfig } from "config"; | ||
import { CollectionActivity } from "lib/marketplaces"; | ||
|
||
jest.mock("lib/solana/NFTData", () => { | ||
return { | ||
fetchNFTData: () => { | ||
return {}; | ||
}, | ||
}; | ||
}); | ||
|
||
jest.mock("axios", () => { | ||
return { | ||
get: () => { | ||
return { | ||
data: [ | ||
{ | ||
type: "buyNow", | ||
blockTime: 96292000003, | ||
tokenMint: "tokenxxx2", | ||
signature: "signature-2", | ||
source: "xxx", | ||
collection: "mlk", | ||
buyer: "buyer-2", | ||
seller: "seller-2", | ||
price: 0.2, | ||
}, | ||
{ | ||
type: "buyNow", | ||
blockTime: 96292000001, | ||
tokenMint: "tokenxxx1", | ||
signature: "signature-1", | ||
source: "xxx", | ||
collection: "mlk", | ||
buyer: "buyer-1", | ||
seller: "seller-1", | ||
price: 0.2, | ||
}, | ||
] as CollectionActivity[], | ||
}; | ||
}, | ||
}; | ||
}); | ||
|
||
import newWorker from "./notifyMagicEdenNFTSalesWorker"; | ||
import { Connection } from "@solana/web3.js"; | ||
|
||
describe("notifyMagicEdenNFTSalesWorker", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
describe("execute", () => { | ||
const notifier = { | ||
notify: jest.fn(), | ||
}; | ||
|
||
const conn = new Connection("https://test/"); | ||
|
||
test("on sale activity", async () => { | ||
const config: MagicEdenConfig = { | ||
url: "https://magiceden.io", | ||
collection: "mlk", | ||
discordChannelId: "", | ||
}; | ||
|
||
const worker = newWorker(notifier, conn, config); | ||
|
||
await worker.execute(); | ||
|
||
expect(notifier.notify.mock.calls.length).toEqual(2); | ||
const expectedArgs = notifier.notify.mock.calls[0]; | ||
expect(expectedArgs[0]).toEqual(NotificationType.Sale); | ||
expect(expectedArgs[1].buyer).toEqual("buyer-1"); // Should fires the earliest sale first | ||
|
||
await worker.execute(); | ||
|
||
expect(notifier.notify.mock.calls.length).toEqual(2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import axios from "axios"; | ||
import { MagicEdenConfig } from "config"; | ||
import { NFTSale, SaleMethod } from "lib/marketplaces"; | ||
import MagicEden from "lib/marketplaces/magicEden"; | ||
import { fetchNFTData } from "lib/solana/NFTData"; | ||
import { Worker } from "./types"; | ||
import { Connection } from "@solana/web3.js"; | ||
import logger from "lib/logger"; | ||
import { NotificationType, Notifier } from "lib/notifier"; | ||
|
||
export interface CollectionActivity { | ||
signature: string; | ||
type: string; | ||
source: string; | ||
tokenMint: string; | ||
collection: string; | ||
slot: number; | ||
blockTime: number; | ||
buyer: string; | ||
buyerReferral: string; | ||
seller?: any; | ||
sellerReferral: string; | ||
price: number; | ||
} | ||
|
||
function newNotificationsTracker(limit: number = 50) { | ||
let notifiedTxs: string[] = []; | ||
|
||
return { | ||
alreadyNotified(tx: string) { | ||
return notifiedTxs.includes(tx); | ||
}, | ||
trackNotifiedTx(tx: string) { | ||
notifiedTxs = [tx, ...notifiedTxs]; | ||
if (notifiedTxs.length > limit) { | ||
notifiedTxs.pop(); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
export default function newWorker( | ||
notifier: Notifier, | ||
web3Conn: Connection, | ||
config: MagicEdenConfig | ||
): Worker { | ||
const timestamp = Date.now(); | ||
let notifyAfter = new Date(timestamp); | ||
|
||
/** | ||
* Keep track of the latest notifications, so we don't notify them again | ||
*/ | ||
const latestNotifications = newNotificationsTracker(); | ||
|
||
return { | ||
async execute() { | ||
let activities: CollectionActivity[] = []; | ||
try { | ||
// Reference: https://api.magiceden.dev/#95fed531-fd1f-4cbb-8137-30e0f2294cd7 | ||
const res = await axios.get( | ||
`${config.url}/collections/${config.collection}/activities?offset=0&limit=100` | ||
); | ||
activities = res.data as CollectionActivity[]; | ||
} catch (e) { | ||
logger.error(e); | ||
return; | ||
} | ||
|
||
const sortByEarliest = activities.sort( | ||
(a: CollectionActivity, b: CollectionActivity) => { | ||
return a.blockTime - b.blockTime; | ||
} | ||
); | ||
|
||
for (let i = 0; i < sortByEarliest.length; i++) { | ||
const activity = sortByEarliest[i]; | ||
if (activity.type !== "buyNow") { | ||
continue; | ||
} | ||
|
||
const nftData = await fetchNFTData(web3Conn, activity.tokenMint); | ||
if (!nftData) { | ||
return; | ||
} | ||
const nftSale: NFTSale = { | ||
transaction: activity.signature, | ||
soldAt: new Date((activity.blockTime || 0) * 1000), | ||
seller: activity.seller, | ||
buyer: activity.buyer, | ||
token: activity.tokenMint, | ||
method: SaleMethod.Direct, | ||
marketplace: MagicEden, | ||
transfers: [], | ||
nftData, | ||
getPriceInLamport() { | ||
return activity.price / 1000000; | ||
}, | ||
getPriceInSOL() { | ||
return activity.price; | ||
}, | ||
}; | ||
|
||
if (notifyAfter > nftSale.soldAt) { | ||
return; | ||
} | ||
|
||
// Don't notify if transaction was previously notified. | ||
if (latestNotifications.alreadyNotified(nftSale.transaction)) { | ||
logger.warn(`Duplicate tx ignored: ${nftSale.transaction}`); | ||
return; | ||
} | ||
|
||
await notifier.notify(NotificationType.Sale, nftSale); | ||
|
||
latestNotifications.trackNotifiedTx(nftSale.transaction); | ||
notifyAfter = nftSale.soldAt; | ||
} | ||
}, | ||
}; | ||
} |