@logswap/sdk 0.5.102 → 0.5.103

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/write.ts DELETED
@@ -1,236 +0,0 @@
1
- /**
2
- * write.ts — shared plumbing for every mutating call.
3
- *
4
- * Two conventions every write helper inherits, so no caller has to remember them:
5
- *
6
- * **Deadlines are mandatory on-chain and defaulted here.** Every mutating router entry carries
7
- * `checkDeadline`. Passing `type(uint256).max` disables it, which is what most SDKs do implicitly
8
- * and is a real hazard for a user whose transaction sits in the mempool through a price move. The
9
- * default is a bounded window; pass `deadline: NEVER` only deliberately.
10
- *
11
- * **Slippage is expressed as a bound, never as a percentage of a stale quote.** Helpers take the
12
- * raw `minOut`/`maxIn` the contract wants. `withSlippage()` derives one from a quote when that is
13
- * what you have — but the bound is what ships.
14
- */
15
-
16
- import type { Address, Hash, Hex } from "viem";
17
- import { routerOf, type LogswapClient, type Side } from "./client.js";
18
- import { managerCAbi, managerFAbi, policyFAbi } from "./generated.js";
19
-
20
- /** Disables the deadline check. Deliberate opt-out, never a default. */
21
- export const NEVER = (1n << 256n) - 1n;
22
-
23
- /** Default deadline window: 20 minutes, the same order as every major DEX front end. */
24
- export const DEFAULT_DEADLINE_SECONDS = 20n * 60n;
25
-
26
- export interface WriteOptions {
27
- /** Absolute unix seconds. Defaults to now + {@link DEFAULT_DEADLINE_SECONDS}. */
28
- deadline?: bigint;
29
- /** Who receives the output. Defaults to the wallet account. */
30
- recipient?: Address;
31
- }
32
-
33
- export function resolveDeadline(o: WriteOptions | undefined, now = Date.now()): bigint {
34
- return o?.deadline ?? BigInt(Math.floor(now / 1000)) + DEFAULT_DEADLINE_SECONDS;
35
- }
36
-
37
- /**
38
- * The wallet's account, as the ACCOUNT OBJECT rather than its address.
39
- *
40
- * This distinction decides how the transaction is signed, and getting it wrong fails in only half
41
- * the cases — which is how it survived a long time here. viem treats a bare `0x…` address as a
42
- * *JSON-RPC account* and calls `eth_sendTransaction`, asking the NODE to sign. That is right for an
43
- * injected wallet and wrong for a local account, which must sign in-process and send
44
- * `eth_sendRawTransaction`.
45
- *
46
- * A dev node holds the keys for its own accounts, so passing an address appears to work right up
47
- * until someone uses a key the node does not have — a bot, a script, CI, or a real user's wallet.
48
- * Passing the object lets viem pick the correct path for both kinds.
49
- */
50
- /**
51
- * The wallet's account OBJECT for a write the caller named by address — see {@link requireWallet}
52
- * for why the object and never the address. The F helpers take `account: Address` in their
53
- * arguments; passed straight to viem it became a JSON-RPC account and the NODE was asked to sign,
54
- * which works only on a dev node holding the key (every agent on the devnet did exactly that).
55
- * A named account that is not the wallet's is refused: nothing else could sign for it.
56
- */
57
- export function signerFor(c: LogswapClient, address: Address): NonNullable<LogswapClient["wallet"]>["account"] {
58
- const { account } = requireWallet(c);
59
- if (account!.address.toLowerCase() !== address.toLowerCase()) {
60
- throw new Error(`logswap: the write names ${address} but the wallet signs as ${account!.address}`);
61
- }
62
- return account;
63
- }
64
-
65
- export function requireWallet(c: LogswapClient): { account: NonNullable<LogswapClient["wallet"]>["account"]; address: Address } {
66
- const account = c.wallet?.account;
67
- if (!c.wallet || !account) {
68
- throw new Error("logswap: this call needs a wallet client — createLogswapClient({ wallet, … })");
69
- }
70
- return { account, address: account.address };
71
- }
72
-
73
- export function resolveRecipient(c: LogswapClient, o?: WriteOptions): Address {
74
- return o?.recipient ?? requireWallet(c).address;
75
- }
76
-
77
- /**
78
- * Turn a quoted amount into a slippage bound.
79
- *
80
- * `bps` is basis points of tolerance. Direction matters: an output bound floors, an input bound
81
- * ceilings. Getting that backwards produces a bound that can never bind, which is worse than no
82
- * bound at all because it looks protective.
83
- */
84
- export function withSlippage(quoted: bigint, bps: number, side: "output" | "input"): bigint {
85
- if (bps < 0 || bps > 10_000) throw new Error(`logswap: slippage bps out of range: ${bps}`);
86
- const b = BigInt(Math.round(bps));
87
- // an input bound rounds UP: flooring it would put a wei-exact quote at bps = 0 one wei short
88
- return side === "output" ? (quoted * (10_000n - b)) / 10_000n : (quoted * (10_000n + b) + 9_999n) / 10_000n;
89
- }
90
-
91
- type AbiError = { type: "error"; name: string; inputs: readonly { type: string }[] };
92
-
93
- /** Permit2's pull errors — Uniswap's and the devnet mock's — which a router write can revert with. */
94
- const permit2Errors: readonly AbiError[] = [
95
- { type: "error", name: "AllowanceExpired", inputs: [{ type: "uint256" }] },
96
- { type: "error", name: "InsufficientAllowance", inputs: [{ type: "uint256" }] },
97
- { type: "error", name: "AllowanceExpired", inputs: [] },
98
- { type: "error", name: "NotAllowed", inputs: [] },
99
- ];
100
-
101
- const errorKey = (e: AbiError) => `${e.name}(${e.inputs.map((i) => i.type).join(",")})`;
102
-
103
- /**
104
- * Every custom error a router call can revert with that the router's own ABI does not declare: the
105
- * C and F managers' (a mint, a swap or a settlement bubbles them up unchanged), the pad policy's
106
- * (a launch's `NoThreshold`, `PadExists`, `OutOfRange`; a graduation's `NotGraduated`) and Permit2's.
107
- * Without them viem can name only the router's errors, and anything raised in the manager comes
108
- * back as a bare selector.
109
- */
110
- export const routerRevertErrors: readonly AbiError[] = (() => {
111
- const seen = new Set<string>();
112
- const out: AbiError[] = [];
113
- for (const x of [...managerCAbi, ...managerFAbi, ...policyFAbi, ...permit2Errors] as readonly { type: string }[]) {
114
- if (x.type !== "error") continue;
115
- const e = x as AbiError;
116
- if (!seen.has(errorKey(e))) {
117
- seen.add(errorKey(e));
118
- out.push(e);
119
- }
120
- }
121
- return out;
122
- })();
123
-
124
- /** `abi` plus every error in {@link routerRevertErrors} it does not already declare. */
125
- export function withRevertErrors(abi: readonly unknown[]): readonly unknown[] {
126
- const have = new Set(
127
- (abi as readonly { type: string }[]).filter((x) => x.type === "error").map((x) => errorKey(x as AbiError)),
128
- );
129
- return [...abi, ...routerRevertErrors.filter((e) => !have.has(errorKey(e)))];
130
- }
131
-
132
- /**
133
- * The decoded revert of a contract call, e.g. `SlippageIn()` or `AllowanceExpired(1700000000)`,
134
- * found however deep viem nested it; the raw selector when the error is not in the ABI; undefined
135
- * when the failure was not a revert. viem's `message` puts the name on its second line, so a UI
136
- * that shows only the first line ("The contract function … reverted.") loses it.
137
- */
138
- export function revertName(e: unknown): string | undefined {
139
- const seen = new Set<unknown>();
140
- for (let x: unknown = e; x && typeof x === "object" && !seen.has(x); x = (x as { cause?: unknown }).cause) {
141
- seen.add(x);
142
- const o = x as { name?: string; data?: { errorName?: string; args?: readonly unknown[] }; signature?: string };
143
- if (o.name !== "ContractFunctionRevertedError") continue;
144
- if (o.data?.errorName) return `${o.data.errorName}(${(o.data.args ?? []).map(String).join(", ")})`;
145
- if (o.signature) return o.signature;
146
- }
147
- return undefined;
148
- }
149
-
150
- /**
151
- * Send a router write with a gas buffer over an explicit estimate.
152
- *
153
- * A swap's gas is path-dependent: how many ticks it crosses depends on the price when it LANDS, not
154
- * when it was estimated, and the variance kernel does more work when a block boundary has passed
155
- * since the last touch. An exact `eth_estimateGas` can therefore OutOfGas one block later. Every
156
- * write goes through here.
157
- */
158
- export async function sendRouterWrite(
159
- c: LogswapClient,
160
- /** `side` picks the router (decision 096: one per manager, each its own Permit2 spender). */
161
- args: { side: Side; abi: readonly unknown[]; functionName: string; args: readonly unknown[]; bufferPct?: bigint },
162
- ): Promise<Hash> {
163
- const { account } = requireWallet(c);
164
- const params = {
165
- address: routerOf(c, args.side),
166
- abi: withRevertErrors(args.abi),
167
- functionName: args.functionName,
168
- args: args.args,
169
- account,
170
- chain: c.wallet!.chain,
171
- } as never;
172
-
173
- const estimate = await c.public.estimateContractGas(params);
174
- const gas = (estimate * (100n + (args.bufferPct ?? 30n))) / 100n;
175
- return c.wallet!.writeContract({ ...(params as object), gas } as never);
176
- }
177
-
178
- /**
179
- * Whether an error is the user saying no in their wallet (EIP-1193 code 4001, viem's
180
- * `UserRejectedRequestError`), however deep viem nested it.
181
- */
182
- export function isUserRejection(e: unknown): boolean {
183
- const seen = new Set<unknown>();
184
- for (let x: unknown = e; x && typeof x === "object" && !seen.has(x); x = (x as { cause?: unknown }).cause) {
185
- seen.add(x);
186
- const o = x as { name?: string; code?: number };
187
- if (o.name === "UserRejectedRequestError" || o.code === 4001) return true;
188
- }
189
- return false;
190
- }
191
-
192
- /**
193
- * The permit signatures are an optimisation: when they cannot be produced (a wallet without
194
- * `signTypedData`, a read that failed) the write goes out plain and relies on a standing
195
- * allowance. A REJECTED signature is not that — it was swallowed too, and the user who had just
196
- * said no was shown a transaction prompt, which then reverted with a Permit2 error that hid why.
197
- */
198
- export function fallBackUnlessRejected(e: unknown): Hex[] {
199
- if (isUserRejection(e)) throw e;
200
- return [];
201
- }
202
-
203
- /** Simulate a router write and return its decoded result, without sending. */
204
- export async function simulateRouterWrite<T>(
205
- c: LogswapClient,
206
- args: { side: Side; abi: readonly unknown[]; functionName: string; args: readonly unknown[]; account?: Address },
207
- ): Promise<T> {
208
- const account = args.account ?? c.wallet?.account?.address;
209
- const { result } = await c.public.simulateContract({
210
- address: routerOf(c, args.side),
211
- abi: withRevertErrors(args.abi),
212
- functionName: args.functionName,
213
- args: args.args,
214
- account,
215
- } as never);
216
- return result as T;
217
- }
218
-
219
- /**
220
- * A router write that carries its own Permit2 permissions: missing allowances for `pullTokens`
221
- * are SIGNED (no gas, no extra transaction) and submitted as `applyPermit2` calls in one
222
- * multicall with the action. The 3-transaction mint becomes 1. Falls through to a plain write
223
- * when nothing is missing or Permit2 is not wired.
224
- */
225
- export async function sendRouterWriteWithPermits(
226
- c: LogswapClient,
227
- args: { side: Side; abi: readonly unknown[]; functionName: string; args: readonly unknown[]; bufferPct?: bigint },
228
- pullTokens: Address[],
229
- ): Promise<Hash> {
230
- const { permit2SigCalls } = await import("./onboard.js");
231
- const sigCalls = await permit2SigCalls(c, args.side, pullTokens).catch(fallBackUnlessRejected);
232
- if (sigCalls.length === 0) return sendRouterWrite(c, args);
233
- const { encodeFunctionData } = await import("viem");
234
- const inner = encodeFunctionData({ abi: args.abi, functionName: args.functionName, args: args.args } as never);
235
- return sendRouterWrite(c, { side: args.side, abi: args.abi, functionName: "multicall", args: [[...sigCalls, inner]], bufferPct: args.bufferPct });
236
- }