@logswap/sdk 0.5.75 → 0.5.77

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.
package/src/fpool.ts CHANGED
@@ -47,9 +47,10 @@ export { fPoolManagerAbi, padPolicyAbi, polPolicyAbi, privatePolicyAbi, publicPo
47
47
  * in for the base — a call through it to any of the four policies decodes the base's functions.
48
48
  */
49
49
  export const fPolicyBaseAbi = polPolicyAbi;
50
- import type { LogswapClient } from "./client.js";
50
+ import { clientChainId, type LogswapClient } from "./client.js";
51
51
  import { fallBackUnlessRejected, signerFor } from "./write.js";
52
52
  import { getEventLogs } from "./history.js";
53
+ import { poolHolders } from "./views.js";
53
54
  import { presetMintFee } from "./positions.js";
54
55
 
55
56
  const WAD = 10n ** 18n;
@@ -86,10 +87,11 @@ export interface FPoolState {
86
87
  /** This generation's income still in the pool, quote: L·(θ₀ − θ). */
87
88
  unharvested: bigint;
88
89
  /**
89
- * The common cap (decisions 061): d = ln(cap price / floor price) in 1e-6 log units, from the key;
90
- * 0 is uncapped. Leverage at the floor is 1/(1 − e^{−d}): {@link fPoolLeverageOfCap}.
90
+ * The common cap as a log distance (decisions 061): ln(cap price / floor price) in 1e-6 log units,
91
+ * the key's `lnCapOverFloor`; 0 is uncapped (cap ∞). Leverage at the floor is 1/(1 − e^{−value}):
92
+ * {@link fPoolLeverageOfCap}.
91
93
  */
92
- cap: number;
94
+ lnCapOverFloor: number;
93
95
  /** Σ = ∫ Σ_j w_j (dx_j)², accrued even though the fee is fixed — the kernel stays calculable. */
94
96
  bigSigma: bigint;
95
97
  /**
@@ -102,8 +104,8 @@ export interface FPoolState {
102
104
  asks: number;
103
105
  /** The sponsor's own label for this pool — the policy's word, mutable and never identity. "" when unnamed. */
104
106
  name: string;
105
- /** The block of the last harvest / composition action: no swap runs in it. */
106
- restructureBlock: number;
107
+ /** The time (unix seconds) of the last harvest / composition action: no swap runs at that timestamp (F version 29; a block number before). */
108
+ restructureTime: number;
107
109
  /** What the pool is — its POLICY's self-description; see {@link FPoolKind}. */
108
110
  kind: FPoolKind;
109
111
  /** False before the first seed — and between a retire and the next one. */
@@ -171,12 +173,12 @@ export async function getFPool(c: LogswapClient, poolId: Hex): Promise<FPoolStat
171
173
  logFloor: v.logFloor,
172
174
  compositeX: v.compositeX,
173
175
  unharvested: v.unharvested,
174
- cap: Number(p.cap),
176
+ lnCapOverFloor: Number(p.lnCapOverFloor),
175
177
  bigSigma: p.bigSigma,
176
178
  policy: p.policy,
177
179
  asks: Number(p.asks),
178
180
  name,
179
- restructureBlock: Number(p.restructureBlock),
181
+ restructureTime: Number(p.restructureTime),
180
182
  kind,
181
183
  seeded: p.seeded,
182
184
  gen: Number(p.gen),
@@ -190,7 +192,7 @@ export async function getFPool(c: LogswapClient, poolId: Hex): Promise<FPoolStat
190
192
  /**
191
193
  * The kind of pool — the POLICY's self-description (the manager knows no kind; the
192
194
  * policy the key names is the kind, and what it promises is its code). `public`: the protocol's
193
- * own pool — its policy requires the collector as creator; no gate, no operator. `private`, the
195
+ * own pool — its policy requires the LOG Authority as creator; no gate, no operator. `private`, the
194
196
  * desk: the one whose policy may gate swaps and mints, with an operator that reshapes the legs
195
197
  * between generations. `pol`, a treasury's pool, one base: composition and access fixed at
196
198
  * birth. `pad`, a launch, one base: its policy answers income only at every harvest and declared
@@ -209,8 +211,9 @@ const kindCache = new Map<string, Promise<FPoolKind>>();
209
211
  * chain: a pool's kind is its policy's word, never the manager's. An unknown tag
210
212
  * (a policy that is not one of the protocol's four shapes) throws; the app has no screen for it.
211
213
  */
212
- export function fPoolKindOfPolicy(c: LogswapClient, policy: Address): Promise<FPoolKind> {
213
- const key = `${c.public.chain?.id ?? 0}:${policy.toLowerCase()}`;
214
+ export async function fPoolKindOfPolicy(c: LogswapClient, policy: Address): Promise<FPoolKind> {
215
+ // the chain id is part of the key: the policies sit at one address on every release chain
216
+ const key = `${await clientChainId(c)}:${policy.toLowerCase()}`;
214
217
  let hit = kindCache.get(key);
215
218
  if (!hit) {
216
219
  hit = (c.public.readContract({ address: policy, abi: fPolicyBaseAbi, functionName: "kind" } as never) as Promise<Hex>).then((word) => {
@@ -343,7 +346,7 @@ export function fPoolPriceOf(x: bigint, scale = 1): number {
343
346
  export function fPoolShareValue(s: FPoolState): bigint {
344
347
  if (s.totalSupply === 0n) return 0n;
345
348
  let v = s.L + s.Q;
346
- if (s.cap !== 0) {
349
+ if (s.lnCapOverFloor !== 0) {
347
350
  v = s.Q;
348
351
  for (let j = 0; j < s.reserves.length; j++) {
349
352
  const rv = s.virtualReserves[j]!;
@@ -1007,10 +1010,15 @@ export interface FPoolCreateArgs {
1007
1010
  */
1008
1011
  mintFee?: bigint;
1009
1012
  /**
1010
- * The common cap (decisions 061): d = ln(cap price / floor price) in 1e-6 log units, a key field;
1011
- * 0 or absent is uncapped. From a leverage λ: {@link fPoolCapOfLeverage}.
1013
+ * The common cap as a log distance (decisions 061): ln(cap price / floor price) in 1e-6 log units,
1014
+ * the key's `lnCapOverFloor`; 0 or absent is uncapped (cap ∞). Pass `lnCapOverFloor` or `leverage`, not both.
1012
1015
  */
1013
- cap?: number;
1016
+ lnCapOverFloor?: number;
1017
+ /**
1018
+ * The leverage at the floor λ (decisions 061), converted to `lnCapOverFloor` by {@link fPoolCapOfLeverage}:
1019
+ * 1 is uncapped (cap ∞, `lnCapOverFloor` 0), 2 puts the cap at twice the floor price.
1020
+ */
1021
+ leverage?: number;
1014
1022
  /**
1015
1023
  * Which of the protocol's four the pool is — see {@link FPoolKind}. NOT a key field (decisions
1016
1024
  * 049: the manager knows no kind): it selects the deployment's policy of that kind when `policy`
@@ -1075,8 +1083,8 @@ export interface FPoolKey {
1075
1083
  policy: Address;
1076
1084
  creator: Address;
1077
1085
  salt: Hex;
1078
- /** The common cap, d in 1e-6 log units; 0 uncapped (decisions 061). Appended in F version 25: every pool id moved. */
1079
- cap: number;
1086
+ /** The common cap as ln(cap price / floor price), in 1e-6 log units; 0 uncapped, cap ∞ (decisions 061). Appended in F version 25 as `cap`; renamed in F version 27 (no id moved). */
1087
+ lnCapOverFloor: number;
1080
1088
  }
1081
1089
 
1082
1090
  const F_POOL_KEY_ABI = [
@@ -1090,7 +1098,7 @@ const F_POOL_KEY_ABI = [
1090
1098
  { name: "policy", type: "address" },
1091
1099
  { name: "creator", type: "address" },
1092
1100
  { name: "salt", type: "bytes32" },
1093
- { name: "cap", type: "uint24" },
1101
+ { name: "lnCapOverFloor", type: "uint24" },
1094
1102
  ],
1095
1103
  },
1096
1104
  ] as const;
@@ -1115,7 +1123,7 @@ export function fPoolKeyOf(c: LogswapClient, a: Omit<FPoolCreateArgs, "account">
1115
1123
  policy: a.policy ?? fPoolPolicyFor(c, a.kind),
1116
1124
  creator: a.creator,
1117
1125
  salt: a.salt ?? ZERO_SALT,
1118
- cap: a.cap ?? 0,
1126
+ lnCapOverFloor: fPoolCapOfArgs(a),
1119
1127
  };
1120
1128
  }
1121
1129
 
@@ -1153,10 +1161,10 @@ export async function fPoolIdOf(c: LogswapClient, a: Omit<FPoolCreateArgs, "acco
1153
1161
  export async function fPoolKeyOfPool(c: LogswapClient, poolId: Hex, opts: { salt?: Hex; fromBlock?: bigint; /** The rindexer GraphQL endpoint: `FInitialize` from there, the node otherwise. The salt is in neither (the event does not carry it). */ indexer?: string } = {}): Promise<FPoolKey> {
1154
1162
  type Ev = Extract<(typeof fPoolManagerAbi)[number], { type: "event"; name: "FInitialize" }>;
1155
1163
  const ev = fPoolManagerAbi.find((x): x is Ev => x.type === "event" && x.name === "FInitialize")!;
1156
- const { logs } = await getEventLogs(c, { indexer: opts.indexer, address: c.addresses.fPoolManager!, event: ev as never, args: { poolId }, fromBlock: opts.fromBlock ?? 0n });
1164
+ const { logs } = await getEventLogs(c, { indexer: opts.indexer, address: c.addresses.fPoolManager!, event: ev as never, args: { poolId }, fromBlock: opts.fromBlock });
1157
1165
  const l = logs[0];
1158
1166
  if (!l) throw new Error(`logswap: no FInitialize for F pool ${poolId}`);
1159
- const a = l.args as unknown as { quote: Address; creator: Address; bases: readonly Address[]; weights: readonly bigint[]; swapFee: bigint; policy: Address; cap: number | bigint };
1167
+ const a = l.args as unknown as { quote: Address; creator: Address; bases: readonly Address[]; weights: readonly bigint[]; swapFee: bigint; policy: Address; lnCapOverFloor?: number | bigint; cap?: number | bigint };
1160
1168
  const key: FPoolKey = {
1161
1169
  quote: a.quote,
1162
1170
  bases: [...a.bases],
@@ -1165,13 +1173,13 @@ export async function fPoolKeyOfPool(c: LogswapClient, poolId: Hex, opts: { salt
1165
1173
  policy: a.policy,
1166
1174
  creator: a.creator,
1167
1175
  salt: opts.salt ?? ZERO_SALT,
1168
- cap: Number(a.cap),
1176
+ lnCapOverFloor: Number(a.lnCapOverFloor ?? a.cap ?? 0), // `cap` before F version 27
1169
1177
  };
1170
1178
  if (fPoolIdOfKey(key).toLowerCase() !== poolId.toLowerCase() && opts.salt === undefined) {
1171
1179
  // a pad's salt carries its split (decisions 056): rebuild it from the policy's record with the default nonce
1172
1180
  try {
1173
- const [creatorShare, burnShare] = (await c.public.readContract({ address: a.policy, abi: padPolicyAbi, functionName: "splitOf", args: [poolId] } as never)) as readonly [bigint, bigint, bigint];
1174
- const padKey = { ...key, salt: padSalt({ creatorShare, burnShare }) };
1181
+ const [creatorShare] = (await c.public.readContract({ address: a.policy, abi: padPolicyAbi, functionName: "splitOf", args: [poolId] } as never)) as readonly [bigint, bigint];
1182
+ const padKey = { ...key, salt: padSalt({ creatorShare }) };
1175
1183
  if (fPoolIdOfKey(padKey).toLowerCase() === poolId.toLowerCase()) return padKey;
1176
1184
  } catch {
1177
1185
  /* not a pad: fall through to the error */
@@ -1202,24 +1210,61 @@ export function fPoolReserveAt(Lj: bigint, x: bigint, k = 0): bigint {
1202
1210
  * 0 on an uncapped pool. Throws when the seed would be priced at or above its cap (Q0/L0 ≥ d), which
1203
1211
  * the manager refuses.
1204
1212
  */
1205
- export function fPoolCapK(cap: number, Q0: bigint, L0: bigint): number {
1206
- if (cap === 0) return 0;
1207
- const e = Number((Q0 * WAD) / L0) / 1e18 - cap / 1e6;
1208
- if (e >= 0) throw new Error(`logswap: a seed with Q0/L0 = ${Number((Q0 * WAD) / L0) / 1e18} is at or above its cap d = ${cap / 1e6}`);
1213
+ export function fPoolCapK(lnCapOverFloor: number, Q0: bigint, L0: bigint): number {
1214
+ if (lnCapOverFloor === 0) return 0;
1215
+ const e = Number((Q0 * WAD) / L0) / 1e18 - lnCapOverFloor / 1e6;
1216
+ if (e >= 0) throw new Error(`logswap: a seed with Q0/L0 = ${Number((Q0 * WAD) / L0) / 1e18} is at or above its cap, ln(cap/floor) = ${lnCapOverFloor / 1e6}`);
1209
1217
  return Math.exp(e);
1210
1218
  }
1211
1219
 
1212
- /** The cap d (1e-6 log units) for a leverage λ > 1 at the floor: d = ln(λ/(λ − 1)) (decisions 061). λ = 2 puts the cap at twice the floor. */
1220
+ /**
1221
+ * The key's `lnCapOverFloor` = ln(cap/floor) (1e-6 log units) for a leverage λ at the floor: ln(λ/(λ − 1))
1222
+ * (decisions 061), so λ = 1/(1 − floor/cap) and the cap sits λ/(λ − 1) × the floor — λ = 2 puts it at twice.
1223
+ * **λ = 1 is uncapped**: cap ∞, `lnCapOverFloor` 0. A λ under 1, or one so close to
1224
+ * 1 (or so large) that d leaves a uint24 of 1e-6 units, is refused.
1225
+ */
1213
1226
  export function fPoolCapOfLeverage(lambda: number): number {
1214
- if (!(lambda > 1)) throw new Error("logswap: leverage must exceed 1 (1 is uncapped: cap 0)");
1227
+ if (lambda === 1) return 0;
1228
+ if (!(lambda > 1) || !Number.isFinite(lambda)) throw new Error(`logswap: leverage must be 1 (uncapped) or a finite number above 1, got ${lambda}`);
1215
1229
  const d = Math.round(Math.log(lambda / (lambda - 1)) * 1e6);
1216
1230
  if (d < 1 || d > 0xffffff) throw new Error(`logswap: leverage ${lambda} needs a cap outside a uint24 of 1e-6 units`);
1217
1231
  return d;
1218
1232
  }
1219
1233
 
1220
- /** The leverage at the floor λ = 1/(1 − e^{−d}) for a cap d in 1e-6 log units; Infinity is impossible, 1 is uncapped. */
1221
- export function fPoolLeverageOfCap(cap: number): number {
1222
- return cap === 0 ? 1 : 1 / (1 - Math.exp(-cap / 1e6));
1234
+ /** The leverage at the floor λ = 1/(1 − e^{−x}) for x = `lnCapOverFloor` (1e-6 log units); 1 for an uncapped pool (cap ∞). */
1235
+ export function fPoolLeverageOfCap(lnCapOverFloor: number): number {
1236
+ return lnCapOverFloor === 0 ? 1 : 1 / (1 - Math.exp(-lnCapOverFloor / 1e6));
1237
+ }
1238
+
1239
+ /** A pool's leverage, ready to tag: `capped` false and `capOverFloor` Infinity for an uncapped pool. */
1240
+ export interface FPoolLeverage {
1241
+ /** λ at the floor; exactly 1 when uncapped. */
1242
+ lambda: number;
1243
+ capped: boolean;
1244
+ /** The cap's price over the floor's, e^d = λ/(λ − 1): 2 at λ = 2; Infinity when uncapped. */
1245
+ capOverFloor: number;
1246
+ /** "1×" uncapped; otherwise λ to two decimals, trailing zeros dropped ("2×", "1.5×"), never "1×" for a capped pool ("1.01×" at least shown as "≈1×"). */
1247
+ label: string;
1248
+ }
1249
+
1250
+ /**
1251
+ * The leverage tag of a pool from its key's `lnCapOverFloor` (decisions 061): `1×` means no cap — the only way a tag
1252
+ * reads 1× — and a capped pool always reads above it, so the two can never be confused on screen.
1253
+ */
1254
+ export function fPoolLeverage(lnCapOverFloor: number): FPoolLeverage {
1255
+ if (lnCapOverFloor === 0) return { lambda: 1, capped: false, capOverFloor: Infinity, label: "1×" };
1256
+ const lambda = fPoolLeverageOfCap(lnCapOverFloor);
1257
+ const rounded = Math.round(lambda * 100) / 100;
1258
+ const label = rounded <= 1 ? "≈1×" : `${String(rounded)}×`;
1259
+ return { lambda, capped: true, capOverFloor: Math.exp(lnCapOverFloor / 1e6), label };
1260
+ }
1261
+
1262
+ /** The key's `lnCapOverFloor` from creation args: as given, else from `leverage`, else 0 (uncapped, cap ∞). Both given and disagreeing is refused. */
1263
+ export function fPoolCapOfArgs(a: { lnCapOverFloor?: number; leverage?: number }): number {
1264
+ const fromLeverage = a.leverage === undefined ? undefined : fPoolCapOfLeverage(a.leverage);
1265
+ if (a.lnCapOverFloor !== undefined && fromLeverage !== undefined && a.lnCapOverFloor !== fromLeverage)
1266
+ throw new Error(`logswap: lnCapOverFloor ${a.lnCapOverFloor} and leverage ${a.leverage} (${fromLeverage}) disagree — pass one`);
1267
+ return a.lnCapOverFloor ?? fromLeverage ?? 0;
1223
1268
  }
1224
1269
 
1225
1270
  /**
@@ -1236,7 +1281,7 @@ export async function fPoolSeed(
1236
1281
  let r0: bigint[];
1237
1282
  if ("r0" in a) r0 = a.r0;
1238
1283
  else {
1239
- const k = fPoolCapK(Number((await rawFPool(c, a.poolId)).cap), a.Q0, a.L0);
1284
+ const k = fPoolCapK(Number((await rawFPool(c, a.poolId)).lnCapOverFloor), a.Q0, a.L0);
1240
1285
  r0 = a.x0.map((x, j) => fPoolReserveAt((a.weights[j]! * a.L0) / 10n ** 18n, x, k));
1241
1286
  }
1242
1287
  return writeFPool(c, a.poolId, "seed", [a.L0, r0, a.Q0], a.account);
@@ -1272,7 +1317,7 @@ export async function fPoolRetire(c: LogswapClient, a: { poolId: Hex; account: A
1272
1317
  /**
1273
1318
  * The harvests that empty an unlocked pool's bid, in order — what a relaunch sends before its
1274
1319
  * `retire` (a retire needs an empty bid on every pool). Since F version 14 one
1275
- * `harvest(Q)` does it: the protocol's cut leaves the pool with the amount (with no collector
1320
+ * `harvest(Q)` does it: the protocol's cut leaves the pool with the amount (with no treasury
1276
1321
  * there is no cut). `[]` when the bid is already empty. Throws on a pad (F version 21: a pad
1277
1322
  * never retires, whatever its bid), and when the pool's policy keeps a floor on the bid
1278
1323
  * (`PolPolicy.keepQuote`): a `retire` under that policy waits for the market.
@@ -1300,11 +1345,11 @@ export async function fPoolEmptyingHarvests(c: LogswapClient, poolId: Hex): Prom
1300
1345
  * and redeems it with {@link fPoolRedeem} / rolls it with {@link fPoolRollIn} (`gen: g`); their
1301
1346
  * dividends on `g` stay claimable ({@link fPoolClaim} with `gen: g`). {@link fPoolClaimsOf}
1302
1347
  * lists them;
1303
- * - the pot, all in quote (decisions 057): `toPayee` to the payee, `protocolCut` to the collector,
1348
+ * - the pot, all in quote (decisions 057): `toPayee` to the payee, `protocolCut` to the treasury,
1304
1349
  * `burn` spent buying base at the curve and burning it (the price up, the floor held), and the
1305
1350
  * rest into the bid (the floor down). No share is minted; L does not move.
1306
1351
  *
1307
- * A pad's gate is `threshold(gen)` (`PadPolicy.NotGraduated` below it); POL and private carry the
1352
+ * A pad's gate is `threshold(poolId, gen)`, in its quote (`PadPolicy.NotGraduated` below it); POL and private carry the
1308
1353
  * authority and send it the whole pot; a public pool refuses (`Refused`). A carried holder with no
1309
1354
  * share is `BadTerms`. Whoever runs the pool.
1310
1355
  */
@@ -1340,7 +1385,7 @@ export async function fPoolRelaunch(
1340
1385
  ) {
1341
1386
  const [raw, harvests] = await Promise.all([rawFPool(c, a.poolId), fPoolEmptyingHarvests(c, a.poolId)]);
1342
1387
  const gen = Number(raw.gen);
1343
- const k = "r0" in a ? 0 : fPoolCapK(Number(raw.cap), a.Q0, a.L0);
1388
+ const k = "r0" in a ? 0 : fPoolCapK(Number(raw.lnCapOverFloor), a.Q0, a.L0);
1344
1389
  const r0 = "r0" in a ? a.r0 : a.x0.map((x, j) => (a.weights[j]! === 0n ? 0n : fPoolReserveAt((a.weights[j]! * a.L0) / WAD, x, k)));
1345
1390
  const enc = (functionName: string, args: unknown[]) =>
1346
1391
  encodeFunctionData({ abi: fPoolManagerAbi, functionName, args } as never) as Hex;
@@ -1432,12 +1477,12 @@ export function fPoolLaunchCalls(
1432
1477
  ): Hex[] {
1433
1478
  const sum = a.weights.reduce((x, y) => x + y, 0n);
1434
1479
  if (sum !== WAD) throw new Error(`logswap: weights sum to ${sum}, expected 1e18`);
1435
- const k = "r0" in a ? 0 : fPoolCapK(a.cap ?? 0, a.Q0, a.L0);
1480
+ const k = "r0" in a ? 0 : fPoolCapK(fPoolCapOfArgs(a), a.Q0, a.L0);
1436
1481
  const given = "r0" in a ? a.r0 : a.x0.map((x, j) => fPoolReserveAt((a.weights[j]! * a.L0) / WAD, x, k));
1437
1482
  if (given.length !== a.bases.length) throw new Error(`logswap: ${given.length} reserves for ${a.bases.length} legs`);
1438
1483
  const { bases, companions } = sortFPoolLegs(a.bases, a.weights, given);
1439
1484
  if (!a.policy) throw new Error("logswap: fPoolLaunchCalls needs the key's policy — fPoolLaunch fills it from the deployment");
1440
- const key = { quote: a.quote, bases, weights: companions[0]!, swapFee: a.swapFee, policy: a.policy, creator: a.creator, salt: a.salt ?? ZERO_SALT, cap: a.cap ?? 0 };
1485
+ const key = { quote: a.quote, bases, weights: companions[0]!, swapFee: a.swapFee, policy: a.policy, creator: a.creator, salt: a.salt ?? ZERO_SALT, lnCapOverFloor: fPoolCapOfArgs(a) };
1441
1486
  const enc = (functionName: string, args: unknown[]) =>
1442
1487
  encodeFunctionData({ abi: fPoolManagerAbi, functionName, args } as never) as Hex;
1443
1488
  const calls: Hex[] = [];
@@ -1803,8 +1848,8 @@ export interface DiscoveredFPool {
1803
1848
  bases: Address[];
1804
1849
  weights: bigint[];
1805
1850
  swapFee: bigint;
1806
- /** The common cap, d in 1e-6 log units; 0 uncapped (decisions 061). */
1807
- cap: number;
1851
+ /** The common cap as ln(cap price / floor price), in 1e-6 log units; 0 uncapped, cap ∞ (decisions 061). */
1852
+ lnCapOverFloor: number;
1808
1853
  kind: FPoolKind;
1809
1854
  /** The pool's policy AT BIRTH — the contract the key names. The LIVE one is {@link getFPool}'s `policy`: the registry's owner may have moved the pool (046, `PolicyMigrated`). */
1810
1855
  policy: Address;
@@ -1828,7 +1873,7 @@ export interface DiscoveredFPool {
1828
1873
  /**
1829
1874
  * Every F pool ever created, from `Initialize` logs alone — plus one `kind()` read per distinct
1830
1875
  * policy, cached. Possible only because the event carries the full key preimage (bases,
1831
- * weights, swapFee, policy, cap) — the poolId is a hash and could never be inverted; contracts PR #17
1876
+ * weights, swapFee, policy, lnCapOverFloor) — the poolId is a hash and could never be inverted; contracts PR #17
1832
1877
  * exists for exactly this call. With `indexer`, the logs come from it (the node when it fails):
1833
1878
  * a scan from block 0 per call is what made the first load wait on the node.
1834
1879
  */
@@ -1855,7 +1900,7 @@ export async function discoverFPools(
1855
1900
  logs.map(async (l) => {
1856
1901
  const a = l.args as {
1857
1902
  poolId: Hex; quote: Address; creator: Address; bases: readonly Address[];
1858
- weights: readonly bigint[]; swapFee: bigint; policy: Address; cap: number;
1903
+ weights: readonly bigint[]; swapFee: bigint; policy: Address; lnCapOverFloor?: number; cap?: number;
1859
1904
  };
1860
1905
  const q0 = q0Of.get(a.poolId.toLowerCase());
1861
1906
  const kind = await fPoolKindOfPolicy(c, a.policy); // one read per distinct policy, cached
@@ -1865,7 +1910,7 @@ export async function discoverFPools(
1865
1910
  bases: [...a.bases],
1866
1911
  weights: [...a.weights],
1867
1912
  swapFee: a.swapFee,
1868
- cap: Number(a.cap ?? 0),
1913
+ lnCapOverFloor: Number(a.lnCapOverFloor ?? a.cap ?? 0), // `cap` before F version 27
1869
1914
  kind,
1870
1915
  policy: a.policy,
1871
1916
  creator: a.creator,
@@ -1901,46 +1946,46 @@ export type FPoolRules =
1901
1946
  gen: number;
1902
1947
  /** @deprecated Equal to {@link gen} since F version 21, kept for callers; read `gen`. */
1903
1948
  rung: number;
1904
- /** The pot the next graduation needs: `PadPolicy.threshold(gen)`. */
1949
+ /** The pot the next graduation needs, in the pad's quote: `PadPolicy.threshold(poolId, gen)` (T(0) is per quote, decision 070). */
1905
1950
  threshold: bigint;
1906
1951
  pot: bigint;
1907
- /** This pad's split, recorded at its birth from the key's salt and never changed (decisions 056, 057): the creator's cut of the pot, the protocol's, and the burn — all fractions of the pot. The rest deepens the bid. */
1952
+ /** This pad's split, recorded at its birth from the key's salt and never changed (decisions 056, 057, 067): the creator's cut of the pot and the protocol's, fractions of the pot. The rest deepens the bid; nothing is bought or burnt (067). */
1908
1953
  creatorShare: bigint;
1909
1954
  protocolShare: bigint;
1910
- burnShare: bigint;
1911
- /** The ranges the collector's policy holds NOW for the pads born after this read: what a new creator may choose. */
1955
+ /** The ranges the LOG Authority set on the policy NOW for the pads born after this read: what a new creator may choose. */
1912
1956
  ranges: PadRanges;
1913
1957
  }
1914
1958
  | { kind: "pol"; keepQuote: bigint; minQuoteAtSeed: bigint }
1915
1959
  | { kind: "public"; curated: boolean }
1916
1960
  | { kind: "private"; gateMint: boolean; gateSwap: boolean; operator: Address };
1917
1961
 
1918
- /** The collector's ranges on a pad's creator choices (`PadPolicy`, decisions 056, 057), WAD fractions of the pot. */
1962
+ /** The LOG Authority's range on a pad's creator choice (`PadPolicy`, decisions 056, 067), WAD fractions of the pot. */
1919
1963
  export interface PadRanges {
1920
1964
  /** The protocol's share of the pot, recorded into each pad at birth. */
1921
1965
  protocolShare: bigint;
1922
1966
  creatorShareMin: bigint;
1967
+ /** At most the F manager's `PAYEE_CUT_MAX` (50%), which bounds a pad's payee whatever the policy (067). */
1923
1968
  creatorShareMax: bigint;
1924
- burnMin: bigint;
1925
- burnMax: bigint;
1926
1969
  }
1927
1970
 
1928
1971
  const MAX_U64 = (1n << 64n) - 1n;
1929
1972
  const MAX_U128 = (1n << 128n) - 1n;
1930
1973
 
1931
1974
  /**
1932
- * The salt that carries a pad's split (`PadPolicy.pack`, decisions 056, 057): `creatorShare` in the top 64 bits, `burnShare` in
1933
- * the next 64 (WAD fractions of the pot), a nonce in the low 128 that tells two otherwise identical pads apart. It is part of the pool's
1934
- * identity, so it is chosen at launch (`FPoolLaunchArgs.salt`) and never changes; the policy refuses a split outside its ranges.
1975
+ * The salt that carries a pad's split (`PadPolicy.pack(creatorShare, nonce)`, decisions 056, 067): `creatorShare` in the top 64
1976
+ * bits (a WAD fraction of the pot), zero in the next 64 — the burn share's place before F version 28, refused by the policy if
1977
+ * set — and a nonce in the low 128 that tells two otherwise identical pads apart. It is part of the pool's identity, so it is
1978
+ * chosen at launch (`FPoolLaunchArgs.salt`) and never changes; the policy refuses a cut outside its range.
1935
1979
  */
1936
- export function padSalt(s: { creatorShare: bigint; burnShare: bigint; nonce?: bigint }): Hex {
1980
+ export function padSalt(s: { creatorShare: bigint; nonce?: bigint; /** @deprecated F version 28 has no burn (067): only 0 is accepted. */ burnShare?: bigint }): Hex {
1937
1981
  const nonce = s.nonce ?? 0n;
1938
- if (s.creatorShare < 0n || s.creatorShare > MAX_U64 || s.burnShare < 0n || s.burnShare > MAX_U64) throw new Error("logswap: a pad's split is a WAD fraction that fits 64 bits");
1982
+ if (s.burnShare !== undefined && s.burnShare !== 0n) throw new Error("logswap: a pad has no burn share since F version 28 (decision 067)");
1983
+ if (s.creatorShare < 0n || s.creatorShare > MAX_U64) throw new Error("logswap: a pad's creator share is a WAD fraction that fits 64 bits");
1939
1984
  if (nonce < 0n || nonce > MAX_U128) throw new Error("logswap: a pad salt's nonce fits 128 bits");
1940
- return `0x${((s.creatorShare << 192n) | (s.burnShare << 128n) | nonce).toString(16).padStart(64, "0")}` as Hex;
1985
+ return `0x${((s.creatorShare << 192n) | nonce).toString(16).padStart(64, "0")}` as Hex;
1941
1986
  }
1942
1987
 
1943
- /** The inverse of {@link padSalt}. */
1988
+ /** The inverse of {@link padSalt}. `burnShare` is the old field's bits, nonzero only on a salt the F version 28 policy refuses. */
1944
1989
  export function padSplitOfSalt(salt: Hex): { creatorShare: bigint; burnShare: bigint; nonce: bigint } {
1945
1990
  const v = BigInt(salt);
1946
1991
  return { creatorShare: v >> 192n, burnShare: (v >> 128n) & MAX_U64, nonce: v & MAX_U128 };
@@ -1954,19 +1999,17 @@ export async function getFPoolRules(c: LogswapClient, s: Pick<FPoolState, "poolI
1954
1999
  case "pad": {
1955
2000
  // the rung is the manager's generation (F version 21): a move carries it, nothing to ask the policy
1956
2001
  const gen = s.gen;
1957
- const [split, pot, threshold, protocolNow, creatorShareMin, creatorShareMax, burnMin, burnMax] = await Promise.all([
1958
- rd<readonly [bigint, bigint, bigint]>(padPolicyAbi, "splitOf", [s.poolId]),
2002
+ const [split, pot, threshold, protocolNow, creatorShareMin, creatorShareMax] = await Promise.all([
2003
+ rd<readonly [bigint, bigint]>(padPolicyAbi, "splitOf", [s.poolId]),
1959
2004
  fPoolDividendOf(c, s.poolId, DEAD_ADDRESS, gen),
1960
- rd<bigint>(padPolicyAbi, "threshold", [BigInt(gen)]),
2005
+ rd<bigint>(padPolicyAbi, "threshold", [s.poolId, BigInt(gen)]),
1961
2006
  rd<bigint>(padPolicyAbi, "protocolShare"),
1962
2007
  rd<bigint>(padPolicyAbi, "creatorShareMin"),
1963
2008
  rd<bigint>(padPolicyAbi, "creatorShareMax"),
1964
- rd<bigint>(padPolicyAbi, "burnMin"),
1965
- rd<bigint>(padPolicyAbi, "burnMax"),
1966
2009
  ]);
1967
- const [creatorShare, burnShare, protocolShare] = split;
1968
- const ranges: PadRanges = { protocolShare: protocolNow, creatorShareMin, creatorShareMax, burnMin, burnMax };
1969
- return { kind: "pad", gen, rung: gen, threshold, pot, creatorShare, protocolShare, burnShare, ranges };
2010
+ const [creatorShare, protocolShare] = split;
2011
+ const ranges: PadRanges = { protocolShare: protocolNow, creatorShareMin, creatorShareMax };
2012
+ return { kind: "pad", gen, rung: gen, threshold, pot, creatorShare, protocolShare, ranges };
1970
2013
  }
1971
2014
  case "pol": {
1972
2015
  const [keepQuote, minQuoteAtSeed] = await Promise.all([rd<bigint>(polPolicyAbi, "keepQuote"), rd<bigint>(polPolicyAbi, "minQuoteAtSeed")]);
@@ -2016,7 +2059,7 @@ export async function fPoolsControlledBy(
2016
2059
  /**
2017
2060
  * Every holder of a pool's shares, from the ERC-6909 Transfer stream — the id is an indexed
2018
2061
  * topic, so one filtered log query aggregates the whole ledger. The 0xdead entry is the
2019
- * MIN_SHARES lock from seeding; callers usually label rather than hide it. Pass `indexer`: a node
2062
+ * MIN_SHARES lock from seeding; callers usually label rather than hide it. Pass `indexer`: the indexer's `pool_holder` view answers directly (the Transfer replay is the fallback) — a node
2020
2063
  * prunes its logs (anvil keeps about a day), and a ledger rebuilt from a pruned stream loses every
2021
2064
  * holder minted before the cut.
2022
2065
  */
@@ -2028,7 +2071,20 @@ export async function fPoolShareHolders(
2028
2071
  type Ev = Extract<(typeof fPoolManagerAbi)[number], { type: "event"; name: "Transfer" }>;
2029
2072
  const ev = fPoolManagerAbi.find((x): x is Ev => x.type === "event" && x.name === "Transfer");
2030
2073
  if (!ev) throw new Error("logswap: 6909 Transfer event missing from the generated ABI");
2031
- const id = fPoolShareId(poolIdHex, opts.gen ?? (await fPoolGen(c, poolIdHex)));
2074
+ const gen = opts.gen ?? (await fPoolGen(c, poolIdHex));
2075
+ const indexer = opts.indexer ?? c.indexerUrl;
2076
+ if (indexer) {
2077
+ try {
2078
+ const rows = await poolHolders(indexer, { condition: { side: "F", poolId: poolIdHex.toLowerCase(), gen: Number(gen), manager: c.addresses.fPoolManager!.toLowerCase() } });
2079
+ return rows
2080
+ .filter((r) => r.balance > 0n)
2081
+ .map((r) => ({ holder: r.holder, shares: r.balance }))
2082
+ .sort((a, b) => (b.shares > a.shares ? 1 : -1));
2083
+ } catch {
2084
+ /* the view is a cache: the Transfer replay below answers the same */
2085
+ }
2086
+ }
2087
+ const id = fPoolShareId(poolIdHex, gen);
2032
2088
  const { logs } = await getEventLogs(c, { indexer: opts.indexer, address: c.addresses.fPoolManager!, event: ev, args: { id }, fromBlock: opts.fromBlock });
2033
2089
  const bal = new Map<string, bigint>();
2034
2090
  for (const l of logs) {
@@ -2043,18 +2099,18 @@ export async function fPoolShareHolders(
2043
2099
  }
2044
2100
 
2045
2101
  /**
2046
- * What a policy answers `onRegenerate` with: `IFPolicy.Terms`. `carried` is F version 21's fifth word — the holder the next generation
2047
- * carries beside the dead shares; the reply is exactly 160 bytes.
2102
+ * What a policy answers `onRegenerate` with: `IFPolicy.Terms` — four words, exactly 128 bytes since F version 28 (decision 067
2103
+ * removed `burn`). `carried` is the holder the next generation carries beside the dead shares; on a pad the manager requires
2104
+ * `0xdead` and caps `toPayee` at `PAYEE_CUT_MAX` of the pot.
2048
2105
  */
2049
2106
  export interface FPolicyTerms {
2050
2107
  payee: Address;
2051
2108
  toPayee: bigint;
2052
2109
  protocolCut: bigint;
2053
- burn: bigint;
2054
2110
  carried: Address;
2055
2111
  }
2056
2112
 
2057
- /** Every F pool's raw state in one lens call (the manager's `Pool` struct: quote, swapFee, policy, seeded, gen, n, restructureBlock, asks, cap, Q, bigSigma, L, shares, refLogFloor). */
2113
+ /** Every F pool's raw state in one lens call (the manager's `Pool` struct: quote, swapFee, policy, seeded, gen, n, restructureTime, asks, lnCapOverFloor, Q, bigSigma, L, shares, refLogFloor). */
2058
2114
  export async function getFPoolsRaw(c: LogswapClient, poolIds: Hex[]): Promise<readonly unknown[]> {
2059
2115
  const { logswapLensAbi } = await import("./generated.js");
2060
2116
  return (await c.public.readContract({ address: c.addresses.lens, abi: logswapLensAbi, functionName: "getFPools", args: [poolIds] } as never)) as readonly unknown[];