Skip to content

Commit 7b7c8dc

Browse files
committed
fix: docs & getPool params
1 parent 6894f5b commit 7b7c8dc

File tree

10 files changed

+140
-107
lines changed

10 files changed

+140
-107
lines changed

README.md

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ npm install uniswap-dev-kit
1919

2020
## Quick Start
2121

22-
### 1. Configure and create an SDK instance
22+
### 1. Configure and create SDK instances
2323

2424
```ts
2525
import { createInstance } from "uniswap-dev-kit";
2626

27-
const config = {
27+
// Create instance for Ethereum mainnet
28+
createInstance({
2829
chainId: 1,
2930
rpcUrl: "https://eth.llamarpc.com",
3031
contracts: {
@@ -36,9 +37,29 @@ const config = {
3637
universalRouter: "0x...",
3738
permit2: "0x..."
3839
}
39-
};
40+
});
4041

41-
createInstance(config);
42+
// Create instance for another chain (e.g., Base)
43+
createInstance({
44+
chainId: 8453,
45+
rpcUrl: "https://mainnet.base.org",
46+
contracts: {
47+
// Base Uniswap V4 contract addresses...
48+
}
49+
});
50+
```
51+
52+
The SDK automatically manages multiple instances based on chainId. When using hooks or utilities, just specify the chainId to use the corresponding instance:
53+
54+
```ts
55+
// Will use Ethereum mainnet instance
56+
const ethPool = await getPool({ tokens: [...] }, 1);
57+
58+
// Will use Base instance
59+
const basePool = await getPool({ tokens: [...] }, 8453);
60+
61+
// If you only have one instance, chainId is optional
62+
const singleChainPool = await getPool({ tokens: [...] });
4263
```
4364

4465
### 2. Get a quote (vanilla JS/TS)
@@ -47,16 +68,25 @@ createInstance(config);
4768
import { getQuote } from "uniswap-dev-kit";
4869
import { parseEther } from "viem";
4970

50-
const quote = await getQuote({
51-
tokens: [
52-
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
53-
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" // WETH
54-
],
55-
feeTier: 3000,
56-
tickSpacing: 60,
57-
amountIn: parseEther("1"),
58-
zeroForOne: true
59-
});
71+
const quote = await getQuote(
72+
{
73+
tokens: [
74+
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
75+
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
76+
],
77+
feeTier: 3000,
78+
tickSpacing: 60,
79+
amountIn: parseEther("1"),
80+
},
81+
1,
82+
{
83+
enabled: true,
84+
staleTime: 30000,
85+
gcTime: 300000,
86+
retry: 3,
87+
onSuccess: (data) => console.log("Quote received:", data),
88+
},
89+
);
6090
console.log(quote.amountOut);
6191
```
6292

@@ -79,7 +109,8 @@ function QuoteComponent() {
79109
tickSpacing: 60,
80110
amountIn: parseEther("1"),
81111
zeroForOne: true
82-
}
112+
},
113+
chainId: 1
83114
});
84115

85116
if (isLoading) return <span>Loading...</span>;
@@ -101,8 +132,8 @@ function PoolComponent() {
101132
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
102133
],
103134
fee: 3000,
104-
chainId: 1
105-
}
135+
},
136+
chainId: 1
106137
});
107138

108139
if (isLoading) return <span>Loading...</span>;

src/hooks/useGetPool.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import { useQuery } from "@tanstack/react-query";
1515
* const { data, isLoading, error, refetch } = useGetPool({
1616
* params: {
1717
* tokens: [token0, token1],
18-
* chainId: 1,
1918
* fee: FeeTier.MEDIUM,
2019
* hooks: "0x0000000000000000000000000000000000000000"
2120
* },
21+
* chainId: 1,
2222
* queryOptions: {
2323
* enabled: true,
2424
* staleTime: 30000,
@@ -39,12 +39,13 @@ function serializeParams(params?: PoolParams) {
3939

4040
export function useGetPool({
4141
params,
42+
chainId,
4243
queryOptions = {},
4344
}: UseGetPoolOptions = {}) {
4445
if (!params) throw new Error("No params provided");
4546
return useQuery<PoolResponse, Error, PoolResponse, unknown[]>({
46-
queryKey: ["pool", serializeParams(params)],
47-
queryFn: () => getPool(params),
47+
queryKey: ["pool", serializeParams(params), chainId],
48+
queryFn: () => getPool(params, chainId),
4849
...queryOptions,
4950
});
5051
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export * from "@/hooks/useGetQuote";
88

99
// Utils
1010
export * from "@/utils/getQuote";
11-
export * from "@/utils/getTokenInstance";
11+
export * from "@/utils/getTokens";
1212

1313
// Types
1414
export * from "@/types";

src/test/hooks/useGetPool.test.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { UseGetPoolOptions } from "@/types/hooks/useGetPool";
33
import { getPool } from "@/utils/getPool";
44
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
55
import { renderHook, waitFor } from "@testing-library/react";
6+
import { Token } from "@uniswap/sdk-core";
67
import { jsx as _jsx } from "react/jsx-runtime";
78
import type { Mock } from "vitest";
89
import { beforeEach, describe, expect, it, vi } from "vitest";
@@ -35,18 +36,8 @@ describe("useGetPool", () => {
3536

3637
it("should fetch pool data successfully", async () => {
3738
const mockPool = {
38-
token0: {
39-
address: USDC,
40-
decimals: 6,
41-
name: "USD Coin",
42-
symbol: "USDC",
43-
},
44-
token1: {
45-
address: WETH,
46-
decimals: 18,
47-
name: "Wrapped Ether",
48-
symbol: "WETH",
49-
},
39+
token0: new Token(1, USDC, 6, "USDC", "USD Coin"),
40+
token1: new Token(1, WETH, 18, "WETH", "Wrapped Ether"),
5041
fee: 3000,
5142
tickSpacing: 60,
5243
};
@@ -59,8 +50,8 @@ describe("useGetPool", () => {
5950
params: {
6051
tokens: [USDC, WETH],
6152
fee: 3000,
62-
chainId: 1,
6353
},
54+
chainId: 1,
6455
}),
6556
{ wrapper },
6657
);
@@ -73,11 +64,13 @@ describe("useGetPool", () => {
7364
expect(result.current.error).toBeNull();
7465
expect(result.current.isLoading).toBe(false);
7566
expect(result.current.status).toBe("success");
76-
expect(getPool).toHaveBeenCalledWith({
77-
tokens: [USDC, WETH],
78-
fee: 3000,
79-
chainId: 1,
80-
});
67+
expect(getPool).toHaveBeenCalledWith(
68+
{
69+
tokens: [USDC, WETH],
70+
fee: 3000,
71+
},
72+
1,
73+
);
8174
});
8275

8376
it("should handle errors", async () => {
@@ -92,8 +85,8 @@ describe("useGetPool", () => {
9285
params: {
9386
tokens: [USDC, WETH],
9487
fee: 3000,
95-
chainId: 1,
9688
},
89+
chainId: 1,
9790
}),
9891
{ wrapper },
9992
);
@@ -118,18 +111,8 @@ describe("useGetPool", () => {
118111

119112
it("should handle custom query options", async () => {
120113
const mockPool = {
121-
token0: {
122-
address: USDC,
123-
decimals: 6,
124-
name: "USD Coin",
125-
symbol: "USDC",
126-
},
127-
token1: {
128-
address: WETH,
129-
decimals: 18,
130-
name: "Wrapped Ether",
131-
symbol: "WETH",
132-
},
114+
token0: new Token(1, USDC, 6, "USDC", "USD Coin"),
115+
token1: new Token(1, WETH, 18, "WETH", "Wrapped Ether"),
133116
fee: 3000,
134117
tickSpacing: 60,
135118
};
@@ -142,8 +125,8 @@ describe("useGetPool", () => {
142125
params: {
143126
tokens: [USDC, WETH],
144127
fee: 3000,
145-
chainId: 1,
146128
},
129+
chainId: 1,
147130
queryOptions: {
148131
enabled: true,
149132
staleTime: 5000,

src/test/utils/getPool.test.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { type Address, zeroAddress } from "viem";
55
import { beforeEach, describe, expect, it, vi } from "vitest";
66

77
const mockGetInstance = vi.fn();
8-
const mockGetTokenInstances = vi.fn();
8+
const mockGetTokens = vi.fn();
99
const mockUseReadContracts = vi.fn();
1010

1111
vi.mock("@/core/uniDevKitV4Factory", () => ({
1212
getInstance: () => mockGetInstance(),
1313
}));
1414

15-
vi.mock("@/utils/getTokenInstance", () => ({
16-
getTokenInstances: () => mockGetTokenInstances(),
15+
vi.mock("@/utils/getTokens", () => ({
16+
getTokens: () => mockGetTokens(),
1717
}));
1818

1919
vi.mock("wagmi", () => ({
@@ -35,11 +35,13 @@ describe("useV4Pool", () => {
3535
it("should throw error if SDK instance not found", async () => {
3636
mockGetInstance.mockReturnValueOnce(undefined);
3737
await expect(
38-
getPool({
39-
tokens: mockTokens,
40-
chainId: mockChainId,
41-
fee: FeeTier.MEDIUM,
42-
}),
38+
getPool(
39+
{
40+
tokens: mockTokens,
41+
fee: FeeTier.MEDIUM,
42+
},
43+
mockChainId,
44+
),
4345
).rejects.toThrow("SDK not found");
4446
});
4547

@@ -48,14 +50,16 @@ describe("useV4Pool", () => {
4850
getClient: vi.fn(),
4951
getContractAddress: vi.fn(),
5052
});
51-
mockGetTokenInstances.mockResolvedValueOnce(null);
53+
mockGetTokens.mockResolvedValueOnce(null);
5254

5355
await expect(
54-
getPool({
55-
tokens: mockTokens,
56-
chainId: mockChainId,
57-
fee: FeeTier.MEDIUM,
58-
}),
56+
getPool(
57+
{
58+
tokens: mockTokens,
59+
fee: FeeTier.MEDIUM,
60+
},
61+
mockChainId,
62+
),
5963
).rejects.toThrow("Failed to fetch token instances");
6064
});
6165

@@ -78,17 +82,19 @@ describe("useV4Pool", () => {
7882
getContractAddress: vi.fn(() => "0xMockAddress"),
7983
});
8084

81-
mockGetTokenInstances.mockResolvedValueOnce(mockTokenInstances);
85+
mockGetTokens.mockResolvedValueOnce(mockTokenInstances);
8286
mockUseReadContracts.mockReturnValueOnce({
8387
data: mockPoolData,
8488
isLoading: false,
8589
});
8690

87-
const result = await getPool({
88-
tokens: mockTokens,
89-
chainId: mockChainId,
90-
fee: FeeTier.MEDIUM,
91-
});
91+
const result = await getPool(
92+
{
93+
tokens: mockTokens,
94+
fee: FeeTier.MEDIUM,
95+
},
96+
mockChainId,
97+
);
9298

9399
expect(result.data).toBeDefined();
94100
expect(result.isLoading).toBe(false);
@@ -106,17 +112,19 @@ describe("useV4Pool", () => {
106112
getContractAddress: vi.fn(() => "0xMockAddress"),
107113
});
108114

109-
mockGetTokenInstances.mockResolvedValueOnce(mockTokenInstances);
115+
mockGetTokens.mockResolvedValueOnce(mockTokenInstances);
110116
mockUseReadContracts.mockReturnValueOnce({
111117
data: null,
112118
isLoading: false,
113119
});
114120

115-
const result = await getPool({
116-
tokens: mockTokens,
117-
chainId: mockChainId,
118-
fee: FeeTier.MEDIUM,
119-
});
121+
const result = await getPool(
122+
{
123+
tokens: mockTokens,
124+
fee: FeeTier.MEDIUM,
125+
},
126+
mockChainId,
127+
);
120128

121129
expect(result.data).toBeUndefined();
122130
expect(result.isLoading).toBe(false);
@@ -140,17 +148,19 @@ describe("useV4Pool", () => {
140148
getContractAddress: vi.fn(() => "0xMockAddress"),
141149
});
142150

143-
mockGetTokenInstances.mockResolvedValueOnce(mockTokenInstances);
151+
mockGetTokens.mockResolvedValueOnce(mockTokenInstances);
144152
mockUseReadContracts.mockReturnValueOnce({
145153
data: mockPoolData,
146154
isLoading: false,
147155
});
148156

149-
const result = await getPool({
150-
tokens: mockTokens,
151-
chainId: mockChainId,
152-
fee: FeeTier.MEDIUM,
153-
});
157+
const result = await getPool(
158+
{
159+
tokens: mockTokens,
160+
fee: FeeTier.MEDIUM,
161+
},
162+
mockChainId,
163+
);
154164

155165
expect(result.data).toBeUndefined();
156166
expect(result.isLoading).toBe(false);

0 commit comments

Comments
 (0)