-
Notifications
You must be signed in to change notification settings - Fork 4
/
Scanner.js
66 lines (56 loc) · 2.88 KB
/
Scanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Connection, PublicKey } from '@solana/web3.js';
import fs from 'fs';
import * as txUtils from './utils/txUtils.js';
import * as utils from './utils/utils.js';
import * as constants from './utils/constants.js';
import * as tweet from './tweet.js';
async function monitor() {
const collectionMetadata = JSON.parse(fs.readFileSync('./json/CollectionMetadata.json', 'utf8'));
const connection = new Connection(constants.RPC_ENDPOINT);
const magicEdenPubkey = new PublicKey(constants.MAGIC_EDEN_ADDRESS);
const alphaArtPubkey = new PublicKey(constants.ALPHA_ART_ADDRESS);
let [lastMEHash, lastAAHash] = await Promise.all([txUtils.getHashOfMostRecentConfirmedTx(connection, magicEdenPubkey),
txUtils.getHashOfMostRecentConfirmedTx(connection, alphaArtPubkey)]);
monitorMarketPlace(constants.MAGIC_EDEN, lastMEHash, magicEdenPubkey, connection, collectionMetadata);
monitorMarketPlace(constants.ALPHA_ART, lastAAHash, alphaArtPubkey, connection, collectionMetadata);
}
async function monitorMarketPlace(marketplace, lastHash, pubkey, connection, collectionMetadata) {
console.log(`Starting scan of ${marketplace}`);
while (true) {
try {
const signatures = await connection.getConfirmedSignaturesForAddress2(pubkey, {until: lastHash}, 'confirmed');
if (signatures.length == 0) {
await utils.sleep(10000);
continue;
}
console.log(`Scanning ${signatures.length} signatures from ${marketplace}`);
for (let i = signatures.length - 1; i >= 0; i--) {
lastHash = signatures[i].signature;
const tx = await connection.getParsedConfirmedTransaction(signatures[i].signature, 'confirmed');
if (txUtils.isNullOrFailedTx(tx) || !txUtils.isCollectionSale(tx, marketplace)) continue;
const decodedTx = txUtils.decodeSaleTx(tx, marketplace);
const nftMetadata = getNftMetadataFromCollectionMetadata(decodedTx.mint, collectionMetadata);
if (nftMetadata == null) continue;
console.log(`Found sale from ${marketplace}`);
const hash = signatures[i].signature;
const imageUrl = await utils.fetchImageUri(nftMetadata.uri);
const tweetText = tweet.formatTweetText(decodedTx, nftMetadata, hash);
console.log(tweetText);
await tweet.tweetWithImage(tweetText, imageUrl);
}
} catch (err) {
console.log(err);
} finally {
await utils.sleep(15000);
}
}
}
function getNftMetadataFromCollectionMetadata(mint, collectionMetadata) {
for (const nftMetadata of collectionMetadata) {
if (mint === nftMetadata.mint) {
return nftMetadata;
}
}
return null;
}
(async () => await monitor())();