-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionClassifier.cs
468 lines (386 loc) · 14.6 KB
/
TransactionClassifier.cs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System.Linq;
using System.Numerics;
using CirclesLand.BlockchainIndexer.TransactionDetailModels;
using Nethereum.ABI.FunctionEncoding;
using Nethereum.ABI.Model;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs;
using Newtonsoft.Json.Linq;
namespace CirclesLand.BlockchainIndexer.DetailExtractors
{
public static class TransactionClassifier
{
public const string HubAddress = "0x29b9a7fbb8995b2423a71cc17cf9810798f6c543";
public const string AddressEmptyBytesPrefix = "0x000000000000000000000000";
public const string CrcHubTransferEventTopic =
"0x8451019aab65b4193860ef723cb0d56b475a26a72b7bfc55c1dbd6121015285a";
public const string CrcTrustEventTopic = "0xe60c754dd8ab0b1b5fccba257d6ebcd7d09e360ab7dd7a6e58198ca1f57cdcec";
public const string TransferEventTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
public const string CrcSignupEventTopic = "0x358ba8f768af134eb5af120e9a61dc1ef29b29f597f047b555fc3675064a0342";
public const string GnosisSafeOwnerAddedTopic = "";
public const string CrcOrganisationSignupEventTopic =
"0xb0b94cff8b84fc67513b977d68a5cdd67550bd9b8d99a34b570e3367b7843786";
public const string GnosisSafeExecutionSuccessEventTopic =
"0x442e715f626346e8c54381002da614f62bee8d27386535b2521ec8540898556e";
public const string EmptyUInt256 = "0x0000000000000000000000000000000000000000000000000000000000000000";
public const string EmptyAddress = "0x0000000000000000000000000000000000000000";
public const string ExecTransactionMethodId = "0x6a761202";
public const string ExecutionSuccessEventTopic =
"0x442e715f626346e8c54381002da614f62bee8d27386535b2521ec8540898556e";
/// <summary>
/// A crc signup transaction always has three logs: Signup, Trust and Transfer.
/// </summary>
public static bool IsCrcSignup(
TransactionReceipt receipt,
out string? userAddress,
out string? tokenAddress)
{
userAddress = null;
tokenAddress = null;
if (receipt.Logs.Count < 3)
{
return false;
}
//
// Decode and check the Signup-log
//
var signupLog = receipt.Logs.SingleOrDefault(log =>
log.SelectToken("topics").Values<string>().Contains(CrcSignupEventTopic));
if (signupLog == null)
{
return false;
}
var signupLogAddress = signupLog.Value<string>("address");
if (signupLogAddress != HubAddress)
{
return false;
}
var signupLogTopics = signupLog.SelectToken("topics").Values<string>().ToArray();
if (signupLogTopics.Length != 2)
{
return false;
}
userAddress = signupLogTopics[1].Replace(AddressEmptyBytesPrefix, "0x");
tokenAddress = signupLog.Value<string>("data").Replace(AddressEmptyBytesPrefix, "0x");
//
// Decode and check the Trust-log
//
var trustLog = receipt.Logs.SingleOrDefault(o => IsCrcTrust(o, out _, out _, out _));
if (trustLog == null)
{
return false;
}
IsCrcTrust(
trustLog,
out var canSendTo,
out var user,
out var limit);
if (canSendTo != user || canSendTo != userAddress || user != userAddress)
{
return false;
}
if (limit == null || limit.Value < 0 || limit.Value > 100)
{
return false;
}
//
// Decode and check the Transfer-log
//
var erc20TransferLog = receipt.Logs.SingleOrDefault(o => IsErc20Transfer(o, out _, out _, out _, out _));
if (erc20TransferLog == null)
{
return false;
}
IsErc20Transfer(
erc20TransferLog,
out var transferTokenAddress,
out var from,
out var to,
out var value);
if (transferTokenAddress != tokenAddress)
{
return false;
}
if (from != EmptyAddress)
{
return false;
}
if (to != userAddress)
{
return false;
}
if (value == null || value.ToString() != "50000000000000000000")
{
return false;
}
return true;
}
public static bool IsCrcOrganisationSignup(
JToken logEntry,
out string? organisationAddress)
{
organisationAddress = null;
var topics = logEntry.SelectToken("topics");
if (topics == null)
{
return false;
}
if (!topics.Values<string>().Contains(CrcOrganisationSignupEventTopic))
{
return false;
}
var organisationSignupLogAddress = logEntry.Value<string>("address");
if (organisationSignupLogAddress != HubAddress)
{
return false;
}
var organisationSignupLogTopics = topics.Values<string>().ToArray();
if (organisationSignupLogTopics.Length != 2)
{
return false;
}
organisationAddress = organisationSignupLogTopics[1].Replace(AddressEmptyBytesPrefix, "0x");
return true;
}
public static bool IsCrcHubTransfer(
TransactionReceipt receipt,
out string? from,
out string? to,
out HexBigInteger? amount)
{
from = null;
to = null;
amount = null;
var hubTransferLog = receipt.Logs.SingleOrDefault(log =>
log.SelectToken("topics").Values<string>().Contains(CrcHubTransferEventTopic));
if (hubTransferLog == null)
{
return false;
}
var hubTransferLogAddress = hubTransferLog.Value<string>("address");
if (hubTransferLogAddress != HubAddress)
{
return false;
}
var hubTransferLogTopics = hubTransferLog.SelectToken("topics").Values<string>().ToArray();
if (hubTransferLogTopics.Length != 3)
{
return false;
}
from = hubTransferLogTopics[1].Replace(AddressEmptyBytesPrefix, "0x");
to = hubTransferLogTopics[2].Replace(AddressEmptyBytesPrefix, "0x");
var amountStr = hubTransferLog.Value<string>("data").Replace(AddressEmptyBytesPrefix, "0x");
amount = new HexBigInteger(amountStr);
// TODO: Check if "from" and "to" previously appeared in a CrcSignup
// Hub transfers always appear together with at least one ERC20 transfer
return receipt.Logs.Any(o => IsErc20Transfer(o, out _, out _, out _, out _));
}
public static bool IsCrcTrust(
JToken logEntry,
out string? canSendTo,
out string? user,
out HexBigInteger? limit)
{
canSendTo = null;
user = null;
limit = null;
var topics = logEntry.SelectToken("topics");
if (topics == null)
{
return false;
}
if (!topics.Values<string>().Contains(CrcTrustEventTopic))
{
return false;
}
var trustLogAddress = logEntry.Value<string>("address");
if (trustLogAddress != HubAddress)
{
return false;
}
var trustLogTopics = topics.Values<string>().ToArray();
if (trustLogTopics.Length != 3)
{
return false;
}
canSendTo = trustLogTopics[1].Replace(AddressEmptyBytesPrefix, "0x");
user = trustLogTopics[2].Replace(AddressEmptyBytesPrefix, "0x");
limit = new HexBigInteger(logEntry.Value<string>("data"));
if (limit.Value < 0 || limit.Value > 100)
{
return false;
}
// TODO: Check if "canSendTo" and "user" previously appeared in a CrcSignup
return true;
}
public static bool IsErc20Transfer(
JToken logEntry,
out string? tokenAddress,
out string? from,
out string? to,
out HexBigInteger? value)
{
tokenAddress = null;
from = null;
to = null;
value = null;
var topics = logEntry.SelectToken("topics");
if (topics == null)
{
return false;
}
if (!topics.Values<string>().Contains(TransferEventTopic))
{
return false;
}
var transferLogTopics = topics.Values<string>().ToArray();
if (transferLogTopics.Length != 3)
{
return false;
}
tokenAddress = logEntry.Value<string>("address");
from = transferLogTopics[1].Replace(AddressEmptyBytesPrefix, "0x");
to = transferLogTopics[2].Replace(AddressEmptyBytesPrefix, "0x");
var valueStr = logEntry.Value<string>("data").Replace(AddressEmptyBytesPrefix, "0x");
value = new HexBigInteger(valueStr);
return true;
}
public static bool IsSafeEthTransfer(
Transaction transaction,
TransactionReceipt receipt,
out string? initiator,
out string? from,
out string? to,
out HexBigInteger? value)
{
initiator = null;
from = null;
to = null;
value = null;
if (!transaction.Input.StartsWith(ExecTransactionMethodId))
{
return false;
}
var decoded = new FunctionCallDecoder()
.DecodeFunctionInput(
"0x6a761202",
transaction.Input,
new Parameter("address", 0),
new Parameter("uint256", 1),
new Parameter("bytes", 2),
new Parameter("uint8", 3),
new Parameter("uint256", 4),
new Parameter("uint256", 5),
new Parameter("uint256", 6),
new Parameter("address", 7),
new Parameter("address", 8),
new Parameter("bytes", 9));
initiator = transaction.From;
from = transaction.To;
to = decoded[0].Result as string;
var v = decoded[1].Result as BigInteger?;
if (v == null)
{
return false;
}
value = new HexBigInteger(v.Value);
var data = decoded[2].Result as byte[];
if (data == null || data.Length > 0)
{
return false;
}
var operation = decoded[3].Result as BigInteger?;
if (operation != 0)
{
return false;
}
var executionSuccessLog = receipt.Logs.SingleOrDefault(log =>
log.SelectToken("topics").Values<string>().Contains(ExecutionSuccessEventTopic));
if (executionSuccessLog == null)
{
return false;
}
return true;
}
public static bool IsEoaEthTransfer (
Transaction transaction,
TransactionReceipt receipt,
out string? from,
out string? to,
out HexBigInteger? value)
{
from = null;
to = null;
value = null;
if (transaction.Value.Value == 0)
{
return false;
}
if (receipt.Logs.Count > 0)
{
return false;
}
if (transaction.Input != "0x")
{
return false;
}
if (transaction.To == null)
{
return false;
}
from = transaction.From;
to = transaction.To;
value = transaction.Value;
return true;
}
public static TransactionClass Classify(Transaction transaction, TransactionReceipt receipt,
TransactionClass? externalClasses)
{
var isEoaEthTransfer = IsEoaEthTransfer(transaction, receipt, out _, out _, out _);
if (!isEoaEthTransfer && receipt.Logs == null)
{
return TransactionClass.Unknown;
}
var isErc20Transfer = receipt.Logs.Any(o => IsErc20Transfer(o, out _, out _, out _, out _));
var isCrcSignup = IsCrcSignup(receipt, out _, out _);
var isCrcOrganisationSignup = receipt.Logs.Any(o => IsCrcOrganisationSignup(o, out _));
var isCrcHubTransfer = IsCrcHubTransfer(receipt, out _, out _, out _);
var isCrcTrust = receipt.Logs.Any(o => IsCrcTrust(o, out _, out _, out _));
var isSafeEthTransfer = IsSafeEthTransfer(transaction, receipt, out _, out _, out _, out _);
var classification = TransactionClass.Unknown;
if (isErc20Transfer)
{
classification |= TransactionClass.Erc20Transfer;
}
if (isCrcSignup)
{
classification |= TransactionClass.CrcSignup;
}
if (isCrcOrganisationSignup)
{
classification |= TransactionClass.CrcOrganisationSignup;
}
if (isCrcHubTransfer)
{
classification |= TransactionClass.CrcHubTransfer;
}
if (isCrcTrust)
{
classification |= TransactionClass.CrcTrust;
}
if (isSafeEthTransfer)
{
classification |= TransactionClass.SafeEthTransfer;
}
if (isSafeEthTransfer)
{
classification |= TransactionClass.SafeEthTransfer;
}
if (isEoaEthTransfer)
{
classification |= TransactionClass.EoaEthTransfer;
}
return classification;
}
}
}