-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEVMTransactionSanitizer.test.ts
143 lines (129 loc) · 4.33 KB
/
EVMTransactionSanitizer.test.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
import "reflect-metadata";
import { IBigNumberUtils, ILogUtils } from "@snickerdoodlelabs/common-utils";
import {
EChain,
EDataProvider,
EVMTransaction,
} from "@snickerdoodlelabs/objects";
import * as td from "testdouble";
import { IEVMTransactionSanitizer } from "@indexers/interfaces/index.js";
import { EVMTransactionSanitizer } from "@indexers/sanitizers/index.js";
import {
validEVMTransactions,
validUpperCaseEVMTransactions,
invalidEVMTransactions,
invalidButCanBeNormalizedTransactions,
validMixedCaseAddress,
} from "@indexers-test/mock/EthereumTransactions.js";
class EVMTransactionSanitizerMocks {
public logUtils: ILogUtils;
public bigNumberUtils: IBigNumberUtils;
public constructor() {
this.logUtils = td.object<ILogUtils>();
this.bigNumberUtils = td.object<IBigNumberUtils>();
td.when(this.bigNumberUtils.validateBNS(td.matchers.anything())).thenReturn(
true,
);
}
public factory(): IEVMTransactionSanitizer {
return new EVMTransactionSanitizer(this.logUtils, this.bigNumberUtils);
}
}
describe("EVMTransactionSanitizer", () => {
describe("valid transaction", () => {
test("should return EVMTransaction without modifying for valid transaction", () => {
// Arrange
const mocks = new EVMTransactionSanitizerMocks();
const service = mocks.factory();
for (const validTransaction of validEVMTransactions) {
// Act
const result = service.sanitize(
validTransaction,
EDataProvider.Alchemy,
EChain.EthereumMainnet,
);
// Assert
expect(result).not.toBeNull();
expect(result).toBeInstanceOf(EVMTransaction);
expect(result).toEqual(validTransaction);
}
});
test("should return EVMTransaction with lower casing the account address for valid transaction", () => {
// Arrange
const mocks = new EVMTransactionSanitizerMocks();
const service = mocks.factory();
//Both`to` and `from` are same
const expectedAddress = validMixedCaseAddress.toLowerCase();
for (const validUpperCaseTx of validUpperCaseEVMTransactions) {
// Act
const result = service.sanitize(
validUpperCaseTx,
EDataProvider.Alchemy,
EChain.EthereumMainnet,
);
// Assert
expect(result).not.toBeNull();
expect(result).toBeInstanceOf(EVMTransaction);
if (result != null) {
expect(result.to).toEqual(expectedAddress);
expect(result.from).toEqual(expectedAddress);
} else {
fail();
}
}
});
});
describe("invalid transaction", () => {
test("should return null for invalid transaction", () => {
// Arrange
const mocks = new EVMTransactionSanitizerMocks();
const service = mocks.factory();
for (const invalidTransaction of invalidEVMTransactions) {
// Act
const result = service.sanitize(
invalidTransaction,
EDataProvider.Alchemy,
EChain.EthereumMainnet,
);
// Assert
expect(result).toBeNull();
}
});
test("should return valid EVM transaction from normalizable invalid transaction", () => {
// Arrange
const mocks = new EVMTransactionSanitizerMocks();
const service = mocks.factory();
for (const invalidTransaction of invalidButCanBeNormalizedTransactions) {
// Act
const result = service.sanitize(
invalidTransaction,
EDataProvider.Alchemy,
EChain.EthereumMainnet,
);
// Assert
expect(result).not.toBeNull();
expect(result).toBeInstanceOf(EVMTransaction);
}
});
});
describe("Timestamp conversion", () => {
test("should return null for invalid transaction", () => {
// Arrange
const mocks = new EVMTransactionSanitizerMocks();
const service = mocks.factory();
const invalidTxs = invalidButCanBeNormalizedTransactions.slice(0, 2);
for (const invalidTransaction of invalidTxs) {
// Act
const result = service.sanitize(
invalidTransaction,
EDataProvider.Alchemy,
EChain.EthereumMainnet,
);
// Assert
expect(result).not.toBeNull();
expect(result).toBeInstanceOf(EVMTransaction);
expect(result?.timestamp).toBe(1659609391);
}
});
});
});