diff --git a/packages/chain-providers/README.md b/packages/chain-providers/README.md index bf09f87..b330313 100644 --- a/packages/chain-providers/README.md +++ b/packages/chain-providers/README.md @@ -64,7 +64,7 @@ Available methods - `getMulticall3Address()` - `getBlockNumber()` -- `getBlockByNumber(blockNumber: number)` +- `getBlockByNumber(blockNumber: bigint)` - `readContract(contractAddress: Address, abi: TAbi functionName: TFunctionName, args?: TArgs)` - `batchRequest(abi: AbiWithConstructor,bytecode: Hex, args: ContractConstructorArgs, constructorReturnParams: ReturnType)` - `multicall(args: MulticallParameters)` diff --git a/packages/chain-providers/src/providers/evmProvider.ts b/packages/chain-providers/src/providers/evmProvider.ts index 5ceda9e..22c048a 100644 --- a/packages/chain-providers/src/providers/evmProvider.ts +++ b/packages/chain-providers/src/providers/evmProvider.ts @@ -86,8 +86,8 @@ export class EvmProvider { * Retrieves the current block number. * @returns {Promise} Latest block number. */ - async getBlockByNumber(blockNumber: number): Promise { - return this.client.getBlock({ blockNumber: BigInt(blockNumber) }); + async getBlockByNumber(blockNumber: bigint): Promise { + return this.client.getBlock({ blockNumber }); } /** diff --git a/packages/chain-providers/test/unit/providers/evmProvider.service.spec.ts b/packages/chain-providers/test/unit/providers/evmProvider.service.spec.ts index 8944d6c..da4681a 100644 --- a/packages/chain-providers/test/unit/providers/evmProvider.service.spec.ts +++ b/packages/chain-providers/test/unit/providers/evmProvider.service.spec.ts @@ -71,7 +71,7 @@ describe("EvmProvider", () => { }); describe("getBalance", () => { - it("should return the balance of the specified address", async () => { + it("returns the balance of the specified address", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const address = "0x123456789"; const expectedBalance = 100n; @@ -85,7 +85,7 @@ describe("EvmProvider", () => { }); describe("getBlockNumber", () => { - it("should return the current block number", async () => { + it("returns the current block number", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const expectedBlockNumber = 1000n; vi.spyOn(mockClient, "getBlockNumber").mockResolvedValue(expectedBlockNumber); @@ -97,7 +97,7 @@ describe("EvmProvider", () => { }); describe("getGasPrice", () => { - it("should return the current gas price", async () => { + it("returns the current gas price", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const expectedGasPrice = BigInt(100); // Mock the getGasPrice method of the Viem client @@ -129,7 +129,7 @@ describe("EvmProvider", () => { }); describe("getStorageAt", () => { - it("should return the value of the storage slot at the given address and slot number", async () => { + it("returns the value of the storage slot at the given address and slot number", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const address = "0x123456789"; const slot = 1; @@ -142,7 +142,7 @@ describe("EvmProvider", () => { expect(mockClient.getStorageAt).toHaveBeenCalledWith({ address, slot: "0x1" }); }); - it("should return the value of the storage slot at the given address and slot value", async () => { + it("returns the value of the storage slot at the given address and slot value", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const address = "0x123456789"; const slot = "0x12"; @@ -155,7 +155,7 @@ describe("EvmProvider", () => { expect(mockClient.getStorageAt).toHaveBeenCalledWith({ address, slot: "0x12" }); }); - it("should throw an error if the slot is not a positive integer", async () => { + it("throws an error if the slot is not a positive integer", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const address = "0x123456789"; const slot = -1; @@ -167,7 +167,7 @@ describe("EvmProvider", () => { }); describe("readContract", () => { - it("should call the readContract method of the Viem client with the correct arguments", async () => { + it("calls the readContract method of the Viem client with the correct arguments", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const contractAddress = "0x123456789"; const abi = testAbi; @@ -187,7 +187,7 @@ describe("EvmProvider", () => { }); }); - it("should call the readContract method of the Viem client with the correct arguments when args are provided", async () => { + it("calls the readContract method of the Viem client with the correct arguments when args are provided", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const contractAddress = "0x123456789"; const functionName = "tokenURI"; @@ -215,7 +215,7 @@ describe("EvmProvider", () => { }); describe("batchRequest", () => { - it("should properly encode bytecode data and decode return data from batch request call", async () => { + it("encodes bytecode data and decode return data from batch request call", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const returnAbiParams = viem.parseAbiParameters([ "TokenData[] returnData", @@ -244,7 +244,7 @@ describe("EvmProvider", () => { ]); }); - it("should fail if no data is returned", async () => { + it("fails if no data is returned", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const returnAbiParams = viem.parseAbiParameters([ "TokenData[] returnData", @@ -263,7 +263,7 @@ describe("EvmProvider", () => { ).rejects.toThrowError(DataDecodeException); }); - it("should fail if decoded data does not match validator (missing struct fields)", async () => { + it("fails if decoded data does not match validator (missing struct fields)", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); // this schema is incorrect, it should have 3 fields instead of 2 const returnAbiParams = viem.parseAbiParameters([ @@ -285,7 +285,7 @@ describe("EvmProvider", () => { ); }); - it("should fail if decoded data does not match validator (not struct vs struct)", async () => { + it("fails if decoded data does not match validator (not struct vs struct)", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); // this schema is incorrect, it should have 3 fields instead of 2 const returnAbiParams = viem.parseAbiParameters("uint8 decimals, address[] owners"); @@ -304,7 +304,7 @@ describe("EvmProvider", () => { ); }); - it("should properly decode address[]", async () => { + it("decodes address[]", async () => { viemProvider = new EvmProvider(defaultRpcUrls, defaultMockChain, mockLogger); const returnAbiParams = viem.parseAbiParameters("address[]"); diff --git a/packages/chain-providers/tsconfig.build.json b/packages/chain-providers/tsconfig.build.json index 0be123c..32768e3 100644 --- a/packages/chain-providers/tsconfig.build.json +++ b/packages/chain-providers/tsconfig.build.json @@ -7,5 +7,5 @@ "outDir": "dist" }, "include": ["src/**/*"], - "exclude": ["node_modules", "dist", "tests"] + "exclude": ["node_modules", "dist", "test"] } diff --git a/packages/data-flow/test/unit/eventsFetcher.spec.ts b/packages/data-flow/test/unit/eventsFetcher.spec.ts index a547b5e..371f616 100644 --- a/packages/data-flow/test/unit/eventsFetcher.spec.ts +++ b/packages/data-flow/test/unit/eventsFetcher.spec.ts @@ -17,26 +17,26 @@ describe("EventsFetcher", () => { eventsFetcher = new EventsFetcher(indexerClientMock); }); - it("should fetch events by block number and log index", async () => { + it("fetches events by block number and log index", async () => { const mockEvents: AnyProtocolEvent[] = [ { - chain_id: 1, - block_number: 12345, - block_timestamp: 123123123, - contract_name: "Allo", - event_name: "PoolCreated", - src_address: "0x1234567890123456789012345678901234567890", - log_index: 0, + chainId: 1, + blockNumber: 12345, + blockTimestamp: 123123123, + contractName: "Allo", + eventName: "PoolCreated", + srcAddress: "0x1234567890123456789012345678901234567890", + logIndex: 0, params: { contractAddress: "0x1234" }, }, { - chain_id: 1, - block_number: 12345, - block_timestamp: 123123123, - contract_name: "Allo", - event_name: "PoolCreated", - src_address: "0x1234567890123456789012345678901234567890", - log_index: 0, + chainId: 1, + blockNumber: 12345, + blockTimestamp: 123123123, + contractName: "Allo", + eventName: "PoolCreated", + srcAddress: "0x1234567890123456789012345678901234567890", + logIndex: 0, params: { contractAddress: "0x1234" }, }, ]; @@ -62,7 +62,7 @@ describe("EventsFetcher", () => { expect(result).toEqual(mockEvents); }); - it("should handle errors thrown by indexer client", async () => { + it("handles errors thrown by indexer client", async () => { const chainId = 1n; const blockNumber = 1000n; const logIndex = 0; diff --git a/packages/data-flow/tsconfig.build.json b/packages/data-flow/tsconfig.build.json index 56d3112..a9bfa3d 100644 --- a/packages/data-flow/tsconfig.build.json +++ b/packages/data-flow/tsconfig.build.json @@ -9,5 +9,5 @@ "outDir": "dist" }, "include": ["src/**/*"], - "exclude": ["node_modules", "dist", "tests"] + "exclude": ["node_modules", "dist", "test"] } diff --git a/packages/data-flow/tsconfig.json b/packages/data-flow/tsconfig.json index 66bb87a..21c1c5b 100644 --- a/packages/data-flow/tsconfig.json +++ b/packages/data-flow/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "../../tsconfig.json", - "include": ["src/**/*"] + "include": ["src/**/*", "test/**/*"] } diff --git a/packages/indexer-client/tsconfig.build.json b/packages/indexer-client/tsconfig.build.json index 56d3112..a9bfa3d 100644 --- a/packages/indexer-client/tsconfig.build.json +++ b/packages/indexer-client/tsconfig.build.json @@ -9,5 +9,5 @@ "outDir": "dist" }, "include": ["src/**/*"], - "exclude": ["node_modules", "dist", "tests"] + "exclude": ["node_modules", "dist", "test"] } diff --git a/packages/pricing/package.json b/packages/pricing/package.json index a6bfd4f..69e12af 100644 --- a/packages/pricing/package.json +++ b/packages/pricing/package.json @@ -32,6 +32,6 @@ "axios": "1.7.7" }, "devDependencies": { - "axios-mock-adapter": "2.0.0" + "axios-mock-adapter": "2.1.0" } } diff --git a/packages/pricing/test/providers/coingecko.provider.spec.ts b/packages/pricing/test/providers/coingecko.provider.spec.ts index 7210197..126da8b 100644 --- a/packages/pricing/test/providers/coingecko.provider.spec.ts +++ b/packages/pricing/test/providers/coingecko.provider.spec.ts @@ -1,5 +1,5 @@ -import MockAdapter from "axios-mock-adapter"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import axios, { isAxiosError } from "axios"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { Address, NATIVE_TOKEN_ADDRESS } from "@grants-stack-indexer/shared"; @@ -10,20 +10,40 @@ import { UnsupportedChainException, } from "../../src/external.js"; +const mock = vi.hoisted(() => ({ + get: vi.fn(), + post: vi.fn(), +})); + +vi.mock("axios", async (importActual) => { + const actual = await importActual(); + + const mockAxios = { + default: { + ...actual.default, + create: vi.fn(() => ({ + ...actual.default.create(), + get: mock.get, + post: mock.post, + })), + }, + isAxiosError: actual.isAxiosError, // Return it directly from the mock + }; + + return mockAxios; +}); describe("CoingeckoProvider", () => { let provider: CoingeckoProvider; - let mock: MockAdapter; beforeEach(() => { provider = new CoingeckoProvider({ apiKey: "test-api-key", apiType: "demo", }); - mock = new MockAdapter(provider["axios"]); }); afterEach(() => { - mock.reset(); + vi.restoreAllMocks(); }); describe("getTokenPrice", () => { @@ -31,7 +51,7 @@ describe("CoingeckoProvider", () => { const mockResponse = { prices: [[1609459200000, 100]], }; - mock.onGet().reply(200, mockResponse); + mock.get.mockResolvedValueOnce({ status: 200, data: mockResponse }); const result = await provider.getTokenPrice( 1, @@ -46,7 +66,7 @@ describe("CoingeckoProvider", () => { }; expect(result).toEqual(expectedPrice); - expect(mock.history.get[0].url).toContain( + expect(mock.get).toHaveBeenCalledWith( "/coins/ethereum/contract/0x1234567890123456789012345678901234567890/market_chart/range?vs_currency=usd&from=1609459200&to=1609545600&precision=full", ); }); @@ -55,7 +75,7 @@ describe("CoingeckoProvider", () => { const mockResponse = { prices: [[1609459200000, 100]], }; - mock.onGet().reply(200, mockResponse); + mock.get.mockResolvedValueOnce({ status: 200, data: mockResponse }); const result = await provider.getTokenPrice( 10, @@ -70,7 +90,7 @@ describe("CoingeckoProvider", () => { }; expect(result).toEqual(expectedPrice); - expect(mock.history.get[0].url).toContain( + expect(mock.get).toHaveBeenCalledWith( "/coins/ethereum/market_chart/range?vs_currency=usd&from=1609459200&to=1609545600&precision=full", ); }); @@ -79,7 +99,7 @@ describe("CoingeckoProvider", () => { const mockResponse = { prices: [], }; - mock.onGet().reply(200, mockResponse); + mock.get.mockResolvedValueOnce({ status: 200, data: mockResponse }); const result = await provider.getTokenPrice( 1, @@ -103,7 +123,11 @@ describe("CoingeckoProvider", () => { }); it("return undefined if 400 family error", async () => { - mock.onGet().replyOnce(400, "Bad Request"); + mock.get.mockRejectedValueOnce({ + status: 400, + data: "Bad Request", + isAxiosError: true, + }); const result = await provider.getTokenPrice( 1, @@ -126,9 +150,12 @@ describe("CoingeckoProvider", () => { ).rejects.toThrow(UnsupportedChainException); }); - it("should throw NetworkException for 500 family errors", async () => { - mock.onGet().reply(500, "Internal Server Error"); - + it("throws NetworkException for 500 family errors", async () => { + mock.get.mockRejectedValueOnce({ + status: 500, + data: "Internal Server Error", + isAxiosError: true, + }); await expect( provider.getTokenPrice( 1, @@ -140,7 +167,11 @@ describe("CoingeckoProvider", () => { }); it("throw NetworkException for network errors", async () => { - mock.onGet().networkErrorOnce(); + mock.get.mockRejectedValueOnce({ + status: 500, + data: "Network Error", + isAxiosError: true, + }); await expect( provider.getTokenPrice( diff --git a/packages/pricing/tsconfig.json b/packages/pricing/tsconfig.json index 66bb87a..21c1c5b 100644 --- a/packages/pricing/tsconfig.json +++ b/packages/pricing/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "../../tsconfig.json", - "include": ["src/**/*"] + "include": ["src/**/*", "test/**/*"] } diff --git a/packages/processors/tsconfig.json b/packages/processors/tsconfig.json index 66bb87a..21c1c5b 100644 --- a/packages/processors/tsconfig.json +++ b/packages/processors/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "../../tsconfig.json", - "include": ["src/**/*"] + "include": ["src/**/*", "test/**/*"] } diff --git a/packages/repository/tsconfig.json b/packages/repository/tsconfig.json index 66bb87a..21c1c5b 100644 --- a/packages/repository/tsconfig.json +++ b/packages/repository/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "../../tsconfig.json", - "include": ["src/**/*"] + "include": ["src/**/*", "test/**/*"] } diff --git a/packages/shared/tsconfig.build.json b/packages/shared/tsconfig.build.json index 56d3112..a9bfa3d 100644 --- a/packages/shared/tsconfig.build.json +++ b/packages/shared/tsconfig.build.json @@ -9,5 +9,5 @@ "outDir": "dist" }, "include": ["src/**/*"], - "exclude": ["node_modules", "dist", "tests"] + "exclude": ["node_modules", "dist", "test"] } diff --git a/packages/shared/tsconfig.json b/packages/shared/tsconfig.json index 66bb87a..21c1c5b 100644 --- a/packages/shared/tsconfig.json +++ b/packages/shared/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "../../tsconfig.json", - "include": ["src/**/*"] + "include": ["src/**/*", "test/**/*"] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7cd70c6..0051848 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -155,8 +155,8 @@ importers: version: 1.7.7 devDependencies: axios-mock-adapter: - specifier: 2.0.0 - version: 2.0.0(axios@1.7.7) + specifier: 2.1.0 + version: 2.1.0(axios@1.7.7) packages/processors: dependencies: @@ -1492,6 +1492,14 @@ packages: peerDependencies: axios: ">= 0.17.0" + axios-mock-adapter@2.1.0: + resolution: + { + integrity: sha512-AZUe4OjECGCNNssH8SOdtneiQELsqTsat3SQQCWLPjN436/H+L9AjWfV7bF+Zg/YL9cgbhrz5671hoh+Tbn98w==, + } + peerDependencies: + axios: ">= 0.17.0" + axios@1.7.7: resolution: { @@ -5328,6 +5336,12 @@ snapshots: fast-deep-equal: 3.1.3 is-buffer: 2.0.5 + axios-mock-adapter@2.1.0(axios@1.7.7): + dependencies: + axios: 1.7.7 + fast-deep-equal: 3.1.3 + is-buffer: 2.0.5 + axios@1.7.7: dependencies: follow-redirects: 1.15.9