This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener.ts
258 lines (247 loc) · 7.48 KB
/
listener.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-console */
import * as yargs from 'yargs';
import fetch from 'node-fetch';
import EthDater from 'ethereum-block-by-date';
import {
IEventHandler,
CWEvent,
SubstrateEvents,
CompoundEvents,
MolochEvents,
AaveEvents,
Erc20Events,
SupportedNetwork,
Erc721Events,
} from '../src/index';
import { contracts, networkSpecs, networkUrls } from './listenerUtils';
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config();
const { argv } = yargs
.options({
network: {
alias: 'n',
choices: Object.values(SupportedNetwork),
demandOption: true,
description: 'network listener to use',
},
chain: {
alias: 'c',
type: 'string',
description: 'chain to listen on',
},
url: {
alias: 'u',
type: 'string',
description: 'node url',
},
contractAddress: {
alias: 'a',
type: 'string',
description: 'eth contract address',
},
archival: {
alias: 'A',
type: 'boolean',
description: 'run listener in archival mode or not',
},
startBlock: {
alias: 'b',
type: 'number',
description:
'when running in archival mode, which block should we start from',
},
})
.check((data) => {
if (!data.url && !data.chain) {
if (data.network === SupportedNetwork.Substrate) {
throw new Error('Must pass either URL or chain name!');
} else {
// default to eth mainnet if not on substrate
data.chain = 'erc20';
}
}
if (!networkUrls[data.chain] && !data.url) {
throw new Error(`no URL found for ${data.chain}`);
}
if (
data.network !== SupportedNetwork.Substrate &&
data.network !== SupportedNetwork.ERC20 &&
data.network !== SupportedNetwork.ERC721 &&
!data.contractAddress &&
!contracts[data.chain]
) {
throw new Error(`no contract found for ${data.chain}`);
}
if (
data.network === SupportedNetwork.Substrate &&
!networkSpecs[data.chain]
) {
throw new Error(`no spec found for ${data.chain}`);
}
return true;
});
const { archival } = argv;
// if running in archival mode then which block shall we star from
const startBlock: number = argv.startBlock ?? 0;
const { network } = argv;
const chain = argv.chain || 'dummy';
const url = argv.url || networkUrls[chain];
const spec = networkSpecs[chain];
const contract = argv.contractAddress || contracts[chain];
class StandaloneEventHandler extends IEventHandler {
// eslint-disable-next-line class-methods-use-this
public async handle(event: CWEvent): Promise<null> {
console.log(`Received event: ${JSON.stringify(event, null, 2)}`);
return null;
}
}
const skipCatchup = false;
const tokenListUrls = ['https://gateway.ipfs.io/ipns/tokens.uniswap.org'];
const nftListUrls = [
'https://raw.githubusercontent.com/jnaviask/collectible-lists/main/test/schema/bigexample.collectiblelist.json',
];
interface TokenListEntry {
chainId: number;
address: string;
name: string;
symbol: string;
standard?: string; // erc721
decimals?: number; // 18
}
async function getTokenList(tokenListUrl: string): Promise<TokenListEntry[]> {
const data = await fetch(tokenListUrl)
.then((o) => o.json())
.catch((e) => {
console.error(e);
return [];
});
return data?.tokens?.filter((o) => o);
}
console.log(`Connecting to ${chain} on url ${url}...`);
if (network === SupportedNetwork.Substrate) {
SubstrateEvents.createApi(url, spec as any).then(async (api) => {
const fetcher = new SubstrateEvents.StorageFetcher(api);
try {
const fetched = await fetcher.fetch();
console.log(fetched.map((f) => f.data));
} catch (err) {
console.log(err);
console.error(`Got error from fetcher: ${JSON.stringify(err, null, 2)}.`);
}
SubstrateEvents.subscribeEvents({
chain,
api,
handlers: [new StandaloneEventHandler()],
skipCatchup,
archival,
startBlock,
verbose: true,
enricherConfig: { balanceTransferThresholdPermill: 1_000 }, // 0.1% of total issuance
});
});
} else if (network === SupportedNetwork.Moloch) {
const contractVersion = 1;
MolochEvents.createApi(url, contractVersion, contract).then(async (api) => {
const dater = new EthDater(api.provider);
const fetcher = new MolochEvents.StorageFetcher(
api,
contractVersion,
dater
);
try {
const fetched = await fetcher.fetch(
{ startBlock: 11000000, maxResults: 3 },
true
);
// const fetched = await fetcher.fetchOne('132');
console.log(fetched.map((f) => f.data));
} catch (err) {
console.log(err);
console.error(`Got error from fetcher: ${JSON.stringify(err, null, 2)}.`);
}
MolochEvents.subscribeEvents({
chain,
api,
contractVersion,
handlers: [new StandaloneEventHandler()],
skipCatchup,
verbose: true,
});
});
} else if (network === SupportedNetwork.Compound) {
CompoundEvents.createApi(url, contract).then(async (api) => {
const fetcher = new CompoundEvents.StorageFetcher(api);
try {
const fetched = await fetcher.fetch({
startBlock: 13353227,
maxResults: 1,
});
// const fetched = await fetcher.fetchOne('2');
console.log(fetched.map((f) => f.data));
} catch (err) {
console.log(err);
console.error(`Got error from fetcher: ${JSON.stringify(err, null, 2)}.`);
}
CompoundEvents.subscribeEvents({
chain,
api,
handlers: [new StandaloneEventHandler()],
skipCatchup,
verbose: true,
});
});
} else if (network === SupportedNetwork.Aave) {
AaveEvents.createApi(url, contract).then(async (api) => {
const fetcher = new AaveEvents.StorageFetcher(api);
try {
const fetched = await fetcher.fetch({
startBlock: 13353227,
maxResults: 1,
});
// const fetched = await fetcher.fetchOne('10');
console.log(fetched.sort((a, b) => a.blockNumber - b.blockNumber));
} catch (err) {
console.log(err);
console.error(`Got error from fetcher: ${JSON.stringify(err, null, 2)}.`);
}
AaveEvents.subscribeEvents({
chain,
api,
handlers: [new StandaloneEventHandler()],
skipCatchup,
verbose: true,
});
});
} else if (network === SupportedNetwork.ERC20) {
getTokenList(tokenListUrls[0]).then(async (tokens) => {
const validTokens = tokens.filter((t) => t.chainId === 1);
const tokenAddresses = validTokens.map((o) => o.address);
const tokenNames = validTokens.map((o) => o.name);
const api = await Erc20Events.createApi(url, tokenAddresses, tokenNames);
Erc20Events.subscribeEvents({
chain,
api,
handlers: [new StandaloneEventHandler()],
skipCatchup,
verbose: false,
enricherConfig: { balanceTransferThresholdPermill: 500_000 }, // 50% of total supply
});
});
} else if (network === SupportedNetwork.ERC721) {
getTokenList(nftListUrls[0]).then(async (tokens) => {
const validTokens = tokens.filter(
(t) => t.chainId === 1 && t.standard === 'erc721'
);
const tokenAddresses = validTokens.map((o) => o.address);
const tokenNames = validTokens.map((o) => o.name);
const api = await Erc721Events.createApi(url, tokenAddresses, tokenNames);
Erc721Events.subscribeEvents({
chain,
api,
handlers: [new StandaloneEventHandler()],
skipCatchup,
verbose: false,
});
});
}