@logswap/sdk 0.5.102 → 0.5.104
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 +14 -418
- package/dist/actions.d.ts +0 -39
- package/dist/actions.js +0 -39
- package/dist/admin.d.ts +0 -106
- package/dist/admin.js +1 -96
- package/dist/chains.d.ts +0 -49
- package/dist/chains.js +0 -30
- package/dist/claims.d.ts +0 -32
- package/dist/claims.js +0 -30
- package/dist/client.d.ts +0 -116
- package/dist/client.js +1 -231
- package/dist/fees.d.ts +1 -45
- package/dist/fees.js +0 -44
- package/dist/fpool.d.ts +4 -612
- package/dist/fpool.js +14 -490
- package/dist/generated.js +0 -30
- package/dist/history.d.ts +0 -61
- package/dist/history.js +5 -74
- package/dist/ids.d.ts +0 -30
- package/dist/ids.js +0 -31
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -7
- package/dist/indexer.d.ts +0 -68
- package/dist/indexer.js +1 -77
- package/dist/keys.d.ts +0 -50
- package/dist/keys.js +0 -35
- package/dist/liquidity.d.ts +0 -128
- package/dist/liquidity.js +1 -107
- package/dist/onboard.d.ts +0 -52
- package/dist/onboard.js +1 -59
- package/dist/pools.d.ts +3 -157
- package/dist/pools.js +1 -164
- package/dist/positions.d.ts +0 -102
- package/dist/positions.js +1 -90
- package/dist/quote.d.ts +0 -14
- package/dist/quote.js +0 -14
- package/dist/swap.d.ts +0 -31
- package/dist/swap.js +5 -37
- package/dist/tokencache.d.ts +0 -48
- package/dist/tokencache.js +1 -73
- package/dist/write.d.ts +1 -87
- package/dist/write.js +1 -87
- package/package.json +3 -8
- package/src/actions.ts +0 -208
- package/src/admin.ts +0 -350
- package/src/chains.ts +0 -278
- package/src/claims.ts +0 -111
- package/src/client.ts +0 -436
- package/src/fees.ts +0 -138
- package/src/fpool.ts +0 -2049
- package/src/generated.ts +0 -4345
- package/src/history.ts +0 -383
- package/src/ids.ts +0 -78
- package/src/index.ts +0 -26
- package/src/indexer.ts +0 -264
- package/src/keys.ts +0 -138
- package/src/liquidity.ts +0 -456
- package/src/onboard.ts +0 -253
- package/src/pools.ts +0 -465
- package/src/positions.ts +0 -297
- package/src/quote.ts +0 -36
- package/src/swap.ts +0 -181
- package/src/tokencache.ts +0 -178
- package/src/write.ts +0 -236
package/src/liquidity.ts
DELETED
|
@@ -1,456 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* liquidity.ts — providing, editing and exiting a C position.
|
|
3
|
-
*
|
|
4
|
-
* A class is (ξ, μ, L): floor, cap, exposure. One primitive sets all three — `update` — and every
|
|
5
|
-
* edit is a constraint on it. The vocabulary follows the geometry: **the verb
|
|
6
|
-
* names the axis, the direction is a signed rung count**, and only two direction-words survive,
|
|
7
|
-
* because each names a wallet flow the user must see.
|
|
8
|
-
*
|
|
9
|
-
* update(ξ', μ', L') the primitive
|
|
10
|
-
* ├─ move(ξ', μ') L pinned
|
|
11
|
-
* │ ├─ floor(±n) μ pinned · ↑ harvest (receive quote) · ↓ deepen (pay quote)
|
|
12
|
-
* │ ├─ cap(±n) ξ pinned · no direction words
|
|
13
|
-
* │ └─ shift(±n) width pinned — the translation
|
|
14
|
-
* ├─ resize(L') ξ, μ pinned; L' = 0 is the exit
|
|
15
|
-
* └─ reprice(ξ') R pinned: L' = L·e^{ξ' − ξ}, dormant only
|
|
16
|
-
* mint · burn · claim
|
|
17
|
-
*
|
|
18
|
-
* Every edit is ONE action of the router's batch (`actions.ts`), which is what
|
|
19
|
-
* lets it settle in kind, or in base, or in quote: `settleIn` appends the one swap that nets the
|
|
20
|
-
* other side, and the batch moves a single token. `previewEdit` is the lens's `previewUpdate` —
|
|
21
|
-
* the exact flows, mint fee and swept fees included — and is what an editor shows before its one
|
|
22
|
-
* apply.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import type { Address, Hash } from "viem";
|
|
26
|
-
import { lensCAbi, routerCAbi } from "./generated.js";
|
|
27
|
-
import { lensC, type LogswapClient } from "./client.js";
|
|
28
|
-
import { poolId as keyId, tickSpacingOf, type PoolKey } from "./keys.js";
|
|
29
|
-
import { NO_CAP } from "./ids.js";
|
|
30
|
-
import { describeId, getPositionClass } from "./positions.js";
|
|
31
|
-
import {
|
|
32
|
-
execute,
|
|
33
|
-
OPEN_DELTA,
|
|
34
|
-
settleInKind,
|
|
35
|
-
settleOnly,
|
|
36
|
-
swapExactInAction,
|
|
37
|
-
swapExactOutAction,
|
|
38
|
-
updateAction,
|
|
39
|
-
type Action,
|
|
40
|
-
type PairBounds,
|
|
41
|
-
type Settle,
|
|
42
|
-
} from "./actions.js";
|
|
43
|
-
import {
|
|
44
|
-
requireWallet,
|
|
45
|
-
resolveDeadline,
|
|
46
|
-
resolveRecipient,
|
|
47
|
-
sendRouterWrite,
|
|
48
|
-
sendRouterWriteWithPermits,
|
|
49
|
-
withSlippage,
|
|
50
|
-
type WriteOptions,
|
|
51
|
-
} from "./write.js";
|
|
52
|
-
import { quoteExactIn, quoteExactOut } from "./swap.js";
|
|
53
|
-
|
|
54
|
-
/* mint ====================================================================== */
|
|
55
|
-
|
|
56
|
-
export interface MintArgs extends WriteOptions {
|
|
57
|
-
key: PoolKey;
|
|
58
|
-
/** Log-price floor. Need not be on the grid — see the blending note below. */
|
|
59
|
-
targetFloor: bigint;
|
|
60
|
-
cap?: bigint;
|
|
61
|
-
/** Exposure to add. */
|
|
62
|
-
L: bigint;
|
|
63
|
-
/**
|
|
64
|
-
* The most the mint may pull, per token. REQUIRED: the pulled amounts depend on spot when the
|
|
65
|
-
* transaction lands, so an unbounded mint can be sandwiched into depositing far more than the
|
|
66
|
-
* preview showed. Derive them from the preview with {@link withSlippage} (`"input"`); pass
|
|
67
|
-
* {@link UNBOUNDED} only where the price cannot move under you.
|
|
68
|
-
*/
|
|
69
|
-
maxBaseIn: bigint;
|
|
70
|
-
maxQuoteIn: bigint;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Mint at `targetFloor`, blending across the two adjacent rungs when it is off-grid.
|
|
75
|
-
*
|
|
76
|
-
* This is why the floor ladder can be coarse without the UX being coarse: a blend of two adjacent
|
|
77
|
-
* classes IS exactly one position at the weighted-mean floor, so a continuous floor slider works
|
|
78
|
-
* over a discrete ladder. The router settles once per token, not once per rung.
|
|
79
|
-
*
|
|
80
|
-
* **A mint at floor == spot costs zero quote** ($Q(\xi) = 0$), and one above spot is dormant and
|
|
81
|
-
* costs zero quote too — that is what makes single-asset entry possible in both directions.
|
|
82
|
-
*/
|
|
83
|
-
export async function mint(c: LogswapClient, a: MintArgs): Promise<Hash> {
|
|
84
|
-
return sendRouterWriteWithPermits(c, {
|
|
85
|
-
side: "C",
|
|
86
|
-
abi: routerCAbi,
|
|
87
|
-
functionName: "mintBlended",
|
|
88
|
-
args: [
|
|
89
|
-
keyId(a.key),
|
|
90
|
-
a.key.base,
|
|
91
|
-
a.key.quote,
|
|
92
|
-
a.targetFloor,
|
|
93
|
-
a.cap ?? NO_CAP,
|
|
94
|
-
a.L,
|
|
95
|
-
a.maxBaseIn,
|
|
96
|
-
a.maxQuoteIn,
|
|
97
|
-
resolveRecipient(c, a),
|
|
98
|
-
resolveDeadline(a),
|
|
99
|
-
],
|
|
100
|
-
}, [a.key.base, a.key.quote]);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export interface ZapArgs extends WriteOptions {
|
|
104
|
-
key: PoolKey;
|
|
105
|
-
/** Fund entirely from base (true) or entirely from quote (false). */
|
|
106
|
-
fundWithBase: boolean;
|
|
107
|
-
amountIn: bigint;
|
|
108
|
-
/** The least the swap leg may return: the zap's own slippage bound, as `swapExactIn`'s. Required. */
|
|
109
|
-
minSwapOut: bigint;
|
|
110
|
-
targetFloor: bigint;
|
|
111
|
-
cap?: bigint;
|
|
112
|
-
L: bigint;
|
|
113
|
-
/** The most of the funding token the zap may pull. Required. */
|
|
114
|
-
maxIn: bigint;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Fund a mint from a single asset: the router swaps the imbalanced part through the curve, then
|
|
119
|
-
* mints, all inside one lock — the two-action batch `[swapExactIn, mint]`.
|
|
120
|
-
*
|
|
121
|
-
* Routing the imbalanced part through the CURVE is the point, not an implementation detail. A
|
|
122
|
-
* pro-rata deposit of one asset would be a zero-slippage, zero-fee swap at stale spot — a subsidy
|
|
123
|
-
* paid by the incumbent LPs. Paying the curve removes it.
|
|
124
|
-
*/
|
|
125
|
-
export async function zapIn(c: LogswapClient, a: ZapArgs): Promise<Hash> {
|
|
126
|
-
return sendRouterWriteWithPermits(c, {
|
|
127
|
-
side: "C",
|
|
128
|
-
abi: routerCAbi,
|
|
129
|
-
functionName: "zapIn",
|
|
130
|
-
args: [
|
|
131
|
-
keyId(a.key),
|
|
132
|
-
a.key.base,
|
|
133
|
-
a.key.quote,
|
|
134
|
-
a.fundWithBase,
|
|
135
|
-
a.amountIn,
|
|
136
|
-
a.minSwapOut,
|
|
137
|
-
a.targetFloor,
|
|
138
|
-
a.cap ?? NO_CAP,
|
|
139
|
-
a.L,
|
|
140
|
-
a.maxIn,
|
|
141
|
-
resolveRecipient(c, a),
|
|
142
|
-
resolveDeadline(a),
|
|
143
|
-
],
|
|
144
|
-
}, [a.fundWithBase ? a.key.base : a.key.quote]);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/* the primitive, and its named entry ======================================== */
|
|
148
|
-
|
|
149
|
-
export interface UpdateArgs extends WriteOptions {
|
|
150
|
-
key: PoolKey;
|
|
151
|
-
fromId: bigint;
|
|
152
|
-
shares: bigint;
|
|
153
|
-
toFloor: bigint;
|
|
154
|
-
toCap?: bigint;
|
|
155
|
-
/** New exposure. `0n` exits the position entirely. */
|
|
156
|
-
newL: bigint;
|
|
157
|
-
/** Required: see {@link MintArgs}. {@link previewEdit} gives the exact flows to bound. */
|
|
158
|
-
maxBaseIn: bigint;
|
|
159
|
-
maxQuoteIn: bigint;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/** The primitive, through the router's named entry: resize and reposition in one call, in kind. */
|
|
163
|
-
export async function update(c: LogswapClient, a: UpdateArgs): Promise<Hash> {
|
|
164
|
-
return sendRouterWriteWithPermits(c, {
|
|
165
|
-
side: "C",
|
|
166
|
-
abi: routerCAbi,
|
|
167
|
-
functionName: "update",
|
|
168
|
-
args: [
|
|
169
|
-
keyId(a.key),
|
|
170
|
-
a.key.base,
|
|
171
|
-
a.key.quote,
|
|
172
|
-
a.fromId,
|
|
173
|
-
a.shares,
|
|
174
|
-
a.toFloor,
|
|
175
|
-
a.toCap ?? NO_CAP,
|
|
176
|
-
a.newL,
|
|
177
|
-
a.maxBaseIn,
|
|
178
|
-
a.maxQuoteIn,
|
|
179
|
-
resolveRecipient(c, a),
|
|
180
|
-
resolveDeadline(a),
|
|
181
|
-
],
|
|
182
|
-
}, [a.key.base, a.key.quote]);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export interface MoveArgs extends WriteOptions {
|
|
186
|
-
key: PoolKey;
|
|
187
|
-
fromId: bigint;
|
|
188
|
-
shares: bigint;
|
|
189
|
-
toFloor: bigint;
|
|
190
|
-
toCap?: bigint;
|
|
191
|
-
/** Required: see {@link MintArgs}. */
|
|
192
|
-
maxBaseIn: bigint;
|
|
193
|
-
maxQuoteIn: bigint;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Reposition at constant exposure — the L-pinned parent of `floor`, `cap` and `shift`; in kind.
|
|
198
|
-
*
|
|
199
|
-
* **Bound it.** The router used to settle this entry with no limit, which meant a reposition that
|
|
200
|
-
* priced differently by inclusion time could pull the whole allowance.
|
|
201
|
-
* The bounds are required; {@link UNBOUNDED} is an explicit, visible opt-out.
|
|
202
|
-
*/
|
|
203
|
-
export async function move(c: LogswapClient, a: MoveArgs): Promise<Hash> {
|
|
204
|
-
return sendRouterWrite(c, {
|
|
205
|
-
side: "C",
|
|
206
|
-
abi: routerCAbi,
|
|
207
|
-
functionName: "move",
|
|
208
|
-
args: [
|
|
209
|
-
keyId(a.key),
|
|
210
|
-
a.key.base,
|
|
211
|
-
a.key.quote,
|
|
212
|
-
a.fromId,
|
|
213
|
-
a.shares,
|
|
214
|
-
a.toFloor,
|
|
215
|
-
a.toCap ?? NO_CAP,
|
|
216
|
-
a.maxBaseIn,
|
|
217
|
-
a.maxQuoteIn,
|
|
218
|
-
resolveRecipient(c, a),
|
|
219
|
-
resolveDeadline(a),
|
|
220
|
-
],
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/* the edit tree ============================================================= */
|
|
225
|
-
|
|
226
|
-
/** Which token the edit settles in. `kind` moves both legs; `base` / `quote` nets the other side through the curve. */
|
|
227
|
-
export type SettleIn = "kind" | "base" | "quote";
|
|
228
|
-
|
|
229
|
-
export interface EditArgs extends WriteOptions, PairBounds {
|
|
230
|
-
key: PoolKey;
|
|
231
|
-
fromId: bigint;
|
|
232
|
-
/** The shares to edit — all of a holding, or part of it. */
|
|
233
|
-
shares: bigint;
|
|
234
|
-
/** Default `kind`. */
|
|
235
|
-
settleIn?: SettleIn;
|
|
236
|
-
/**
|
|
237
|
-
* Tolerance in basis points, applied to the flows the edit is expected to move (the lens's
|
|
238
|
-
* exact preview, plus a quote of the netting swap when `settleIn` is one token). Required: an
|
|
239
|
-
* edit's flows depend on spot when it lands. An explicit bound in `PairBounds` overrides the one
|
|
240
|
-
* derived for that token.
|
|
241
|
-
*/
|
|
242
|
-
slippageBps: number;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export interface EditPreview {
|
|
246
|
-
/** Signed, + = the caller deposits; the mint fee is inside `dQuote`. */
|
|
247
|
-
dBase: bigint;
|
|
248
|
-
dQuote: bigint;
|
|
249
|
-
/** Accrued fees the edit sweeps, delivered in the same settlement. */
|
|
250
|
-
fees: bigint;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/** The exact flows of an edit before it is sent — the lens's `previewUpdate`. */
|
|
254
|
-
export async function previewEdit(
|
|
255
|
-
c: LogswapClient,
|
|
256
|
-
a: { key: PoolKey; fromId: bigint; holder?: Address; shares: bigint; toFloor: bigint; toCap?: bigint; newL: bigint },
|
|
257
|
-
): Promise<EditPreview> {
|
|
258
|
-
const holder = a.holder ?? requireWallet(c).address;
|
|
259
|
-
const r = (await c.public.readContract({
|
|
260
|
-
address: lensC(c),
|
|
261
|
-
abi: lensCAbi,
|
|
262
|
-
functionName: "previewUpdate",
|
|
263
|
-
args: [keyId(a.key), a.fromId, holder, a.shares, a.toFloor, a.toCap ?? NO_CAP, a.newL],
|
|
264
|
-
})) as readonly [bigint, bigint, bigint];
|
|
265
|
-
return { dBase: r[0], dQuote: r[1], fees: r[2] };
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Bounds for a pair of signed flows (+ = the caller pays, − = the caller receives), at `bps` of
|
|
270
|
-
* tolerance: a payment is capped from above, a receipt floored from below, and a token that does
|
|
271
|
-
* not move in that direction is bound to zero.
|
|
272
|
-
*/
|
|
273
|
-
export function boundsFromFlows(dBase: bigint, dQuote: bigint, bps: number): Required<PairBounds> {
|
|
274
|
-
const inb = (d: bigint) => (d > 0n ? withSlippage(d, bps, "input") : 0n);
|
|
275
|
-
const outb = (d: bigint) => (d < 0n ? withSlippage(-d, bps, "output") : 0n);
|
|
276
|
-
return { maxBaseIn: inb(dBase), maxQuoteIn: inb(dQuote), minBaseOut: outb(dBase), minQuoteOut: outb(dQuote) };
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* The general edit as a batch: `[update]`, plus — when `settleIn` is one token — the one swap that
|
|
281
|
-
* nets the other. The other side's flow is read off the preview: released → sold with an exact-in
|
|
282
|
-
* of `OPEN_DELTA`; owed → bought with an exact-out of `OPEN_DELTA`. The settlement then binds the
|
|
283
|
-
* other token to zero both ways, so a stale preview fails loudly rather than moving it.
|
|
284
|
-
*
|
|
285
|
-
* **Bounded by default.** The preview gives the edit's exact flows at today's state; `slippageBps`
|
|
286
|
-
* turns them into the settlement's bounds. With `settleIn` one token, the netting swap is quoted
|
|
287
|
-
* on today's curve, before the update changes the active liquidity — close for a small edit, and
|
|
288
|
-
* for a large one a bound that reverts rather than one that lets a sandwich through.
|
|
289
|
-
*/
|
|
290
|
-
export async function edit(c: LogswapClient, a: EditArgs & { toFloor: bigint; toCap?: bigint; newL: bigint }): Promise<Hash> {
|
|
291
|
-
const { key } = a;
|
|
292
|
-
const recipient = resolveRecipient(c, a);
|
|
293
|
-
const actions: Action[] = [
|
|
294
|
-
updateAction({ key, fromId: a.fromId, shares: a.shares, toFloor: a.toFloor, toCap: a.toCap, newL: a.newL }),
|
|
295
|
-
];
|
|
296
|
-
const settleIn = a.settleIn ?? "kind";
|
|
297
|
-
const p = await previewEdit(c, { key, fromId: a.fromId, shares: a.shares, toFloor: a.toFloor, toCap: a.toCap, newL: a.newL });
|
|
298
|
-
let settle: Settle;
|
|
299
|
-
if (settleIn === "kind") {
|
|
300
|
-
settle = settleInKind(key, recipient, { ...boundsFromFlows(p.dBase, p.dQuote, a.slippageBps), ...defined(a) });
|
|
301
|
-
} else {
|
|
302
|
-
// the swept fees ride INSIDE dQuote (the manager burns by owner and nets them),
|
|
303
|
-
// so quote's open delta after the update is dQuote itself — `fees` is reported for display only
|
|
304
|
-
const otherFlow = settleIn === "base" ? p.dQuote : p.dBase; // + = owed, − = released
|
|
305
|
-
const otherIsBase = settleIn === "quote";
|
|
306
|
-
let own = settleIn === "base" ? p.dBase : p.dQuote;
|
|
307
|
-
if (otherFlow < 0n) {
|
|
308
|
-
actions.push(swapExactInAction({ key, baseIn: otherIsBase, amountIn: OPEN_DELTA }));
|
|
309
|
-
own -= await quoteExactIn(c, key, otherIsBase, -otherFlow);
|
|
310
|
-
} else if (otherFlow > 0n) {
|
|
311
|
-
actions.push(swapExactOutAction({ key, baseOut: otherIsBase, amountOut: OPEN_DELTA }));
|
|
312
|
-
own += await quoteExactOut(c, key, otherIsBase, otherFlow);
|
|
313
|
-
}
|
|
314
|
-
const b = settleIn === "base" ? boundsFromFlows(own, 0n, a.slippageBps) : boundsFromFlows(0n, own, a.slippageBps);
|
|
315
|
-
settle = settleOnly(key, recipient, settleIn, {
|
|
316
|
-
maxIn: settleIn === "base" ? (a.maxBaseIn ?? b.maxBaseIn) : (a.maxQuoteIn ?? b.maxQuoteIn),
|
|
317
|
-
minOut: settleIn === "base" ? (a.minBaseOut ?? b.minBaseOut) : (a.minQuoteOut ?? b.minQuoteOut),
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
return execute(c, { actions, settle, deadline: a.deadline, recipient });
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/** The explicit bounds a caller passed, without the undefined ones (so they do not erase derived ones). */
|
|
324
|
-
function defined(b: PairBounds): PairBounds {
|
|
325
|
-
return Object.fromEntries(
|
|
326
|
-
(["maxBaseIn", "maxQuoteIn", "minBaseOut", "minQuoteOut"] as const).filter((k) => b[k] !== undefined).map((k) => [k, b[k]]),
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/** The class behind `shares` of `id`: its floor, cap, and the exposure those shares carry. */
|
|
331
|
-
async function classOf(c: LogswapClient, key: PoolKey, id: bigint, shares: bigint) {
|
|
332
|
-
const [d, cls] = await Promise.all([describeId(c, id, tickSpacingOf(key)), getPositionClass(c, id)]);
|
|
333
|
-
if (d.poolId === `0x${"0".repeat(64)}`) throw new Error(`logswap: id ${id} was never minted`);
|
|
334
|
-
const L = cls.shares === 0n ? 0n : (cls.L * shares) / cls.shares;
|
|
335
|
-
return { floor: d.floor!, cap: d.cap!, capped: d.capped, L };
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/** Move the floor by `rungs` (signed), cap and L pinned. Positive = harvest, negative = deepen. */
|
|
339
|
-
export async function floor(c: LogswapClient, a: EditArgs & { rungs: bigint }): Promise<Hash> {
|
|
340
|
-
const k = await classOf(c, a.key, a.fromId, a.shares);
|
|
341
|
-
return edit(c, { ...a, toFloor: k.floor + a.rungs * tickSpacingOf(a.key), toCap: k.capped ? k.cap : undefined, newL: k.L });
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Raise the floor by `rungs` (default 1) and withdraw PURE QUOTE — the base leg untouched.
|
|
346
|
-
*
|
|
347
|
-
* A burn is in kind: it returns base and quote pro rata, so taking α of the proceeds also takes α
|
|
348
|
-
* of the unsold inventory off the market. Raising the floor instead releases
|
|
349
|
-
*
|
|
350
|
-
* ΔQ = L (ξ' − ξ), ΔR = 0
|
|
351
|
-
*
|
|
352
|
-
* because $R = Le^{-x}$ depends on neither the floor nor the withdrawal. No base moves beyond one
|
|
353
|
-
* wei (the edit's mint half rounds what it pays in up; the preview returns it), the price
|
|
354
|
-
* does not move, and depth is unchanged. **The cost, which a UI must show:** the position stops
|
|
355
|
-
* bidding below ξ'. `settleIn: "base"` swaps the proceeds into base in the same batch.
|
|
356
|
-
*/
|
|
357
|
-
export async function harvest(c: LogswapClient, a: EditArgs & { rungs?: bigint }): Promise<Hash> {
|
|
358
|
-
const n = a.rungs ?? 1n;
|
|
359
|
-
if (n <= 0n) throw new Error("logswap: harvest raises the floor — rungs must be positive; to lower it, deepen()");
|
|
360
|
-
return floor(c, { ...a, rungs: n });
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Lower the floor by `rungs` (default 1), paying L·Δξ of quote for the deeper bid — harvest's
|
|
365
|
-
* mirror. `settleIn: "base"` pays it in base instead (an exact-out of the quote owed).
|
|
366
|
-
*/
|
|
367
|
-
export async function deepen(c: LogswapClient, a: EditArgs & { rungs?: bigint }): Promise<Hash> {
|
|
368
|
-
const n = a.rungs ?? 1n;
|
|
369
|
-
if (n <= 0n) throw new Error("logswap: deepen lowers the floor — rungs must be positive; to raise it, harvest()");
|
|
370
|
-
return floor(c, { ...a, rungs: -n });
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/** Move the cap by `rungs` (signed), floor and L pinned. Base flows while the class converts; quote once capped out. */
|
|
374
|
-
export async function cap(c: LogswapClient, a: EditArgs & { rungs: bigint }): Promise<Hash> {
|
|
375
|
-
const k = await classOf(c, a.key, a.fromId, a.shares);
|
|
376
|
-
if (!k.capped) throw new Error("logswap: cap() needs a capped class — an uncapped one has no cap to move");
|
|
377
|
-
const toCap = k.cap + a.rungs * tickSpacingOf(a.key);
|
|
378
|
-
if (toCap <= k.floor) throw new Error("logswap: the cap cannot sit at or below the floor");
|
|
379
|
-
return edit(c, { ...a, toFloor: k.floor, toCap, newL: k.L });
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
/** Translate the class by `rungs` (signed): floor and cap together, width and L pinned. */
|
|
383
|
-
export async function shift(c: LogswapClient, a: EditArgs & { rungs: bigint }): Promise<Hash> {
|
|
384
|
-
const k = await classOf(c, a.key, a.fromId, a.shares);
|
|
385
|
-
const d = a.rungs * tickSpacingOf(a.key);
|
|
386
|
-
return edit(c, { ...a, toFloor: k.floor + d, toCap: k.capped ? k.cap + d : undefined, newL: k.L });
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/** Set the exposure to `newL`, floor and cap pinned; both legs pro rata. The mint fee bites on an increase only. */
|
|
390
|
-
export async function resize(c: LogswapClient, a: EditArgs & { newL: bigint }): Promise<Hash> {
|
|
391
|
-
const k = await classOf(c, a.key, a.fromId, a.shares);
|
|
392
|
-
return edit(c, { ...a, toFloor: k.floor, toCap: k.capped ? k.cap : undefined, newL: a.newL });
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/** Exit `shares` — `resize(0)`. `settleIn: "quote"` (or `"base"`) leaves with one asset. */
|
|
396
|
-
export async function exit(c: LogswapClient, a: EditArgs): Promise<Hash> {
|
|
397
|
-
const k = await classOf(c, a.key, a.fromId, a.shares);
|
|
398
|
-
return edit(c, { ...a, toFloor: k.floor, toCap: k.capped ? k.cap : undefined, newL: 0n });
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Re-express a DORMANT class's base at a new trigger price: the base is pinned, so
|
|
403
|
-
* L' = L·e^{ξ' − ξ} and nothing moves but the trigger. `toFloor` must stay above spot.
|
|
404
|
-
*
|
|
405
|
-
* The exponential is evaluated in double precision (~1e-16 relative), which is far inside the
|
|
406
|
-
* wei-rounding of the class it produces; an exact L' is not what a trigger edit is about.
|
|
407
|
-
*/
|
|
408
|
-
export async function reprice(c: LogswapClient, a: EditArgs & { toFloor: bigint }): Promise<Hash> {
|
|
409
|
-
const k = await classOf(c, a.key, a.fromId, a.shares);
|
|
410
|
-
const ratio = Math.exp(Number(a.toFloor - k.floor) / 1e18);
|
|
411
|
-
const newL = (k.L * BigInt(Math.round(ratio * 1e18))) / 10n ** 18n;
|
|
412
|
-
return edit(c, { ...a, toFloor: a.toFloor, toCap: k.capped ? k.cap : undefined, newL });
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/** Quote released by a harvest, exactly: ΔQ = L(ξ' − ξ). Base is untouched. */
|
|
416
|
-
export function harvestProceeds(L: bigint, fromFloor: bigint, toFloor: bigint): bigint {
|
|
417
|
-
if (toFloor <= fromFloor) return 0n;
|
|
418
|
-
return (L * (toFloor - fromFloor)) / 10n ** 18n;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/* burn ====================================================================== */
|
|
422
|
-
|
|
423
|
-
export interface BurnArgs extends WriteOptions {
|
|
424
|
-
key: PoolKey;
|
|
425
|
-
ids: readonly bigint[];
|
|
426
|
-
shares: readonly bigint[];
|
|
427
|
-
/** The least of each token the burn must return. Required: a burn's legs depend on spot. */
|
|
428
|
-
minBaseOut: bigint;
|
|
429
|
-
minQuoteOut: bigint;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* Exit, in kind, across one or more classes. Accrued fees are swept into the same settlement.
|
|
434
|
-
*
|
|
435
|
-
* In kind means both legs, pro rata. To take proceeds while leaving inventory in the market, that
|
|
436
|
-
* is {@link harvest}; to leave with one asset, {@link exit} with `settleIn`.
|
|
437
|
-
*/
|
|
438
|
-
export async function burn(c: LogswapClient, a: BurnArgs): Promise<Hash> {
|
|
439
|
-
if (a.ids.length !== a.shares.length) throw new Error("logswap: ids and shares length mismatch");
|
|
440
|
-
return sendRouterWrite(c, {
|
|
441
|
-
side: "C",
|
|
442
|
-
abi: routerCAbi,
|
|
443
|
-
functionName: "burn",
|
|
444
|
-
args: [
|
|
445
|
-
keyId(a.key),
|
|
446
|
-
a.key.base,
|
|
447
|
-
a.key.quote,
|
|
448
|
-
a.ids,
|
|
449
|
-
a.shares,
|
|
450
|
-
a.minBaseOut,
|
|
451
|
-
a.minQuoteOut,
|
|
452
|
-
resolveRecipient(c, a),
|
|
453
|
-
resolveDeadline(a),
|
|
454
|
-
],
|
|
455
|
-
});
|
|
456
|
-
}
|