1
1
import { ethers } from 'ethers' ;
2
2
3
- import { generateMockDapiDataRegistry , generateReadDapisResponse } from '../../test/fixtures/dapi-data-registry' ;
3
+ import {
4
+ generateMockDapiDataRegistry ,
5
+ generateReadDapiWithIndexResponse ,
6
+ } from '../../test/fixtures/dapi-data-registry' ;
4
7
import { allowPartial } from '../../test/utils' ;
5
8
import type { DapiDataRegistry } from '../../typechain-types' ;
6
9
import type { Chain } from '../config/schema' ;
@@ -38,7 +41,7 @@ describe(startUpdateFeedLoops.name, () => {
38
41
39
42
// Expect the intervals to be called with the correct stagger time.
40
43
expect ( setInterval ) . toHaveBeenCalledTimes ( 2 ) ;
41
- expect ( intervalCalls [ 1 ] ! - intervalCalls [ 0 ] ! ) . toBeGreaterThanOrEqual ( 40 ) ; // Reserving 10s as the buffer for computing stagger time.
44
+ expect ( intervalCalls [ 1 ] ! - intervalCalls [ 0 ] ! ) . toBeGreaterThanOrEqual ( 40 ) ; // Reserving 10ms as the buffer for computing stagger time.
42
45
43
46
// Expect the logs to be called with the correct context.
44
47
expect ( logger . debug ) . toHaveBeenCalledTimes ( 3 ) ;
@@ -92,21 +95,21 @@ describe(startUpdateFeedLoops.name, () => {
92
95
93
96
// Expect the logs to be called with the correct context.
94
97
expect ( logger . debug ) . toHaveBeenCalledTimes ( 4 ) ;
95
- expect ( logger . debug ) . toHaveBeenCalledWith ( 'Starting update loops for chain' , {
98
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 1 , 'Starting update loops for chain' , {
96
99
chainId : '123' ,
97
100
staggerTime : 100 ,
98
101
providerNames : [ 'first-provider' ] ,
99
102
} ) ;
100
- expect ( logger . debug ) . toHaveBeenCalledWith ( 'Starting update loops for chain' , {
103
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 2 , 'Starting update feed loop' , {
104
+ chainId : '123' ,
105
+ providerName : 'first-provider' ,
106
+ } ) ;
107
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 3 , 'Starting update loops for chain' , {
101
108
chainId : '456' ,
102
109
staggerTime : 100 ,
103
110
providerNames : [ 'another-provider' ] ,
104
111
} ) ;
105
- expect ( logger . debug ) . toHaveBeenCalledWith ( 'Starting update feed loop' , {
106
- chainId : '123' ,
107
- providerName : 'first-provider' ,
108
- } ) ;
109
- expect ( logger . debug ) . toHaveBeenCalledWith ( 'Starting update feed loop' , {
112
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 4 , 'Starting update feed loop' , {
110
113
chainId : '456' ,
111
114
providerName : 'another-provider' ,
112
115
} ) ;
@@ -119,7 +122,7 @@ describe(runUpdateFeed.name, () => {
119
122
jest
120
123
. spyOn ( dapiDataRegistryModule , 'getDapiDataRegistry' )
121
124
. mockReturnValue ( dapiDataRegistry as unknown as DapiDataRegistry ) ;
122
- dapiDataRegistry . readDapis . mockRejectedValueOnce ( new Error ( 'provider-error' ) ) ;
125
+ dapiDataRegistry . callStatic . tryMulticall . mockRejectedValueOnce ( new Error ( 'provider-error' ) ) ;
123
126
jest . spyOn ( logger , 'error' ) ;
124
127
125
128
await runUpdateFeed (
@@ -145,16 +148,19 @@ describe(runUpdateFeed.name, () => {
145
148
146
149
it ( 'fetches other batches in a staggered way and logs errors' , async ( ) => {
147
150
// Prepare the mocked contract so it returns three batches (of size 1) of dAPIs and the second batch fails to load.
148
- const firstBatch = generateReadDapisResponse ( ) ;
149
- const thirdBatch = generateReadDapisResponse ( ) ;
151
+ const firstBatch = generateReadDapiWithIndexResponse ( ) ;
152
+ const thirdBatch = generateReadDapiWithIndexResponse ( ) ;
150
153
const dapiDataRegistry = generateMockDapiDataRegistry ( ) ;
151
154
jest
152
155
. spyOn ( dapiDataRegistryModule , 'getDapiDataRegistry' )
153
156
. mockReturnValue ( dapiDataRegistry as unknown as DapiDataRegistry ) ;
154
- dapiDataRegistry . readDapis . mockResolvedValueOnce ( firstBatch ) ;
155
- dapiDataRegistry . readDapis . mockRejectedValueOnce ( new Error ( 'provider-error' ) ) ;
156
- dapiDataRegistry . readDapis . mockResolvedValueOnce ( thirdBatch ) ;
157
- dapiDataRegistry . dapisCount . mockResolvedValueOnce ( ethers . BigNumber . from ( 3 ) ) ;
157
+ dapiDataRegistry . interface . decodeFunctionResult . mockImplementation ( ( _fn , value ) => value ) ;
158
+ dapiDataRegistry . callStatic . tryMulticall . mockResolvedValueOnce ( {
159
+ successes : [ true , true ] ,
160
+ returndata : [ [ ethers . BigNumber . from ( 3 ) ] , firstBatch ] ,
161
+ } ) ;
162
+ dapiDataRegistry . callStatic . tryMulticall . mockResolvedValueOnce ( { successes : [ false ] , returndata : [ ] } ) ;
163
+ dapiDataRegistry . callStatic . tryMulticall . mockResolvedValueOnce ( { successes : [ true ] , returndata : [ thirdBatch ] } ) ;
158
164
const sleepCalls = [ ] as number [ ] ;
159
165
const originalSleep = utilsModule . sleep ;
160
166
jest . spyOn ( utilsModule , 'sleep' ) . mockImplementation ( async ( ms ) => {
@@ -179,36 +185,42 @@ describe(runUpdateFeed.name, () => {
179
185
180
186
// Expect the contract to fetch the batches to be called with the correct stagger time.
181
187
expect ( utilsModule . sleep ) . toHaveBeenCalledTimes ( 3 ) ;
182
- expect ( sleepCalls [ 0 ] ) . toBeGreaterThanOrEqual ( 40 ) ; // Reserving 10s as the buffer for computing stagger time.
188
+ expect ( sleepCalls [ 0 ] ) . toBeGreaterThanOrEqual ( 40 ) ; // Reserving 10ms as the buffer for computing stagger time.
183
189
expect ( sleepCalls [ 1 ] ) . toBeGreaterThanOrEqual ( 0 ) ;
184
190
expect ( sleepCalls [ 2 ] ) . toBe ( 49.999_999_999_999_99 ) ; // Stagger time is actually 150 / 3 = 50, but there is an rounding error.
185
191
186
192
// Expect the logs to be called with the correct context.
187
193
expect ( logger . error ) . toHaveBeenCalledTimes ( 1 ) ;
188
- expect ( logger . error ) . toHaveBeenCalledWith ( 'Failed to get active dAPIs batch' , new Error ( 'provider-error' ) , {
189
- chainId : '123' ,
190
- providerName : 'provider-name' ,
191
- } ) ;
192
- expect ( logger . debug ) . toHaveBeenCalledTimes ( 4 ) ;
193
- expect ( logger . debug ) . toHaveBeenCalledWith ( 'Fetching first batch of dAPIs batches' , {
194
+ expect ( logger . error ) . toHaveBeenCalledWith (
195
+ 'Failed to get active dAPIs batch' ,
196
+ new Error ( 'One of the multicalls failed' ) ,
197
+ {
198
+ chainId : '123' ,
199
+ providerName : 'provider-name' ,
200
+ }
201
+ ) ;
202
+ expect ( logger . debug ) . toHaveBeenCalledTimes ( 6 ) ;
203
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 1 , 'Fetching first batch of dAPIs batches' , {
194
204
chainId : '123' ,
195
205
providerName : 'provider-name' ,
196
206
} ) ;
197
- expect ( logger . debug ) . toHaveBeenCalledWith ( 'Fetching batches of active dAPIs' , {
207
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 2 , 'Processing batch of active dAPIs' , expect . anything ( ) ) ;
208
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 3 , 'Fetching batches of active dAPIs' , {
198
209
batchesCount : 3 ,
199
- staggerTime : 49.999_999_999_999_99 ,
200
210
chainId : '123' ,
201
211
providerName : 'provider-name' ,
212
+ staggerTime : 49.999_999_999_999_99 ,
202
213
} ) ;
203
- expect ( logger . debug ) . toHaveBeenCalledWith ( 'Fetching batch of active dAPIs' , {
214
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 4 , 'Fetching batch of active dAPIs' , {
204
215
batchIndex : 1 ,
205
216
chainId : '123' ,
206
217
providerName : 'provider-name' ,
207
218
} ) ;
208
- expect ( logger . debug ) . toHaveBeenCalledWith ( 'Fetching batch of active dAPIs' , {
219
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 5 , 'Fetching batch of active dAPIs' , {
209
220
batchIndex : 2 ,
210
221
chainId : '123' ,
211
222
providerName : 'provider-name' ,
212
223
} ) ;
224
+ expect ( logger . debug ) . toHaveBeenNthCalledWith ( 6 , 'Processing batch of active dAPIs' , expect . anything ( ) ) ;
213
225
} ) ;
214
226
} ) ;
0 commit comments