Skip to content

Commit f08fdc6

Browse files
committed
chore: update README & package version
1 parent a5aabc3 commit f08fdc6

File tree

4 files changed

+157
-9
lines changed

4 files changed

+157
-9
lines changed

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,40 @@ const poolKey = await uniDevKit.getPoolKeyFromPoolId({
125125
```
126126

127127
### `buildSwapCallData`
128-
Construct calldata and value for a Universal Router swap.
128+
Construct calldata for a Universal Router swap.
129129
```ts
130+
// Basic swap
130131
const { calldata, value } = await uniDevKit.buildSwapCallData({
131-
tokenIn,
132-
tokenOut,
133-
amountIn: "1000000000000000000",
134-
recipient,
135-
slippageBips: 50
132+
tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
133+
amountIn: parseUnits("100", 6), // 100 USDC
134+
pool: pool,
135+
slippageTolerance: 50, // 0.5%
136+
recipient: "0x..."
137+
});
138+
139+
// Swap with permit2
140+
const permitData = await uniDevKit.preparePermit2Data({
141+
token: tokenIn,
142+
spender: uniDevKit.getContractAddress('universalRouter'),
143+
owner: userAddress
144+
});
145+
146+
const signature = await signer._signTypedData(permitData.toSign);
147+
const permitWithSignature = permitData.buildPermit2DataWithSignature(signature);
148+
149+
const { calldata: calldataWithPermit, value: valueWithPermit } = await uniDevKit.buildSwapCallData({
150+
tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
151+
amountIn: parseUnits("100", 6),
152+
pool: pool,
153+
slippageTolerance: 50,
154+
recipient: "0x...",
155+
permit2Signature: permitWithSignature
156+
});
157+
158+
const tx = await sendTransaction({
159+
to: uniDevKit.getContractAddress('universalRouter'),
160+
data: calldata,
161+
value
136162
});
137163
```
138164

@@ -208,4 +234,4 @@ See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
208234
- Releases are automated with [semantic-release](https://semantic-release.gitbook.io/semantic-release/).
209235

210236
## License
211-
MIT
237+
MIT

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uniswap-dev-kit",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"description": "A modern TypeScript library for integrating Uniswap into your dapp.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { createMockSdkInstance } from "@/test/helpers/sdkInstance";
2+
import { PERMIT2_ADDRESS } from "@uniswap/permit2-sdk";
3+
import { type Block, zeroAddress } from "viem";
4+
import { beforeEach, describe, expect, it, vi } from "vitest";
5+
import { preparePermit2Data } from "../preparePermit2Data";
6+
7+
describe("preparePermit2Data", () => {
8+
const mockInstance = createMockSdkInstance();
9+
const mockBlockTimestamp = 1234567890n;
10+
11+
const mockParams = {
12+
token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
13+
spender: "0x1234567890123456789012345678901234567890",
14+
owner: "0x0987654321098765432109876543210987654321",
15+
};
16+
17+
beforeEach(() => {
18+
vi.spyOn(mockInstance.client, "readContract").mockImplementation(
19+
async () => ({
20+
amount: "0",
21+
expiration: "0",
22+
nonce: "0",
23+
}),
24+
);
25+
vi.spyOn(mockInstance.client, "getBlock").mockResolvedValue({
26+
timestamp: mockBlockTimestamp,
27+
} as Block);
28+
});
29+
30+
it("should throw error for native token", async () => {
31+
await expect(
32+
preparePermit2Data(
33+
{
34+
...mockParams,
35+
token: zeroAddress,
36+
},
37+
mockInstance,
38+
),
39+
).rejects.toThrow("Native tokens are not supported for permit2");
40+
});
41+
42+
it("should prepare permit2 data with default sigDeadline", async () => {
43+
const result = await preparePermit2Data(mockParams, mockInstance);
44+
45+
expect(result.owner).toBe(mockParams.owner);
46+
expect(result.permit.details.token).toBe(mockParams.token);
47+
expect(result.permit.spender).toBe(mockParams.spender);
48+
expect(result.permit.sigDeadline).toBe(Number(mockBlockTimestamp) + 3600); // block timestamp + 1 hour
49+
expect(result.toSign.domain).toBeDefined();
50+
expect(result.toSign.types).toBeDefined();
51+
expect(result.toSign.values).toBeDefined();
52+
expect(result.buildPermit2DataWithSignature).toBeDefined();
53+
});
54+
55+
it("should prepare permit2 data with custom sigDeadline", async () => {
56+
const customDeadline = Number(mockBlockTimestamp) + 7200; // 2 hours from now
57+
const result = await preparePermit2Data(
58+
{
59+
...mockParams,
60+
sigDeadline: customDeadline,
61+
},
62+
mockInstance,
63+
);
64+
65+
expect(result.permit.sigDeadline).toBe(customDeadline);
66+
});
67+
68+
it("should build permit2 data with signature", async () => {
69+
const result = await preparePermit2Data(mockParams, mockInstance);
70+
const signature =
71+
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
72+
const permitWithSignature = result.buildPermit2DataWithSignature(signature);
73+
74+
expect(permitWithSignature).toEqual({
75+
owner: mockParams.owner,
76+
permit: result.permit,
77+
signature,
78+
});
79+
});
80+
81+
it("should fetch current allowance details", async () => {
82+
const mockAllowance = {
83+
amount: "1000000",
84+
expiration: "1234567890",
85+
nonce: "42",
86+
};
87+
88+
vi.spyOn(mockInstance.client, "readContract").mockImplementationOnce(
89+
async () => mockAllowance,
90+
);
91+
92+
const result = await preparePermit2Data(mockParams, mockInstance);
93+
94+
expect(mockInstance.client.readContract).toHaveBeenCalledWith({
95+
address: PERMIT2_ADDRESS,
96+
abi: expect.any(Array),
97+
functionName: "allowance",
98+
args: [mockParams.owner, mockParams.token, mockParams.spender],
99+
});
100+
101+
expect(result.permit.details.expiration).toBe(mockAllowance.expiration);
102+
expect(result.permit.details.nonce).toBe(mockAllowance.nonce);
103+
});
104+
});

src/utils/buildSwapCallData.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,32 @@ const buildPermit2StructInput = (permit: PermitSingle, signature: Hex) => {
3636
*
3737
* @example
3838
* ```typescript
39+
* // Basic swap
3940
* const swapParams = {
4041
* tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
4142
* amountIn: parseUnits("100", 6), // 100 USDC
4243
* pool: pool,
4344
* slippageTolerance: 50, // 0.5%
4445
* };
4546
*
46-
* const calldata = await buildSwapCallData(swapParams);
47+
* const calldata = await buildSwapCallData(swapParams, instance);
48+
*
49+
* // Swap with permit2
50+
* const permitData = await preparePermit2Data({
51+
* token: tokenIn,
52+
* spender: universalRouterAddress,
53+
* owner: userAddress
54+
* }, instance);
55+
*
56+
* const signature = await signer._signTypedData(permitData.toSign);
57+
* const permitWithSignature = permitData.buildPermit2DataWithSignature(signature);
58+
*
59+
* const swapParamsWithPermit = {
60+
* ...swapParams,
61+
* permit2Signature: permitWithSignature
62+
* };
63+
*
64+
* const calldataWithPermit = await buildSwapCallData(swapParamsWithPermit, instance);
4765
*
4866
* // Send transaction
4967
* const tx = await sendTransaction({

0 commit comments

Comments
 (0)