@@ -5,6 +5,29 @@ import { determineTransactionType } from './transaction-type';
5
5
import { FakeProvider } from '../../../../tests/fake-provider' ;
6
6
import { TransactionType } from '../types' ;
7
7
8
+ type GetCodeCallback = ( err : Error | null , result ?: string ) => void ;
9
+
10
+ /**
11
+ * Creates a mock EthQuery instance for testing.
12
+ *
13
+ * @param getCodeResponse The response string to return from getCode, or undefined/null.
14
+ * @param shouldThrow Whether getCode should throw an error instead of returning a response.
15
+ * @returns An EthQuery instance with a mocked getCode method.
16
+ */
17
+ function createMockEthQuery (
18
+ getCodeResponse : string | undefined | null ,
19
+ shouldThrow = false ,
20
+ ) : EthQuery {
21
+ return new ( class extends EthQuery {
22
+ getCode ( _to : string , cb : GetCodeCallback ) : void {
23
+ if ( shouldThrow ) {
24
+ return cb ( new Error ( 'Some error' ) ) ;
25
+ }
26
+ return cb ( null , getCodeResponse ?? undefined ) ;
27
+ }
28
+ } ) ( new FakeProvider ( ) ) ;
29
+ }
30
+
8
31
describe ( 'determineTransactionType' , ( ) => {
9
32
const FROM_MOCK = '0x9e' ;
10
33
const txParams = {
@@ -14,20 +37,13 @@ describe('determineTransactionType', () => {
14
37
} ;
15
38
16
39
it ( 'returns a token transfer type when the recipient is a contract, there is no value passed, and data is for the respective method call' , async ( ) => {
17
- class MockEthQuery extends EthQuery {
18
- // TODO: Replace `any` with type
19
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
- getCode ( _to : any , cb : any ) {
21
- cb ( null , '0xab' ) ;
22
- }
23
- }
24
40
const result = await determineTransactionType (
25
41
{
26
42
to : '0x9e673399f795D01116e9A8B2dD2F156705131ee9' ,
27
43
data : '0xa9059cbb0000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C970000000000000000000000000000000000000000000000000000000000000000a' ,
28
44
from : FROM_MOCK ,
29
45
} ,
30
- new MockEthQuery ( new FakeProvider ( ) ) ,
46
+ createMockEthQuery ( '0xab' ) ,
31
47
) ;
32
48
33
49
expect ( result ) . toMatchObject ( {
@@ -40,17 +56,10 @@ describe('determineTransactionType', () => {
40
56
'does NOT return a token transfer type and instead returns contract interaction' +
41
57
' when the recipient is a contract, the data matches the respective method call, but there is a value passed' ,
42
58
async ( ) => {
43
- class MockEthQuery extends EthQuery {
44
- // TODO: Replace `any` with type
45
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
- getCode ( _to : any , cb : any ) {
47
- cb ( null , '0xab' ) ;
48
- }
49
- }
50
59
const resultWithEmptyValue = await determineTransactionType (
51
60
txParams ,
52
61
53
- new MockEthQuery ( new FakeProvider ( ) ) ,
62
+ createMockEthQuery ( '0xab' ) ,
54
63
) ;
55
64
expect ( resultWithEmptyValue ) . toMatchObject ( {
56
65
type : TransactionType . tokenMethodTransfer ,
@@ -63,7 +72,7 @@ describe('determineTransactionType', () => {
63
72
...txParams ,
64
73
} ,
65
74
66
- new MockEthQuery ( new FakeProvider ( ) ) ,
75
+ createMockEthQuery ( '0xab' ) ,
67
76
) ;
68
77
69
78
expect ( resultWithEmptyValue2 ) . toMatchObject ( {
@@ -77,7 +86,7 @@ describe('determineTransactionType', () => {
77
86
...txParams ,
78
87
} ,
79
88
80
- new MockEthQuery ( new FakeProvider ( ) ) ,
89
+ createMockEthQuery ( '0xab' ) ,
81
90
) ;
82
91
expect ( resultWithValue ) . toMatchObject ( {
83
92
type : TransactionType . contractInteraction ,
@@ -87,16 +96,9 @@ describe('determineTransactionType', () => {
87
96
) ;
88
97
89
98
it ( 'does NOT return a token transfer type when the recipient is not a contract but the data matches the respective method call' , async ( ) => {
90
- class MockEthQuery extends EthQuery {
91
- // TODO: Replace `any` with type
92
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
- getCode ( _to : any , cb : any ) {
94
- cb ( null , '0x' ) ;
95
- }
96
- }
97
99
const result = await determineTransactionType (
98
100
txParams ,
99
- new MockEthQuery ( new FakeProvider ( ) ) ,
101
+ createMockEthQuery ( '0x' ) ,
100
102
) ;
101
103
expect ( result ) . toMatchObject ( {
102
104
type : TransactionType . simpleSend ,
@@ -105,21 +107,13 @@ describe('determineTransactionType', () => {
105
107
} ) ;
106
108
107
109
it ( 'does not identify contract codes with DELEGATION_PREFIX as contract addresses' , async ( ) => {
108
- class MockEthQuery extends EthQuery {
109
- // TODO: Replace `any` with type
110
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
- getCode ( _to : any , cb : any ) {
112
- cb ( null , `${ DELEGATION_PREFIX } 1234567890abcdef` ) ;
113
- }
114
- }
115
-
116
110
const result = await determineTransactionType (
117
111
{
118
112
to : '0x9e673399f795D01116e9A8B2dD2F156705131ee9' ,
119
113
data : '0xabd' ,
120
114
from : FROM_MOCK ,
121
115
} ,
122
- new MockEthQuery ( new FakeProvider ( ) ) ,
116
+ createMockEthQuery ( ` ${ DELEGATION_PREFIX } 1234567890abcdef` ) ,
123
117
) ;
124
118
125
119
expect ( result ) . toMatchObject ( {
@@ -129,19 +123,26 @@ describe('determineTransactionType', () => {
129
123
} ) ;
130
124
131
125
it ( 'returns a token approve type when the recipient is a contract and data is for the respective method call' , async ( ) => {
132
- class MockEthQuery extends EthQuery {
133
- // TODO: Replace `any` with type
134
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
135
- getCode ( _to : any , cb : any ) {
136
- cb ( null , '0xab' ) ;
137
- }
138
- }
139
126
const result = await determineTransactionType (
140
127
{
141
128
...txParams ,
142
129
data : '0x095ea7b30000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C9700000000000000000000000000000000000000000000000000000000000000005' ,
143
130
} ,
144
- new MockEthQuery ( new FakeProvider ( ) ) ,
131
+ createMockEthQuery ( '0xab' ) ,
132
+ ) ;
133
+ expect ( result ) . toMatchObject ( {
134
+ type : TransactionType . tokenMethodApprove ,
135
+ getCodeResponse : '0xab' ,
136
+ } ) ;
137
+ } ) ;
138
+
139
+ it ( 'returns a token approve type when data is uppercase' , async ( ) => {
140
+ const result = await determineTransactionType (
141
+ {
142
+ ...txParams ,
143
+ data : '0x095EA7B30000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C9700000000000000000000000000000000000000000000000000000000000000005' ,
144
+ } ,
145
+ createMockEthQuery ( '0xab' ) ,
145
146
) ;
146
147
expect ( result ) . toMatchObject ( {
147
148
type : TransactionType . tokenMethodApprove ,
@@ -150,20 +151,13 @@ describe('determineTransactionType', () => {
150
151
} ) ;
151
152
152
153
it ( 'returns a contract deployment type when "to" is falsy and there is data' , async ( ) => {
153
- class MockEthQuery extends EthQuery {
154
- // TODO: Replace `any` with type
155
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
156
- getCode ( _to : any , cb : any ) {
157
- cb ( null , '' ) ;
158
- }
159
- }
160
154
const result = await determineTransactionType (
161
155
{
162
156
...txParams ,
163
157
to : '' ,
164
158
data : '0xabd' ,
165
159
} ,
166
- new MockEthQuery ( new FakeProvider ( ) ) ,
160
+ createMockEthQuery ( '' ) ,
167
161
) ;
168
162
expect ( result ) . toMatchObject ( {
169
163
type : TransactionType . deployContract ,
@@ -172,19 +166,12 @@ describe('determineTransactionType', () => {
172
166
} ) ;
173
167
174
168
it ( 'returns a simple send type with a 0x getCodeResponse when there is data, but the "to" address is not a contract address' , async ( ) => {
175
- class MockEthQuery extends EthQuery {
176
- // TODO: Replace `any` with type
177
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
178
- getCode ( _to : any , cb : any ) {
179
- cb ( null , '0x' ) ;
180
- }
181
- }
182
169
const result = await determineTransactionType (
183
170
{
184
171
...txParams ,
185
172
data : '0xabd' ,
186
173
} ,
187
- new MockEthQuery ( new FakeProvider ( ) ) ,
174
+ createMockEthQuery ( '0x' ) ,
188
175
) ;
189
176
expect ( result ) . toMatchObject ( {
190
177
type : TransactionType . simpleSend ,
@@ -193,19 +180,12 @@ describe('determineTransactionType', () => {
193
180
} ) ;
194
181
195
182
it ( 'returns a simple send type with a null getCodeResponse when "to" is truthy and there is data, but getCode returns an error' , async ( ) => {
196
- class MockEthQuery extends EthQuery {
197
- // TODO: Replace `any` with type
198
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
199
- getCode ( _to : any , cb : any ) {
200
- cb ( new Error ( 'Some error' ) ) ;
201
- }
202
- }
203
183
const result = await determineTransactionType (
204
184
{
205
185
...txParams ,
206
186
data : '0xabd' ,
207
187
} ,
208
- new MockEthQuery ( new FakeProvider ( ) ) ,
188
+ createMockEthQuery ( null , true ) ,
209
189
) ;
210
190
expect ( result ) . toMatchObject ( {
211
191
type : TransactionType . simpleSend ,
@@ -214,19 +194,12 @@ describe('determineTransactionType', () => {
214
194
} ) ;
215
195
216
196
it ( 'returns a contract interaction type with the correct getCodeResponse when "to" is truthy and there is data, and it is not a token transaction' , async ( ) => {
217
- class MockEthQuery extends EthQuery {
218
- // TODO: Replace `any` with type
219
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
220
- getCode ( _to : any , cb : any ) {
221
- cb ( null , '0xa' ) ;
222
- }
223
- }
224
197
const result = await determineTransactionType (
225
198
{
226
199
...txParams ,
227
200
data : 'abd' ,
228
201
} ,
229
- new MockEthQuery ( new FakeProvider ( ) ) ,
202
+ createMockEthQuery ( '0xa' ) ,
230
203
) ;
231
204
expect ( result ) . toMatchObject ( {
232
205
type : TransactionType . contractInteraction ,
@@ -235,19 +208,12 @@ describe('determineTransactionType', () => {
235
208
} ) ;
236
209
237
210
it ( 'returns a contract interaction type with the correct getCodeResponse when "to" is a contract address and data is falsy' , async ( ) => {
238
- class MockEthQuery extends EthQuery {
239
- // TODO: Replace `any` with type
240
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
241
- getCode ( _to : any , cb : any ) {
242
- cb ( null , '0xa' ) ;
243
- }
244
- }
245
211
const result = await determineTransactionType (
246
212
{
247
213
...txParams ,
248
214
data : '' ,
249
215
} ,
250
- new MockEthQuery ( new FakeProvider ( ) ) ,
216
+ createMockEthQuery ( '0xa' ) ,
251
217
) ;
252
218
expect ( result ) . toMatchObject ( {
253
219
type : TransactionType . contractInteraction ,
@@ -256,21 +222,13 @@ describe('determineTransactionType', () => {
256
222
} ) ;
257
223
258
224
it ( 'returns contractInteraction for send with approve' , async ( ) => {
259
- class MockEthQuery extends EthQuery {
260
- // TODO: Replace `any` with type
261
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
262
- getCode ( _to : any , cb : any ) {
263
- cb ( null , '0xa' ) ;
264
- }
265
- }
266
-
267
225
const result = await determineTransactionType (
268
226
{
269
227
...txParams ,
270
228
value : '0x5af3107a4000' ,
271
229
data : '0x095ea7b30000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C9700000000000000000000000000000000000000000000000000000000000000005' ,
272
230
} ,
273
- new MockEthQuery ( new FakeProvider ( ) ) ,
231
+ createMockEthQuery ( '0xa' ) ,
274
232
) ;
275
233
expect ( result ) . toMatchObject ( {
276
234
type : TransactionType . contractInteraction ,
0 commit comments