-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildSwapCallData.test.ts
More file actions
110 lines (95 loc) · 2.97 KB
/
Copy pathbuildSwapCallData.test.ts
File metadata and controls
110 lines (95 loc) · 2.97 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { createMockSdkInstance } from "@/test/helpers/sdkInstance";
import { buildSwapCallData } from "@/utils/buildSwapCallData";
import * as getQuoteModule from "@/utils/getQuote";
import { Token } from "@uniswap/sdk-core";
import { Pool } from "@uniswap/v4-sdk";
import { type Address, zeroAddress } from "viem";
import { describe, expect, it, vi } from "vitest";
const sdkInstance = createMockSdkInstance();
// Mock getQuote to return a fixed amount
vi.spyOn(getQuoteModule, "getQuote").mockImplementation(async () => ({
amountOut: BigInt(1000000000000000000), // 1 WETH
estimatedGasUsed: BigInt(100000),
timestamp: Date.now(),
}));
describe("buildSwapCallData", () => {
// USDC and WETH on Mainnet
const mockTokens: [Address, Address] = [
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
];
const mockTokenInstances = [
new Token(1, mockTokens[0], 6, "USDC", "USD Coin"),
new Token(1, mockTokens[1], 18, "WETH", "Wrapped Ether"),
];
const mockPool = new Pool(
mockTokenInstances[0],
mockTokenInstances[1],
3000,
60,
zeroAddress,
"79228162514264337593543950336",
"1000000",
0,
);
it("should build swap calldata for token0 to token1", async () => {
const params = {
tokenIn: mockTokens[0],
amountIn: BigInt(1000000), // 1 USDC
slippageTolerance: 50,
pool: mockPool,
recipient: zeroAddress,
};
const calldata = await buildSwapCallData(params, sdkInstance);
expect(calldata).toBeDefined();
expect(calldata).toMatch(/^0x/); // Should be a hex string
});
it("should build swap calldata for token1 to token0", async () => {
const params = {
tokenIn: mockTokens[1],
amountIn: BigInt(1000000000000000000), // 1 WETH
slippageTolerance: 50,
pool: mockPool,
recipient: zeroAddress,
};
const calldata = await buildSwapCallData(params, sdkInstance);
expect(calldata).toBeDefined();
expect(calldata).toMatch(/^0x/);
});
it("should include minimum output amount in calldata", async () => {
const params = {
tokenIn: mockTokens[0],
amountIn: BigInt(1000000),
slippageTolerance: 50,
pool: mockPool,
recipient: zeroAddress,
};
const calldata = await buildSwapCallData(params, sdkInstance);
expect(calldata).toBeDefined();
expect(calldata).toMatch(/^0x/);
});
it("should handle zero minimum output amount", async () => {
const params = {
tokenIn: mockTokens[0],
amountIn: BigInt(1000000),
slippageTolerance: 50,
pool: mockPool,
recipient: zeroAddress,
};
const calldata = await buildSwapCallData(params, sdkInstance);
expect(calldata).toBeDefined();
expect(calldata).toMatch(/^0x/);
});
it("should include deadline in calldata", async () => {
const params = {
tokenIn: mockTokens[0],
amountIn: BigInt(1000000),
slippageTolerance: 50,
pool: mockPool,
recipient: zeroAddress,
};
const calldata = await buildSwapCallData(params, sdkInstance);
expect(calldata).toBeDefined();
expect(calldata).toMatch(/^0x/);
});
});