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 16, 2024
1 parent 44d8c71 commit dd1a922
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 59 deletions.
2 changes: 1 addition & 1 deletion packages/chain-providers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof abi>, constructorReturnParams: ReturnType)`
- `multicall(args: MulticallParameters<contracts, allowFailure>)`
Expand Down
4 changes: 2 additions & 2 deletions packages/chain-providers/src/providers/evmProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ export class EvmProvider {
* Retrieves the current block number.
* @returns {Promise<GetBlockReturnType>} Latest block number.
*/
async getBlockByNumber(blockNumber: number): Promise<GetBlockReturnType> {
return this.client.getBlock({ blockNumber: BigInt(blockNumber) });
async getBlockByNumber(blockNumber: bigint): Promise<GetBlockReturnType> {
return this.client.getBlock({ blockNumber });
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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([
Expand All @@ -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");
Expand All @@ -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[]");

Expand Down
2 changes: 1 addition & 1 deletion packages/chain-providers/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "tests"]
"exclude": ["node_modules", "dist", "test"]
}
32 changes: 16 additions & 16 deletions packages/data-flow/test/unit/eventsFetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
},
];
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/data-flow/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "tests"]
"exclude": ["node_modules", "dist", "test"]
}
2 changes: 1 addition & 1 deletion packages/data-flow/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
"include": ["src/**/*", "test/**/*"]
}
2 changes: 1 addition & 1 deletion packages/indexer-client/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "tests"]
"exclude": ["node_modules", "dist", "test"]
}
2 changes: 1 addition & 1 deletion packages/pricing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"axios": "1.7.7"
},
"devDependencies": {
"axios-mock-adapter": "2.0.0"
"axios-mock-adapter": "2.1.0"
}
}
61 changes: 46 additions & 15 deletions packages/pricing/test/providers/coingecko.provider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MockAdapter from "axios-mock-adapter";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import axios, { isAxiosError } from "axios";

Check failure on line 1 in packages/pricing/test/providers/coingecko.provider.spec.ts

View workflow job for this annotation

GitHub Actions / lint / Run Linters

'axios' is defined but never used

Check failure on line 1 in packages/pricing/test/providers/coingecko.provider.spec.ts

View workflow job for this annotation

GitHub Actions / lint / Run Linters

'isAxiosError' is defined but never used
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { Address, NATIVE_TOKEN_ADDRESS } from "@grants-stack-indexer/shared";

Expand All @@ -10,28 +10,48 @@ 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<typeof import("axios")>();

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", () => {
it("return token price for a supported chain and valid token", async () => {
const mockResponse = {
prices: [[1609459200000, 100]],
};
mock.onGet().reply(200, mockResponse);
mock.get.mockResolvedValueOnce({ status: 200, data: mockResponse });

const result = await provider.getTokenPrice(
1,
Expand All @@ -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",
);
});
Expand All @@ -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,
Expand All @@ -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",
);
});
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/pricing/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
"include": ["src/**/*", "test/**/*"]
}
2 changes: 1 addition & 1 deletion packages/processors/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
"include": ["src/**/*", "test/**/*"]
}
2 changes: 1 addition & 1 deletion packages/repository/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
"include": ["src/**/*", "test/**/*"]
}
2 changes: 1 addition & 1 deletion packages/shared/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "tests"]
"exclude": ["node_modules", "dist", "test"]
}
2 changes: 1 addition & 1 deletion packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
"include": ["src/**/*", "test/**/*"]
}
Loading

0 comments on commit dd1a922

Please sign in to comment.