Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
0xkenj1 committed Oct 10, 2024
1 parent 22d1c1a commit 9cdbedf
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 101 deletions.
4 changes: 2 additions & 2 deletions packages/data-flow/src/eventsFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export class EventsFetcher implements IEventsFetcher {
constructor(private indexerClient: IIndexerClient) {}
/* @inheritdoc */
async fetchEventsByBlockNumberAndLogIndex(
chainId: number,
blockNumber: number,
chainId: bigint,
blockNumber: bigint,
logIndex: number,
limit: number = 100,
): Promise<AnyProtocolEvent[]> {
Expand Down
4 changes: 2 additions & 2 deletions packages/data-flow/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export interface IEventsFetcher {
* @param limit limit of events to fetch
*/
fetchEventsByBlockNumberAndLogIndex(
chainId: number,
blockNumber: number,
chainId: bigint,
blockNumber: bigint,
logIndex: number,
limit?: number,
): Promise<AnyProtocolEvent[]>;
Expand Down
10 changes: 4 additions & 6 deletions packages/data-flow/test/unit/eventsFetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("EventsFetcher", () => {
block_timestamp: 123123123,
contract_name: "Allo",
event_name: "PoolCreated",
event_id: "123",
src_address: "0x1234567890123456789012345678901234567890",
log_index: 0,
params: { contractAddress: "0x1234" },
Expand All @@ -35,14 +34,13 @@ describe("EventsFetcher", () => {
block_timestamp: 123123123,
contract_name: "Allo",
event_name: "PoolCreated",
event_id: "123",
src_address: "0x1234567890123456789012345678901234567890",
log_index: 0,
params: { contractAddress: "0x1234" },
},
];
const chainId = 1;
const blockNumber = 1000;
const chainId = 1n;
const blockNumber = 1000n;
const logIndex = 0;
const limit = 100;

Expand All @@ -64,8 +62,8 @@ describe("EventsFetcher", () => {
});

it("should handle errors thrown by indexer client", async () => {
const chainId = 1;
const blockNumber = 1000;
const chainId = 1n;
const blockNumber = 1000n;
const logIndex = 0;

indexerClientMock.getEventsAfterBlockNumberAndLogIndex.mockRejectedValue(
Expand Down
4 changes: 2 additions & 2 deletions packages/indexer-client/src/interfaces/indexerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export interface IIndexerClient {
* @param limit Limit of events to fetch
*/
getEventsAfterBlockNumberAndLogIndex(
chainId: number,
fromBlock: number,
chainId: bigint,
fromBlock: bigint,
logIndex: number,
limit?: number,
): Promise<AnyProtocolEvent[]>;
Expand Down
50 changes: 29 additions & 21 deletions packages/indexer-client/src/providers/envioIndexerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,41 @@ export class EnvioIndexerClient implements IIndexerClient {
}
/* @inheritdoc */
public async getEventsAfterBlockNumberAndLogIndex(
chainId: number,
blockNumber: number,
chainId: bigint,
blockNumber: bigint,
logIndex: number,
limit: number = 100,
): Promise<AnyProtocolEvent[]> {
try {
const response = (await this.client.rawRequest(gql`
query getEventsAfterBlockNumberAndLogIndex {
raw_events(
where: {
chain_id: { _eq: ${chainId} }
block_number: { _gte: ${blockNumber} }
log_index: { _gt: ${logIndex} }
}
limit: ${limit}
const response = (await this.client.request(
gql`
query getEventsAfterBlockNumberAndLogIndex(
$chainId: Int!
$blockNumber: Int!
$logIndex: Int!
$limit: Int!
) {
block_number
block_timestamp
chain_id
contract_name
event_name
log_index
params
src_address
raw_events(
where: {
chain_id: { _eq: $chainId }
block_number: { _gte: $blockNumber }
log_index: { _gt: $logIndex }
}
limit: $limit
) {
block_number: blockNumber
block_timestamp: blockTimestamp
chain_id: chainId
contract_name: contractName
event_name: eventName
log_index: logIndex
params
src_address: srcAddress
}
}
}
`)) as { data: { raw_events: AnyProtocolEvent[] } };
`,
{ chainId, blockNumber, logIndex, limit },
)) as { data: { raw_events: AnyProtocolEvent[] } };
if (response?.data?.raw_events) {
return response.data.raw_events;
} else {
Expand Down
94 changes: 34 additions & 60 deletions packages/indexer-client/test/unit/envioIndexerClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ vi.mock("graphql-request", async (importOriginal) => {
...mod,
GraphQLClient: vi.fn().mockImplementation(() => ({
setHeader: vi.fn(),
rawRequest: vi.fn(),
request: vi.fn(),
})),
};
});
Expand All @@ -31,11 +31,11 @@ describe("EnvioIndexerClient", () => {
});

describe("constructor", () => {
it("should create a GraphQLClient with the provided URL", () => {
it("creates a GraphQLClient with the provided URL", () => {
expect(GraphQLClient).toHaveBeenCalledWith("http://example.com/graphql");
});

it("should set the x-hasura-admin-secret header", () => {
it("sets the x-hasura-admin-secret header", () => {
expect(graphqlClient.setHeader).toHaveBeenCalledWith("x-hasura-admin-secret", "secret");
});
});
Expand All @@ -48,123 +48,97 @@ describe("EnvioIndexerClient", () => {
block_timestamp: 123123123,
contract_name: "Allo",
event_name: "PoolCreated",
event_id: "123",
src_address: "0x1234567890123456789012345678901234567890",
log_index: 0,
params: { contractAddress: "0x1234" },
},
];

it("should return events when the query is successful", async () => {
it("returns events when the query is successful", async () => {
const mockedResponse = {
status: 200,
headers: {},
data: {
raw_events: mockEvents,
},
};
graphqlClient.rawRequest.mockResolvedValue(mockedResponse);
graphqlClient.request.mockResolvedValue(mockedResponse);

const result = await envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(
1,
12345,
1n,
12345n,
0,
100,
);
expect(result).toEqual(mockEvents);
});

it("should use default limit when not provided", async () => {
const mockedResponse = {
status: 200,
headers: {},
data: {
raw_events: mockEvents,
},
};
graphqlClient.rawRequest.mockResolvedValue(mockedResponse);

await envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(1, 12345, 0);
expect(graphqlClient.rawRequest).toHaveBeenCalledWith(
expect.stringContaining("limit: 100"),
);
});

it("should use provided limit", async () => {
const mockedResponse = {
status: 200,
headers: {},
data: {
raw_events: mockEvents,
},
};
graphqlClient.rawRequest.mockResolvedValue(mockedResponse);

await envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(1, 12345, 0, 50);
expect(graphqlClient.rawRequest).toHaveBeenCalledWith(
expect.stringContaining("limit: 50"),
);
});

it("should throw InvalidIndexerResponse when response structure is incorrect", async () => {
it("throws InvalidIndexerResponse when response structure is incorrect", async () => {
const mockedResponse = {
status: 200,
headers: {},
data: {
raw_events: undefined,
},
};
graphqlClient.rawRequest.mockResolvedValue(mockedResponse);
graphqlClient.request.mockResolvedValue(mockedResponse);

await expect(
envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(1, 12345, 0),
envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(1n, 12345n, 0),
).rejects.toThrow(InvalidIndexerResponse);
});

it("should throw IndexerClientError when GraphQL request fails", async () => {
it("throws IndexerClientError when GraphQL request fails", async () => {
const error = new Error("GraphQL request failed");
graphqlClient.rawRequest.mockRejectedValue(error);
graphqlClient.request.mockRejectedValue(error);

await expect(
envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(1, 12345, 0),
envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(1n, 12345n, 0),
).rejects.toThrow(IndexerClientError);
});

it("should include chainId, blockNumber, and logIndex in the query", async () => {
it("uses the default limit value when limit is not provided", async () => {
const mockedResponse = {
status: 200,
headers: {},
data: {
raw_events: mockEvents,
},
};
graphqlClient.rawRequest.mockResolvedValue(mockedResponse);
graphqlClient.request.mockResolvedValue(mockedResponse);

await envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(1, 12345, 0);
expect(graphqlClient.rawRequest).toHaveBeenCalledWith(
expect.stringContaining("chain_id: { _eq: 1 }"),
);
expect(graphqlClient.rawRequest).toHaveBeenCalledWith(
expect.stringContaining("block_number: { _gte: 12345 }"),
// Call the method without the limit argument
const result = await envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(
1n,
12345n,
0,
);
expect(graphqlClient.rawRequest).toHaveBeenCalledWith(
expect.stringContaining("log_index: { _gt: 0 }"),

expect(result).toEqual(mockEvents);
expect(graphqlClient.request).toHaveBeenCalledWith(
expect.any(String), // We can check the query string later if necessary
{
chainId: 1n,
blockNumber: 12345n,
logIndex: 0,
limit: 100, // Ensure the default limit is used
},
);
});

it("should return an empty array when no events are found", async () => {
it("returns an empty array when no events are found", async () => {
const mockedResponse = {
status: 200,
headers: {},
data: {
raw_events: [],
},
};
graphqlClient.rawRequest.mockResolvedValue(mockedResponse);
graphqlClient.request.mockResolvedValue(mockedResponse);

const result = await envioIndexerClient.getEventsAfterBlockNumberAndLogIndex(
1,
12345,
1n,
12345n,
0,
);
expect(result).toEqual([]);
Expand Down
16 changes: 8 additions & 8 deletions packages/shared/src/types/events/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export type EventParams<T extends ContractName, E extends ContractToEventName<T>
* This type is used to represent a protocol event.
*/
export type ProtocolEvent<T extends ContractName, E extends ContractToEventName<T>> = {
block_number: number;
block_timestamp: number;
chain_id: number;
contract_name: T;
event_id: string;
event_name: E;
log_index: number;
//TODO: make blocknumber and chainId bigints, implies implementing adapter patterns in the EventsFetcher or IndexerClient
blockNumber: number;
blockTimestamp: number;
chainId: number;
contractName: T;
eventName: E;
logIndex: number;
params: EventParams<T, E>;
src_address: Address;
srcAddress: Address;
};

export type AnyProtocolEvent = ProtocolEvent<ContractName, ContractToEventName<ContractName>>;

0 comments on commit 9cdbedf

Please sign in to comment.