Skip to content

Check which beacons of a beacon set need to be updated #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/signed-data-store/signed-data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from 'ethers';

import { logger } from '../logger';
import { getState, updateState } from '../state';
import type { SignedData, AirnodeAddress, TemplateId, DataFeedId } from '../types';
import type { SignedData, AirnodeAddress, TemplateId, BeaconId } from '../types';
import { deriveBeaconId } from '../utils';

export const verifySignedData = ({ airnode, templateId, timestamp, signature, encodedValue }: SignedData) => {
Expand Down Expand Up @@ -90,7 +90,7 @@ export const setStoreDataPoint = (signedData: SignedData) => {
export const getStoreDataPointByAirnodeAndTemplate = (airnode: AirnodeAddress, template: TemplateId) =>
getState().signedApiStore[deriveBeaconId(airnode, template)!];

export const getStoreDataPoint = (dataFeedId: DataFeedId) => getState().signedApiStore[dataFeedId];
export const getStoreDataPoint = (dataFeedId: BeaconId) => getState().signedApiStore[dataFeedId];

export const clear = () => {
updateState((draft) => {
Expand Down
4 changes: 2 additions & 2 deletions src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
DecodedDataFeed,
ChainId,
SignedData,
DataFeedId,
BeaconId,
ProviderName,
SignedApiUrl,
AirnodeAddress,
Expand Down Expand Up @@ -41,7 +41,7 @@ export interface State {
dataFetcherInterval?: NodeJS.Timeout;
gasPriceStore: Record<ChainId, Record<ProviderName, GasState>>;
derivedSponsorWallets: Record<DapiName, PrivateKey>;
signedApiStore: Record<DataFeedId, SignedData>;
signedApiStore: Record<BeaconId, SignedData>;
signedApiUrlStore: Record<ChainId, Record<ProviderName, Record<AirnodeAddress, SignedApiUrl>>>;
dapis: Record<DapiName, DapiState>;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type AirnodeAddress = EvmAddress;
export type TemplateId = EvmId;
export type DapiName = string;
export type PrivateKey = string;
export type DataFeedId = EvmId;
export type BeaconId = EvmId;
export type ChainId = string;
export type ProviderName = string;
export type SignedApiUrl = string;
Expand Down
197 changes: 197 additions & 0 deletions src/update-feeds/check-feeds.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import { ethers } from 'ethers';

import { initializeState } from '../../test/fixtures/mock-config';
import { allowPartial } from '../../test/utils';
import * as signedDataStore from '../signed-data-store/signed-data-store';
import { updateState } from '../state';
import type { BeaconId, SignedData } from '../types';

import * as contractUtils from './api3-server-v1';
import { multicallBeaconValues, getUpdatableFeeds } from './check-feeds';
import * as checkFeedsModule from './check-feeds';
import type { ReadDapiWithIndexResponse } from './dapi-data-registry';

// https://github.com/api3dao/airnode-protocol-v1/blob/fa95f043ce4b50e843e407b96f7ae3edcf899c32/contracts/api3-server-v1/DataFeedServer.sol#L132
const encodeBeaconValue = (numericValue: string) => {
const numericValueAsBigNumber = ethers.BigNumber.from(numericValue);

return ethers.utils.defaultAbiCoder.encode(['int256'], [numericValueAsBigNumber]);
};

describe('getUpdatableFeeds', () => {
const feedIds = [
'0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
'0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc7',
'0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc8',
] as const;

beforeEach(() => {
initializeState();
updateState((draft) => {
draft.signedApiStore = allowPartial<Record<BeaconId, SignedData>>({
'0x000a': { timestamp: '100', encodedValue: encodeBeaconValue('200') },
'0x000b': { timestamp: '150', encodedValue: encodeBeaconValue('250') },
'0x000c': { timestamp: '200', encodedValue: encodeBeaconValue('300') },
});
});
});

it('calls and parses a multicall', async () => {
const tryMulticallMock = jest.fn().mockReturnValue({
successes: [true, true, true],
returndata: [
ethers.utils.defaultAbiCoder.encode(['int224', 'uint32'], [100, 105]),
ethers.utils.defaultAbiCoder.encode(['int224', 'uint32'], [101, 106]),
ethers.utils.defaultAbiCoder.encode(['int224', 'uint32'], [102, 107]),
],
});

const encodeFunctionDataMock = jest.fn();
encodeFunctionDataMock.mockReturnValueOnce('0xfirst');
encodeFunctionDataMock.mockReturnValueOnce('0xsecond');
encodeFunctionDataMock.mockReturnValueOnce('0xthird');

const mockContract = {
connect: jest.fn().mockReturnValue({
callStatic: {
tryMulticall: tryMulticallMock,
},
}),
interface: { encodeFunctionData: encodeFunctionDataMock },
};

jest.spyOn(contractUtils, 'getApi3ServerV1').mockReturnValue(mockContract as any);

const callAndParseMulticallPromise = multicallBeaconValues(feedIds as unknown as string[], 'hardhat', '31337');

await expect(callAndParseMulticallPromise).resolves.toStrictEqual([
{
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
onChainValue: {
timestamp: ethers.BigNumber.from(105),
value: ethers.BigNumber.from(100),
},
},
{
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc7',
onChainValue: {
timestamp: ethers.BigNumber.from(106),
value: ethers.BigNumber.from(101),
},
},
{
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc8',
onChainValue: {
timestamp: ethers.BigNumber.from(107),
value: ethers.BigNumber.from(102),
},
},
]);
expect(tryMulticallMock).toHaveBeenCalledWith(['0xfirst', '0xsecond', '0xthird']);
});

it('returns updatable feeds', async () => {
jest.useFakeTimers().setSystemTime(90);

const multicallResult = [
{
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
onChainValue: {
timestamp: ethers.BigNumber.from(150),
value: ethers.BigNumber.from('400'),
},
},
{
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc7',
onChainValue: {
timestamp: ethers.BigNumber.from(160),
value: ethers.BigNumber.from('500'),
},
},
{
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc8',
onChainValue: {
timestamp: ethers.BigNumber.from(170),
value: ethers.BigNumber.from('600'),
},
},
];

// Only the third feed will satisfy the timestamp check
const mockSignedDataStore = allowPartial<Record<string, SignedData>>({
[feedIds[0]]: {
timestamp: '101',
encodedValue: encodeBeaconValue('200'),
},
[feedIds[1]]: {
timestamp: '150',
encodedValue: encodeBeaconValue('250'),
},
[feedIds[2]]: {
timestamp: '200',
encodedValue: encodeBeaconValue('300'),
},
});

const getStoreDataPointSpy = jest.spyOn(signedDataStore, 'getStoreDataPoint');
getStoreDataPointSpy.mockImplementation((dataFeedId: string) => mockSignedDataStore[dataFeedId]!);

// None of the feeds failed to update
jest.spyOn(checkFeedsModule, 'multicallBeaconValues').mockResolvedValue(multicallResult);

const batch = allowPartial<ReadDapiWithIndexResponse[]>([
{
updateParameters: { deviationThresholdInPercentage: ethers.BigNumber.from(1) },
dataFeedValue: {
value: ethers.BigNumber.from(10),
timestamp: 95,
},
decodedDataFeed: {
dataFeedId: '0x000',
beacons: [{ beaconId: feedIds[0] }, { beaconId: feedIds[1] }, { beaconId: feedIds[2] }],
},
dapiName: 'test',
},
]);

const checkFeedsResult = getUpdatableFeeds(batch, 1, 'hardhat', '31337');

await expect(checkFeedsResult).resolves.toStrictEqual([
{
updatableBeacons: [
expect.objectContaining({
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc8',
signedData: {
encodedValue: '0x000000000000000000000000000000000000000000000000000000000000012c',
timestamp: '200',
},
}),
],
dapiInfo: {
dapiName: 'test',
dataFeedValue: {
timestamp: 95,
value: ethers.BigNumber.from('10'),
},
decodedDataFeed: {
beacons: [
expect.objectContaining({
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc6',
}),
expect.objectContaining({
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc7',
}),
expect.objectContaining({
beaconId: '0xf5c140bcb4814dfec311d38f6293e86c02d32ba1b7da027fe5b5202cae35dbc8',
}),
],
dataFeedId: '0x000',
},
updateParameters: {
deviationThresholdInPercentage: ethers.BigNumber.from(1),
},
},
},
]);
});
});
Loading