@logswap/sdk 0.5.76 → 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/README.md +59 -1
- package/dist/admin.d.ts +116 -39
- package/dist/admin.js +151 -59
- package/dist/chains.d.ts +155 -0
- package/dist/chains.js +186 -0
- package/dist/client.d.ts +36 -5
- package/dist/client.js +47 -5
- package/dist/fees.js +3 -2
- package/dist/fpool.d.ts +71 -45
- package/dist/fpool.js +77 -47
- package/dist/generated.d.ts +321 -271
- package/dist/generated.js +282 -243
- package/dist/history.d.ts +10 -1
- package/dist/history.js +35 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/indexer.d.ts +1 -1
- package/dist/indexer.js +1 -1
- package/dist/pools.d.ts +5 -3
- package/dist/pools.js +5 -4
- package/dist/positions.d.ts +1 -1
- package/dist/positions.js +4 -3
- package/dist/tokencache.d.ts +2 -10
- package/dist/tokencache.js +14 -6
- package/dist/views.d.ts +1 -1
- package/dist/write.d.ts +2 -1
- package/dist/write.js +4 -3
- package/package.json +1 -1
- package/src/admin.ts +182 -73
- package/src/chains.ts +259 -0
- package/src/client.ts +71 -8
- package/src/fees.ts +3 -2
- package/src/fpool.ts +116 -73
- package/src/generated.ts +282 -243
- package/src/history.ts +40 -12
- package/src/index.ts +1 -0
- package/src/indexer.ts +1 -1
- package/src/pools.ts +7 -5
- package/src/positions.ts +5 -4
- package/src/tokencache.ts +15 -6
- package/src/views.ts +1 -1
- package/src/write.ts +4 -3
package/src/fpool.ts
CHANGED
|
@@ -47,7 +47,7 @@ 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
|
|
50
|
+
import { clientChainId, type LogswapClient } from "./client.js";
|
|
51
51
|
import { fallBackUnlessRejected, signerFor } from "./write.js";
|
|
52
52
|
import { getEventLogs } from "./history.js";
|
|
53
53
|
import { poolHolders } from "./views.js";
|
|
@@ -87,10 +87,11 @@ export interface FPoolState {
|
|
|
87
87
|
/** This generation's income still in the pool, quote: L·(θ₀ − θ). */
|
|
88
88
|
unharvested: bigint;
|
|
89
89
|
/**
|
|
90
|
-
* The common cap (decisions 061):
|
|
91
|
-
* 0 is uncapped. Leverage at the floor is 1/(1 − e^{−
|
|
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}.
|
|
92
93
|
*/
|
|
93
|
-
|
|
94
|
+
lnCapOverFloor: number;
|
|
94
95
|
/** Σ = ∫ Σ_j w_j (dx_j)², accrued even though the fee is fixed — the kernel stays calculable. */
|
|
95
96
|
bigSigma: bigint;
|
|
96
97
|
/**
|
|
@@ -103,8 +104,8 @@ export interface FPoolState {
|
|
|
103
104
|
asks: number;
|
|
104
105
|
/** The sponsor's own label for this pool — the policy's word, mutable and never identity. "" when unnamed. */
|
|
105
106
|
name: string;
|
|
106
|
-
/** The
|
|
107
|
-
|
|
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;
|
|
108
109
|
/** What the pool is — its POLICY's self-description; see {@link FPoolKind}. */
|
|
109
110
|
kind: FPoolKind;
|
|
110
111
|
/** False before the first seed — and between a retire and the next one. */
|
|
@@ -172,12 +173,12 @@ export async function getFPool(c: LogswapClient, poolId: Hex): Promise<FPoolStat
|
|
|
172
173
|
logFloor: v.logFloor,
|
|
173
174
|
compositeX: v.compositeX,
|
|
174
175
|
unharvested: v.unharvested,
|
|
175
|
-
|
|
176
|
+
lnCapOverFloor: Number(p.lnCapOverFloor),
|
|
176
177
|
bigSigma: p.bigSigma,
|
|
177
178
|
policy: p.policy,
|
|
178
179
|
asks: Number(p.asks),
|
|
179
180
|
name,
|
|
180
|
-
|
|
181
|
+
restructureTime: Number(p.restructureTime),
|
|
181
182
|
kind,
|
|
182
183
|
seeded: p.seeded,
|
|
183
184
|
gen: Number(p.gen),
|
|
@@ -191,7 +192,7 @@ export async function getFPool(c: LogswapClient, poolId: Hex): Promise<FPoolStat
|
|
|
191
192
|
/**
|
|
192
193
|
* The kind of pool — the POLICY's self-description (the manager knows no kind; the
|
|
193
194
|
* policy the key names is the kind, and what it promises is its code). `public`: the protocol's
|
|
194
|
-
* own pool — its policy requires the
|
|
195
|
+
* own pool — its policy requires the LOG Authority as creator; no gate, no operator. `private`, the
|
|
195
196
|
* desk: the one whose policy may gate swaps and mints, with an operator that reshapes the legs
|
|
196
197
|
* between generations. `pol`, a treasury's pool, one base: composition and access fixed at
|
|
197
198
|
* birth. `pad`, a launch, one base: its policy answers income only at every harvest and declared
|
|
@@ -210,8 +211,9 @@ const kindCache = new Map<string, Promise<FPoolKind>>();
|
|
|
210
211
|
* chain: a pool's kind is its policy's word, never the manager's. An unknown tag
|
|
211
212
|
* (a policy that is not one of the protocol's four shapes) throws; the app has no screen for it.
|
|
212
213
|
*/
|
|
213
|
-
export function fPoolKindOfPolicy(c: LogswapClient, policy: Address): Promise<FPoolKind> {
|
|
214
|
-
|
|
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()}`;
|
|
215
217
|
let hit = kindCache.get(key);
|
|
216
218
|
if (!hit) {
|
|
217
219
|
hit = (c.public.readContract({ address: policy, abi: fPolicyBaseAbi, functionName: "kind" } as never) as Promise<Hex>).then((word) => {
|
|
@@ -344,7 +346,7 @@ export function fPoolPriceOf(x: bigint, scale = 1): number {
|
|
|
344
346
|
export function fPoolShareValue(s: FPoolState): bigint {
|
|
345
347
|
if (s.totalSupply === 0n) return 0n;
|
|
346
348
|
let v = s.L + s.Q;
|
|
347
|
-
if (s.
|
|
349
|
+
if (s.lnCapOverFloor !== 0) {
|
|
348
350
|
v = s.Q;
|
|
349
351
|
for (let j = 0; j < s.reserves.length; j++) {
|
|
350
352
|
const rv = s.virtualReserves[j]!;
|
|
@@ -1008,10 +1010,15 @@ export interface FPoolCreateArgs {
|
|
|
1008
1010
|
*/
|
|
1009
1011
|
mintFee?: bigint;
|
|
1010
1012
|
/**
|
|
1011
|
-
* The common cap (decisions 061):
|
|
1012
|
-
* 0 or absent is uncapped.
|
|
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.
|
|
1013
1015
|
*/
|
|
1014
|
-
|
|
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;
|
|
1015
1022
|
/**
|
|
1016
1023
|
* Which of the protocol's four the pool is — see {@link FPoolKind}. NOT a key field (decisions
|
|
1017
1024
|
* 049: the manager knows no kind): it selects the deployment's policy of that kind when `policy`
|
|
@@ -1076,8 +1083,8 @@ export interface FPoolKey {
|
|
|
1076
1083
|
policy: Address;
|
|
1077
1084
|
creator: Address;
|
|
1078
1085
|
salt: Hex;
|
|
1079
|
-
/** The common cap,
|
|
1080
|
-
|
|
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;
|
|
1081
1088
|
}
|
|
1082
1089
|
|
|
1083
1090
|
const F_POOL_KEY_ABI = [
|
|
@@ -1091,7 +1098,7 @@ const F_POOL_KEY_ABI = [
|
|
|
1091
1098
|
{ name: "policy", type: "address" },
|
|
1092
1099
|
{ name: "creator", type: "address" },
|
|
1093
1100
|
{ name: "salt", type: "bytes32" },
|
|
1094
|
-
{ name: "
|
|
1101
|
+
{ name: "lnCapOverFloor", type: "uint24" },
|
|
1095
1102
|
],
|
|
1096
1103
|
},
|
|
1097
1104
|
] as const;
|
|
@@ -1116,7 +1123,7 @@ export function fPoolKeyOf(c: LogswapClient, a: Omit<FPoolCreateArgs, "account">
|
|
|
1116
1123
|
policy: a.policy ?? fPoolPolicyFor(c, a.kind),
|
|
1117
1124
|
creator: a.creator,
|
|
1118
1125
|
salt: a.salt ?? ZERO_SALT,
|
|
1119
|
-
|
|
1126
|
+
lnCapOverFloor: fPoolCapOfArgs(a),
|
|
1120
1127
|
};
|
|
1121
1128
|
}
|
|
1122
1129
|
|
|
@@ -1154,10 +1161,10 @@ export async function fPoolIdOf(c: LogswapClient, a: Omit<FPoolCreateArgs, "acco
|
|
|
1154
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> {
|
|
1155
1162
|
type Ev = Extract<(typeof fPoolManagerAbi)[number], { type: "event"; name: "FInitialize" }>;
|
|
1156
1163
|
const ev = fPoolManagerAbi.find((x): x is Ev => x.type === "event" && x.name === "FInitialize")!;
|
|
1157
|
-
const { logs } = await getEventLogs(c, { indexer: opts.indexer, address: c.addresses.fPoolManager!, event: ev as never, args: { poolId }, fromBlock: opts.fromBlock
|
|
1164
|
+
const { logs } = await getEventLogs(c, { indexer: opts.indexer, address: c.addresses.fPoolManager!, event: ev as never, args: { poolId }, fromBlock: opts.fromBlock });
|
|
1158
1165
|
const l = logs[0];
|
|
1159
1166
|
if (!l) throw new Error(`logswap: no FInitialize for F pool ${poolId}`);
|
|
1160
|
-
const a = l.args as unknown as { quote: Address; creator: Address; bases: readonly Address[]; weights: readonly bigint[]; swapFee: bigint; policy: Address; cap
|
|
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 };
|
|
1161
1168
|
const key: FPoolKey = {
|
|
1162
1169
|
quote: a.quote,
|
|
1163
1170
|
bases: [...a.bases],
|
|
@@ -1166,13 +1173,13 @@ export async function fPoolKeyOfPool(c: LogswapClient, poolId: Hex, opts: { salt
|
|
|
1166
1173
|
policy: a.policy,
|
|
1167
1174
|
creator: a.creator,
|
|
1168
1175
|
salt: opts.salt ?? ZERO_SALT,
|
|
1169
|
-
|
|
1176
|
+
lnCapOverFloor: Number(a.lnCapOverFloor ?? a.cap ?? 0), // `cap` before F version 27
|
|
1170
1177
|
};
|
|
1171
1178
|
if (fPoolIdOfKey(key).toLowerCase() !== poolId.toLowerCase() && opts.salt === undefined) {
|
|
1172
1179
|
// a pad's salt carries its split (decisions 056): rebuild it from the policy's record with the default nonce
|
|
1173
1180
|
try {
|
|
1174
|
-
const [creatorShare
|
|
1175
|
-
const padKey = { ...key, salt: padSalt({ creatorShare
|
|
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 }) };
|
|
1176
1183
|
if (fPoolIdOfKey(padKey).toLowerCase() === poolId.toLowerCase()) return padKey;
|
|
1177
1184
|
} catch {
|
|
1178
1185
|
/* not a pad: fall through to the error */
|
|
@@ -1203,24 +1210,61 @@ export function fPoolReserveAt(Lj: bigint, x: bigint, k = 0): bigint {
|
|
|
1203
1210
|
* 0 on an uncapped pool. Throws when the seed would be priced at or above its cap (Q0/L0 ≥ d), which
|
|
1204
1211
|
* the manager refuses.
|
|
1205
1212
|
*/
|
|
1206
|
-
export function fPoolCapK(
|
|
1207
|
-
if (
|
|
1208
|
-
const e = Number((Q0 * WAD) / L0) / 1e18 -
|
|
1209
|
-
if (e >= 0) throw new Error(`logswap: a seed with Q0/L0 = ${Number((Q0 * WAD) / L0) / 1e18} is at or above its cap
|
|
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}`);
|
|
1210
1217
|
return Math.exp(e);
|
|
1211
1218
|
}
|
|
1212
1219
|
|
|
1213
|
-
/**
|
|
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
|
+
*/
|
|
1214
1226
|
export function fPoolCapOfLeverage(lambda: number): number {
|
|
1215
|
-
if (
|
|
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}`);
|
|
1216
1229
|
const d = Math.round(Math.log(lambda / (lambda - 1)) * 1e6);
|
|
1217
1230
|
if (d < 1 || d > 0xffffff) throw new Error(`logswap: leverage ${lambda} needs a cap outside a uint24 of 1e-6 units`);
|
|
1218
1231
|
return d;
|
|
1219
1232
|
}
|
|
1220
1233
|
|
|
1221
|
-
/** The leverage at the floor λ = 1/(1 − e^{−
|
|
1222
|
-
export function fPoolLeverageOfCap(
|
|
1223
|
-
return
|
|
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;
|
|
1224
1268
|
}
|
|
1225
1269
|
|
|
1226
1270
|
/**
|
|
@@ -1237,7 +1281,7 @@ export async function fPoolSeed(
|
|
|
1237
1281
|
let r0: bigint[];
|
|
1238
1282
|
if ("r0" in a) r0 = a.r0;
|
|
1239
1283
|
else {
|
|
1240
|
-
const k = fPoolCapK(Number((await rawFPool(c, a.poolId)).
|
|
1284
|
+
const k = fPoolCapK(Number((await rawFPool(c, a.poolId)).lnCapOverFloor), a.Q0, a.L0);
|
|
1241
1285
|
r0 = a.x0.map((x, j) => fPoolReserveAt((a.weights[j]! * a.L0) / 10n ** 18n, x, k));
|
|
1242
1286
|
}
|
|
1243
1287
|
return writeFPool(c, a.poolId, "seed", [a.L0, r0, a.Q0], a.account);
|
|
@@ -1273,7 +1317,7 @@ export async function fPoolRetire(c: LogswapClient, a: { poolId: Hex; account: A
|
|
|
1273
1317
|
/**
|
|
1274
1318
|
* The harvests that empty an unlocked pool's bid, in order — what a relaunch sends before its
|
|
1275
1319
|
* `retire` (a retire needs an empty bid on every pool). Since F version 14 one
|
|
1276
|
-
* `harvest(Q)` does it: the protocol's cut leaves the pool with the amount (with no
|
|
1320
|
+
* `harvest(Q)` does it: the protocol's cut leaves the pool with the amount (with no treasury
|
|
1277
1321
|
* there is no cut). `[]` when the bid is already empty. Throws on a pad (F version 21: a pad
|
|
1278
1322
|
* never retires, whatever its bid), and when the pool's policy keeps a floor on the bid
|
|
1279
1323
|
* (`PolPolicy.keepQuote`): a `retire` under that policy waits for the market.
|
|
@@ -1301,11 +1345,11 @@ export async function fPoolEmptyingHarvests(c: LogswapClient, poolId: Hex): Prom
|
|
|
1301
1345
|
* and redeems it with {@link fPoolRedeem} / rolls it with {@link fPoolRollIn} (`gen: g`); their
|
|
1302
1346
|
* dividends on `g` stay claimable ({@link fPoolClaim} with `gen: g`). {@link fPoolClaimsOf}
|
|
1303
1347
|
* lists them;
|
|
1304
|
-
* - the pot, all in quote (decisions 057): `toPayee` to the payee, `protocolCut` to the
|
|
1348
|
+
* - the pot, all in quote (decisions 057): `toPayee` to the payee, `protocolCut` to the treasury,
|
|
1305
1349
|
* `burn` spent buying base at the curve and burning it (the price up, the floor held), and the
|
|
1306
1350
|
* rest into the bid (the floor down). No share is minted; L does not move.
|
|
1307
1351
|
*
|
|
1308
|
-
* A pad's gate is `threshold(gen)
|
|
1352
|
+
* A pad's gate is `threshold(poolId, gen)`, in its quote (`PadPolicy.NotGraduated` below it); POL and private carry the
|
|
1309
1353
|
* authority and send it the whole pot; a public pool refuses (`Refused`). A carried holder with no
|
|
1310
1354
|
* share is `BadTerms`. Whoever runs the pool.
|
|
1311
1355
|
*/
|
|
@@ -1341,7 +1385,7 @@ export async function fPoolRelaunch(
|
|
|
1341
1385
|
) {
|
|
1342
1386
|
const [raw, harvests] = await Promise.all([rawFPool(c, a.poolId), fPoolEmptyingHarvests(c, a.poolId)]);
|
|
1343
1387
|
const gen = Number(raw.gen);
|
|
1344
|
-
const k = "r0" in a ? 0 : fPoolCapK(Number(raw.
|
|
1388
|
+
const k = "r0" in a ? 0 : fPoolCapK(Number(raw.lnCapOverFloor), a.Q0, a.L0);
|
|
1345
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)));
|
|
1346
1390
|
const enc = (functionName: string, args: unknown[]) =>
|
|
1347
1391
|
encodeFunctionData({ abi: fPoolManagerAbi, functionName, args } as never) as Hex;
|
|
@@ -1433,12 +1477,12 @@ export function fPoolLaunchCalls(
|
|
|
1433
1477
|
): Hex[] {
|
|
1434
1478
|
const sum = a.weights.reduce((x, y) => x + y, 0n);
|
|
1435
1479
|
if (sum !== WAD) throw new Error(`logswap: weights sum to ${sum}, expected 1e18`);
|
|
1436
|
-
const k = "r0" in a ? 0 : fPoolCapK(a
|
|
1480
|
+
const k = "r0" in a ? 0 : fPoolCapK(fPoolCapOfArgs(a), a.Q0, a.L0);
|
|
1437
1481
|
const given = "r0" in a ? a.r0 : a.x0.map((x, j) => fPoolReserveAt((a.weights[j]! * a.L0) / WAD, x, k));
|
|
1438
1482
|
if (given.length !== a.bases.length) throw new Error(`logswap: ${given.length} reserves for ${a.bases.length} legs`);
|
|
1439
1483
|
const { bases, companions } = sortFPoolLegs(a.bases, a.weights, given);
|
|
1440
1484
|
if (!a.policy) throw new Error("logswap: fPoolLaunchCalls needs the key's policy — fPoolLaunch fills it from the deployment");
|
|
1441
|
-
const key = { quote: a.quote, bases, weights: companions[0]!, swapFee: a.swapFee, policy: a.policy, creator: a.creator, salt: a.salt ?? ZERO_SALT,
|
|
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) };
|
|
1442
1486
|
const enc = (functionName: string, args: unknown[]) =>
|
|
1443
1487
|
encodeFunctionData({ abi: fPoolManagerAbi, functionName, args } as never) as Hex;
|
|
1444
1488
|
const calls: Hex[] = [];
|
|
@@ -1804,8 +1848,8 @@ export interface DiscoveredFPool {
|
|
|
1804
1848
|
bases: Address[];
|
|
1805
1849
|
weights: bigint[];
|
|
1806
1850
|
swapFee: bigint;
|
|
1807
|
-
/** The common cap,
|
|
1808
|
-
|
|
1851
|
+
/** The common cap as ln(cap price / floor price), in 1e-6 log units; 0 uncapped, cap ∞ (decisions 061). */
|
|
1852
|
+
lnCapOverFloor: number;
|
|
1809
1853
|
kind: FPoolKind;
|
|
1810
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`). */
|
|
1811
1855
|
policy: Address;
|
|
@@ -1829,7 +1873,7 @@ export interface DiscoveredFPool {
|
|
|
1829
1873
|
/**
|
|
1830
1874
|
* Every F pool ever created, from `Initialize` logs alone — plus one `kind()` read per distinct
|
|
1831
1875
|
* policy, cached. Possible only because the event carries the full key preimage (bases,
|
|
1832
|
-
* weights, swapFee, policy,
|
|
1876
|
+
* weights, swapFee, policy, lnCapOverFloor) — the poolId is a hash and could never be inverted; contracts PR #17
|
|
1833
1877
|
* exists for exactly this call. With `indexer`, the logs come from it (the node when it fails):
|
|
1834
1878
|
* a scan from block 0 per call is what made the first load wait on the node.
|
|
1835
1879
|
*/
|
|
@@ -1856,7 +1900,7 @@ export async function discoverFPools(
|
|
|
1856
1900
|
logs.map(async (l) => {
|
|
1857
1901
|
const a = l.args as {
|
|
1858
1902
|
poolId: Hex; quote: Address; creator: Address; bases: readonly Address[];
|
|
1859
|
-
weights: readonly bigint[]; swapFee: bigint; policy: Address; cap
|
|
1903
|
+
weights: readonly bigint[]; swapFee: bigint; policy: Address; lnCapOverFloor?: number; cap?: number;
|
|
1860
1904
|
};
|
|
1861
1905
|
const q0 = q0Of.get(a.poolId.toLowerCase());
|
|
1862
1906
|
const kind = await fPoolKindOfPolicy(c, a.policy); // one read per distinct policy, cached
|
|
@@ -1866,7 +1910,7 @@ export async function discoverFPools(
|
|
|
1866
1910
|
bases: [...a.bases],
|
|
1867
1911
|
weights: [...a.weights],
|
|
1868
1912
|
swapFee: a.swapFee,
|
|
1869
|
-
|
|
1913
|
+
lnCapOverFloor: Number(a.lnCapOverFloor ?? a.cap ?? 0), // `cap` before F version 27
|
|
1870
1914
|
kind,
|
|
1871
1915
|
policy: a.policy,
|
|
1872
1916
|
creator: a.creator,
|
|
@@ -1902,46 +1946,46 @@ export type FPoolRules =
|
|
|
1902
1946
|
gen: number;
|
|
1903
1947
|
/** @deprecated Equal to {@link gen} since F version 21, kept for callers; read `gen`. */
|
|
1904
1948
|
rung: number;
|
|
1905
|
-
/** 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). */
|
|
1906
1950
|
threshold: bigint;
|
|
1907
1951
|
pot: bigint;
|
|
1908
|
-
/** 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
|
|
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). */
|
|
1909
1953
|
creatorShare: bigint;
|
|
1910
1954
|
protocolShare: bigint;
|
|
1911
|
-
|
|
1912
|
-
/** 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. */
|
|
1913
1956
|
ranges: PadRanges;
|
|
1914
1957
|
}
|
|
1915
1958
|
| { kind: "pol"; keepQuote: bigint; minQuoteAtSeed: bigint }
|
|
1916
1959
|
| { kind: "public"; curated: boolean }
|
|
1917
1960
|
| { kind: "private"; gateMint: boolean; gateSwap: boolean; operator: Address };
|
|
1918
1961
|
|
|
1919
|
-
/** The
|
|
1962
|
+
/** The LOG Authority's range on a pad's creator choice (`PadPolicy`, decisions 056, 067), WAD fractions of the pot. */
|
|
1920
1963
|
export interface PadRanges {
|
|
1921
1964
|
/** The protocol's share of the pot, recorded into each pad at birth. */
|
|
1922
1965
|
protocolShare: bigint;
|
|
1923
1966
|
creatorShareMin: bigint;
|
|
1967
|
+
/** At most the F manager's `PAYEE_CUT_MAX` (50%), which bounds a pad's payee whatever the policy (067). */
|
|
1924
1968
|
creatorShareMax: bigint;
|
|
1925
|
-
burnMin: bigint;
|
|
1926
|
-
burnMax: bigint;
|
|
1927
1969
|
}
|
|
1928
1970
|
|
|
1929
1971
|
const MAX_U64 = (1n << 64n) - 1n;
|
|
1930
1972
|
const MAX_U128 = (1n << 128n) - 1n;
|
|
1931
1973
|
|
|
1932
1974
|
/**
|
|
1933
|
-
* The salt that carries a pad's split (`PadPolicy.pack`, decisions 056,
|
|
1934
|
-
*
|
|
1935
|
-
*
|
|
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.
|
|
1936
1979
|
*/
|
|
1937
|
-
export function padSalt(s: { creatorShare: bigint;
|
|
1980
|
+
export function padSalt(s: { creatorShare: bigint; nonce?: bigint; /** @deprecated F version 28 has no burn (067): only 0 is accepted. */ burnShare?: bigint }): Hex {
|
|
1938
1981
|
const nonce = s.nonce ?? 0n;
|
|
1939
|
-
if (s.
|
|
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");
|
|
1940
1984
|
if (nonce < 0n || nonce > MAX_U128) throw new Error("logswap: a pad salt's nonce fits 128 bits");
|
|
1941
|
-
return `0x${((s.creatorShare << 192n) |
|
|
1985
|
+
return `0x${((s.creatorShare << 192n) | nonce).toString(16).padStart(64, "0")}` as Hex;
|
|
1942
1986
|
}
|
|
1943
1987
|
|
|
1944
|
-
/** 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. */
|
|
1945
1989
|
export function padSplitOfSalt(salt: Hex): { creatorShare: bigint; burnShare: bigint; nonce: bigint } {
|
|
1946
1990
|
const v = BigInt(salt);
|
|
1947
1991
|
return { creatorShare: v >> 192n, burnShare: (v >> 128n) & MAX_U64, nonce: v & MAX_U128 };
|
|
@@ -1955,19 +1999,17 @@ export async function getFPoolRules(c: LogswapClient, s: Pick<FPoolState, "poolI
|
|
|
1955
1999
|
case "pad": {
|
|
1956
2000
|
// the rung is the manager's generation (F version 21): a move carries it, nothing to ask the policy
|
|
1957
2001
|
const gen = s.gen;
|
|
1958
|
-
const [split, pot, threshold, protocolNow, creatorShareMin, creatorShareMax
|
|
1959
|
-
rd<readonly [bigint, bigint
|
|
2002
|
+
const [split, pot, threshold, protocolNow, creatorShareMin, creatorShareMax] = await Promise.all([
|
|
2003
|
+
rd<readonly [bigint, bigint]>(padPolicyAbi, "splitOf", [s.poolId]),
|
|
1960
2004
|
fPoolDividendOf(c, s.poolId, DEAD_ADDRESS, gen),
|
|
1961
|
-
rd<bigint>(padPolicyAbi, "threshold", [BigInt(gen)]),
|
|
2005
|
+
rd<bigint>(padPolicyAbi, "threshold", [s.poolId, BigInt(gen)]),
|
|
1962
2006
|
rd<bigint>(padPolicyAbi, "protocolShare"),
|
|
1963
2007
|
rd<bigint>(padPolicyAbi, "creatorShareMin"),
|
|
1964
2008
|
rd<bigint>(padPolicyAbi, "creatorShareMax"),
|
|
1965
|
-
rd<bigint>(padPolicyAbi, "burnMin"),
|
|
1966
|
-
rd<bigint>(padPolicyAbi, "burnMax"),
|
|
1967
2009
|
]);
|
|
1968
|
-
const [creatorShare,
|
|
1969
|
-
const ranges: PadRanges = { protocolShare: protocolNow, creatorShareMin, creatorShareMax
|
|
1970
|
-
return { kind: "pad", gen, rung: gen, threshold, pot, creatorShare, protocolShare,
|
|
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 };
|
|
1971
2013
|
}
|
|
1972
2014
|
case "pol": {
|
|
1973
2015
|
const [keepQuote, minQuoteAtSeed] = await Promise.all([rd<bigint>(polPolicyAbi, "keepQuote"), rd<bigint>(polPolicyAbi, "minQuoteAtSeed")]);
|
|
@@ -2030,9 +2072,10 @@ export async function fPoolShareHolders(
|
|
|
2030
2072
|
const ev = fPoolManagerAbi.find((x): x is Ev => x.type === "event" && x.name === "Transfer");
|
|
2031
2073
|
if (!ev) throw new Error("logswap: 6909 Transfer event missing from the generated ABI");
|
|
2032
2074
|
const gen = opts.gen ?? (await fPoolGen(c, poolIdHex));
|
|
2033
|
-
|
|
2075
|
+
const indexer = opts.indexer ?? c.indexerUrl;
|
|
2076
|
+
if (indexer) {
|
|
2034
2077
|
try {
|
|
2035
|
-
const rows = await poolHolders(
|
|
2078
|
+
const rows = await poolHolders(indexer, { condition: { side: "F", poolId: poolIdHex.toLowerCase(), gen: Number(gen), manager: c.addresses.fPoolManager!.toLowerCase() } });
|
|
2036
2079
|
return rows
|
|
2037
2080
|
.filter((r) => r.balance > 0n)
|
|
2038
2081
|
.map((r) => ({ holder: r.holder, shares: r.balance }))
|
|
@@ -2056,18 +2099,18 @@ export async function fPoolShareHolders(
|
|
|
2056
2099
|
}
|
|
2057
2100
|
|
|
2058
2101
|
/**
|
|
2059
|
-
* What a policy answers `onRegenerate` with: `IFPolicy.Terms
|
|
2060
|
-
* carries beside the dead shares;
|
|
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.
|
|
2061
2105
|
*/
|
|
2062
2106
|
export interface FPolicyTerms {
|
|
2063
2107
|
payee: Address;
|
|
2064
2108
|
toPayee: bigint;
|
|
2065
2109
|
protocolCut: bigint;
|
|
2066
|
-
burn: bigint;
|
|
2067
2110
|
carried: Address;
|
|
2068
2111
|
}
|
|
2069
2112
|
|
|
2070
|
-
/** Every F pool's raw state in one lens call (the manager's `Pool` struct: quote, swapFee, policy, seeded, gen, n,
|
|
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). */
|
|
2071
2114
|
export async function getFPoolsRaw(c: LogswapClient, poolIds: Hex[]): Promise<readonly unknown[]> {
|
|
2072
2115
|
const { logswapLensAbi } = await import("./generated.js");
|
|
2073
2116
|
return (await c.public.readContract({ address: c.addresses.lens, abi: logswapLensAbi, functionName: "getFPools", args: [poolIds] } as never)) as readonly unknown[];
|