@logswap/sdk 0.5.99 → 0.5.101
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 +104 -36
- package/dist/actions.d.ts +8 -8
- package/dist/actions.js +8 -8
- package/dist/admin.d.ts +16 -36
- package/dist/admin.js +26 -57
- package/dist/chains.d.ts +1 -1
- package/dist/client.d.ts +5 -5
- package/dist/client.js +19 -3
- package/dist/fees.js +2 -24
- package/dist/fpool.d.ts +10 -6
- package/dist/fpool.js +15 -25
- package/dist/generated.d.ts +1611 -2189
- package/dist/generated.js +947 -1224
- package/dist/history.d.ts +1 -1
- package/dist/history.js +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/indexer.d.ts +1 -1
- package/dist/indexer.js +5 -12
- package/dist/keys.d.ts +39 -52
- package/dist/keys.js +59 -38
- package/dist/liquidity.d.ts +2 -24
- package/dist/liquidity.js +7 -33
- package/dist/pools.d.ts +23 -14
- package/dist/pools.js +36 -28
- package/dist/positions.d.ts +2 -7
- package/dist/positions.js +6 -25
- package/package.json +1 -1
- package/src/actions.ts +12 -12
- package/src/admin.ts +42 -85
- package/src/chains.ts +1 -1
- package/src/client.ts +21 -5
- package/src/fees.ts +2 -22
- package/src/fpool.ts +19 -26
- package/src/generated.ts +951 -1228
- package/src/history.ts +2 -2
- package/src/index.ts +1 -2
- package/src/indexer.ts +6 -13
- package/src/keys.ts +80 -74
- package/src/liquidity.ts +7 -47
- package/src/pools.ts +45 -33
- package/src/positions.ts +7 -27
- package/dist/views.d.ts +0 -281
- package/dist/views.js +0 -188
- package/src/views.ts +0 -480
package/dist/pools.js
CHANGED
|
@@ -16,7 +16,7 @@ import { lensCAbi, managerCAbi } from "./generated.js";
|
|
|
16
16
|
import { lensC, managerC, clientChainId } from "./client.js";
|
|
17
17
|
import { signerFor } from "./write.js";
|
|
18
18
|
import { readCachedToken, registerCacheClear, writeCachedToken } from "./tokencache.js";
|
|
19
|
-
import { poolId } from "./keys.js";
|
|
19
|
+
import { poolId, swapFeeOfKey } from "./keys.js";
|
|
20
20
|
import { getEventLogs } from "./history.js";
|
|
21
21
|
/** Read a market's full state by key, through the lens (`getPools`; C version 7). All zero, `initialized` false, for a market nobody opened. */
|
|
22
22
|
export async function getPool(c, key) {
|
|
@@ -29,16 +29,15 @@ export async function getPool(c, key) {
|
|
|
29
29
|
return v[0];
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
|
-
* The market's swap fee
|
|
33
|
-
* `DEFAULT_FEE` / the manager's `swapFeeOf` are gone (decisions 065). Constant for the market's
|
|
32
|
+
* The market's swap fee, WAD: `FEE_LIST[key.swapFeeIdx]` (C 18, decision 099), constant for the market's
|
|
34
33
|
* life. Still a read: it refuses a key nobody initialized, which `discoverMarkets` relies on to
|
|
35
|
-
* drop a stale indexer's rows. (`swapFeeOf` in `fees.ts` is the fee one swap paid.)
|
|
34
|
+
* drop a stale indexer's rows. Offline: {@link swapFeeOfKey}. (`swapFeeOf` in `fees.ts` is the fee one swap paid.)
|
|
36
35
|
*/
|
|
37
36
|
export async function marketSwapFee(c, key) {
|
|
38
37
|
const s = await getPool(c, key);
|
|
39
38
|
if (!s.initialized)
|
|
40
39
|
throw new Error(`logswap: market ${poolId(key)} is not initialized`);
|
|
41
|
-
return key
|
|
40
|
+
return swapFeeOfKey(key);
|
|
42
41
|
}
|
|
43
42
|
/**
|
|
44
43
|
* Price from the stored log-price. **Lossy** — `x` is exact and this is a float, so use it for
|
|
@@ -113,10 +112,13 @@ export async function discoverMarkets(c, opts = {}) {
|
|
|
113
112
|
if (!ev)
|
|
114
113
|
throw new Error("logswap: PoolCreated event missing from the generated ABI");
|
|
115
114
|
const args = {};
|
|
116
|
-
//
|
|
117
|
-
// C
|
|
115
|
+
// C 20: the topics are `poolId`, `quote`, `policy`; `base` and `creator` are data and filter below.
|
|
116
|
+
// C 18 (decision 099): the event carries the six key fields, so the whole key is rebuilt from the log; `creator` is the account
|
|
117
|
+
// that opened the market and no key field.
|
|
118
118
|
if (opts.quote)
|
|
119
119
|
args.quote = opts.quote;
|
|
120
|
+
if (opts.policy)
|
|
121
|
+
args.policy = opts.policy;
|
|
120
122
|
const { logs } = await getEventLogs(c, {
|
|
121
123
|
indexer: opts.indexer,
|
|
122
124
|
address: managerC(c),
|
|
@@ -126,21 +128,16 @@ export async function discoverMarkets(c, opts = {}) {
|
|
|
126
128
|
toBlock: opts.toBlock,
|
|
127
129
|
});
|
|
128
130
|
const wantBase = opts.base?.toLowerCase();
|
|
129
|
-
|
|
131
|
+
const wantCreator = opts.creator?.toLowerCase();
|
|
132
|
+
return logs
|
|
133
|
+
.filter((l) => (wantBase === undefined || l.args.base.toLowerCase() === wantBase) && (wantCreator === undefined || l.args.creator.toLowerCase() === wantCreator))
|
|
134
|
+
.map((l) => {
|
|
130
135
|
const a = l.args;
|
|
131
|
-
const key = {
|
|
132
|
-
base: a.base,
|
|
133
|
-
quote: a.quote,
|
|
134
|
-
tickSpacing: a.tickSpacing,
|
|
135
|
-
policy: a.policy,
|
|
136
|
-
creator: a.creator,
|
|
137
|
-
salt: a.salt,
|
|
138
|
-
swapFee: a.swapFee,
|
|
139
|
-
mintFee: a.mintFee,
|
|
140
|
-
};
|
|
136
|
+
const key = { quote: a.quote, base: a.base, swapFeeIdx: Number(a.swapFeeIdx), spacingIdx: Number(a.spacingIdx), policy: a.policy, salt: a.salt };
|
|
141
137
|
return {
|
|
142
138
|
key,
|
|
143
139
|
poolId: poolId(key),
|
|
140
|
+
creator: a.creator,
|
|
144
141
|
initialLogPrice: a.initialLogPrice,
|
|
145
142
|
blockNumber: l.blockNumber,
|
|
146
143
|
transactionHash: l.transactionHash,
|
|
@@ -343,18 +340,14 @@ export async function marketOpenedAt(c, m) {
|
|
|
343
340
|
return b.timestamp;
|
|
344
341
|
}
|
|
345
342
|
/**
|
|
346
|
-
* Create a C market: `initialize(key, initialLogPrice)` on the manager — a call, not a deployment, and
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
* user to the gate and mints the backstop in the same transaction (064).
|
|
343
|
+
* Create a C market: `initialize(key, initialLogPrice)` on the manager — a call, not a deployment, and the one creation path
|
|
344
|
+
* (C 18, decisions 099, 100: `RouterC.createMarket` is gone). The call mints the permanent backstop (`minBackstopL` of uncapped
|
|
345
|
+
* liquidity to 0xdead on the rung under the opening price), so the market is born live and `account` pays dust of BOTH tokens,
|
|
346
|
+
* and the mint fee on it, by plain ERC-20 allowance to the MANAGER: {@link approveTokenForCManager} for each first. Whoever opens
|
|
347
|
+
* a market holds nothing of it. The policy answers who may create (`PolicyC.mayInit`): anyone at salt zero on an open policy,
|
|
348
|
+
* the policy's authority on a closed one; {@link cGateRules} reads the same checks before a wallet is asked.
|
|
353
349
|
*/
|
|
354
350
|
export async function initializeMarket(c, a) {
|
|
355
|
-
if (a.account.toLowerCase() !== a.key.creator.toLowerCase()) {
|
|
356
|
-
throw new Error(`logswap: the key's creator ${a.key.creator} must be the account that initializes it (${a.account}) — NotCreator`);
|
|
357
|
-
}
|
|
358
351
|
const { request } = await c.public.simulateContract({
|
|
359
352
|
address: managerC(c),
|
|
360
353
|
abi: managerCAbi,
|
|
@@ -364,3 +357,18 @@ export async function initializeMarket(c, a) {
|
|
|
364
357
|
});
|
|
365
358
|
return c.wallet.writeContract(request);
|
|
366
359
|
}
|
|
360
|
+
const ERC20_APPROVE = [{ type: "function", name: "approve", stateMutability: "nonpayable", inputs: [{ name: "s", type: "address" }, { name: "a", type: "uint256" }], outputs: [{ type: "bool" }] }];
|
|
361
|
+
const ERC20_ALLOWANCE = [{ type: "function", name: "allowance", stateMutability: "view", inputs: [{ name: "o", type: "address" }, { name: "s", type: "address" }], outputs: [{ type: "uint256" }] }];
|
|
362
|
+
/**
|
|
363
|
+
* Plain ERC-20 approval to the C manager — the allowance {@link initializeMarket} pulls the backstop against (C 18). Every
|
|
364
|
+
* other C write goes through the router and Permit2 (`onboardToken`). `amount` defaults to the maximum; the backstop is dust.
|
|
365
|
+
*/
|
|
366
|
+
export async function approveTokenForCManager(c, token, account, amount = (1n << 256n) - 1n) {
|
|
367
|
+
const { request } = await c.public.simulateContract({ address: token, abi: ERC20_APPROVE, functionName: "approve", args: [managerC(c), amount], account: signerFor(c, account) });
|
|
368
|
+
return c.wallet.writeContract(request);
|
|
369
|
+
}
|
|
370
|
+
/** Does `owner` have enough allowance on `token` for the C MANAGER to pull `need`? */
|
|
371
|
+
export async function cManagerAllowanceOk(c, token, owner, need) {
|
|
372
|
+
const allowance = (await c.public.readContract({ address: token, abi: ERC20_ALLOWANCE, functionName: "allowance", args: [owner, managerC(c)] }));
|
|
373
|
+
return allowance >= need;
|
|
374
|
+
}
|
package/dist/positions.d.ts
CHANGED
|
@@ -109,16 +109,11 @@ export declare function positionQuote(p: {
|
|
|
109
109
|
cap: bigint;
|
|
110
110
|
capped: boolean;
|
|
111
111
|
}, x: bigint): bigint;
|
|
112
|
-
/**
|
|
113
|
-
* The mint fee a create form presets: the least the managers accept, twice the swap fee (C 12 / F 38, decision
|
|
114
|
-
* 078: `2 * swapFee <= mintFee <= {@link MINT_FEE_MAX}`). The creator selects it in the key; there is no menu.
|
|
115
|
-
*/
|
|
116
|
-
export declare function presetMintFee(swapFee: bigint): bigint;
|
|
117
112
|
/**
|
|
118
113
|
* The one-time mint fee at log-price `x`, in quote units — the amount `_mintCore` pulls ON TOP of
|
|
119
114
|
* the position's own legs, so a mint's total quote pull is `positionQuote(p, x) + mintFee(p, x, rate)`.
|
|
120
|
-
* `rate` is the MARKET's mint fee
|
|
121
|
-
* fixed for life; there is no global `MINT_FEE`.
|
|
115
|
+
* `rate` is the MARKET's mint fee, WAD of L (`mintFeeOfKey(key)`): the fee list's next entry after the key's swap fee
|
|
116
|
+
* (C 18, decision 099), fixed for life; there is no global `MINT_FEE`.
|
|
122
117
|
*
|
|
123
118
|
* Mirrors the manager's exemption cases: a DORMANT mint pays nothing — floor above spot, or a
|
|
124
119
|
* capped class spot has already passed (`cap <= x`). Anything earning pays `rate · L`, all of it
|
package/dist/positions.js
CHANGED
|
@@ -8,13 +8,12 @@
|
|
|
8
8
|
import { encodeAbiParameters, keccak256, parseAbiItem } from "viem";
|
|
9
9
|
import { lensCAbi, managerCAbi } from "./generated.js";
|
|
10
10
|
import { managerC, lensC } from "./client.js";
|
|
11
|
-
import {
|
|
11
|
+
import { poolId, tickSpacingOf } from "./keys.js";
|
|
12
12
|
/** `CPoolState.POSITIONS_SLOT`: the manager's `positions` mapping (`forge inspect CPoolManager storageLayout`). */
|
|
13
13
|
const C_POSITIONS_SLOT = 4n;
|
|
14
14
|
const U128 = (1n << 128n) - 1n;
|
|
15
15
|
import { isPosition, NO_CAP, positionId, unpackFloorCap } from "./ids.js";
|
|
16
16
|
import { getEventLogs } from "./history.js";
|
|
17
|
-
import { poolHolders } from "./views.js";
|
|
18
17
|
/**
|
|
19
18
|
* A class's aggregate state, read through `extsload` (C version 7: the manager's public
|
|
20
19
|
* `positions` mapping is gone behind it, decisions 065). The slot is `CPoolState`'s:
|
|
@@ -61,7 +60,7 @@ export async function feesOf(c, id, owner) {
|
|
|
61
60
|
* reads and no id lookup.
|
|
62
61
|
*/
|
|
63
62
|
export async function getHolderPosition(c, key, owner, floor, cap = NO_CAP) {
|
|
64
|
-
const id = positionId(poolId(key), key
|
|
63
|
+
const id = positionId(poolId(key), tickSpacingOf(key), floor, cap);
|
|
65
64
|
const [cls, shares, claimableFees] = await Promise.all([
|
|
66
65
|
getPositionClass(c, id),
|
|
67
66
|
balanceOf(c, owner, id),
|
|
@@ -144,19 +143,11 @@ export function positionQuote(p, x) {
|
|
|
144
143
|
const upper = p.capped && p.cap <= x ? p.cap : x;
|
|
145
144
|
return (p.L * (upper - p.floor)) / 10n ** 18n;
|
|
146
145
|
}
|
|
147
|
-
/**
|
|
148
|
-
* The mint fee a create form presets: the least the managers accept, twice the swap fee (C 12 / F 38, decision
|
|
149
|
-
* 078: `2 * swapFee <= mintFee <= {@link MINT_FEE_MAX}`). The creator selects it in the key; there is no menu.
|
|
150
|
-
*/
|
|
151
|
-
export function presetMintFee(swapFee) {
|
|
152
|
-
const m = 2n * swapFee;
|
|
153
|
-
return m > MINT_FEE_MAX ? MINT_FEE_MAX : m;
|
|
154
|
-
}
|
|
155
146
|
/**
|
|
156
147
|
* The one-time mint fee at log-price `x`, in quote units — the amount `_mintCore` pulls ON TOP of
|
|
157
148
|
* the position's own legs, so a mint's total quote pull is `positionQuote(p, x) + mintFee(p, x, rate)`.
|
|
158
|
-
* `rate` is the MARKET's mint fee
|
|
159
|
-
* fixed for life; there is no global `MINT_FEE`.
|
|
149
|
+
* `rate` is the MARKET's mint fee, WAD of L (`mintFeeOfKey(key)`): the fee list's next entry after the key's swap fee
|
|
150
|
+
* (C 18, decision 099), fixed for life; there is no global `MINT_FEE`.
|
|
160
151
|
*
|
|
161
152
|
* Mirrors the manager's exemption cases: a DORMANT mint pays nothing — floor above spot, or a
|
|
162
153
|
* capped class spot has already passed (`cap <= x`). Anything earning pays `rate · L`, all of it
|
|
@@ -182,17 +173,7 @@ export async function positionsOf(c, key, owner) {
|
|
|
182
173
|
}));
|
|
183
174
|
return rows.map((r) => ({ id: r.id, floor: r.floor, cap: NO_CAP, capped: false, shares: r.shares, L: r.L, fees: r.fees }));
|
|
184
175
|
}
|
|
185
|
-
const ID_REGISTERED = parseAbiItem("event ClassRegistered(
|
|
186
|
-
/** The class ids `owner` holds in `pool`, from the indexer's `pool_holder` view; null when the indexer cannot answer. */
|
|
187
|
-
async function heldIdsFromView(c, indexer, pool, owner) {
|
|
188
|
-
try {
|
|
189
|
-
const rows = await poolHolders(indexer, { condition: { side: "C", poolId: pool.toLowerCase(), holder: owner.toLowerCase(), manager: managerC(c).toLowerCase() } });
|
|
190
|
-
return rows.filter((r) => r.balance > 0n && !r.isBackstop).map((r) => r.id);
|
|
191
|
-
}
|
|
192
|
-
catch {
|
|
193
|
-
return null; // the view is a cache: the ClassRegistered replay below answers the same
|
|
194
|
-
}
|
|
195
|
-
}
|
|
176
|
+
const ID_REGISTERED = parseAbiItem("event ClassRegistered(bytes32 indexed poolId, uint256 indexed id, int256 floor, int256 cap, bool capped)"); // C 17: poolId first;
|
|
196
177
|
/** Every class id ever registered in `pool` (`ClassRegistered`), from the indexer's table or the node. */
|
|
197
178
|
async function registeredIds(c, pool, fromBlock, indexer) {
|
|
198
179
|
const { logs } = await getEventLogs(c, { indexer, address: managerC(c), event: ID_REGISTERED, args: { poolId: pool }, fromBlock });
|
|
@@ -211,7 +192,7 @@ async function registeredIds(c, pool, fromBlock, indexer) {
|
|
|
211
192
|
export async function holdingsOf(c, key, owner, fromBlock, opts = {}) {
|
|
212
193
|
const pool = poolId(key);
|
|
213
194
|
const indexer = opts.indexer ?? c.indexerUrl;
|
|
214
|
-
const ids =
|
|
195
|
+
const ids = await registeredIds(c, pool, fromBlock, indexer);
|
|
215
196
|
if (ids.length === 0)
|
|
216
197
|
return [];
|
|
217
198
|
// one lens call: balance, class L, fees, floor and cap for every id the owner holds
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logswap/sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.101",
|
|
4
4
|
"description": "TypeScript SDK for Logswap, the liquidity primitive introducing the logarithmic invariant — typed wrappers over the ManagerF / ManagerC, RouterF / RouterC and LensF / LensC surface.",
|
|
5
5
|
"license": "GPL-2.0-or-later",
|
|
6
6
|
"type": "module",
|
package/src/actions.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* `execute(actions, settle)` runs a list of actions — mint, burn, update, claim, swap, path, in any
|
|
5
5
|
* order, across any C markets — under ONE manager lock. Each action accrues deltas on the router
|
|
6
6
|
* and settles nothing; the batch settles once per token in `settle.tokens`, pulling at most
|
|
7
|
-
* `maxIn[i]` from the caller and taking at least `minOut[i]` to
|
|
7
|
+
* `maxIn[i]` from the caller and taking at least `minOut[i]` to `settle.to`. A touched token
|
|
8
8
|
* left off the list fails the manager's own close (`TokensNotSettled`), so the list is also the
|
|
9
9
|
* caller's statement of which tokens may move.
|
|
10
10
|
*
|
|
@@ -51,8 +51,8 @@ export interface Settle {
|
|
|
51
51
|
tokens: readonly Address[];
|
|
52
52
|
maxIn: readonly bigint[];
|
|
53
53
|
minOut: readonly bigint[];
|
|
54
|
-
/** Receives whatever is taken, and the shares the actions mint. */
|
|
55
|
-
|
|
54
|
+
/** Receives whatever is taken, and the shares the actions mint (`Settle.to`; `recipient` before C 17). */
|
|
55
|
+
to: Address;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/* payload encoding ========================================================== */
|
|
@@ -131,13 +131,13 @@ export function settlement(a: {
|
|
|
131
131
|
tokens: readonly Address[];
|
|
132
132
|
maxIn?: readonly bigint[];
|
|
133
133
|
minOut?: readonly bigint[];
|
|
134
|
-
|
|
134
|
+
to: Address;
|
|
135
135
|
}): Settle {
|
|
136
136
|
const n = a.tokens.length;
|
|
137
137
|
const maxIn = a.maxIn ?? a.tokens.map(() => UNBOUNDED);
|
|
138
138
|
const minOut = a.minOut ?? a.tokens.map(() => 0n);
|
|
139
139
|
if (maxIn.length !== n || minOut.length !== n) throw new Error("logswap: settlement bounds length mismatch");
|
|
140
|
-
return { tokens: a.tokens, maxIn, minOut,
|
|
140
|
+
return { tokens: a.tokens, maxIn, minOut, to: a.to };
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
export interface PairBounds {
|
|
@@ -148,12 +148,12 @@ export interface PairBounds {
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
/** Settle both of a market's tokens, in kind. */
|
|
151
|
-
export function settleInKind(key: PoolKey,
|
|
151
|
+
export function settleInKind(key: PoolKey, to: Address, b: PairBounds = {}): Settle {
|
|
152
152
|
return settlement({
|
|
153
153
|
tokens: [key.base, key.quote],
|
|
154
154
|
maxIn: [b.maxBaseIn ?? UNBOUNDED, b.maxQuoteIn ?? UNBOUNDED],
|
|
155
155
|
minOut: [b.minBaseOut ?? 0n, b.minQuoteOut ?? 0n],
|
|
156
|
-
|
|
156
|
+
to,
|
|
157
157
|
});
|
|
158
158
|
}
|
|
159
159
|
|
|
@@ -161,13 +161,13 @@ export function settleInKind(key: PoolKey, recipient: Address, b: PairBounds = {
|
|
|
161
161
|
* Settle in ONE of a market's tokens: the other is bound to zero both ways, so the batch must have
|
|
162
162
|
* netted it — which is what a trailing `OPEN_DELTA` swap does.
|
|
163
163
|
*/
|
|
164
|
-
export function settleOnly(key: PoolKey,
|
|
164
|
+
export function settleOnly(key: PoolKey, to: Address, side: "base" | "quote", b: { maxIn?: bigint; minOut?: bigint } = {}): Settle {
|
|
165
165
|
const base = side === "base";
|
|
166
166
|
return settlement({
|
|
167
167
|
tokens: [key.base, key.quote],
|
|
168
168
|
maxIn: [base ? (b.maxIn ?? UNBOUNDED) : 0n, base ? 0n : (b.maxIn ?? UNBOUNDED)],
|
|
169
169
|
minOut: [base ? (b.minOut ?? 0n) : 0n, base ? 0n : (b.minOut ?? 0n)],
|
|
170
|
-
|
|
170
|
+
to,
|
|
171
171
|
});
|
|
172
172
|
}
|
|
173
173
|
|
|
@@ -175,14 +175,14 @@ export function settleOnly(key: PoolKey, recipient: Address, side: "base" | "quo
|
|
|
175
175
|
|
|
176
176
|
export interface ExecuteArgs extends WriteOptions {
|
|
177
177
|
actions: readonly Action[];
|
|
178
|
-
settle: Omit<Settle, "
|
|
178
|
+
settle: Omit<Settle, "to"> & { to?: Address };
|
|
179
179
|
/** Tokens the batch may pull, for the Permit2 signatures. Defaults to every token with `maxIn > 0`. */
|
|
180
180
|
pullTokens?: readonly Address[];
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
/** Run a batch: one lock, one settlement per token, one transaction (Permit2 signatures ride inside). */
|
|
184
184
|
export async function execute(c: LogswapClient, a: ExecuteArgs): Promise<Hash> {
|
|
185
|
-
const settle: Settle = { ...a.settle,
|
|
185
|
+
const settle: Settle = { ...a.settle, to: a.settle.to ?? resolveRecipient(c, a) };
|
|
186
186
|
const pull = a.pullTokens ?? settle.tokens.filter((_, i) => settle.maxIn[i]! > 0n);
|
|
187
187
|
return sendRouterWriteWithPermits(
|
|
188
188
|
c,
|
|
@@ -196,7 +196,7 @@ export async function simulateExecute(
|
|
|
196
196
|
c: LogswapClient,
|
|
197
197
|
a: ExecuteArgs & { account?: Address },
|
|
198
198
|
): Promise<{ results: readonly Hex[]; flows: readonly bigint[] }> {
|
|
199
|
-
const settle: Settle = { ...a.settle,
|
|
199
|
+
const settle: Settle = { ...a.settle, to: a.settle.to ?? a.account ?? resolveRecipient(c, a) };
|
|
200
200
|
const [results, flows] = await simulateRouterWrite<readonly [readonly Hex[], readonly bigint[]]>(c, {
|
|
201
201
|
side: "C",
|
|
202
202
|
abi: routerCAbi,
|
package/src/admin.ts
CHANGED
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import type { Address, Hash } from "viem";
|
|
19
|
-
import { poolId as keyId, type PoolKey } from "./keys.js";
|
|
20
|
-
import { managerC,
|
|
19
|
+
import { poolId as keyId, FEE_LIST, type PoolKey } from "./keys.js";
|
|
20
|
+
import { managerC, type LogswapClient, type Side } from "./client.js";
|
|
21
21
|
import { getPool } from "./pools.js";
|
|
22
|
-
import { managerCAbi, managerFAbi, policyFAbi,
|
|
22
|
+
import { managerCAbi, managerFAbi, policyFAbi, policyCAbi, policyRegistryAbi } from "./generated.js";
|
|
23
23
|
import { bytes32ToText, fFeeListed, fPoolKindOfPolicy, fPolicyTermsOf, textToBytes32, type FPoolKind } from "./fpool.js";
|
|
24
24
|
|
|
25
25
|
function wallet(c: LogswapClient) {
|
|
@@ -40,9 +40,9 @@ function write(c: LogswapClient, address: Address, abi: readonly unknown[], func
|
|
|
40
40
|
*/
|
|
41
41
|
export async function cSwapFeeTax(c: LogswapClient, policy?: Address): Promise<{ tax: bigint; max: bigint }> {
|
|
42
42
|
const address = policy ?? c.addresses.publicPolicyC;
|
|
43
|
-
if (!address) throw new Error("logswap: this deployment carries no C
|
|
43
|
+
if (!address) throw new Error("logswap: this deployment carries no C policy (addresses.publicPolicyC); pass one");
|
|
44
44
|
const [tax, max] = await Promise.all([
|
|
45
|
-
c.public.readContract({ address, abi:
|
|
45
|
+
c.public.readContract({ address, abi: policyCAbi, functionName: "swapFeeTax" } as never) as Promise<bigint>,
|
|
46
46
|
c.public.readContract({ address: managerC(c), abi: managerCAbi, functionName: "SWAP_FEE_TAX_MAX" }),
|
|
47
47
|
]);
|
|
48
48
|
return { tax, max };
|
|
@@ -77,7 +77,7 @@ export async function policyApproved(c: LogswapClient, policy: Address): Promise
|
|
|
77
77
|
/** One policy on the registry's list, with what it is. */
|
|
78
78
|
export interface RegistryEntry {
|
|
79
79
|
policy: Address;
|
|
80
|
-
/** The label the policy's terms spell ({@link fPolicyTermsOf}) — what a pool born under it is; `null` for a policy that answers no F terms (
|
|
80
|
+
/** The label the policy's terms spell ({@link fPolicyTermsOf}) — what a pool born under it is; `null` for a policy that answers no F terms (a `PolicyC`). */
|
|
81
81
|
kind: FPoolKind | null;
|
|
82
82
|
/** The name recorded at approval (`approve(policy, name)`). */
|
|
83
83
|
name: string;
|
|
@@ -220,25 +220,14 @@ export async function policyFeeAllowed(c: LogswapClient, policy: Address, swapFe
|
|
|
220
220
|
return { curated: feeMask !== 0, listed, allowed: listed };
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
/** `PolicyRegistry.CreationMode` (decisions 073, 098): who may create under a policy. Open = 0, SponsorOnly = 1 (the policy's own immutable sponsor, who must also be the key's creator). `Restricted` and the initializer list are gone. */
|
|
224
|
-
export type CreationMode = "open" | "sponsorOnly";
|
|
225
|
-
const CREATION_MODES: readonly CreationMode[] = ["open", "sponsorOnly"];
|
|
226
|
-
|
|
227
|
-
/** `PolicyRegistry.setCreationMode(policy, mode)` (the LOG Authority). A sponsored policy is approved `sponsorOnly`. */
|
|
228
|
-
export async function registrySetCreationMode(c: LogswapClient, policy: Address, mode: CreationMode): Promise<Hash> {
|
|
229
|
-
return registryWrite(c, "setCreationMode", [policy, CREATION_MODES.indexOf(mode)]);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
223
|
/** `PolicyRegistry.setForwarderTrusted` (the LOG Authority): a router whose `user()` names the creator. Moved off the public policy in F version 33. */
|
|
233
224
|
export async function registrySetForwarderTrusted(c: LogswapClient, forwarder: Address, trusted: boolean): Promise<Hash> {
|
|
234
225
|
return registryWrite(c, "setForwarderTrusted", [forwarder, trusted]);
|
|
235
226
|
}
|
|
236
227
|
|
|
237
|
-
/** `PolicyRegistry.creationMode(policy)`. */
|
|
238
228
|
/**
|
|
239
229
|
* `PolicyRegistry.userOf(caller)` (C 15, decision 097): the account a caller acts for — the user a trusted forwarder
|
|
240
|
-
* (a router) names, else the caller itself. The account
|
|
241
|
-
* first minter; what a create form compares to the key's `creator`.
|
|
230
|
+
* (a router) names, else the caller itself. The account a manager names as a pool's creator and asks its policy about at a birth.
|
|
242
231
|
*/
|
|
243
232
|
export async function registryUserOf(c: LogswapClient, caller: Address): Promise<Address> {
|
|
244
233
|
const registry = await policyRegistryOf(c);
|
|
@@ -246,94 +235,62 @@ export async function registryUserOf(c: LogswapClient, caller: Address): Promise
|
|
|
246
235
|
return (await c.public.readContract({ address: registry, abi: policyRegistryAbi, functionName: "userOf", args: [caller] } as never)) as Address;
|
|
247
236
|
}
|
|
248
237
|
|
|
249
|
-
export async function registryCreationMode(c: LogswapClient, policy: Address): Promise<CreationMode> {
|
|
250
|
-
const registry = await policyRegistryOf(c);
|
|
251
|
-
if (registry === ZERO) return "open";
|
|
252
|
-
const m = (await c.public.readContract({ address: registry, abi: policyRegistryAbi, functionName: "creationMode", args: [policy] } as never)) as number;
|
|
253
|
-
return CREATION_MODES[Number(m)] ?? "open";
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/** Whether `policy` (a `SponsorPolicyC`) admits C tick spacing `tickSpacing`: it curates spacings (`spacingCurated`) and lists it, or it does not curate. */
|
|
257
|
-
export async function policySpacingAllowed(c: LogswapClient, policy: Address, tickSpacing: bigint): Promise<{ curated: boolean; listed: boolean; allowed: boolean }> {
|
|
258
|
-
const [curated, listed] = await Promise.all([
|
|
259
|
-
c.public.readContract({ address: policy, abi: sponsorPolicyCAbi, functionName: "spacingCurated" } as never) as Promise<boolean>,
|
|
260
|
-
c.public.readContract({ address: policy, abi: sponsorPolicyCAbi, functionName: "spacingListed", args: [tickSpacing] } as never) as Promise<boolean>,
|
|
261
|
-
]);
|
|
262
|
-
return { curated, listed, allowed: !curated || listed };
|
|
263
|
-
}
|
|
264
|
-
|
|
265
238
|
export interface CGateRules {
|
|
266
|
-
/**
|
|
267
|
-
* The registry admits `creator` to create under `key.policy` (creation mode and sponsor binding; decisions 073, 097, 098):
|
|
268
|
-
* `admit(policy, creator, creator)` resolves to `creator`. Both managers then require the initializer to BE the key's creator (`NotCreator`).
|
|
269
|
-
*/
|
|
239
|
+
/** The policy admits `creator` (`PolicyBase._admitBirth`): anyone on an open policy, the policy's authority on a closed one (`Refused`). */
|
|
270
240
|
creatorOk: boolean;
|
|
271
241
|
/** The quote is on the policy's list, or the policy lists none (`quoteCurated`). */
|
|
272
242
|
quoteOk: boolean;
|
|
273
|
-
/** The
|
|
243
|
+
/** The base is on the policy's list, or the policy lists none (`baseCurated`; `BaseNotListed`). */
|
|
244
|
+
baseOk: boolean;
|
|
245
|
+
/** The swap fee's index is on the policy's `feeMask` (an empty mask lists all eight; `FeeNotListed`). The mint fee follows it. */
|
|
274
246
|
feeOk: boolean;
|
|
275
|
-
/**
|
|
276
|
-
mintFeeOk: boolean;
|
|
277
|
-
/** The tick spacing is on the policy's list, or it lists none (`spacingCurated`). */
|
|
278
|
-
spacingOk: boolean;
|
|
279
|
-
/** A canonical policy (the LOG's open instance) needs `salt` zero (`NotCanonical`); a desk's may run several markets of one shape. */
|
|
247
|
+
/** An open policy needs `salt` zero (`NotCanonical`); a closed one may run several markets of one shape. */
|
|
280
248
|
saltOk: boolean;
|
|
249
|
+
/** The registry lists the policy and its sunset has not passed (`PolicyNotApproved`); true on a deployment with no registry. */
|
|
250
|
+
policyOk: boolean;
|
|
281
251
|
/** This exact market (the key's id) is already initialized: another is a different key. */
|
|
282
252
|
exists: boolean;
|
|
283
|
-
/**
|
|
284
|
-
routerTrusted: boolean;
|
|
285
|
-
/** Every check passes: the policy would admit this market from this creator (the manager checks the fee domain, `BadParam`). */
|
|
253
|
+
/** Every check passes: the policy would admit this market from this creator (the manager checks the two indices, `BadParam`). */
|
|
286
254
|
admitted: boolean;
|
|
287
255
|
}
|
|
288
256
|
|
|
289
257
|
/**
|
|
290
|
-
* A
|
|
291
|
-
* is asked (
|
|
292
|
-
*
|
|
293
|
-
* trusted forwarder's `user()`). The policy's pair list is gone (098).
|
|
258
|
+
* A C policy's birth checks for one candidate market and creator, in one round of reads — what the create form shows before a
|
|
259
|
+
* wallet is asked (C 18, decisions 099, 100: `PolicyC.mayInit`). The policy is `key.policy`; `creator` is the account that will call
|
|
260
|
+
* {@link initializeMarket}. The spacing is the manager's list ({@link SPACING_LIST}) and none of the policy's.
|
|
294
261
|
*/
|
|
295
|
-
export async function cGateRules(c: LogswapClient, a: { key: PoolKey; creator
|
|
262
|
+
export async function cGateRules(c: LogswapClient, a: { key: PoolKey; creator: Address; gate?: Address }): Promise<CGateRules> {
|
|
296
263
|
const g = a.gate ?? (a.key.policy as Address);
|
|
297
264
|
const rd = <T>(functionName: string, args: readonly unknown[] = []) =>
|
|
298
|
-
c.public.readContract({ address: g, abi:
|
|
265
|
+
c.public.readContract({ address: g, abi: policyCAbi, functionName, args } as never) as Promise<T>;
|
|
299
266
|
const { key } = a;
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
rd<boolean>("spacingCurated"),
|
|
312
|
-
rd<boolean>("spacingListed", [key.tickSpacing]),
|
|
313
|
-
rd<boolean>("canonical"),
|
|
314
|
-
getPool(c, key),
|
|
315
|
-
registry === ZERO ? Promise.resolve(false) : registryRd<boolean>("forwarderTrusted", [router]),
|
|
316
|
-
registry === ZERO
|
|
317
|
-
? Promise.resolve(true)
|
|
318
|
-
: registryRd<Address>("admit", [g, creator, creator]).then((who) => who.toLowerCase() === creator.toLowerCase(), () => false),
|
|
319
|
-
]);
|
|
267
|
+
const [open, authority, feeMask, quoteCurated, quoteListed, baseCurated, baseListed, state, policyOk] = await Promise.all([
|
|
268
|
+
rd<boolean>("open"),
|
|
269
|
+
rd<Address>("authority"),
|
|
270
|
+
rd<number | bigint>("feeMask"),
|
|
271
|
+
rd<boolean>("quoteCurated"),
|
|
272
|
+
rd<boolean>("quoteListed", [key.quote]),
|
|
273
|
+
rd<boolean>("baseCurated"),
|
|
274
|
+
rd<boolean>("baseListed", [key.base]),
|
|
275
|
+
getPool(c, key),
|
|
276
|
+
policyApproved(c, g),
|
|
277
|
+
]);
|
|
320
278
|
const r = {
|
|
321
|
-
creatorOk,
|
|
279
|
+
creatorOk: open || authority.toLowerCase() === a.creator.toLowerCase(),
|
|
322
280
|
quoteOk: !quoteCurated || quoteListed,
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
281
|
+
baseOk: !baseCurated || baseListed,
|
|
282
|
+
feeOk: key.swapFeeIdx < FEE_LIST.length && fFeeListed(Number(feeMask), key.swapFeeIdx),
|
|
283
|
+
saltOk: !open || key.salt === "0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
284
|
+
policyOk,
|
|
327
285
|
exists: state.initialized,
|
|
328
|
-
routerTrusted,
|
|
329
286
|
};
|
|
330
|
-
return { ...r, admitted: r.creatorOk && r.quoteOk && r.
|
|
287
|
+
return { ...r, admitted: r.creatorOk && r.quoteOk && r.baseOk && r.feeOk && r.saltOk && r.policyOk && !r.exists };
|
|
331
288
|
}
|
|
332
289
|
|
|
333
290
|
/**
|
|
334
291
|
* Every refusal a birth can meet at a policy or the registry (064, 065, 098; policy.md §4.4), for `decodeErrorResult` on a
|
|
335
|
-
* failed simulation: the
|
|
336
|
-
* `BaseNotListed`, `TooManyLegs`, `AlreadyHeld`, …) and the registry's
|
|
292
|
+
* failed simulation: the C policy's errors, the F policy's (`Refused`, `NotCanonical`, `FeeNotListed`, `QuoteNotListed`,
|
|
293
|
+
* `BaseNotListed`, `TooManyLegs`, `AlreadyHeld`, …) and the registry's `NotListed`. One entry per distinct signature.
|
|
337
294
|
*/
|
|
338
295
|
type AbiError = { type: "error"; name: string; inputs: readonly { type: string; name?: string }[] };
|
|
339
296
|
const isAbiError = (x: { type: string }): x is AbiError => x.type === "error";
|
|
@@ -342,10 +299,10 @@ export const gateErrorAbi: readonly AbiError[] = (() => {
|
|
|
342
299
|
const sig = (x: AbiError) => `${x.name}(${x.inputs.map((i) => i.type).join(",")})`;
|
|
343
300
|
const all = (abi: readonly unknown[]) => (abi as readonly { type: string }[]).filter(isAbiError);
|
|
344
301
|
const errors: AbiError[] = [
|
|
345
|
-
...all(
|
|
302
|
+
...all(policyCAbi),
|
|
346
303
|
...all(policyFAbi),
|
|
347
|
-
//
|
|
348
|
-
...all(policyRegistryAbi).filter((x) =>
|
|
304
|
+
// who creates under a policy is the policy's rule (C 18, decision 100); the registry only lists the policy
|
|
305
|
+
...all(policyRegistryAbi).filter((x) => x.name === "NotListed"),
|
|
349
306
|
];
|
|
350
307
|
return errors.filter((x) => (seen.has(sig(x)) ? false : (seen.add(sig(x)), true)));
|
|
351
308
|
})();
|
package/src/chains.ts
CHANGED
|
@@ -168,7 +168,7 @@ export interface LogswapDeployment {
|
|
|
168
168
|
polPolicy: Address;
|
|
169
169
|
/** The LOG's pad policy on F (`PolicyF`, `seedToDead`: income-only harvests, never closes). */
|
|
170
170
|
padPolicy: Address;
|
|
171
|
-
/** The LOG's open
|
|
171
|
+
/** The LOG's open policy on C (`PolicyC`, `open`: anyone creates at salt zero, no gate ever). Absent while the chain has no C stack. */
|
|
172
172
|
publicPolicyC?: Address;
|
|
173
173
|
/** The devnet's token factory; local deployments only. */
|
|
174
174
|
tokenFactory?: Address;
|
package/src/client.ts
CHANGED
|
@@ -27,7 +27,7 @@ export interface LogswapAddresses {
|
|
|
27
27
|
* The C stack is optional: F ships first, and a chain that has not opted into C carries none.
|
|
28
28
|
*/
|
|
29
29
|
managerC?: Address;
|
|
30
|
-
/** C's user entry: the batch, the named one-action entries,
|
|
30
|
+
/** C's user entry: the batch, the named one-action entries, Permit2 (a market is opened on the manager). Its own Permit2 spender. */
|
|
31
31
|
routerC?: Address;
|
|
32
32
|
/** C's read-only lens: ladder derivations and views. */
|
|
33
33
|
lensC?: Address;
|
|
@@ -42,7 +42,7 @@ export interface LogswapAddresses {
|
|
|
42
42
|
privatePolicy?: Address;
|
|
43
43
|
polPolicy?: Address;
|
|
44
44
|
padPolicy?: Address;
|
|
45
|
-
/** The LOG's open
|
|
45
|
+
/** The LOG's open policy on C (`PolicyC`): the policy of a public market. Absent with no C stack. */
|
|
46
46
|
publicPolicyC?: Address;
|
|
47
47
|
/**
|
|
48
48
|
* The policy registry: what a pool of each kind may be born under, and the
|
|
@@ -219,7 +219,7 @@ function guardChain(wallet: WalletClient, pub: PublicClient): WalletClient {
|
|
|
219
219
|
* **8** since 2026-10-13 (logswap-contract decision 066): the collector is split into two roles —
|
|
220
220
|
* `feeCollector` / `proposeCollector` / `acceptCollector` are `treasury` / `proposeTreasury` (the
|
|
221
221
|
* LOG Authority's) / `acceptTreasury`; `setSwapFeeTax` and `setGate` leave the manager: the tax is
|
|
222
|
-
* each market's, answered by its policy at birth (
|
|
222
|
+
* each market's, answered by its policy at birth (`PolicyC.swapFeeTax`, an immutable; `SponsorPolicyC` before C 18) and
|
|
223
223
|
* read as `getPool(key).swapFeeTax`; the gate is fixed at construction; `collectProtocolFees(key)` is
|
|
224
224
|
* permissionless and pays the treasury. No pool id moved.
|
|
225
225
|
* 9 (decision 071): the events — one `Swap` on both managers (`taker`, `feeToken`), `CSwapState`, C's `swap`
|
|
@@ -243,7 +243,18 @@ function guardChain(wallet: WalletClient, pub: PublicClient): WalletClient {
|
|
|
243
243
|
// `PermissionsSet` are `setGates(poolId, gates, operator)` / `GatesSet`; the birth answer is `(flags, tax, operator)`; the policy's
|
|
244
244
|
// numbers (tiers, quotes, spacings, tax) are constructor immutables; the swap and mint gates are one bit each (`GATE_SWAP`,
|
|
245
245
|
// `GATE_MINT`) and the cached operator passes with no policy call. No pool id moved.
|
|
246
|
-
|
|
246
|
+
// 17 (decision 107): every event names its actor and the routers emit none (`RouterCall` is gone). `mint(poolId, payer, to, floor, cap, L)`;
|
|
247
|
+
// `PositionMinted(poolId, to, id, sender, payer, shares, dBase, dQuote)`; `ProtocolFeesCollected(poolId, sender, to, amount)`;
|
|
248
|
+
// `ClassRegistered(poolId, id, floor, cap, capped)` (poolId first); the account acted on is `owner` on `PositionBurned` (was `from`),
|
|
249
|
+
// `PositionUpdated` and `FeesClaimed` (was `holder`); `RouterC`'s entries and `Settle` name the receiver `to` (was `recipient`). No pool id moved.
|
|
250
|
+
// 18 (decisions 099, 100): `PoolKeyC` is `(quote, base, swapFeeIdx, spacingIdx, policy, salt)` — every C pool id and position id moved; the
|
|
251
|
+
// fee is an index into the family's list (the mint fee the next entry) and the spacing an index into `[1e15, 1e16, 1e17]`; `initialize(key, x0)`
|
|
252
|
+
// mints the backstop in the call (the caller pays dust of both tokens by allowance to the manager) and `RouterC.createMarket` is gone;
|
|
253
|
+
// `PoolCreated` carries the two indices; `claim(poolId, id, owner, to)`; `PoolView` has no `backstopSeeded`; `LensC.params()` is
|
|
254
|
+
// `(spacingList, noCap, cVersion)`; `PolicyC` replaces `SponsorPolicyC` (F's gate surface plus `baseListed`, `swapFeeTax`, `mayInit`); the
|
|
255
|
+
// registry lost its creation modes (`admit`, `creationMode`, `setCreationMode`, `sponsorOf`). `SpacingZero`, `BackstopTooSmall`,
|
|
256
|
+
// `BackstopMustBeUncapped` and `NotCreator` are gone, `PolicyNotApproved` is new.
|
|
257
|
+
export const EXPECTED_VERSION = 20n;
|
|
247
258
|
/**
|
|
248
259
|
* The F manager's own tag (5 — the key's salt, pool names, the sponsor registry;
|
|
249
260
|
* 4 was the income-only harvest cut).
|
|
@@ -362,7 +373,12 @@ export const EXPECTED_VERSION = 16n;
|
|
|
362
373
|
// `IncomeHarvested(poolId, to, gen, income, capital, tax, toOperator, Q)`; `GenerationFrozen` from `close` alone; `IncomeReleased` and
|
|
363
374
|
// `PolicyMigrated` are gone. The policy's name is an event (`NameSet`), its lists are per policy (`setRights`, `RightsSet`), its
|
|
364
375
|
// modes per pool (`setSwapMode` / `setMintMode`, each Open < Listed < Operator with `mintMode ≥ swapMode`).
|
|
365
|
-
|
|
376
|
+
// 42 (decision 107): every event names its actor and the routers emit none (`RouterCall` is gone). `mint(poolId, payer, to, L, maxQuoteIn)`;
|
|
377
|
+
// `claim(poolId, gen, owner, to)` (the owner or its ERC-6909 operator); `SharesMinted(poolId, to, gen, sender, payer, L, shares, dQuote,
|
|
378
|
+
// poolL, supply)`; `SharesBurned` indexes `gen` and names `owner` (was `from`), `DividendClaimed` names `owner` (was `holder`);
|
|
379
|
+
// `PoolCreated(poolId, quote, creator, policy, bases, swapFeeIdx, salt)` (`creator` a topic, `policy` data); `LiquidityDeployed`,
|
|
380
|
+
// `IncomeHarvested` and `GenerationFrozen` carry `operator` as a topic; `RouterF.BadToken` is `WrongToken`. No pool id moved.
|
|
381
|
+
export const EXPECTED_F_VERSION = 44n;
|
|
366
382
|
|
|
367
383
|
export async function assertVersion(c: LogswapClient, expected = EXPECTED_VERSION): Promise<void> {
|
|
368
384
|
const { managerCAbi, managerFAbi } = await import("./generated.js");
|