@logswap/sdk 0.5.90 → 0.5.92

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 (59) hide show
  1. package/README.md +136 -31
  2. package/dist/actions.js +4 -3
  3. package/dist/admin.d.ts +96 -925
  4. package/dist/admin.js +113 -194
  5. package/dist/chains.d.ts +13 -4
  6. package/dist/chains.js +22 -9
  7. package/dist/claims.d.ts +1 -1
  8. package/dist/claims.js +11 -9
  9. package/dist/client.d.ts +42 -20
  10. package/dist/client.js +86 -26
  11. package/dist/fees.d.ts +1 -1
  12. package/dist/fees.js +5 -4
  13. package/dist/fpool.d.ts +327 -1120
  14. package/dist/fpool.js +408 -475
  15. package/dist/generated.d.ts +5017 -7631
  16. package/dist/generated.js +3040 -4879
  17. package/dist/history.js +12 -14
  18. package/dist/index.d.ts +5 -7
  19. package/dist/index.js +5 -7
  20. package/dist/indexer.d.ts +2 -2
  21. package/dist/indexer.js +12 -9
  22. package/dist/keys.d.ts +15 -3
  23. package/dist/keys.js +5 -1
  24. package/dist/liquidity.d.ts +1 -1
  25. package/dist/liquidity.js +16 -9
  26. package/dist/onboard.d.ts +16 -11
  27. package/dist/onboard.js +27 -21
  28. package/dist/pools.d.ts +2 -0
  29. package/dist/pools.js +21 -13
  30. package/dist/positions.d.ts +1 -1
  31. package/dist/positions.js +18 -17
  32. package/dist/quote.d.ts +15 -0
  33. package/dist/quote.js +36 -0
  34. package/dist/swap.d.ts +7 -9
  35. package/dist/swap.js +40 -25
  36. package/dist/views.d.ts +1 -1
  37. package/dist/write.d.ts +7 -2
  38. package/dist/write.js +10 -7
  39. package/package.json +3 -3
  40. package/src/actions.ts +4 -3
  41. package/src/admin.ts +143 -249
  42. package/src/chains.ts +32 -13
  43. package/src/claims.ts +11 -10
  44. package/src/client.ts +111 -39
  45. package/src/fees.ts +6 -6
  46. package/src/fpool.ts +555 -648
  47. package/src/generated.ts +3063 -4904
  48. package/src/history.ts +11 -11
  49. package/src/index.ts +5 -7
  50. package/src/indexer.ts +13 -11
  51. package/src/keys.ts +19 -3
  52. package/src/liquidity.ts +16 -10
  53. package/src/onboard.ts +29 -21
  54. package/src/pools.ts +23 -15
  55. package/src/positions.ts +18 -18
  56. package/src/quote.ts +36 -0
  57. package/src/swap.ts +46 -25
  58. package/src/views.ts +1 -2
  59. package/src/write.ts +11 -10
package/src/positions.ts CHANGED
@@ -8,8 +8,8 @@
8
8
 
9
9
  import { encodeAbiParameters, keccak256, parseAbiItem } from "viem";
10
10
  import type { Address, Hex } from "viem";
11
- import { lensAbi, cPoolManagerAbi } from "./generated.js";
12
- import type { LogswapClient } from "./client.js";
11
+ import { lensCAbi, managerCAbi } from "./generated.js";
12
+ import { managerC, lensC, type LogswapClient } from "./client.js";
13
13
  import { MINT_FEE_MAX, poolId, type PoolKey } from "./keys.js";
14
14
 
15
15
  /** `CPoolState.POSITIONS_SLOT`: the manager's `positions` mapping (`forge inspect CPoolManager storageLayout`). */
@@ -40,8 +40,8 @@ export interface PositionClass {
40
40
  export async function getPositionClass(c: LogswapClient, id: bigint): Promise<PositionClass> {
41
41
  const slot = keccak256(encodeAbiParameters([{ type: "uint256" }, { type: "uint256" }], [id, C_POSITIONS_SLOT]));
42
42
  const w = (await c.public.readContract({
43
- address: c.addresses.cPoolManager,
44
- abi: cPoolManagerAbi,
43
+ address: managerC(c),
44
+ abi: managerCAbi,
45
45
  functionName: "extsload",
46
46
  args: [slot, 2n],
47
47
  } as never)) as readonly Hex[];
@@ -53,8 +53,8 @@ export async function getPositionClass(c: LogswapClient, id: bigint): Promise<Po
53
53
  /** A holder's share balance in a class. */
54
54
  export async function balanceOf(c: LogswapClient, owner: Address, id: bigint): Promise<bigint> {
55
55
  return c.public.readContract({
56
- address: c.addresses.cPoolManager,
57
- abi: cPoolManagerAbi,
56
+ address: managerC(c),
57
+ abi: managerCAbi,
58
58
  functionName: "balanceOf",
59
59
  args: [owner, id],
60
60
  }) as Promise<bigint>;
@@ -68,8 +68,8 @@ export async function balanceOf(c: LogswapClient, owner: Address, id: bigint): P
68
68
  */
69
69
  export async function feesOf(c: LogswapClient, id: bigint, owner: Address): Promise<bigint> {
70
70
  return c.public.readContract({
71
- address: c.addresses.lens,
72
- abi: lensAbi,
71
+ address: lensC(c),
72
+ abi: lensCAbi,
73
73
  functionName: "feesOfById",
74
74
  args: [id, owner],
75
75
  }) as Promise<bigint>;
@@ -142,8 +142,8 @@ export async function describeId(
142
142
  cap?: bigint;
143
143
  }> {
144
144
  const r = (await c.public.readContract({
145
- address: c.addresses.lens,
146
- abi: lensAbi,
145
+ address: lensC(c),
146
+ abi: lensCAbi,
147
147
  functionName: "unpack",
148
148
  args: [id],
149
149
  })) as readonly [`0x${string}`, bigint, bigint, boolean];
@@ -253,8 +253,8 @@ export interface HeldPositionRow {
253
253
 
254
254
  export async function positionsOf(c: LogswapClient, key: PoolKey, owner: Address): Promise<HeldPositionRow[]> {
255
255
  const rows = (await c.public.readContract({
256
- address: c.addresses.lens,
257
- abi: lensAbi,
256
+ address: lensC(c),
257
+ abi: lensCAbi,
258
258
  functionName: "positionsOf",
259
259
  args: [poolId(key), owner],
260
260
  } as never)) as readonly { id: bigint; floor: bigint; shares: bigint; L: bigint; fees: bigint }[];
@@ -266,7 +266,7 @@ const ID_REGISTERED = parseAbiItem("event ClassRegistered(uint256 indexed id, by
266
266
  /** The class ids `owner` holds in `pool`, from the indexer's `pool_holder` view; null when the indexer cannot answer. */
267
267
  async function heldIdsFromView(c: LogswapClient, indexer: string, pool: Hex, owner: Address): Promise<bigint[] | null> {
268
268
  try {
269
- const rows = await poolHolders(indexer, { condition: { side: "C", poolId: pool.toLowerCase(), holder: owner.toLowerCase(), manager: c.addresses.cPoolManager.toLowerCase() } });
269
+ const rows = await poolHolders(indexer, { condition: { side: "C", poolId: pool.toLowerCase(), holder: owner.toLowerCase(), manager: managerC(c).toLowerCase() } });
270
270
  return rows.filter((r) => r.balance > 0n && !r.isBackstop).map((r) => r.id);
271
271
  } catch {
272
272
  return null; // the view is a cache: the ClassRegistered replay below answers the same
@@ -275,7 +275,7 @@ async function heldIdsFromView(c: LogswapClient, indexer: string, pool: Hex, own
275
275
 
276
276
  /** Every class id ever registered in `pool` (`ClassRegistered`), from the indexer's table or the node. */
277
277
  async function registeredIds(c: LogswapClient, pool: Hex, fromBlock?: bigint, indexer?: string): Promise<bigint[]> {
278
- const { logs } = await getEventLogs<{ id?: bigint }>(c, { indexer, address: c.addresses.cPoolManager, event: ID_REGISTERED, args: { poolId: pool }, fromBlock });
278
+ const { logs } = await getEventLogs<{ id?: bigint }>(c, { indexer, address: managerC(c), event: ID_REGISTERED, args: { poolId: pool }, fromBlock });
279
279
  return logs.map((l) => l.args.id ?? 0n).filter((id) => id !== 0n);
280
280
  }
281
281
 
@@ -296,8 +296,8 @@ export async function holdingsOf(c: LogswapClient, key: PoolKey, owner: Address,
296
296
  if (ids.length === 0) return [];
297
297
  // one lens call: balance, class L, fees, floor and cap for every id the owner holds
298
298
  const rows = (await c.public.readContract({
299
- address: c.addresses.lens,
300
- abi: lensAbi,
299
+ address: lensC(c),
300
+ abi: lensCAbi,
301
301
  functionName: "heldByIds",
302
302
  args: [poolId(key), owner, ids],
303
303
  } as never)) as readonly { id: bigint; floor: bigint; cap: bigint; capped: boolean; shares: bigint; L: bigint; fees: bigint }[];
@@ -308,8 +308,8 @@ export async function holdingsOf(c: LogswapClient, key: PoolKey, owner: Address,
308
308
  * value = base·p + quote + fees in quote units — L + quote + fees for an uncapped active class. */
309
309
  export async function legsOf(c: LogswapClient, key: PoolKey, id: bigint, holder: Address): Promise<{ base: bigint; quote: bigint; fees: bigint; value: bigint }> {
310
310
  const [base, quote, fees, value] = (await c.public.readContract({
311
- address: c.addresses.lens,
312
- abi: lensAbi,
311
+ address: lensC(c),
312
+ abi: lensCAbi,
313
313
  functionName: "legsOf",
314
314
  args: [poolId(key), id, holder],
315
315
  } as never)) as readonly [bigint, bigint, bigint, bigint];
package/src/quote.ts ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * quote.ts — reading a revert-quoter's answer.
3
+ *
4
+ * Both managers' quoters are the router's (`Router.quoteSwap`, F: decision 049, C: decision 092): the real swap runs
5
+ * in the manager's lock and the contract reverts with `QuoteResult(uint256)`. The value is therefore decoded out of the
6
+ * revert, not returned. There is no second pricing path to drift from the first.
7
+ */
8
+
9
+ import { toFunctionSelector } from "viem";
10
+
11
+ /**
12
+ * `QuoteResult(uint256)` — selector then one word. viem nests the revert data several layers deep
13
+ * depending on the transport, so walk for it rather than guessing the shape.
14
+ *
15
+ * The selector is DERIVED, not written down. A hardcoded one is wrong silently: the decode simply
16
+ * never matches and every quote rethrows as if the swap had failed.
17
+ */
18
+ export function findQuoteResult(err: unknown): bigint | null {
19
+ const SELECTOR = toFunctionSelector("QuoteResult(uint256)");
20
+ const seen = new Set<unknown>();
21
+ const walk = (e: unknown): bigint | null => {
22
+ if (!e || typeof e !== "object" || seen.has(e)) return null;
23
+ seen.add(e);
24
+ const anyE = e as Record<string, unknown>;
25
+ const data = anyE.data;
26
+ if (typeof data === "string" && data.startsWith(SELECTOR) && data.length >= 10 + 64) {
27
+ return BigInt("0x" + data.slice(10, 10 + 64));
28
+ }
29
+ for (const k of ["cause", "walk", "error", "details"]) {
30
+ const found = walk(anyE[k]);
31
+ if (found !== null) return found;
32
+ }
33
+ return null;
34
+ };
35
+ return walk(err);
36
+ }
package/src/swap.ts CHANGED
@@ -6,32 +6,55 @@
6
6
  * is deliberately not exposed here — callers say `swapExactOut`.
7
7
  */
8
8
 
9
- import type { Address, Hash } from "viem";
10
- import { lensAbi, routerAbi } from "./generated.js";
11
- import type { LogswapClient } from "./client.js";
9
+ import { zeroAddress, type Address, type Hash } from "viem";
10
+ import { routerCAbi } from "./generated.js";
11
+ import { routerC, type LogswapClient } from "./client.js";
12
12
  import { poolId as keyId, type PoolKey } from "./keys.js";
13
+ import { findQuoteResult } from "./quote.js";
13
14
  import { resolveDeadline, resolveRecipient, sendRouterWrite, type WriteOptions , sendRouterWriteWithPermits } from "./write.js";
14
15
 
15
16
  /**
16
- * Quote an exact-in swap. Exact, not approximate — the lens is a revert-quoter that runs the real
17
- * swap and reverts with the result, so what it returns is what execution returns, bit for bit
18
- * (including the dynamic fee, even across a block boundary with the variance fold pending).
19
- *
20
- * `simulate`, not a plain read: the function is non-view by construction.
17
+ * The C quoter (C 14, decision 092): `Router.quoteSwap` runs the real swap in the manager's lock for `taker` and reverts
18
+ * with `QuoteResult`, so the answer is decoded out of the revert. `baseSide` says whether the SPECIFIED token is the base.
19
+ * The taker matters only on a gated market (the router must then be the taker's ERC-6909 operator, as for the swap):
20
+ * the connected account, or the zero address.
21
+ */
22
+ async function quoteSwapC(
23
+ c: LogswapClient,
24
+ key: PoolKey,
25
+ exactIn: boolean,
26
+ baseSide: boolean,
27
+ amount: bigint,
28
+ taker?: Address,
29
+ ): Promise<bigint> {
30
+ try {
31
+ await c.public.simulateContract({
32
+ address: routerC(c),
33
+ abi: routerCAbi,
34
+ functionName: "quoteSwap",
35
+ args: [keyId(key), exactIn, baseSide, amount, taker ?? c.wallet?.account?.address ?? zeroAddress],
36
+ } as never);
37
+ } catch (err) {
38
+ const hit = findQuoteResult(err);
39
+ if (hit !== null) return hit;
40
+ throw err; // a real revert — no liquidity, a gate, a bad amount — belongs to the caller
41
+ }
42
+ throw new Error("logswap: quoteSwap returned without reverting, which it must never do");
43
+ }
44
+
45
+ /**
46
+ * Quote an exact-in swap. Exact, not approximate — the router runs the real swap and reverts with the result, so
47
+ * what it returns is what execution returns, bit for bit (including the dynamic fee, even across a block boundary
48
+ * with the variance fold pending).
21
49
  */
22
50
  export async function quoteExactIn(
23
51
  c: LogswapClient,
24
52
  key: PoolKey,
25
53
  baseIn: boolean,
26
54
  amountIn: bigint,
55
+ taker?: Address,
27
56
  ): Promise<bigint> {
28
- const { result } = await c.public.simulateContract({
29
- address: c.addresses.lens,
30
- abi: lensAbi,
31
- functionName: "previewSwapExactIn",
32
- args: [keyId(key), baseIn, amountIn],
33
- });
34
- return result as bigint;
57
+ return quoteSwapC(c, key, true, baseIn, amountIn, taker);
35
58
  }
36
59
 
37
60
  /** Quote an exact-out swap: how much input it will cost. Exact, same mechanism as above. */
@@ -40,14 +63,9 @@ export async function quoteExactOut(
40
63
  key: PoolKey,
41
64
  baseOut: boolean,
42
65
  amountOut: bigint,
66
+ taker?: Address,
43
67
  ): Promise<bigint> {
44
- const { result } = await c.public.simulateContract({
45
- address: c.addresses.lens,
46
- abi: lensAbi,
47
- functionName: "previewSwapExactOut",
48
- args: [keyId(key), baseOut, amountOut],
49
- });
50
- return result as bigint;
68
+ return quoteSwapC(c, key, false, baseOut, amountOut, taker);
51
69
  }
52
70
 
53
71
  export interface SwapExactInArgs extends WriteOptions {
@@ -61,7 +79,8 @@ export interface SwapExactInArgs extends WriteOptions {
61
79
 
62
80
  export async function swapExactIn(c: LogswapClient, a: SwapExactInArgs): Promise<Hash> {
63
81
  return sendRouterWriteWithPermits(c, {
64
- abi: routerAbi,
82
+ side: "C",
83
+ abi: routerCAbi,
65
84
  functionName: "swapExactIn",
66
85
  args: [
67
86
  keyId(a.key),
@@ -87,7 +106,8 @@ export interface SwapExactOutArgs extends WriteOptions {
87
106
 
88
107
  export async function swapExactOut(c: LogswapClient, a: SwapExactOutArgs): Promise<Hash> {
89
108
  return sendRouterWriteWithPermits(c, {
90
- abi: routerAbi,
109
+ side: "C",
110
+ abi: routerCAbi,
91
111
  functionName: "swapExactOut",
92
112
  args: [
93
113
  keyId(a.key),
@@ -125,7 +145,8 @@ export interface SwapPathArgs extends WriteOptions {
125
145
  export async function swapExactInPath(c: LogswapClient, a: SwapPathArgs): Promise<Hash> {
126
146
  if (a.hops.length === 0) throw new Error("logswap: empty path");
127
147
  return sendRouterWrite(c, {
128
- abi: routerAbi,
148
+ side: "C",
149
+ abi: routerCAbi,
129
150
  functionName: "swapExactInPath",
130
151
  args: [
131
152
  a.hops.map((h) => ({ poolId: keyId(h.key), baseIn: h.baseIn })),
package/src/views.ts CHANGED
@@ -17,6 +17,7 @@
17
17
  * keep their node path as the fallback.
18
18
  */
19
19
  import { getAddress, type Address, type Hex } from "viem";
20
+ import type { Side } from "./client.js";
20
21
  import { gql } from "./indexer.js";
21
22
 
22
23
  const PAGE = 1000; // postgraphile's cap
@@ -92,8 +93,6 @@ const secsOr = (v: unknown): number | null => (v === null || v === undefined ? n
92
93
  const hex = (v: unknown): Hex => String(v).toLowerCase() as Hex;
93
94
  const addrs = (v: unknown): Address[] => ((v as unknown[] | null) ?? []).map(addr);
94
95
 
95
- export type Side = "C" | "F";
96
-
97
96
  // ─── pool_directory ──────────────────────────────────────────────────────────
98
97
 
99
98
  /** A pool of either manager: C markets are kind `market`; F pools carry their policy's registry name (`public`, `private`, `pol`, `pad`). */
package/src/write.ts CHANGED
@@ -14,8 +14,8 @@
14
14
  */
15
15
 
16
16
  import type { Address, Hash, Hex } from "viem";
17
- import type { LogswapClient } from "./client.js";
18
- import { cPoolManagerAbi, fPoolManagerAbi, padPolicyAbi } from "./generated.js";
17
+ import { routerOf, type LogswapClient, type Side } from "./client.js";
18
+ import { managerCAbi, managerFAbi, policyFAbi } from "./generated.js";
19
19
 
20
20
  /** Disables the deadline check. Deliberate opt-out, never a default. */
21
21
  export const NEVER = (1n << 256n) - 1n;
@@ -110,7 +110,7 @@ const errorKey = (e: AbiError) => `${e.name}(${e.inputs.map((i) => i.type).join(
110
110
  export const routerRevertErrors: readonly AbiError[] = (() => {
111
111
  const seen = new Set<string>();
112
112
  const out: AbiError[] = [];
113
- for (const x of [...cPoolManagerAbi, ...fPoolManagerAbi, ...padPolicyAbi, ...permit2Errors] as readonly { type: string }[]) {
113
+ for (const x of [...managerCAbi, ...managerFAbi, ...policyFAbi, ...permit2Errors] as readonly { type: string }[]) {
114
114
  if (x.type !== "error") continue;
115
115
  const e = x as AbiError;
116
116
  if (!seen.has(errorKey(e))) {
@@ -157,11 +157,12 @@ export function revertName(e: unknown): string | undefined {
157
157
  */
158
158
  export async function sendRouterWrite(
159
159
  c: LogswapClient,
160
- args: { abi: readonly unknown[]; functionName: string; args: readonly unknown[]; bufferPct?: bigint },
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 },
161
162
  ): Promise<Hash> {
162
163
  const { account } = requireWallet(c);
163
164
  const params = {
164
- address: c.addresses.router,
165
+ address: routerOf(c, args.side),
165
166
  abi: withRevertErrors(args.abi),
166
167
  functionName: args.functionName,
167
168
  args: args.args,
@@ -202,11 +203,11 @@ export function fallBackUnlessRejected(e: unknown): Hex[] {
202
203
  /** Simulate a router write and return its decoded result, without sending. */
203
204
  export async function simulateRouterWrite<T>(
204
205
  c: LogswapClient,
205
- args: { abi: readonly unknown[]; functionName: string; args: readonly unknown[]; account?: Address },
206
+ args: { side: Side; abi: readonly unknown[]; functionName: string; args: readonly unknown[]; account?: Address },
206
207
  ): Promise<T> {
207
208
  const account = args.account ?? c.wallet?.account?.address;
208
209
  const { result } = await c.public.simulateContract({
209
- address: c.addresses.router,
210
+ address: routerOf(c, args.side),
210
211
  abi: withRevertErrors(args.abi),
211
212
  functionName: args.functionName,
212
213
  args: args.args,
@@ -223,13 +224,13 @@ export async function simulateRouterWrite<T>(
223
224
  */
224
225
  export async function sendRouterWriteWithPermits(
225
226
  c: LogswapClient,
226
- args: { abi: readonly unknown[]; functionName: string; args: readonly unknown[]; bufferPct?: bigint },
227
+ args: { side: Side; abi: readonly unknown[]; functionName: string; args: readonly unknown[]; bufferPct?: bigint },
227
228
  pullTokens: Address[],
228
229
  ): Promise<Hash> {
229
230
  const { permit2SigCalls } = await import("./onboard.js");
230
- const sigCalls = await permit2SigCalls(c, pullTokens).catch(fallBackUnlessRejected);
231
+ const sigCalls = await permit2SigCalls(c, args.side, pullTokens).catch(fallBackUnlessRejected);
231
232
  if (sigCalls.length === 0) return sendRouterWrite(c, args);
232
233
  const { encodeFunctionData } = await import("viem");
233
234
  const inner = encodeFunctionData({ abi: args.abi, functionName: args.functionName, args: args.args } as never);
234
- return sendRouterWrite(c, { abi: args.abi, functionName: "multicall", args: [[...sigCalls, inner]], bufferPct: args.bufferPct });
235
+ return sendRouterWrite(c, { side: args.side, abi: args.abi, functionName: "multicall", args: [[...sigCalls, inner]], bufferPct: args.bufferPct });
235
236
  }