-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetQuote.ts
More file actions
61 lines (56 loc) · 1.88 KB
/
Copy pathgetQuote.ts
File metadata and controls
61 lines (56 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import V4QuoterAbi from "@/constants/abis/V4Quoter";
import type { UniDevKitV4Instance } from "@/types/core";
import type { QuoteParams, QuoteResponse } from "@/types/utils/getQuote";
/**
* Fetches a quote for a token swap using the V4 Quoter contract.
* This function uses the provided pool instance to simulate the quote.
*
* @param params - The parameters required for the quote, including pool and amount.
* @param instance - UniDevKitV4 instance for contract interaction
* @returns A Promise that resolves to the quote result, including the amount out and gas estimate.
* @throws Will throw an error if:
* - Simulation fails (e.g., insufficient liquidity, invalid parameters)
* - Contract call reverts
*/
export async function getQuote(
params: QuoteParams,
instance: UniDevKitV4Instance,
): Promise<QuoteResponse> {
const { client, contracts } = instance;
const { quoter } = contracts;
const {
pool: { poolKey },
} = params;
try {
// Build the parameters for quoteExactInputSingle
const quoteParams = {
poolKey: {
currency0: poolKey.currency0 as `0x${string}`,
currency1: poolKey.currency1 as `0x${string}`,
fee: poolKey.fee,
tickSpacing: poolKey.tickSpacing,
hooks: poolKey.hooks as `0x${string}`,
},
zeroForOne: params.zeroForOne,
exactAmount: params.amountIn,
hookData: params.hookData || "0x",
};
// Simulate the quote to estimate the amount out
const simulation = await client.simulateContract({
address: quoter,
abi: V4QuoterAbi,
functionName: "quoteExactInputSingle",
args: [quoteParams],
});
// Extract the results
const [amountOut, gasEstimate] = simulation.result as [bigint, bigint];
return {
amountOut,
estimatedGasUsed: gasEstimate,
timestamp: Date.now(),
};
} catch (error) {
console.error("Error simulating quote:", error);
throw new Error(`Failed to fetch quote: ${(error as Error).message}`);
}
}