@logswap/sdk 0.5.102 → 0.5.104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +14 -418
  2. package/dist/actions.d.ts +0 -39
  3. package/dist/actions.js +0 -39
  4. package/dist/admin.d.ts +0 -106
  5. package/dist/admin.js +1 -96
  6. package/dist/chains.d.ts +0 -49
  7. package/dist/chains.js +0 -30
  8. package/dist/claims.d.ts +0 -32
  9. package/dist/claims.js +0 -30
  10. package/dist/client.d.ts +0 -116
  11. package/dist/client.js +1 -231
  12. package/dist/fees.d.ts +1 -45
  13. package/dist/fees.js +0 -44
  14. package/dist/fpool.d.ts +4 -612
  15. package/dist/fpool.js +14 -490
  16. package/dist/generated.js +0 -30
  17. package/dist/history.d.ts +0 -61
  18. package/dist/history.js +5 -74
  19. package/dist/ids.d.ts +0 -30
  20. package/dist/ids.js +0 -31
  21. package/dist/index.d.ts +0 -7
  22. package/dist/index.js +0 -7
  23. package/dist/indexer.d.ts +0 -68
  24. package/dist/indexer.js +1 -77
  25. package/dist/keys.d.ts +0 -50
  26. package/dist/keys.js +0 -35
  27. package/dist/liquidity.d.ts +0 -128
  28. package/dist/liquidity.js +1 -107
  29. package/dist/onboard.d.ts +0 -52
  30. package/dist/onboard.js +1 -59
  31. package/dist/pools.d.ts +3 -157
  32. package/dist/pools.js +1 -164
  33. package/dist/positions.d.ts +0 -102
  34. package/dist/positions.js +1 -90
  35. package/dist/quote.d.ts +0 -14
  36. package/dist/quote.js +0 -14
  37. package/dist/swap.d.ts +0 -31
  38. package/dist/swap.js +5 -37
  39. package/dist/tokencache.d.ts +0 -48
  40. package/dist/tokencache.js +1 -73
  41. package/dist/write.d.ts +1 -87
  42. package/dist/write.js +1 -87
  43. package/package.json +3 -8
  44. package/src/actions.ts +0 -208
  45. package/src/admin.ts +0 -350
  46. package/src/chains.ts +0 -278
  47. package/src/claims.ts +0 -111
  48. package/src/client.ts +0 -436
  49. package/src/fees.ts +0 -138
  50. package/src/fpool.ts +0 -2049
  51. package/src/generated.ts +0 -4345
  52. package/src/history.ts +0 -383
  53. package/src/ids.ts +0 -78
  54. package/src/index.ts +0 -26
  55. package/src/indexer.ts +0 -264
  56. package/src/keys.ts +0 -138
  57. package/src/liquidity.ts +0 -456
  58. package/src/onboard.ts +0 -253
  59. package/src/pools.ts +0 -465
  60. package/src/positions.ts +0 -297
  61. package/src/quote.ts +0 -36
  62. package/src/swap.ts +0 -181
  63. package/src/tokencache.ts +0 -178
  64. package/src/write.ts +0 -236
package/src/onboard.ts DELETED
@@ -1,253 +0,0 @@
1
- /**
2
- * onboard.ts — getting a wallet to the point where router writes actually work.
3
- *
4
- * There are **three separate approvals** with three separate failure modes, and a UI that surfaces
5
- * them late produces failed transactions instead of prompts:
6
- *
7
- * 1. ERC-20 → Permit2 once per token, forever. Only when the router is Permit2-wired.
8
- * 2. Permit2 → router a standing allowance, by transaction or by signature.
9
- * 3. 6909 operator grant once per wallet, covers every market. Needed by burn/update/move.
10
- *
11
- * **The trap this module exists for:** when the router is deployed with Permit2, approving the
12
- * ERC-20 to the *router* is not enough and not used — the pull goes through Permit2, and the
13
- * failure is `NotAllowed()` from Permit2 rather than anything naming an allowance. Approving the
14
- * router directly looks correct and does nothing.
15
- *
16
- * **One router per manager** (decision 096), and each is its own Permit2 spender: step 1 is shared by
17
- * both (the ERC-20 goes to Permit2 once per token), steps 2 and 3 are per `side` — a wallet that trades
18
- * on both singletons signs one Permit2 allowance per router, and grants the operator on each manager.
19
- * Every helper here that depends on the router takes the `side` (`"F"` or `"C"`) right after the client.
20
- */
21
-
22
- import { parseAbi, zeroAddress, type Address, type Hash, type Hex } from "viem";
23
- import { routerOf, type LogswapClient, type Side } from "./client.js";
24
- import { requireWallet } from "./write.js";
25
-
26
- export const MAX_UINT256 = (1n << 256n) - 1n;
27
- export const MAX_UINT160 = (1n << 160n) - 1n;
28
- export const MAX_UINT48 = (1n << 48n) - 1n;
29
-
30
- /**
31
- * How long a Permit2 allowance to the router lives: 30 days, as Uniswap's own interface does. It
32
- * used to be `MAX_UINT48` — never — so a router bug or a compromised router could pull every
33
- * approved token from every user indefinitely. A lapsed allowance is re-granted by one more
34
- * signature, riding inside the next write ({@link permit2SigCalls}).
35
- */
36
- export const PERMIT2_EXPIRY_SECONDS = 30n * 24n * 3600n;
37
- /** An allowance that lapses within this window is treated as already lapsed: it could die in flight. */
38
- const EXPIRY_MARGIN_SECONDS = 3600;
39
-
40
- const nowSeconds = () => Math.floor(Date.now() / 1000);
41
- /** The expiry to grant now. */
42
- export const permit2Expiry = (): bigint => BigInt(nowSeconds()) + PERMIT2_EXPIRY_SECONDS;
43
- /** Whether a Permit2 allowance (amount, expiration) covers `need` for long enough to land. */
44
- export const permit2Covers = (amount: bigint, expiration: number, need: bigint): boolean =>
45
- amount >= need && expiration > nowSeconds() + EXPIRY_MARGIN_SECONDS;
46
-
47
- const ERC20 = parseAbi([
48
- "function approve(address spender, uint256 amount) returns (bool)",
49
- "function allowance(address owner, address spender) view returns (uint256)",
50
- ]);
51
-
52
- const PERMIT2 = parseAbi([
53
- "function approve(address token, address spender, uint160 amount, uint48 expiration)",
54
- "function allowance(address owner, address token, address spender) view returns (uint160 amount, uint48 expiration, uint48 nonce)",
55
- "function transferFrom(address from, address to, uint160 amount, address token)",
56
- ]);
57
-
58
- /** Is this deployment's router wired to Permit2? Decides which approval path applies. */
59
- export function usesPermit2(c: LogswapClient): boolean {
60
- return !!c.addresses.permit2 && c.addresses.permit2 !== zeroAddress;
61
- }
62
-
63
- /** The contract that will actually pull tokens: Permit2 when wired, otherwise the `side`'s router. */
64
- export function spenderFor(c: LogswapClient, side: Side): Address {
65
- return usesPermit2(c) ? c.addresses.permit2! : routerOf(c, side);
66
- }
67
-
68
- /** Step 1 — approve the *puller* on an ERC-20. Once per token. */
69
- export async function approveToken(
70
- c: LogswapClient,
71
- side: Side,
72
- token: Address,
73
- amount: bigint = MAX_UINT256,
74
- ): Promise<Hash> {
75
- const { account } = requireWallet(c);
76
- return c.wallet!.writeContract({
77
- address: token,
78
- abi: ERC20,
79
- functionName: "approve",
80
- args: [spenderFor(c, side), amount],
81
- account,
82
- chain: c.wallet!.chain,
83
- } as never);
84
- }
85
-
86
- /**
87
- * Step 2 — the standing Permit2 allowance for the `side`'s router.
88
- *
89
- * No-op when the deployment has no Permit2. Prefer the signature flow (`applyPermit2` on the
90
- * router, bundled through `multicall`) in production; this transaction form is what a test or a
91
- * simple UI wants.
92
- */
93
- export async function approvePermit2ForRouter(
94
- c: LogswapClient,
95
- side: Side,
96
- token: Address,
97
- amount: bigint = MAX_UINT160,
98
- expiration: bigint = permit2Expiry(),
99
- ): Promise<Hash | null> {
100
- if (!usesPermit2(c)) return null;
101
- const { account } = requireWallet(c);
102
- return c.wallet!.writeContract({
103
- address: c.addresses.permit2!,
104
- abi: PERMIT2,
105
- functionName: "approve",
106
- args: [token, routerOf(c, side), amount, Number(expiration)],
107
- account,
108
- chain: c.wallet!.chain,
109
- } as never);
110
- }
111
-
112
- /**
113
- * Both token-side steps in order, skipping any already satisfied.
114
- *
115
- * Returns the hashes actually sent, so a UI can report "2 approvals" honestly rather than always
116
- * prompting twice.
117
- */
118
- export async function onboardToken(c: LogswapClient, side: Side, token: Address, need: bigint): Promise<Hash[]> {
119
- // The address, not the account object: these two are READS. Signing paths take the object.
120
- const { address } = requireWallet(c);
121
- const sent: Hash[] = [];
122
-
123
- const allowance = (await c.public.readContract({
124
- address: token,
125
- abi: ERC20,
126
- functionName: "allowance",
127
- args: [address, spenderFor(c, side)],
128
- })) as bigint;
129
- if (allowance < need) {
130
- const h = await approveToken(c, side, token);
131
- await c.public.waitForTransactionReceipt({ hash: h });
132
- sent.push(h);
133
- }
134
-
135
- if (usesPermit2(c)) {
136
- const [amt, expiration] = (await c.public.readContract({
137
- address: c.addresses.permit2!,
138
- abi: PERMIT2,
139
- functionName: "allowance",
140
- args: [address, token, routerOf(c, side)],
141
- })) as readonly [bigint, number, number];
142
- if (!permit2Covers(amt, expiration, need)) {
143
- const h = await approvePermit2ForRouter(c, side, token);
144
- if (h) {
145
- await c.public.waitForTransactionReceipt({ hash: h });
146
- sent.push(h);
147
- }
148
- }
149
- }
150
- return sent;
151
- }
152
-
153
- /** What still stands between this wallet and a working write. */
154
- export interface TokenReadiness {
155
- usesPermit2: boolean;
156
- /** ERC-20 allowance to whoever pulls (Permit2 when wired, else the side's router). */
157
- needsTokenApproval: boolean;
158
- /** Permit2's standing allowance for the side's router. Always false when Permit2 is not wired. */
159
- needsPermit2Approval: boolean;
160
- }
161
-
162
- export async function checkTokenReadiness(
163
- c: LogswapClient,
164
- side: Side,
165
- token: Address,
166
- owner: Address,
167
- need: bigint,
168
- ): Promise<TokenReadiness> {
169
- const allowance = (await c.public.readContract({
170
- address: token,
171
- abi: ERC20,
172
- functionName: "allowance",
173
- args: [owner, spenderFor(c, side)],
174
- })) as bigint;
175
-
176
- let needsPermit2Approval = false;
177
- if (usesPermit2(c)) {
178
- const [amt, expiration] = (await c.public.readContract({
179
- address: c.addresses.permit2!,
180
- abi: PERMIT2,
181
- functionName: "allowance",
182
- args: [owner, token, routerOf(c, side)],
183
- })) as readonly [bigint, number, number];
184
- needsPermit2Approval = !permit2Covers(amt, expiration, need);
185
- }
186
-
187
- return { usesPermit2: usesPermit2(c), needsTokenApproval: allowance < need, needsPermit2Approval };
188
- }
189
-
190
- // ─── the signature path: allowances that ride inside the action's own transaction ─────────────
191
- //
192
- // The rationalized flow. A token's ONLY unavoidable transaction is its one-time ERC-20 approval
193
- // to Permit2; the Permit2→router allowance is set by an EIP-712 SIGNATURE (free, instant, no
194
- // gas) submitted as an `applyPermit2` call INSIDE the same multicall as the action. Minting on a
195
- // fresh token is then one transaction, not three — the pattern the router's `applyPermit2` +
196
- // `multicall` were built for, and the one canonical Permit2 UX uses.
197
-
198
- const PERMIT2_TYPES = {
199
- PermitDetails: [
200
- { name: "token", type: "address" },
201
- { name: "amount", type: "uint160" },
202
- { name: "expiration", type: "uint48" },
203
- { name: "nonce", type: "uint48" },
204
- ],
205
- PermitSingle: [
206
- { name: "details", type: "PermitDetails" },
207
- { name: "spender", type: "address" },
208
- { name: "sigDeadline", type: "uint256" },
209
- ],
210
- } as const;
211
-
212
- /**
213
- * For each token whose Permit2→router allowance is missing: read the nonce, have the wallet SIGN
214
- * a PermitSingle (a signature prompt, not a transaction), and return the encoded `applyPermit2`
215
- * calls ready to ride in a router multicall. Tokens already allowed produce nothing.
216
- */
217
- export async function permit2SigCalls(c: LogswapClient, side: Side, tokens: Address[]): Promise<Hex[]> {
218
- if (!usesPermit2(c)) return [];
219
- const { account, address: owner } = requireWallet(c);
220
- const chainId = await c.public.getChainId();
221
- const calls: Hex[] = [];
222
- for (const token of [...new Set(tokens.map((t) => t.toLowerCase() as Address))]) {
223
- const [amt, expiration, nonce] = (await c.public.readContract({
224
- address: c.addresses.permit2!,
225
- abi: PERMIT2,
226
- functionName: "allowance",
227
- args: [owner, token, routerOf(c, side)],
228
- })) as readonly [bigint, number, number];
229
- if (permit2Covers(amt, expiration, 1n << 120n)) continue; // effectively unlimited, and not lapsing
230
- const message = {
231
- details: { token, amount: MAX_UINT160, expiration: Number(permit2Expiry()), nonce },
232
- spender: routerOf(c, side),
233
- sigDeadline: BigInt(Math.floor(Date.now() / 1000)) + 1800n,
234
- };
235
- const signature = await c.wallet!.signTypedData({
236
- account,
237
- domain: { name: "Permit2", chainId, verifyingContract: c.addresses.permit2! },
238
- types: PERMIT2_TYPES,
239
- primaryType: "PermitSingle",
240
- message,
241
- } as never);
242
- const { encodeFunctionData } = await import("viem");
243
- const { routerCAbi, routerFAbi } = await import("./generated.js");
244
- calls.push(
245
- encodeFunctionData({
246
- abi: side === "F" ? routerFAbi : routerCAbi,
247
- functionName: "applyPermit2",
248
- args: [owner, message, signature],
249
- } as never),
250
- );
251
- }
252
- return calls;
253
- }