Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Nov 9, 2023
1 parent cac0f18 commit 246ad25
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/gas-price/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './gas-price';
6 changes: 3 additions & 3 deletions src/update-feeds/dapi-data-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('helper functions', () => {
beacons: [
{
airnodeAddress: '0xc52EeA00154B4fF1EbbF8Ba39FDe37F1AC3B9Fd4',
dataFeedId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
templateId: '0x457a3b3da67e394a895ea49e534a4d91b2d009477bef15eab8cbed313925b010',
},
],
Expand All @@ -41,12 +41,12 @@ describe('helper functions', () => {
beacons: [
{
airnodeAddress: '0xc52EeA00154B4fF1EbbF8Ba39FDe37F1AC3B9Fd4',
dataFeedId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
templateId: '0x457a3b3da67e394a895ea49e534a4d91b2d009477bef15eab8cbed313925b010',
},
{
airnodeAddress: '0xc52EeA00154B4fF1EbbF8Ba39FDe37F1AC3B9Fd4',
dataFeedId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
templateId: '0x457a3b3da67e394a895ea49e534a4d91b2d009477bef15eab8cbed313925b010',
},
],
Expand Down
28 changes: 20 additions & 8 deletions src/update-feeds/update-feeds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as utilsModule from '../utils';

import * as dapiDataRegistryModule from './dapi-data-registry';
import * as updateFeedsModule from './update-feeds';
import * as updateTransactionModule from './update-transactions';

jest.mock('../state');

Expand Down Expand Up @@ -145,19 +146,19 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {

it('fetches other batches in a staggered way and logs errors', async () => {
// Prepare the mocked contract so it returns three batches (of size 1) of dAPIs and the second batch fails to load.
const firstBatch = generateReadDapiWithIndexResponse();
const thirdBatch = generateReadDapiWithIndexResponse();
const firstDapi = generateReadDapiWithIndexResponse();
const thirdDapi = generateReadDapiWithIndexResponse();
const dapiDataRegistry = generateMockDapiDataRegistry();
jest
.spyOn(dapiDataRegistryModule, 'getDapiDataRegistry')
.mockReturnValue(dapiDataRegistry as unknown as DapiDataRegistry);
dapiDataRegistry.interface.decodeFunctionResult.mockImplementation((_fn, value) => value);
dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({
successes: [true, true],
returndata: [[ethers.BigNumber.from(3)], firstBatch],
returndata: [[ethers.BigNumber.from(3)], firstDapi],
});
dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [false], returndata: [] });
dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [true], returndata: [thirdBatch] });
dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [true], returndata: [thirdDapi] });
const sleepCalls = [] as number[];
const originalSleep = utilsModule.sleep;
jest.spyOn(utilsModule, 'sleep').mockImplementation(async (ms) => {
Expand All @@ -174,7 +175,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
signedApiUrlStore: { '31337': { 'some-test-provider': ['url-one'] } },
signedApiStore: {},
gasPriceStore: {
'123': {
'31337': {
'some-test-provider': {
gasPrices: [],
sponsorLastUpdateTimestampMs: {
Expand All @@ -185,7 +186,13 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
},
})
);
jest.spyOn(updateFeedsModule, 'getFeedsToUpdate').mockImplementation(() => []);
jest
.spyOn(updateFeedsModule, 'getFeedsToUpdate')
.mockImplementation(() => [
allowPartial<updateTransactionModule.UpdateableDapi>({ dapiInfo: firstDapi }),
allowPartial<updateTransactionModule.UpdateableDapi>({ dapiInfo: thirdDapi }),
]);
jest.spyOn(updateTransactionModule, 'updateFeeds').mockResolvedValue([null, null]);

await updateFeedsModule.runUpdateFeed(
'provider-name',
Expand All @@ -197,7 +204,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
DapiDataRegistry: '0xDD78254f864F97f65e2d86541BdaEf88A504D2B2',
},
}),
'123'
'31337'
);

// Expect the contract to fetch the batches to be called with the correct stagger time.
Expand All @@ -212,7 +219,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
'Failed to get active dAPIs batch',
new Error('One of the multicalls failed')
);
expect(logger.debug).toHaveBeenCalledTimes(6);
expect(logger.debug).toHaveBeenCalledTimes(7);
expect(logger.debug).toHaveBeenNthCalledWith(1, 'Fetching first batch of dAPIs batches');
expect(logger.debug).toHaveBeenNthCalledWith(2, 'Processing batch of active dAPIs', expect.anything());
expect(logger.debug).toHaveBeenNthCalledWith(3, 'Fetching batches of active dAPIs', {
Expand All @@ -226,5 +233,10 @@ describe(updateFeedsModule.runUpdateFeed.name, () => {
batchIndex: 2,
});
expect(logger.debug).toHaveBeenNthCalledWith(6, 'Processing batch of active dAPIs', expect.anything());
expect(logger.debug).toHaveBeenNthCalledWith(7, 'Finished processing batches of active dAPIs', {
batchesCount: 3,
errorCount: 4,
successCount: 0,
});
});
});
2 changes: 1 addition & 1 deletion src/update-feeds/update-feeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { range, size, zip } from 'lodash';
import { calculateMedian, checkUpdateConditions } from '../condition-check';
import type { Chain } from '../config/schema';
import { INT224_MAX, INT224_MIN } from '../constants';
import { clearSponsorLastUpdateTimestampMs } from '../gas-price/gas-price';
import { clearSponsorLastUpdateTimestampMs } from '../gas-price';
import { logger } from '../logger';
import { getStoreDataPoint } from '../signed-data-store';
import { getState, updateState } from '../state';
Expand Down
4 changes: 2 additions & 2 deletions src/update-feeds/update-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Api3ServerV1 } from '@api3/airnode-protocol-v1';
import { go } from '@api3/promise-utils';
import { ethers } from 'ethers';

import { getAirseekerRecommendedGasPrice } from '../gas-price/gas-price';
import { getAirseekerRecommendedGasPrice } from '../gas-price';
import { logger } from '../logger';
import { getState, updateState } from '../state';
import type { SignedData, ChainId, ProviderName } from '../types';
Expand Down Expand Up @@ -74,7 +74,7 @@ export const updateFeeds = async (
updateableBeacons.map((beacon) => beacon.beaconId)
);

logger.debug('Deriving sponsor wallet');
logger.debug('Getting derived sponsor wallet');
const sponsorWallet = getDerivedSponsorWallet(sponsorWalletMnemonic, dapiName);

const goGasPrice = await go(async () =>
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/gas-price.feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
multiplyGasPrice,
initializeGasStore,
clearExpiredStoreGasPrices,
} from '../../src/gas-price/gas-price';
} from '../../src/gas-price';
import { getState, updateState } from '../../src/state';
import { init } from '../fixtures/mock-config';

Expand Down
8 changes: 5 additions & 3 deletions test/e2e/update-feeds.feature.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from 'ethers';
import { omit } from 'lodash';

import { initializeGasStore } from '../../src/gas-price/gas-price';
import { initializeGasStore } from '../../src/gas-price';
import { logger } from '../../src/logger';
import * as stateModule from '../../src/state';
import { runUpdateFeed } from '../../src/update-feeds';
Expand Down Expand Up @@ -82,7 +82,9 @@ it('updates blockchain data', async () => {
},
]);

expect(logger.debug).toHaveBeenCalledTimes(4);
expect(logger.debug).toHaveBeenNthCalledWith(1, 'Estimating gas limit');
expect(logger.debug).toHaveBeenNthCalledWith(2, 'Deriving sponsor wallet');
expect(logger.debug).toHaveBeenNthCalledWith(5, 'Successfully updated dAPI');
expect(logger.debug).toHaveBeenNthCalledWith(2, 'Getting derived sponsor wallet');
expect(logger.debug).toHaveBeenNthCalledWith(3, 'Derived new sponsor wallet', expect.anything());
expect(logger.debug).toHaveBeenNthCalledWith(4, 'Updating dAPI', expect.anything());
});

0 comments on commit 246ad25

Please sign in to comment.