@logswap/sdk 0.5.89 → 0.5.91

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 +80 -13
  2. package/dist/actions.js +6 -5
  3. package/dist/admin.d.ts +213 -219
  4. package/dist/admin.js +118 -144
  5. package/dist/chains.d.ts +13 -4
  6. package/dist/chains.js +23 -9
  7. package/dist/claims.d.ts +1 -1
  8. package/dist/claims.js +11 -9
  9. package/dist/client.d.ts +43 -20
  10. package/dist/client.js +75 -26
  11. package/dist/fees.d.ts +1 -1
  12. package/dist/fees.js +5 -4
  13. package/dist/fpool.d.ts +170 -460
  14. package/dist/fpool.js +175 -218
  15. package/dist/generated.d.ts +5753 -6227
  16. package/dist/generated.js +3097 -3664
  17. package/dist/history.js +13 -11
  18. package/dist/index.d.ts +5 -5
  19. package/dist/index.js +5 -5
  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 +6 -5
  41. package/src/admin.ts +168 -171
  42. package/src/chains.ts +33 -13
  43. package/src/claims.ts +11 -10
  44. package/src/client.ts +101 -39
  45. package/src/fees.ts +6 -6
  46. package/src/fpool.ts +240 -272
  47. package/src/generated.ts +3110 -3671
  48. package/src/history.ts +11 -9
  49. package/src/index.ts +5 -5
  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/history.ts CHANGED
@@ -83,22 +83,23 @@ export async function getEventLogs<A = Record<string, unknown>>(
83
83
 
84
84
  /**
85
85
  * What the rindexer deployment calls each contract — the prefix of a table two contracts share
86
- * (logswap-be `devnet/indexer/rindexer.yaml`). The four policies are one factory-tracked contract,
87
- * `Policy`, except `Listed`, whose two signatures are two contracts (`PublicPolicy`, `PrivatePolicy`).
86
+ * (logswap-be `devnet/indexer/rindexer.yaml`). Every policy is one factory-tracked contract, `Policy`: since decision
87
+ * 098 `Listed` has one signature (the sponsored policies', F and C), so no policy event needs a prefix.
88
88
  */
89
89
  function contractName(c: LogswapClient, address: Address, event: string): string | null {
90
90
  const a = address.toLowerCase();
91
91
  const is = (x?: Address) => x !== undefined && a === x.toLowerCase();
92
92
  const { addresses: d } = c;
93
- if (is(d.cPoolManager)) return "CPoolManager";
94
- if (is(d.fPoolManager)) return "FPoolManager";
95
- if (is(d.router)) return "LogswapRouter";
93
+ // the indexer's own labels for the contracts (its `rindexer.yaml`), not the contracts' names (decision 096): ManagerC / ManagerF / RouterC / RouterF
94
+ if (is(d.managerC)) return "CPoolManager";
95
+ if (is(d.managerF)) return "FPoolManager";
96
+ if (is(d.routerF) || is(d.routerC)) return "LogswapRouter";
96
97
  if (is(d.policyRegistry)) return "PolicyRegistry";
97
98
  if (is(d.tokenFactory)) return "TokenFactory";
98
- if (is(d.publicPolicy)) return indexedContractOf("public", event);
99
- if (is(d.privatePolicy)) return indexedContractOf("private", event);
99
+ if (is(d.publicPolicy) || is(d.privatePolicy) || is(d.publicPolicyC)) return indexedContractOf("sponsor", event);
100
100
  if (is(d.polPolicy)) return indexedContractOf("pol", event);
101
101
  if (is(d.padPolicy)) return indexedContractOf("pad", event);
102
+ if (is(d.orphanPolicy)) return indexedContractOf("orphan", event);
102
103
  return null;
103
104
  }
104
105
 
@@ -106,13 +107,14 @@ function contractName(c: LogswapClient, address: Address, event: string): string
106
107
  * A policy's name in the indexer for one of its events: `Policy`. Pass it as {@link EventLogsQuery.contract} for a policy outside the artifact.
107
108
  */
108
109
  export function indexedContractOf(kind: FPoolKind, event: string): string {
109
- void kind; // `Listed` was the public and the private policy's since 064; the public policy dropped its asset list in F 33 (073), so it is unshared
110
+ void kind; // `Listed` was two contracts' (the public and the private policy) until decision 098; now every policy event is unshared
110
111
  return "Policy";
111
112
  }
112
113
 
113
114
  /** Events whose name two indexed contracts share: postgraphile prefixes their tables. */
114
115
  // `CollectorProposed` / `CollectorAccepted` are C 7 / F 26 and older; `TreasuryProposed` / `TreasuryAccepted` replace them (066).
115
- const SHARED = new Set(["PoolCreated", "Swap", "Transfer", "Approval", "OperatorSet", "CollectorProposed", "CollectorAccepted", "TreasuryProposed", "TreasuryAccepted", "Approved"]);
116
+ // `SwapState` (was `CSwapState` / `FSwapState`) and `GatesSet` (`PermissionsSet` before decision 098) are `ManagerBase`'s: declared once, emitted by both managers (decisions 096, #112).
117
+ const SHARED = new Set(["PoolCreated", "Swap", "SwapState", "GatesSet", "Transfer", "Approval", "OperatorSet", "CollectorProposed", "CollectorAccepted", "TreasuryProposed", "TreasuryAccepted", "Approved"]);
116
118
 
117
119
  const plural = (n: string) => (/(s|x|ch|sh)$/.test(n) ? `${n}es` : `${n}s`);
118
120
  /** A column's GraphQL name: postgraphile camelCases the snake_case rindexer writes (`F` → `f`). */
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
- * Logswap SDK — typed, offline-first helpers over the CPoolManager / Router / Lens surface.
2
+ * log(swap) SDK — typed, offline-first helpers over the ManagerF / ManagerC, RouterF / RouterC and LensF / LensC surface.
3
3
  *
4
- * The manager/router/lens split is an EIP-170 artifact, not a design boundary, so it is hidden
5
- * here: callers address markets by `PoolKey` and never choose a contract. A future re-homing of
6
- * a function is then an SDK patch rather than a breaking change (docs/app.md, tier 2).
4
+ * The manager/router/lens split is hidden here: callers address markets by `PoolKey` (C) and pool id (F) and
5
+ * never choose a contract. What a caller does choose is the SIDE (`"F"` or `"C"`) where a router is the
6
+ * spender (decision 096: one router per manager, each its own Permit2 spender) — see `onboard.ts`.
7
7
  */
8
8
  export * from "./keys.js";
9
9
  export * from "./ids.js";
@@ -23,5 +23,5 @@ export * from "./fpool.js";
23
23
  export * from "./fees.js";
24
24
  export * from "./history.js";
25
25
  export * from "./views.js";
26
- export { cPoolManagerAbi, fPoolManagerAbi, logswapLensAbi, logswapRouterAbi, padPolicyAbi, polPolicyAbi, policyRegistryAbi, privatePolicyAbi, publicPolicyAbi } from "./generated.js";
26
+ export { managerCAbi, managerFAbi, lensCAbi, lensFAbi, routerCAbi, routerFAbi, routerActionsAbi, orphanPolicyFAbi, padPolicyAbi, polPolicyAbi, policyRegistryAbi, sponsorPolicyCAbi, sponsorPolicyFAbi } from "./generated.js";
27
27
  export * from "./admin.js";
package/src/indexer.ts CHANGED
@@ -9,8 +9,8 @@
9
9
  */
10
10
 
11
11
  import type { Address, Hex } from "viem";
12
- import type { LogswapClient } from "./client.js";
13
- import { cPoolManagerAbi } from "./generated.js";
12
+ import { managerC, lensC, type LogswapClient } from "./client.js";
13
+ import { managerCAbi } from "./generated.js";
14
14
  import { getPositionClass } from "./positions.js";
15
15
  import { discoverMarkets, type DiscoveredMarket } from "./pools.js";
16
16
  import { poolId, type PoolKey } from "./keys.js";
@@ -178,22 +178,24 @@ export async function indexerHealthy(url: string, signal?: AbortSignal): Promise
178
178
  * the more durable path and should be preferred wherever an indexer is configured, with the log
179
179
  * scan as the fallback for when it is not.
180
180
  *
181
- * `PoolCreated` (the C manager's) carries every field of the `PoolKey` (`mintFee` included since C 12), so the key is rebuilt exactly and `poolId` is
181
+ * `PoolCreated` (the C manager's) carries every field of the `PoolKey` (`mintFee` since C 12, `creator` and `salt` since C 15), so the key is rebuilt exactly and `poolId` is
182
182
  * recomputed locally rather than trusted from the row — the same offline derivation the SDK uses
183
183
  * everywhere else, so a wrong id fails loudly instead of addressing the wrong pool.
184
184
  */
185
185
  export async function discoverMarketsIndexed(url: string, signal?: AbortSignal): Promise<DiscoveredMarket[]> {
186
186
  type Row = {
187
- poolId: Hex; base: Hex; quote: Hex; tickSpacing: string; swapFee: string; mintFee: string; policy: Hex;
187
+ poolId: Hex; base: Hex; quote: Hex; creator: Hex; salt: Hex; tickSpacing: string; swapFee: string; mintFee: string; policy: Hex;
188
188
  initialLogPrice: string; blockNumber: string; txHash: Hex; contractAddress: Hex;
189
189
  };
190
- const nodes = await gqlAll<Row>(url, "allCPoolManagerPoolCreateds", "poolId base quote tickSpacing swapFee mintFee policy initialLogPrice blockNumber txHash contractAddress", { signal });
190
+ const nodes = await gqlAll<Row>(url, "allCPoolManagerPoolCreateds", "poolId base quote creator salt tickSpacing swapFee mintFee policy initialLogPrice blockNumber txHash contractAddress", { signal });
191
191
  return nodes.map((n) => {
192
192
  const key: PoolKey = {
193
193
  base: n.base as Address,
194
194
  quote: n.quote as Address,
195
195
  tickSpacing: BigInt(n.tickSpacing),
196
196
  policy: n.policy as Address,
197
+ creator: n.creator as Address, // a key field since C 15 (097): the event carries it, and the salt
198
+ salt: n.salt,
197
199
  swapFee: BigInt(n.swapFee), // rindexer names the column after the event's field: `swapFee` since C version 6 (logswap-be #40)
198
200
  mintFee: BigInt(n.mintFee), // a key field since C 12 (078): the event carries it
199
201
  };
@@ -227,7 +229,7 @@ export async function discoverMarketsBest(
227
229
  // read revert, refresh-proof. Each row names the manager that emitted it, so keep only the
228
230
  // rows of the manager this client is pointed at — no read per row. None left means the
229
231
  // indexer has not seen this deployment: the chain scan is the answer.
230
- const mine = c.addresses.cPoolManager.toLowerCase();
232
+ const mine = managerC(c).toLowerCase();
231
233
  const alive = markets.filter((m) => m.manager?.toLowerCase() === mine);
232
234
  if (alive.length) return { markets: alive, source: "indexer" };
233
235
  } catch {
@@ -244,10 +246,10 @@ export async function discoverMarketsBest(
244
246
  * use the indexed `poolPositions` and fall back to this when the indexer is missing or stale.
245
247
  */
246
248
  export async function poolPositionsFromChain(c: LogswapClient, key: PoolKey, x: bigint): Promise<PoolPosition[]> {
247
- const { logswapLensAbi } = await import("./generated.js");
249
+ const { lensCAbi } = await import("./generated.js");
248
250
  const floors = (await c.public.readContract({
249
- address: c.addresses.lens,
250
- abi: logswapLensAbi,
251
+ address: lensC(c),
252
+ abi: lensCAbi,
251
253
  functionName: "liveTicks",
252
254
  args: [poolId(key)],
253
255
  } as never)) as readonly bigint[];
@@ -255,8 +257,8 @@ export async function poolPositionsFromChain(c: LogswapClient, key: PoolKey, x:
255
257
  const rows = await Promise.all(
256
258
  [...floors].map(async (floor) => {
257
259
  const id = (await c.public.readContract({
258
- address: c.addresses.cPoolManager,
259
- abi: cPoolManagerAbi,
260
+ address: managerC(c),
261
+ abi: managerCAbi,
260
262
  functionName: "idOf",
261
263
  args: [pool, floor],
262
264
  } as never)) as bigint;
package/src/keys.ts CHANGED
@@ -20,7 +20,7 @@ import { encodeAbiParameters, keccak256, type Address, type Hex } from "viem";
20
20
  * The mint fee lives in the same domain. The F manager no longer answers
21
21
  * `FEE_MIN()` / `FEE_MAX()` (F version 21 cut the getters for bytecode); every F policy still
22
22
  * checks φ against these at `onInitialize`, and the C side re-exposes them through the lens's
23
- * `cParams()`. Constants here, so a create form bounds φ with no read.
23
+ * `params()` (`LensC`, decision 096). Constants here, so a create form bounds φ with no read.
24
24
  */
25
25
  export const FEE_MIN = 10n ** 14n;
26
26
  export const FEE_MAX = 10n ** 16n;
@@ -43,8 +43,8 @@ export function isValidMintFee(swapFee: bigint, mintFee: bigint): boolean {
43
43
  }
44
44
 
45
45
  /**
46
- * The economic tuple that IS the market. Field order is load-bearing: it is the ABI encoding order,
47
- * so it must match `src/types/CPoolKey.sol` exactly.
46
+ * The tuple that IS the market (`PoolKeyC`: base, quote, tickSpacing, policy, creator, salt, swapFee, mintFee). Field order is load-bearing: it is the ABI encoding order,
47
+ * so it must match `src/types/PoolKeyC.sol` exactly.
48
48
  */
49
49
  export interface PoolKey {
50
50
  base: Address;
@@ -57,6 +57,18 @@ export interface PoolKey {
57
57
  * `address(0)` is a bare deployment with no registry.
58
58
  */
59
59
  policy: Address;
60
+ /**
61
+ * Who started the market (C 15, decision 097): the caller of `initialize`, checked by the manager and the registry
62
+ * (`NotCreator`). It prices the market at birth and is the only account that may make the FIRST mint, which seeds
63
+ * the backstop; after that, minting is open. Part of the id: two creators' markets of one shape are two markets.
64
+ * On a router-routed call the creator is the trusted forwarder's user (the connected account), not the router.
65
+ */
66
+ creator: Address;
67
+ /**
68
+ * A creator's way to open a second market of the same shape (C 15, decision 097). Part of the id. Zero on the
69
+ * public policy, which refuses any other value (`NotCanonical`): a public market is canonical per exact key.
70
+ */
71
+ salt: Hex;
60
72
  /**
61
73
  * The swap fee, WAD, fixed for the life of the market: {@link FEE_MIN} (1 bp) ≤ φ ≤ {@link FEE_MAX} (1%),
62
74
  * or `0n`, which since C version 7 is NO fee (decisions 065; `DEFAULT_FEE` is gone). The gate
@@ -82,6 +94,8 @@ const POOL_KEY_ABI = [
82
94
  { name: "quote", type: "address" },
83
95
  { name: "tickSpacing", type: "int256" },
84
96
  { name: "policy", type: "address" },
97
+ { name: "creator", type: "address" },
98
+ { name: "salt", type: "bytes32" },
85
99
  { name: "swapFee", type: "uint256" },
86
100
  { name: "mintFee", type: "uint256" },
87
101
  ],
@@ -103,6 +117,8 @@ export function poolId(key: PoolKey): Hex {
103
117
  quote: key.quote,
104
118
  tickSpacing: key.tickSpacing,
105
119
  policy: key.policy,
120
+ creator: key.creator,
121
+ salt: key.salt,
106
122
  swapFee: key.swapFee,
107
123
  mintFee: key.mintFee,
108
124
  },
package/src/liquidity.ts CHANGED
@@ -23,8 +23,8 @@
23
23
  */
24
24
 
25
25
  import type { Address, Hash } from "viem";
26
- import { logswapLensAbi, logswapRouterAbi } from "./generated.js";
27
- import type { LogswapClient } from "./client.js";
26
+ import { lensCAbi, routerCAbi } from "./generated.js";
27
+ import { lensC, type LogswapClient } from "./client.js";
28
28
  import { poolId as keyId, type PoolKey } from "./keys.js";
29
29
  import { NO_CAP } from "./ids.js";
30
30
  import { describeId, getPositionClass } from "./positions.js";
@@ -82,7 +82,8 @@ export interface MintArgs extends WriteOptions {
82
82
  */
83
83
  export async function mint(c: LogswapClient, a: MintArgs): Promise<Hash> {
84
84
  return sendRouterWriteWithPermits(c, {
85
- abi: logswapRouterAbi,
85
+ side: "C",
86
+ abi: routerCAbi,
86
87
  functionName: "mintBlended",
87
88
  args: [
88
89
  keyId(a.key),
@@ -124,7 +125,8 @@ export interface CreateMarketArgs extends WriteOptions {
124
125
  */
125
126
  export async function createMarket(c: LogswapClient, a: CreateMarketArgs): Promise<Hash> {
126
127
  return sendRouterWriteWithPermits(c, {
127
- abi: logswapRouterAbi,
128
+ side: "C",
129
+ abi: routerCAbi,
128
130
  functionName: "createMarket",
129
131
  args: [
130
132
  a.key,
@@ -163,7 +165,8 @@ export interface ZapArgs extends WriteOptions {
163
165
  */
164
166
  export async function zapIn(c: LogswapClient, a: ZapArgs): Promise<Hash> {
165
167
  return sendRouterWriteWithPermits(c, {
166
- abi: logswapRouterAbi,
168
+ side: "C",
169
+ abi: routerCAbi,
167
170
  functionName: "zapIn",
168
171
  args: [
169
172
  keyId(a.key),
@@ -200,7 +203,8 @@ export interface UpdateArgs extends WriteOptions {
200
203
  /** The primitive, through the router's named entry: resize and reposition in one call, in kind. */
201
204
  export async function update(c: LogswapClient, a: UpdateArgs): Promise<Hash> {
202
205
  return sendRouterWriteWithPermits(c, {
203
- abi: logswapRouterAbi,
206
+ side: "C",
207
+ abi: routerCAbi,
204
208
  functionName: "update",
205
209
  args: [
206
210
  keyId(a.key),
@@ -239,7 +243,8 @@ export interface MoveArgs extends WriteOptions {
239
243
  */
240
244
  export async function move(c: LogswapClient, a: MoveArgs): Promise<Hash> {
241
245
  return sendRouterWrite(c, {
242
- abi: logswapRouterAbi,
246
+ side: "C",
247
+ abi: routerCAbi,
243
248
  functionName: "move",
244
249
  args: [
245
250
  keyId(a.key),
@@ -293,8 +298,8 @@ export async function previewEdit(
293
298
  ): Promise<EditPreview> {
294
299
  const holder = a.holder ?? requireWallet(c).address;
295
300
  const r = (await c.public.readContract({
296
- address: c.addresses.lens,
297
- abi: logswapLensAbi,
301
+ address: lensC(c),
302
+ abi: lensCAbi,
298
303
  functionName: "previewUpdate",
299
304
  args: [keyId(a.key), a.fromId, holder, a.shares, a.toFloor, a.toCap ?? NO_CAP, a.newL],
300
305
  })) as readonly [bigint, bigint, bigint];
@@ -473,7 +478,8 @@ export interface BurnArgs extends WriteOptions {
473
478
  export async function burn(c: LogswapClient, a: BurnArgs): Promise<Hash> {
474
479
  if (a.ids.length !== a.shares.length) throw new Error("logswap: ids and shares length mismatch");
475
480
  return sendRouterWrite(c, {
476
- abi: logswapRouterAbi,
481
+ side: "C",
482
+ abi: routerCAbi,
477
483
  functionName: "burn",
478
484
  args: [
479
485
  keyId(a.key),
package/src/onboard.ts CHANGED
@@ -12,10 +12,15 @@
12
12
  * ERC-20 to the *router* is not enough and not used — the pull goes through Permit2, and the
13
13
  * failure is `NotAllowed()` from Permit2 rather than anything naming an allowance. Approving the
14
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.
15
20
  */
16
21
 
17
22
  import { parseAbi, zeroAddress, type Address, type Hash, type Hex } from "viem";
18
- import type { LogswapClient } from "./client.js";
23
+ import { routerOf, type LogswapClient, type Side } from "./client.js";
19
24
  import { requireWallet } from "./write.js";
20
25
 
21
26
  export const MAX_UINT256 = (1n << 256n) - 1n;
@@ -55,14 +60,15 @@ export function usesPermit2(c: LogswapClient): boolean {
55
60
  return !!c.addresses.permit2 && c.addresses.permit2 !== zeroAddress;
56
61
  }
57
62
 
58
- /** The contract that will actually pull tokens: Permit2 when wired, otherwise the router. */
59
- export function spenderFor(c: LogswapClient): Address {
60
- return usesPermit2(c) ? c.addresses.permit2! : c.addresses.router;
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);
61
66
  }
62
67
 
63
68
  /** Step 1 — approve the *puller* on an ERC-20. Once per token. */
64
69
  export async function approveToken(
65
70
  c: LogswapClient,
71
+ side: Side,
66
72
  token: Address,
67
73
  amount: bigint = MAX_UINT256,
68
74
  ): Promise<Hash> {
@@ -71,14 +77,14 @@ export async function approveToken(
71
77
  address: token,
72
78
  abi: ERC20,
73
79
  functionName: "approve",
74
- args: [spenderFor(c), amount],
80
+ args: [spenderFor(c, side), amount],
75
81
  account,
76
82
  chain: c.wallet!.chain,
77
83
  } as never);
78
84
  }
79
85
 
80
86
  /**
81
- * Step 2 — the standing Permit2 allowance for the router.
87
+ * Step 2 — the standing Permit2 allowance for the `side`'s router.
82
88
  *
83
89
  * No-op when the deployment has no Permit2. Prefer the signature flow (`applyPermit2` on the
84
90
  * router, bundled through `multicall`) in production; this transaction form is what a test or a
@@ -86,6 +92,7 @@ export async function approveToken(
86
92
  */
87
93
  export async function approvePermit2ForRouter(
88
94
  c: LogswapClient,
95
+ side: Side,
89
96
  token: Address,
90
97
  amount: bigint = MAX_UINT160,
91
98
  expiration: bigint = permit2Expiry(),
@@ -96,7 +103,7 @@ export async function approvePermit2ForRouter(
96
103
  address: c.addresses.permit2!,
97
104
  abi: PERMIT2,
98
105
  functionName: "approve",
99
- args: [token, c.addresses.router, amount, Number(expiration)],
106
+ args: [token, routerOf(c, side), amount, Number(expiration)],
100
107
  account,
101
108
  chain: c.wallet!.chain,
102
109
  } as never);
@@ -108,7 +115,7 @@ export async function approvePermit2ForRouter(
108
115
  * Returns the hashes actually sent, so a UI can report "2 approvals" honestly rather than always
109
116
  * prompting twice.
110
117
  */
111
- export async function onboardToken(c: LogswapClient, token: Address, need: bigint): Promise<Hash[]> {
118
+ export async function onboardToken(c: LogswapClient, side: Side, token: Address, need: bigint): Promise<Hash[]> {
112
119
  // The address, not the account object: these two are READS. Signing paths take the object.
113
120
  const { address } = requireWallet(c);
114
121
  const sent: Hash[] = [];
@@ -117,10 +124,10 @@ export async function onboardToken(c: LogswapClient, token: Address, need: bigin
117
124
  address: token,
118
125
  abi: ERC20,
119
126
  functionName: "allowance",
120
- args: [address, spenderFor(c)],
127
+ args: [address, spenderFor(c, side)],
121
128
  })) as bigint;
122
129
  if (allowance < need) {
123
- const h = await approveToken(c, token);
130
+ const h = await approveToken(c, side, token);
124
131
  await c.public.waitForTransactionReceipt({ hash: h });
125
132
  sent.push(h);
126
133
  }
@@ -130,10 +137,10 @@ export async function onboardToken(c: LogswapClient, token: Address, need: bigin
130
137
  address: c.addresses.permit2!,
131
138
  abi: PERMIT2,
132
139
  functionName: "allowance",
133
- args: [address, token, c.addresses.router],
140
+ args: [address, token, routerOf(c, side)],
134
141
  })) as readonly [bigint, number, number];
135
142
  if (!permit2Covers(amt, expiration, need)) {
136
- const h = await approvePermit2ForRouter(c, token);
143
+ const h = await approvePermit2ForRouter(c, side, token);
137
144
  if (h) {
138
145
  await c.public.waitForTransactionReceipt({ hash: h });
139
146
  sent.push(h);
@@ -146,14 +153,15 @@ export async function onboardToken(c: LogswapClient, token: Address, need: bigin
146
153
  /** What still stands between this wallet and a working write. */
147
154
  export interface TokenReadiness {
148
155
  usesPermit2: boolean;
149
- /** ERC-20 allowance to whoever pulls (Permit2 when wired, else the router). */
156
+ /** ERC-20 allowance to whoever pulls (Permit2 when wired, else the side's router). */
150
157
  needsTokenApproval: boolean;
151
- /** Permit2's standing allowance for the router. Always false when Permit2 is not wired. */
158
+ /** Permit2's standing allowance for the side's router. Always false when Permit2 is not wired. */
152
159
  needsPermit2Approval: boolean;
153
160
  }
154
161
 
155
162
  export async function checkTokenReadiness(
156
163
  c: LogswapClient,
164
+ side: Side,
157
165
  token: Address,
158
166
  owner: Address,
159
167
  need: bigint,
@@ -162,7 +170,7 @@ export async function checkTokenReadiness(
162
170
  address: token,
163
171
  abi: ERC20,
164
172
  functionName: "allowance",
165
- args: [owner, spenderFor(c)],
173
+ args: [owner, spenderFor(c, side)],
166
174
  })) as bigint;
167
175
 
168
176
  let needsPermit2Approval = false;
@@ -171,7 +179,7 @@ export async function checkTokenReadiness(
171
179
  address: c.addresses.permit2!,
172
180
  abi: PERMIT2,
173
181
  functionName: "allowance",
174
- args: [owner, token, c.addresses.router],
182
+ args: [owner, token, routerOf(c, side)],
175
183
  })) as readonly [bigint, number, number];
176
184
  needsPermit2Approval = !permit2Covers(amt, expiration, need);
177
185
  }
@@ -206,7 +214,7 @@ const PERMIT2_TYPES = {
206
214
  * a PermitSingle (a signature prompt, not a transaction), and return the encoded `applyPermit2`
207
215
  * calls ready to ride in a router multicall. Tokens already allowed produce nothing.
208
216
  */
209
- export async function permit2SigCalls(c: LogswapClient, tokens: Address[]): Promise<Hex[]> {
217
+ export async function permit2SigCalls(c: LogswapClient, side: Side, tokens: Address[]): Promise<Hex[]> {
210
218
  if (!usesPermit2(c)) return [];
211
219
  const { account, address: owner } = requireWallet(c);
212
220
  const chainId = await c.public.getChainId();
@@ -216,12 +224,12 @@ export async function permit2SigCalls(c: LogswapClient, tokens: Address[]): Prom
216
224
  address: c.addresses.permit2!,
217
225
  abi: PERMIT2,
218
226
  functionName: "allowance",
219
- args: [owner, token, c.addresses.router],
227
+ args: [owner, token, routerOf(c, side)],
220
228
  })) as readonly [bigint, number, number];
221
229
  if (permit2Covers(amt, expiration, 1n << 120n)) continue; // effectively unlimited, and not lapsing
222
230
  const message = {
223
231
  details: { token, amount: MAX_UINT160, expiration: Number(permit2Expiry()), nonce },
224
- spender: c.addresses.router,
232
+ spender: routerOf(c, side),
225
233
  sigDeadline: BigInt(Math.floor(Date.now() / 1000)) + 1800n,
226
234
  };
227
235
  const signature = await c.wallet!.signTypedData({
@@ -232,10 +240,10 @@ export async function permit2SigCalls(c: LogswapClient, tokens: Address[]): Prom
232
240
  message,
233
241
  } as never);
234
242
  const { encodeFunctionData } = await import("viem");
235
- const { logswapRouterAbi } = await import("./generated.js");
243
+ const { routerCAbi, routerFAbi } = await import("./generated.js");
236
244
  calls.push(
237
245
  encodeFunctionData({
238
- abi: logswapRouterAbi,
246
+ abi: side === "F" ? routerFAbi : routerCAbi,
239
247
  functionName: "applyPermit2",
240
248
  args: [owner, message, signature],
241
249
  } as never),
package/src/pools.ts CHANGED
@@ -13,8 +13,8 @@
13
13
  */
14
14
 
15
15
  import { formatUnits, type Address, type Hex } from "viem";
16
- import { logswapLensAbi, cPoolManagerAbi } from "./generated.js";
17
- import { clientChainId, type LogswapClient } from "./client.js";
16
+ import { lensCAbi, managerCAbi } from "./generated.js";
17
+ import { lensC, managerC, clientChainId, type LogswapClient } from "./client.js";
18
18
  import { signerFor } from "./write.js";
19
19
  import { readCachedToken, registerCacheClear, writeCachedToken } from "./tokencache.js";
20
20
  import { poolId, type PoolKey } from "./keys.js";
@@ -48,8 +48,8 @@ export interface PoolState {
48
48
  /** Read a market's full state by key, through the lens (`getPools`; C version 7). All zero, `initialized` false, for a market nobody opened. */
49
49
  export async function getPool(c: LogswapClient, key: PoolKey): Promise<PoolState> {
50
50
  const v = (await c.public.readContract({
51
- address: c.addresses.lens,
52
- abi: logswapLensAbi,
51
+ address: lensC(c),
52
+ abi: lensCAbi,
53
53
  functionName: "getPools",
54
54
  args: [[poolId(key)]],
55
55
  })) as readonly unknown[];
@@ -166,16 +166,17 @@ export async function discoverMarkets(
166
166
  ): Promise<DiscoveredMarket[]> {
167
167
  // Narrow the const ABI to the PoolCreated entry while KEEPING its literal type, so viem can infer
168
168
  // the log shape. `as never` would compile but collapse every arg to `never` at the call site.
169
- type Ev = Extract<(typeof cPoolManagerAbi)[number], { type: "event"; name: "PoolCreated" }>;
170
- const ev = cPoolManagerAbi.find((x): x is Ev => x.type === "event" && x.name === "PoolCreated");
169
+ type Ev = Extract<(typeof managerCAbi)[number], { type: "event"; name: "PoolCreated" }>;
170
+ const ev = managerCAbi.find((x): x is Ev => x.type === "event" && x.name === "PoolCreated");
171
171
  if (!ev) throw new Error("logswap: PoolCreated event missing from the generated ABI");
172
172
 
173
173
  const args: Record<string, unknown> = {};
174
- // `quote` is an indexed topic (C 13: `PoolCreated(poolId, quote, creator, base, …)`); `base` no longer is, so it filters below
174
+ // `quote` is an indexed topic (C 13: `PoolCreated(poolId, quote, creator, base, …)`); `base` no longer is, so it filters below.
175
+ // C 15 (decision 097): the event ends with the key's `salt`, so the whole key — `creator` and `salt` included — is rebuilt from the log.
175
176
  if (opts.quote) args.quote = opts.quote;
176
- const { logs } = await getEventLogs<{ base: Address; quote: Address; tickSpacing: bigint; policy: Address; swapFee: bigint; mintFee: bigint; initialLogPrice: bigint }>(c, {
177
+ const { logs } = await getEventLogs<{ base: Address; quote: Address; creator: Address; salt: Hex; tickSpacing: bigint; policy: Address; swapFee: bigint; mintFee: bigint; initialLogPrice: bigint }>(c, {
177
178
  indexer: opts.indexer,
178
- address: c.addresses.cPoolManager,
179
+ address: managerC(c),
179
180
  event: ev,
180
181
  args,
181
182
  fromBlock: opts.fromBlock, // the client's deployBlock when absent
@@ -190,6 +191,8 @@ export async function discoverMarkets(
190
191
  quote: a.quote,
191
192
  tickSpacing: a.tickSpacing,
192
193
  policy: a.policy,
194
+ creator: a.creator,
195
+ salt: a.salt,
193
196
  swapFee: a.swapFee,
194
197
  mintFee: a.mintFee,
195
198
  };
@@ -213,8 +216,8 @@ export async function discoverMarkets(
213
216
  */
214
217
  export async function liveFloors(c: LogswapClient, key: PoolKey): Promise<readonly bigint[]> {
215
218
  return c.public.readContract({
216
- address: c.addresses.lens,
217
- abi: logswapLensAbi,
219
+ address: lensC(c),
220
+ abi: lensCAbi,
218
221
  functionName: "liveTicks",
219
222
  args: [poolId(key)],
220
223
  }) as Promise<readonly bigint[]>;
@@ -223,8 +226,8 @@ export async function liveFloors(c: LogswapClient, key: PoolKey): Promise<readon
223
226
  /** Is this floor on the market's grid, and inside `MAX_ABS_FLOOR`? Pure — no RPC on the manager. */
224
227
  export async function isValidFloor(c: LogswapClient, key: PoolKey, floor: bigint): Promise<boolean> {
225
228
  return c.public.readContract({
226
- address: c.addresses.lens,
227
- abi: logswapLensAbi,
229
+ address: lensC(c),
230
+ abi: lensCAbi,
228
231
  functionName: "isValidFloor",
229
232
  args: [poolId(key), floor],
230
233
  }) as Promise<boolean>;
@@ -428,15 +431,20 @@ export async function marketOpenedAt(c: LogswapClient, m: DiscoveredMarket): Pro
428
431
 
429
432
  /**
430
433
  * Create a C market: `initialize(key, initialLogPrice)` on the manager — a call, not a deployment, and no funds move. The
434
+ * caller must BE the key's `creator` (C 15, decision 097: `NotCreator`; the registry resolves a routed caller to its user), and only
435
+ * the creator may make the first mint, which seeds the backstop. The
431
436
  * creator-selected mint fee is part of the key (C 12, decision 078: `2 * swapFee <= mintFee <= MINT_FEE_MAX`,
432
437
  * preset at twice the swap fee by {@link presetMintFee}); there is no separate argument. Under a curated gate the
433
438
  * CALLER must be a listed creator; the canonical path is the router's {@link createMarket}, which forwards its
434
439
  * user to the gate and mints the backstop in the same transaction (064).
435
440
  */
436
441
  export async function initializeMarket(c: LogswapClient, a: { key: PoolKey; initialLogPrice: bigint; account: Address }) {
442
+ if (a.account.toLowerCase() !== a.key.creator.toLowerCase()) {
443
+ throw new Error(`logswap: the key's creator ${a.key.creator} must be the account that initializes it (${a.account}) — NotCreator`);
444
+ }
437
445
  const { request } = await c.public.simulateContract({
438
- address: c.addresses.cPoolManager,
439
- abi: cPoolManagerAbi,
446
+ address: managerC(c),
447
+ abi: managerCAbi,
440
448
  functionName: "initialize",
441
449
  args: [a.key, a.initialLogPrice],
442
450
  account: signerFor(c, a.account),