generated from VitorLuizC/typescript-library-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 10
/
SandwichDetector.ts
121 lines (107 loc) · 3.83 KB
/
SandwichDetector.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import type { ethers } from 'ethers';
import _ from 'lodash';
import { ClassifierProvider } from './classifiers/ClassifierProvider';
import { Detector } from './Detector';
import { Fetcher } from './Fetcher';
import type { DexType } from './interfaces/Dex';
import type { Sandwich } from './interfaces/Sandwich';
import type { Swap } from './interfaces/Swap';
import type { TransactionDetails } from './interfaces/TransactionDetails';
import { ProviderWrapper } from './ProviderWrapper';
export interface ISandwichDetector {
getSandwichesForBlock(blockNo: number, dex: DexType): Promise<Sandwich[]>;
getSandwichesForAddress(address: string, dex: DexType): Promise<Sandwich[]>;
}
export class SandwichDetector implements ISandwichDetector {
private classifierProvider: ClassifierProvider;
private fetcher: Fetcher;
private detector: Detector = new Detector();
constructor(
provider: ethers.providers.JsonRpcProvider,
covalentApiKey: string,
backupProviders?: ethers.providers.JsonRpcProvider[],
) {
const providerWrapper = new ProviderWrapper(provider, backupProviders);
this.classifierProvider = new ClassifierProvider(providerWrapper);
this.fetcher = new Fetcher(providerWrapper, covalentApiKey);
}
public async getSandwichesForBlock(
blockNo: number,
dexType: DexType,
): Promise<Sandwich[]> {
const classifier = this.classifierProvider.getClassifierForDex(dexType);
if (!classifier) {
throw new Error('Classifier not found');
}
const transactionDetailsFromBlock =
await this.fetcher.getAllTransactionDetailsFromBlock(blockNo);
const swaps = await classifier.getSwapsFromAllTx(
transactionDetailsFromBlock,
);
const sandwiches = this.detector.sandwichDetectorOnList(
swaps,
classifier.dex,
);
return sandwiches;
}
public async getSandwichesForAddress(
address: string,
dex: DexType,
): Promise<Sandwich[]> {
const classifier = this.classifierProvider.getClassifierForDex(dex);
if (!classifier) {
throw new Error('Classifier not found');
}
const txDetailsFromAddress =
await this.fetcher.getTransactionDetailsFromAddress(address);
const swaps = await classifier.getSwapsFromAllTx(txDetailsFromAddress);
const blocksWithSwapsFromAddress = this.getBlocksFromSwaps(swaps);
console.log(
`Found ${
swaps.length
} 🥪 from address ${address} in 🧱 ${blocksWithSwapsFromAddress.join(
', ',
)}`,
);
const sandwiches = await Promise.all(
blocksWithSwapsFromAddress.map((blockNo) =>
this.getSandwichesForBlock(blockNo, dex),
),
);
return _.flatten(sandwiches).filter(
(sandwich) => sandwich.victimSwap.toAddress === address,
);
}
public async getSwandwichesforAddressWithOffset(
address: string,
offset: number,
dex: DexType,
): Promise<any> {
const classifier = this.classifierProvider.getClassifierForDex(dex);
if (!classifier) {
throw new Error('Classifier not found');
}
const txDetailsFromAddress =
await this.fetcher.getTransactionDetailsFromAddress(address);
const swaps = await classifier.getSwapsFromAllTx(txDetailsFromAddress);
const swapMap: Map<Swap, TransactionDetails[]> =
await this.fetcher.getTransactionsDetailsWithOffset(swaps, offset);
const sandwiches = [];
for (const [victimSwap, txDetails] of swapMap) {
const otherSwaps = await classifier.getSwapsFromAllTx(txDetails);
if (otherSwaps.length >= 2) {
sandwiches.push(
...this.detector.sandwichDetectorOnTargetSwap(
victimSwap,
otherSwaps,
classifier.dex,
),
);
}
}
return _.flatten(sandwiches);
}
private getBlocksFromSwaps(swaps: Swap[]): number[] {
return _.uniq(swaps.map((swap) => swap.blockNumber));
}
}