@logswap/sdk 0.5.88 → 0.5.90
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 +24 -2
- package/dist/actions.d.ts +1 -1
- package/dist/actions.js +13 -12
- package/dist/claims.d.ts +1 -1
- package/dist/claims.js +4 -3
- package/dist/client.d.ts +1 -1
- package/dist/client.js +12 -8
- package/dist/fpool.d.ts +2 -2
- package/dist/fpool.js +27 -27
- package/dist/generated.d.ts +2950 -3945
- package/dist/generated.js +1862 -2341
- package/dist/history.js +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/indexer.d.ts +1 -1
- package/dist/indexer.js +7 -7
- package/dist/liquidity.d.ts +4 -4
- package/dist/liquidity.js +27 -16
- package/dist/onboard.js +2 -2
- package/dist/pools.d.ts +19 -19
- package/dist/pools.js +19 -19
- package/dist/positions.js +9 -9
- package/dist/swap.d.ts +1 -1
- package/dist/swap.js +32 -11
- package/package.json +1 -1
- package/src/actions.ts +13 -13
- package/src/claims.ts +4 -4
- package/src/client.ts +12 -8
- package/src/fpool.ts +29 -29
- package/src/generated.ts +1867 -2344
- package/src/history.ts +1 -1
- package/src/index.ts +3 -1
- package/src/indexer.ts +8 -8
- package/src/liquidity.ts +29 -19
- package/src/onboard.ts +2 -2
- package/src/pools.ts +39 -38
- package/src/positions.ts +9 -9
- package/src/swap.ts +32 -12
package/dist/history.js
CHANGED
|
@@ -72,7 +72,7 @@ export function indexedContractOf(kind, event) {
|
|
|
72
72
|
}
|
|
73
73
|
/** Events whose name two indexed contracts share: postgraphile prefixes their tables. */
|
|
74
74
|
// `CollectorProposed` / `CollectorAccepted` are C 7 / F 26 and older; `TreasuryProposed` / `TreasuryAccepted` replace them (066).
|
|
75
|
-
const SHARED = new Set(["Swap", "Transfer", "Approval", "OperatorSet", "CollectorProposed", "CollectorAccepted", "TreasuryProposed", "TreasuryAccepted", "Approved"]);
|
|
75
|
+
const SHARED = new Set(["PoolCreated", "Swap", "Transfer", "Approval", "OperatorSet", "CollectorProposed", "CollectorAccepted", "TreasuryProposed", "TreasuryAccepted", "Approved"]);
|
|
76
76
|
const plural = (n) => (/(s|x|ch|sh)$/.test(n) ? `${n}es` : `${n}s`);
|
|
77
77
|
/** A column's GraphQL name: postgraphile camelCases the snake_case rindexer writes (`F` → `f`). */
|
|
78
78
|
const column = (name) => name[0].toLowerCase() + name.slice(1);
|
package/dist/index.d.ts
CHANGED
|
@@ -23,5 +23,7 @@ export * from "./fpool.js";
|
|
|
23
23
|
export * from "./fees.js";
|
|
24
24
|
export * from "./history.js";
|
|
25
25
|
export * from "./views.js";
|
|
26
|
-
export { cPoolManagerAbi, fPoolManagerAbi,
|
|
26
|
+
export { cPoolManagerAbi, fPoolManagerAbi, lensAbi, routerAbi, routerActionsAbi, padPolicyAbi, polPolicyAbi, policyRegistryAbi, privatePolicyAbi, publicPolicyAbi } from "./generated.js";
|
|
27
|
+
/** @deprecated the `Logswap` prefix is gone (decision 091): use `routerAbi` / `lensAbi`. Removed in the next minor. */
|
|
28
|
+
export { routerAbi as logswapRouterAbi, lensAbi as logswapLensAbi } from "./generated.js";
|
|
27
29
|
export * from "./admin.js";
|
package/dist/index.js
CHANGED
|
@@ -23,5 +23,7 @@ export * from "./fpool.js";
|
|
|
23
23
|
export * from "./fees.js";
|
|
24
24
|
export * from "./history.js";
|
|
25
25
|
export * from "./views.js";
|
|
26
|
-
export { cPoolManagerAbi, fPoolManagerAbi,
|
|
26
|
+
export { cPoolManagerAbi, fPoolManagerAbi, lensAbi, routerAbi, routerActionsAbi, padPolicyAbi, polPolicyAbi, policyRegistryAbi, privatePolicyAbi, publicPolicyAbi } from "./generated.js";
|
|
27
|
+
/** @deprecated the `Logswap` prefix is gone (decision 091): use `routerAbi` / `lensAbi`. Removed in the next minor. */
|
|
28
|
+
export { routerAbi as logswapRouterAbi, lensAbi as logswapLensAbi } from "./generated.js";
|
|
27
29
|
export * from "./admin.js";
|
package/dist/indexer.d.ts
CHANGED
|
@@ -82,7 +82,7 @@ export declare function indexerHealthy(url: string, signal?: AbortSignal): Promi
|
|
|
82
82
|
* the more durable path and should be preferred wherever an indexer is configured, with the log
|
|
83
83
|
* scan as the fallback for when it is not.
|
|
84
84
|
*
|
|
85
|
-
* `
|
|
85
|
+
* `PoolCreated` (the C manager's) carries every field of the `PoolKey` (`mintFee` included since C 12), so the key is rebuilt exactly and `poolId` is
|
|
86
86
|
* recomputed locally rather than trusted from the row — the same offline derivation the SDK uses
|
|
87
87
|
* everywhere else, so a wrong id fails loudly instead of addressing the wrong pool.
|
|
88
88
|
*/
|
package/dist/indexer.js
CHANGED
|
@@ -133,12 +133,12 @@ export async function indexerHealthy(url, signal) {
|
|
|
133
133
|
* the more durable path and should be preferred wherever an indexer is configured, with the log
|
|
134
134
|
* scan as the fallback for when it is not.
|
|
135
135
|
*
|
|
136
|
-
* `
|
|
136
|
+
* `PoolCreated` (the C manager's) carries every field of the `PoolKey` (`mintFee` included since C 12), so the key is rebuilt exactly and `poolId` is
|
|
137
137
|
* recomputed locally rather than trusted from the row — the same offline derivation the SDK uses
|
|
138
138
|
* everywhere else, so a wrong id fails loudly instead of addressing the wrong pool.
|
|
139
139
|
*/
|
|
140
140
|
export async function discoverMarketsIndexed(url, signal) {
|
|
141
|
-
const nodes = await gqlAll(url, "
|
|
141
|
+
const nodes = await gqlAll(url, "allCPoolManagerPoolCreateds", "poolId base quote tickSpacing swapFee mintFee policy initialLogPrice blockNumber txHash contractAddress", { signal });
|
|
142
142
|
return nodes.map((n) => {
|
|
143
143
|
const key = {
|
|
144
144
|
base: n.base,
|
|
@@ -151,7 +151,7 @@ export async function discoverMarketsIndexed(url, signal) {
|
|
|
151
151
|
return {
|
|
152
152
|
key,
|
|
153
153
|
poolId: poolId(key),
|
|
154
|
-
|
|
154
|
+
initialLogPrice: BigInt(n.initialLogPrice),
|
|
155
155
|
blockNumber: BigInt(n.blockNumber),
|
|
156
156
|
transactionHash: n.txHash,
|
|
157
157
|
manager: n.contractAddress,
|
|
@@ -191,12 +191,12 @@ export async function discoverMarketsBest(c, url = c.indexerUrl, signal) {
|
|
|
191
191
|
* use the indexed `poolPositions` and fall back to this when the indexer is missing or stale.
|
|
192
192
|
*/
|
|
193
193
|
export async function poolPositionsFromChain(c, key, x) {
|
|
194
|
-
const {
|
|
194
|
+
const { lensAbi } = await import("./generated.js");
|
|
195
195
|
const floors = (await c.public.readContract({
|
|
196
196
|
address: c.addresses.lens,
|
|
197
|
-
abi:
|
|
197
|
+
abi: lensAbi,
|
|
198
198
|
functionName: "liveTicks",
|
|
199
|
-
args: [key],
|
|
199
|
+
args: [poolId(key)],
|
|
200
200
|
}));
|
|
201
201
|
const pool = poolId(key);
|
|
202
202
|
const rows = await Promise.all([...floors].map(async (floor) => {
|
|
@@ -204,7 +204,7 @@ export async function poolPositionsFromChain(c, key, x) {
|
|
|
204
204
|
address: c.addresses.cPoolManager,
|
|
205
205
|
abi: cPoolManagerAbi,
|
|
206
206
|
functionName: "idOf",
|
|
207
|
-
args: [
|
|
207
|
+
args: [pool, floor],
|
|
208
208
|
}));
|
|
209
209
|
const { L, shares } = await getPositionClass(c, id); // through extsload (C version 7)
|
|
210
210
|
if (L === 0n)
|
package/dist/liquidity.d.ts
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import type { Address, Hash } from "viem";
|
|
25
25
|
import type { LogswapClient } from "./client.js";
|
|
26
|
-
import type
|
|
26
|
+
import { type PoolKey } from "./keys.js";
|
|
27
27
|
import { type PairBounds } from "./actions.js";
|
|
28
28
|
import { type WriteOptions } from "./write.js";
|
|
29
29
|
export interface MintArgs extends WriteOptions {
|
|
@@ -56,17 +56,17 @@ export declare function mint(c: LogswapClient, a: MintArgs): Promise<Hash>;
|
|
|
56
56
|
export interface CreateMarketArgs extends WriteOptions {
|
|
57
57
|
key: PoolKey;
|
|
58
58
|
/** Launch log-price. Bounded by the manager to ±46 (the price domain). */
|
|
59
|
-
|
|
59
|
+
initialLogPrice: bigint;
|
|
60
60
|
/** The backstop's floor — the first mint's rung, permanent for the pool's life. */
|
|
61
61
|
targetFloor: bigint;
|
|
62
62
|
/** The founding exposure; must exceed the manager's `minBackstopL` lock. */
|
|
63
63
|
L: bigint;
|
|
64
|
-
/** Required, as on {@link mint}. Creation is atomic at `
|
|
64
|
+
/** Required, as on {@link mint}. Creation is atomic at `initialLogPrice`, so the flows are fixed by the arguments; {@link UNBOUNDED} is safe here only for that reason. */
|
|
65
65
|
maxBaseIn: bigint;
|
|
66
66
|
maxQuoteIn: bigint;
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
|
-
* Create a market ATOMICALLY: `initialize` at `
|
|
69
|
+
* Create a market ATOMICALLY: `initialize` at `initialLogPrice` plus the backstop-establishing first mint in
|
|
70
70
|
* one transaction — the canonical creation path. Under the C gate (064) the router is a trusted
|
|
71
71
|
* forwarder: the gate checks the router's `user()` — the wallet sending this — against the
|
|
72
72
|
* creator list, so a listed creator opens and funds a market in this one call. The
|
package/dist/liquidity.js
CHANGED
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
* the exact flows, mint fee and swept fees included — and is what an editor shows before its one
|
|
22
22
|
* apply.
|
|
23
23
|
*/
|
|
24
|
-
import {
|
|
24
|
+
import { lensAbi, routerAbi } from "./generated.js";
|
|
25
|
+
import { poolId as keyId } from "./keys.js";
|
|
25
26
|
import { NO_CAP } from "./ids.js";
|
|
26
27
|
import { describeId, getPositionClass } from "./positions.js";
|
|
27
28
|
import { execute, OPEN_DELTA, settleInKind, settleOnly, swapExactInAction, swapExactOutAction, updateAction, } from "./actions.js";
|
|
@@ -39,10 +40,12 @@ import { quoteExactIn, quoteExactOut } from "./swap.js";
|
|
|
39
40
|
*/
|
|
40
41
|
export async function mint(c, a) {
|
|
41
42
|
return sendRouterWriteWithPermits(c, {
|
|
42
|
-
abi:
|
|
43
|
+
abi: routerAbi,
|
|
43
44
|
functionName: "mintBlended",
|
|
44
45
|
args: [
|
|
45
|
-
a.key,
|
|
46
|
+
keyId(a.key),
|
|
47
|
+
a.key.base,
|
|
48
|
+
a.key.quote,
|
|
46
49
|
a.targetFloor,
|
|
47
50
|
a.cap ?? NO_CAP,
|
|
48
51
|
a.L,
|
|
@@ -54,7 +57,7 @@ export async function mint(c, a) {
|
|
|
54
57
|
}, [a.key.base, a.key.quote]);
|
|
55
58
|
}
|
|
56
59
|
/**
|
|
57
|
-
* Create a market ATOMICALLY: `initialize` at `
|
|
60
|
+
* Create a market ATOMICALLY: `initialize` at `initialLogPrice` plus the backstop-establishing first mint in
|
|
58
61
|
* one transaction — the canonical creation path. Under the C gate (064) the router is a trusted
|
|
59
62
|
* forwarder: the gate checks the router's `user()` — the wallet sending this — against the
|
|
60
63
|
* creator list, so a listed creator opens and funds a market in this one call. The
|
|
@@ -65,11 +68,11 @@ export async function mint(c, a) {
|
|
|
65
68
|
*/
|
|
66
69
|
export async function createMarket(c, a) {
|
|
67
70
|
return sendRouterWriteWithPermits(c, {
|
|
68
|
-
abi:
|
|
71
|
+
abi: routerAbi,
|
|
69
72
|
functionName: "createMarket",
|
|
70
73
|
args: [
|
|
71
74
|
a.key,
|
|
72
|
-
a.
|
|
75
|
+
a.initialLogPrice,
|
|
73
76
|
a.targetFloor,
|
|
74
77
|
a.L,
|
|
75
78
|
a.maxBaseIn,
|
|
@@ -89,10 +92,12 @@ export async function createMarket(c, a) {
|
|
|
89
92
|
*/
|
|
90
93
|
export async function zapIn(c, a) {
|
|
91
94
|
return sendRouterWriteWithPermits(c, {
|
|
92
|
-
abi:
|
|
95
|
+
abi: routerAbi,
|
|
93
96
|
functionName: "zapIn",
|
|
94
97
|
args: [
|
|
95
|
-
a.key,
|
|
98
|
+
keyId(a.key),
|
|
99
|
+
a.key.base,
|
|
100
|
+
a.key.quote,
|
|
96
101
|
a.fundWithBase,
|
|
97
102
|
a.amountIn,
|
|
98
103
|
a.minSwapOut,
|
|
@@ -108,10 +113,12 @@ export async function zapIn(c, a) {
|
|
|
108
113
|
/** The primitive, through the router's named entry: resize and reposition in one call, in kind. */
|
|
109
114
|
export async function update(c, a) {
|
|
110
115
|
return sendRouterWriteWithPermits(c, {
|
|
111
|
-
abi:
|
|
116
|
+
abi: routerAbi,
|
|
112
117
|
functionName: "update",
|
|
113
118
|
args: [
|
|
114
|
-
a.key,
|
|
119
|
+
keyId(a.key),
|
|
120
|
+
a.key.base,
|
|
121
|
+
a.key.quote,
|
|
115
122
|
a.fromId,
|
|
116
123
|
a.shares,
|
|
117
124
|
a.toFloor,
|
|
@@ -133,10 +140,12 @@ export async function update(c, a) {
|
|
|
133
140
|
*/
|
|
134
141
|
export async function move(c, a) {
|
|
135
142
|
return sendRouterWrite(c, {
|
|
136
|
-
abi:
|
|
143
|
+
abi: routerAbi,
|
|
137
144
|
functionName: "move",
|
|
138
145
|
args: [
|
|
139
|
-
a.key,
|
|
146
|
+
keyId(a.key),
|
|
147
|
+
a.key.base,
|
|
148
|
+
a.key.quote,
|
|
140
149
|
a.fromId,
|
|
141
150
|
a.shares,
|
|
142
151
|
a.toFloor,
|
|
@@ -153,9 +162,9 @@ export async function previewEdit(c, a) {
|
|
|
153
162
|
const holder = a.holder ?? requireWallet(c).address;
|
|
154
163
|
const r = (await c.public.readContract({
|
|
155
164
|
address: c.addresses.lens,
|
|
156
|
-
abi:
|
|
165
|
+
abi: lensAbi,
|
|
157
166
|
functionName: "previewUpdate",
|
|
158
|
-
args: [a.key, a.fromId, holder, a.shares, a.toFloor, a.toCap ?? NO_CAP, a.newL],
|
|
167
|
+
args: [keyId(a.key), a.fromId, holder, a.shares, a.toFloor, a.toCap ?? NO_CAP, a.newL],
|
|
159
168
|
}));
|
|
160
169
|
return { dBase: r[0], dQuote: r[1], fees: r[2] };
|
|
161
170
|
}
|
|
@@ -314,10 +323,12 @@ export async function burn(c, a) {
|
|
|
314
323
|
if (a.ids.length !== a.shares.length)
|
|
315
324
|
throw new Error("logswap: ids and shares length mismatch");
|
|
316
325
|
return sendRouterWrite(c, {
|
|
317
|
-
abi:
|
|
326
|
+
abi: routerAbi,
|
|
318
327
|
functionName: "burn",
|
|
319
328
|
args: [
|
|
320
|
-
a.key,
|
|
329
|
+
keyId(a.key),
|
|
330
|
+
a.key.base,
|
|
331
|
+
a.key.quote,
|
|
321
332
|
a.ids,
|
|
322
333
|
a.shares,
|
|
323
334
|
a.minBaseOut,
|
package/dist/onboard.js
CHANGED
|
@@ -191,9 +191,9 @@ export async function permit2SigCalls(c, tokens) {
|
|
|
191
191
|
message,
|
|
192
192
|
});
|
|
193
193
|
const { encodeFunctionData } = await import("viem");
|
|
194
|
-
const {
|
|
194
|
+
const { routerAbi } = await import("./generated.js");
|
|
195
195
|
calls.push(encodeFunctionData({
|
|
196
|
-
abi:
|
|
196
|
+
abi: routerAbi,
|
|
197
197
|
functionName: "applyPermit2",
|
|
198
198
|
args: [owner, message, signature],
|
|
199
199
|
}));
|
package/dist/pools.d.ts
CHANGED
|
@@ -21,14 +21,14 @@ import { type PoolKey } from "./keys.js";
|
|
|
21
21
|
export interface PoolState {
|
|
22
22
|
initialized: boolean;
|
|
23
23
|
backstopSeeded: boolean;
|
|
24
|
-
/** $x = \log p$, WAD — the stored state. */
|
|
25
|
-
|
|
24
|
+
/** $x = \log p$, WAD — the stored state (`logPrice`; `x` before C 13). */
|
|
25
|
+
logPrice: bigint;
|
|
26
26
|
/** Exposure currently earning. Steps only when a floor or cap is crossed. */
|
|
27
27
|
lActive: bigint;
|
|
28
28
|
reserveBase: bigint;
|
|
29
29
|
reserveQuote: bigint;
|
|
30
|
-
/** Fee-per-unit-L accumulator
|
|
31
|
-
|
|
30
|
+
/** Fee-per-unit-L accumulator $F = \phi \cdot \mathrm{TV}(\log p)$ (`feeGrowth`; `F` before C 13). */
|
|
31
|
+
feeGrowth: bigint;
|
|
32
32
|
/** Quadratic variation of log-price, $\Sigma = \sum_{\text{swaps}} (\Delta x_{\text{swap}})^2$ — one term per swap. */
|
|
33
33
|
realizedVariance: bigint;
|
|
34
34
|
currentK: bigint;
|
|
@@ -51,7 +51,7 @@ export declare function marketSwapFee(c: LogswapClient, key: PoolKey): Promise<b
|
|
|
51
51
|
* Price from the stored log-price. **Lossy** — `x` is exact and this is a float, so use it for
|
|
52
52
|
* display and never for arithmetic you intend to send back on-chain.
|
|
53
53
|
*/
|
|
54
|
-
export declare function priceOf(state: Pick<PoolState, "
|
|
54
|
+
export declare function priceOf(state: Pick<PoolState, "logPrice">, scale?: number): number;
|
|
55
55
|
/**
|
|
56
56
|
* The factor between the chain's price and the human one. The chain's log-price is of the WEI
|
|
57
57
|
* ratio, so with a 6-dec USDC quote and an 18-dec base the human price is `e^x · 10^12`. Pass
|
|
@@ -96,20 +96,20 @@ export declare function cListable(price: number, baseDecimals: number, quoteDeci
|
|
|
96
96
|
*
|
|
97
97
|
* Signed: negative means variation has outrun fee income over the pool's life.
|
|
98
98
|
*/
|
|
99
|
-
export declare function lpEdge(state: Pick<PoolState, "
|
|
100
|
-
/** A market discovered from a `
|
|
99
|
+
export declare function lpEdge(state: Pick<PoolState, "feeGrowth" | "realizedVariance">): bigint;
|
|
100
|
+
/** A market discovered from a `PoolCreated` log — the full key, plus where it was created. */
|
|
101
101
|
export interface DiscoveredMarket {
|
|
102
102
|
key: PoolKey;
|
|
103
103
|
poolId: Hex;
|
|
104
|
-
/** The market's opening log-price. */
|
|
105
|
-
|
|
104
|
+
/** The market's opening log-price (`x0` before C 13). */
|
|
105
|
+
initialLogPrice: bigint;
|
|
106
106
|
blockNumber: bigint;
|
|
107
107
|
transactionHash: Hex;
|
|
108
|
-
/** The manager that emitted the `
|
|
108
|
+
/** The manager that emitted the `PoolCreated` — set when the row came from the indexer. */
|
|
109
109
|
manager?: Address;
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
|
-
* Enumerate markets from `
|
|
112
|
+
* Enumerate markets from the C manager's `PoolCreated` logs.
|
|
113
113
|
*
|
|
114
114
|
* This is the whole market registry: the event carries every key field rather than just the id,
|
|
115
115
|
* deliberately, so nothing downstream needs a configured list. The scan starts at the client's
|
|
@@ -119,7 +119,7 @@ export declare function discoverMarkets(c: LogswapClient, opts?: {
|
|
|
119
119
|
fromBlock?: bigint;
|
|
120
120
|
toBlock?: bigint;
|
|
121
121
|
base?: Address;
|
|
122
|
-
quote?: Address; /** The rindexer GraphQL endpoint: `
|
|
122
|
+
quote?: Address; /** The rindexer GraphQL endpoint: `PoolCreated` from there, the node otherwise. */
|
|
123
123
|
indexer?: string;
|
|
124
124
|
}): Promise<DiscoveredMarket[]>;
|
|
125
125
|
/**
|
|
@@ -176,13 +176,13 @@ export declare function marketTokens(c: LogswapClient, key: PoolKey): Promise<{
|
|
|
176
176
|
/** Format a quote-denominated amount for display, given the quote's decimals. */
|
|
177
177
|
export declare function formatQuote(amount: bigint, quoteDecimals: number): string;
|
|
178
178
|
/** Fees the pool has accrued over its life, in QUOTE units: $F \cdot L$. */
|
|
179
|
-
export declare function feesQuote(state: Pick<PoolState, "
|
|
179
|
+
export declare function feesQuote(state: Pick<PoolState, "feeGrowth" | "lActive">): bigint;
|
|
180
180
|
/** The LP edge in QUOTE units: $(F - \tfrac12\Sigma) \cdot L$. Signed. */
|
|
181
|
-
export declare function edgeQuote(state: Pick<PoolState, "
|
|
181
|
+
export declare function edgeQuote(state: Pick<PoolState, "feeGrowth" | "realizedVariance" | "lActive">): bigint;
|
|
182
182
|
/** The pool floor as a price. **Lossy** — display only, like {@link priceOf}. */
|
|
183
183
|
export declare function floorPrice(state: Pick<PoolState, "backstopFloor">, scale?: number): number;
|
|
184
184
|
/** $\log(p / p_{\text{floor}}) = x - \xi$ — how far spot sits above the floor, in log units (WAD). */
|
|
185
|
-
export declare function floorDistance(state: Pick<PoolState, "
|
|
185
|
+
export declare function floorDistance(state: Pick<PoolState, "logPrice" | "backstopFloor">): bigint;
|
|
186
186
|
/**
|
|
187
187
|
* $-\log(p/p_{\text{floor}})$ as a fraction: the log-drawdown from spot down to the floor.
|
|
188
188
|
*
|
|
@@ -193,9 +193,9 @@ export declare function floorDistance(state: Pick<PoolState, "x" | "backstopFloo
|
|
|
193
193
|
* $e^{-\Delta} - 1$, which differs materially once the distance is large (a log distance of 0.7
|
|
194
194
|
* is a 50% price fall, not 70%).
|
|
195
195
|
*/
|
|
196
|
-
export declare function floorDrawdown(state: Pick<PoolState, "
|
|
196
|
+
export declare function floorDrawdown(state: Pick<PoolState, "logPrice" | "backstopFloor">): number;
|
|
197
197
|
/** The arithmetic price drawdown to the floor, $p_{\text{floor}}/p - 1$. Negative above the floor. */
|
|
198
|
-
export declare function floorDrawdownPrice(state: Pick<PoolState, "
|
|
198
|
+
export declare function floorDrawdownPrice(state: Pick<PoolState, "logPrice" | "backstopFloor">): number;
|
|
199
199
|
/**
|
|
200
200
|
* Annualise a per-unit-L WAD rate over the window it accrued in.
|
|
201
201
|
*
|
|
@@ -209,7 +209,7 @@ export declare function annualise(perUnitL: bigint, elapsedSeconds: bigint): num
|
|
|
209
209
|
/** When a market was created, as a unix timestamp — the denominator for {@link annualise}. */
|
|
210
210
|
export declare function marketOpenedAt(c: LogswapClient, m: DiscoveredMarket): Promise<bigint>;
|
|
211
211
|
/**
|
|
212
|
-
* Create a C market: `initialize(key,
|
|
212
|
+
* Create a C market: `initialize(key, initialLogPrice)` on the manager — a call, not a deployment, and no funds move. The
|
|
213
213
|
* creator-selected mint fee is part of the key (C 12, decision 078: `2 * swapFee <= mintFee <= MINT_FEE_MAX`,
|
|
214
214
|
* preset at twice the swap fee by {@link presetMintFee}); there is no separate argument. Under a curated gate the
|
|
215
215
|
* CALLER must be a listed creator; the canonical path is the router's {@link createMarket}, which forwards its
|
|
@@ -217,6 +217,6 @@ export declare function marketOpenedAt(c: LogswapClient, m: DiscoveredMarket): P
|
|
|
217
217
|
*/
|
|
218
218
|
export declare function initializeMarket(c: LogswapClient, a: {
|
|
219
219
|
key: PoolKey;
|
|
220
|
-
|
|
220
|
+
initialLogPrice: bigint;
|
|
221
221
|
account: Address;
|
|
222
222
|
}): Promise<`0x${string}`>;
|
package/dist/pools.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* conversion is. Prefer comparing in log space when precision matters.
|
|
13
13
|
*/
|
|
14
14
|
import { formatUnits } from "viem";
|
|
15
|
-
import {
|
|
15
|
+
import { lensAbi, cPoolManagerAbi } from "./generated.js";
|
|
16
16
|
import { clientChainId } from "./client.js";
|
|
17
17
|
import { signerFor } from "./write.js";
|
|
18
18
|
import { readCachedToken, registerCacheClear, writeCachedToken } from "./tokencache.js";
|
|
@@ -22,7 +22,7 @@ import { getEventLogs } from "./history.js";
|
|
|
22
22
|
export async function getPool(c, key) {
|
|
23
23
|
const v = (await c.public.readContract({
|
|
24
24
|
address: c.addresses.lens,
|
|
25
|
-
abi:
|
|
25
|
+
abi: lensAbi,
|
|
26
26
|
functionName: "getPools",
|
|
27
27
|
args: [[poolId(key)]],
|
|
28
28
|
}));
|
|
@@ -45,7 +45,7 @@ export async function marketSwapFee(c, key) {
|
|
|
45
45
|
* display and never for arithmetic you intend to send back on-chain.
|
|
46
46
|
*/
|
|
47
47
|
export function priceOf(state, scale = 1) {
|
|
48
|
-
return Math.exp(Number(state.
|
|
48
|
+
return Math.exp(Number(state.logPrice) / 1e18) * scale;
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
51
|
* The factor between the chain's price and the human one. The chain's log-price is of the WEI
|
|
@@ -99,22 +99,21 @@ export function cListable(price, baseDecimals, quoteDecimals) {
|
|
|
99
99
|
* Signed: negative means variation has outrun fee income over the pool's life.
|
|
100
100
|
*/
|
|
101
101
|
export function lpEdge(state) {
|
|
102
|
-
return state.
|
|
102
|
+
return state.feeGrowth - state.realizedVariance / 2n;
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
|
-
* Enumerate markets from `
|
|
105
|
+
* Enumerate markets from the C manager's `PoolCreated` logs.
|
|
106
106
|
*
|
|
107
107
|
* This is the whole market registry: the event carries every key field rather than just the id,
|
|
108
108
|
* deliberately, so nothing downstream needs a configured list. The scan starts at the client's
|
|
109
109
|
* `deployBlock` unless `fromBlock` says otherwise.
|
|
110
110
|
*/
|
|
111
111
|
export async function discoverMarkets(c, opts = {}) {
|
|
112
|
-
const ev = cPoolManagerAbi.find((x) => x.type === "event" && x.name === "
|
|
112
|
+
const ev = cPoolManagerAbi.find((x) => x.type === "event" && x.name === "PoolCreated");
|
|
113
113
|
if (!ev)
|
|
114
|
-
throw new Error("logswap:
|
|
114
|
+
throw new Error("logswap: PoolCreated event missing from the generated ABI");
|
|
115
115
|
const args = {};
|
|
116
|
-
|
|
117
|
-
args.base = opts.base;
|
|
116
|
+
// `quote` is an indexed topic (C 13: `PoolCreated(poolId, quote, creator, base, …)`); `base` no longer is, so it filters below
|
|
118
117
|
if (opts.quote)
|
|
119
118
|
args.quote = opts.quote;
|
|
120
119
|
const { logs } = await getEventLogs(c, {
|
|
@@ -125,7 +124,8 @@ export async function discoverMarkets(c, opts = {}) {
|
|
|
125
124
|
fromBlock: opts.fromBlock, // the client's deployBlock when absent
|
|
126
125
|
toBlock: opts.toBlock,
|
|
127
126
|
});
|
|
128
|
-
|
|
127
|
+
const wantBase = opts.base?.toLowerCase();
|
|
128
|
+
return logs.filter((l) => wantBase === undefined || l.args.base.toLowerCase() === wantBase).map((l) => {
|
|
129
129
|
const a = l.args;
|
|
130
130
|
const key = {
|
|
131
131
|
base: a.base,
|
|
@@ -138,7 +138,7 @@ export async function discoverMarkets(c, opts = {}) {
|
|
|
138
138
|
return {
|
|
139
139
|
key,
|
|
140
140
|
poolId: poolId(key),
|
|
141
|
-
|
|
141
|
+
initialLogPrice: a.initialLogPrice,
|
|
142
142
|
blockNumber: l.blockNumber,
|
|
143
143
|
transactionHash: l.transactionHash,
|
|
144
144
|
};
|
|
@@ -155,18 +155,18 @@ export async function discoverMarkets(c, opts = {}) {
|
|
|
155
155
|
export async function liveFloors(c, key) {
|
|
156
156
|
return c.public.readContract({
|
|
157
157
|
address: c.addresses.lens,
|
|
158
|
-
abi:
|
|
158
|
+
abi: lensAbi,
|
|
159
159
|
functionName: "liveTicks",
|
|
160
|
-
args: [key],
|
|
160
|
+
args: [poolId(key)],
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
163
|
/** Is this floor on the market's grid, and inside `MAX_ABS_FLOOR`? Pure — no RPC on the manager. */
|
|
164
164
|
export async function isValidFloor(c, key, floor) {
|
|
165
165
|
return c.public.readContract({
|
|
166
166
|
address: c.addresses.lens,
|
|
167
|
-
abi:
|
|
167
|
+
abi: lensAbi,
|
|
168
168
|
functionName: "isValidFloor",
|
|
169
|
-
args: [key, floor],
|
|
169
|
+
args: [poolId(key), floor],
|
|
170
170
|
});
|
|
171
171
|
}
|
|
172
172
|
/**
|
|
@@ -289,7 +289,7 @@ const WAD = 10n ** 18n;
|
|
|
289
289
|
const YEAR_SECONDS = 31536000n;
|
|
290
290
|
/** Fees the pool has accrued over its life, in QUOTE units: $F \cdot L$. */
|
|
291
291
|
export function feesQuote(state) {
|
|
292
|
-
return (state.
|
|
292
|
+
return (state.feeGrowth * state.lActive) / WAD;
|
|
293
293
|
}
|
|
294
294
|
/** The LP edge in QUOTE units: $(F - \tfrac12\Sigma) \cdot L$. Signed. */
|
|
295
295
|
export function edgeQuote(state) {
|
|
@@ -301,7 +301,7 @@ export function floorPrice(state, scale = 1) {
|
|
|
301
301
|
}
|
|
302
302
|
/** $\log(p / p_{\text{floor}}) = x - \xi$ — how far spot sits above the floor, in log units (WAD). */
|
|
303
303
|
export function floorDistance(state) {
|
|
304
|
-
return state.
|
|
304
|
+
return state.logPrice - state.backstopFloor;
|
|
305
305
|
}
|
|
306
306
|
/**
|
|
307
307
|
* $-\log(p/p_{\text{floor}})$ as a fraction: the log-drawdown from spot down to the floor.
|
|
@@ -340,7 +340,7 @@ export async function marketOpenedAt(c, m) {
|
|
|
340
340
|
return b.timestamp;
|
|
341
341
|
}
|
|
342
342
|
/**
|
|
343
|
-
* Create a C market: `initialize(key,
|
|
343
|
+
* Create a C market: `initialize(key, initialLogPrice)` on the manager — a call, not a deployment, and no funds move. The
|
|
344
344
|
* creator-selected mint fee is part of the key (C 12, decision 078: `2 * swapFee <= mintFee <= MINT_FEE_MAX`,
|
|
345
345
|
* preset at twice the swap fee by {@link presetMintFee}); there is no separate argument. Under a curated gate the
|
|
346
346
|
* CALLER must be a listed creator; the canonical path is the router's {@link createMarket}, which forwards its
|
|
@@ -351,7 +351,7 @@ export async function initializeMarket(c, a) {
|
|
|
351
351
|
address: c.addresses.cPoolManager,
|
|
352
352
|
abi: cPoolManagerAbi,
|
|
353
353
|
functionName: "initialize",
|
|
354
|
-
args: [a.key, a.
|
|
354
|
+
args: [a.key, a.initialLogPrice],
|
|
355
355
|
account: signerFor(c, a.account),
|
|
356
356
|
});
|
|
357
357
|
return c.wallet.writeContract(request);
|
package/dist/positions.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* accounts holding the same class can have different claimable amounts.
|
|
7
7
|
*/
|
|
8
8
|
import { encodeAbiParameters, keccak256, parseAbiItem } from "viem";
|
|
9
|
-
import {
|
|
9
|
+
import { lensAbi, cPoolManagerAbi } from "./generated.js";
|
|
10
10
|
import { MINT_FEE_MAX, poolId } from "./keys.js";
|
|
11
11
|
/** `CPoolState.POSITIONS_SLOT`: the manager's `positions` mapping (`forge inspect CPoolManager storageLayout`). */
|
|
12
12
|
const C_POSITIONS_SLOT = 4n;
|
|
@@ -50,7 +50,7 @@ export async function balanceOf(c, owner, id) {
|
|
|
50
50
|
export async function feesOf(c, id, owner) {
|
|
51
51
|
return c.public.readContract({
|
|
52
52
|
address: c.addresses.lens,
|
|
53
|
-
abi:
|
|
53
|
+
abi: lensAbi,
|
|
54
54
|
functionName: "feesOfById",
|
|
55
55
|
args: [id, owner],
|
|
56
56
|
});
|
|
@@ -92,7 +92,7 @@ export async function getHolderPosition(c, key, owner, floor, cap = NO_CAP) {
|
|
|
92
92
|
export async function describeId(c, id, tickSpacing) {
|
|
93
93
|
const r = (await c.public.readContract({
|
|
94
94
|
address: c.addresses.lens,
|
|
95
|
-
abi:
|
|
95
|
+
abi: lensAbi,
|
|
96
96
|
functionName: "unpack",
|
|
97
97
|
args: [id],
|
|
98
98
|
}));
|
|
@@ -175,9 +175,9 @@ export function mintFee(p, x, rate) {
|
|
|
175
175
|
export async function positionsOf(c, key, owner) {
|
|
176
176
|
const rows = (await c.public.readContract({
|
|
177
177
|
address: c.addresses.lens,
|
|
178
|
-
abi:
|
|
178
|
+
abi: lensAbi,
|
|
179
179
|
functionName: "positionsOf",
|
|
180
|
-
args: [key, owner],
|
|
180
|
+
args: [poolId(key), owner],
|
|
181
181
|
}));
|
|
182
182
|
return rows.map((r) => ({ id: r.id, floor: r.floor, cap: NO_CAP, capped: false, shares: r.shares, L: r.L, fees: r.fees }));
|
|
183
183
|
}
|
|
@@ -216,9 +216,9 @@ export async function holdingsOf(c, key, owner, fromBlock, opts = {}) {
|
|
|
216
216
|
// one lens call: balance, class L, fees, floor and cap for every id the owner holds
|
|
217
217
|
const rows = (await c.public.readContract({
|
|
218
218
|
address: c.addresses.lens,
|
|
219
|
-
abi:
|
|
219
|
+
abi: lensAbi,
|
|
220
220
|
functionName: "heldByIds",
|
|
221
|
-
args: [key, owner, ids],
|
|
221
|
+
args: [poolId(key), owner, ids],
|
|
222
222
|
}));
|
|
223
223
|
return rows.map((r) => ({ id: r.id, floor: r.floor, cap: r.capped ? r.cap : NO_CAP, capped: r.capped, shares: r.shares, L: r.L, fees: r.fees }));
|
|
224
224
|
}
|
|
@@ -227,9 +227,9 @@ export async function holdingsOf(c, key, owner, fromBlock, opts = {}) {
|
|
|
227
227
|
export async function legsOf(c, key, id, holder) {
|
|
228
228
|
const [base, quote, fees, value] = (await c.public.readContract({
|
|
229
229
|
address: c.addresses.lens,
|
|
230
|
-
abi:
|
|
230
|
+
abi: lensAbi,
|
|
231
231
|
functionName: "legsOf",
|
|
232
|
-
args: [key, id, holder],
|
|
232
|
+
args: [poolId(key), id, holder],
|
|
233
233
|
}));
|
|
234
234
|
return { base, quote, fees, value };
|
|
235
235
|
}
|
package/dist/swap.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { Address, Hash } from "viem";
|
|
9
9
|
import type { LogswapClient } from "./client.js";
|
|
10
|
-
import type
|
|
10
|
+
import { type PoolKey } from "./keys.js";
|
|
11
11
|
import { type WriteOptions } from "./write.js";
|
|
12
12
|
/**
|
|
13
13
|
* Quote an exact-in swap. Exact, not approximate — the lens is a revert-quoter that runs the real
|
package/dist/swap.js
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* merged `swap(key, baseSide, amount, limit)` uses a NEGATIVE amount for exact-out; that convention
|
|
6
6
|
* is deliberately not exposed here — callers say `swapExactOut`.
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { lensAbi, routerAbi } from "./generated.js";
|
|
9
|
+
import { poolId as keyId } from "./keys.js";
|
|
9
10
|
import { resolveDeadline, resolveRecipient, sendRouterWrite, sendRouterWriteWithPermits } from "./write.js";
|
|
10
11
|
/**
|
|
11
12
|
* Quote an exact-in swap. Exact, not approximate — the lens is a revert-quoter that runs the real
|
|
@@ -17,9 +18,9 @@ import { resolveDeadline, resolveRecipient, sendRouterWrite, sendRouterWriteWith
|
|
|
17
18
|
export async function quoteExactIn(c, key, baseIn, amountIn) {
|
|
18
19
|
const { result } = await c.public.simulateContract({
|
|
19
20
|
address: c.addresses.lens,
|
|
20
|
-
abi:
|
|
21
|
+
abi: lensAbi,
|
|
21
22
|
functionName: "previewSwapExactIn",
|
|
22
|
-
args: [key, baseIn, amountIn],
|
|
23
|
+
args: [keyId(key), baseIn, amountIn],
|
|
23
24
|
});
|
|
24
25
|
return result;
|
|
25
26
|
}
|
|
@@ -27,24 +28,42 @@ export async function quoteExactIn(c, key, baseIn, amountIn) {
|
|
|
27
28
|
export async function quoteExactOut(c, key, baseOut, amountOut) {
|
|
28
29
|
const { result } = await c.public.simulateContract({
|
|
29
30
|
address: c.addresses.lens,
|
|
30
|
-
abi:
|
|
31
|
+
abi: lensAbi,
|
|
31
32
|
functionName: "previewSwapExactOut",
|
|
32
|
-
args: [key, baseOut, amountOut],
|
|
33
|
+
args: [keyId(key), baseOut, amountOut],
|
|
33
34
|
});
|
|
34
35
|
return result;
|
|
35
36
|
}
|
|
36
37
|
export async function swapExactIn(c, a) {
|
|
37
38
|
return sendRouterWriteWithPermits(c, {
|
|
38
|
-
abi:
|
|
39
|
+
abi: routerAbi,
|
|
39
40
|
functionName: "swapExactIn",
|
|
40
|
-
args: [
|
|
41
|
+
args: [
|
|
42
|
+
keyId(a.key),
|
|
43
|
+
a.baseIn ? a.key.base : a.key.quote, // tokenIn: the router checks the tokens the caller names against the pool's
|
|
44
|
+
a.baseIn ? a.key.quote : a.key.base, // tokenOut
|
|
45
|
+
a.baseIn,
|
|
46
|
+
a.amountIn,
|
|
47
|
+
a.minOut,
|
|
48
|
+
resolveRecipient(c, a),
|
|
49
|
+
resolveDeadline(a),
|
|
50
|
+
],
|
|
41
51
|
}, [a.baseIn ? a.key.base : a.key.quote]);
|
|
42
52
|
}
|
|
43
53
|
export async function swapExactOut(c, a) {
|
|
44
54
|
return sendRouterWriteWithPermits(c, {
|
|
45
|
-
abi:
|
|
55
|
+
abi: routerAbi,
|
|
46
56
|
functionName: "swapExactOut",
|
|
47
|
-
args: [
|
|
57
|
+
args: [
|
|
58
|
+
keyId(a.key),
|
|
59
|
+
a.baseOut ? a.key.quote : a.key.base, // tokenIn
|
|
60
|
+
a.baseOut ? a.key.base : a.key.quote, // tokenOut
|
|
61
|
+
a.baseOut,
|
|
62
|
+
a.amountOut,
|
|
63
|
+
a.maxIn,
|
|
64
|
+
resolveRecipient(c, a),
|
|
65
|
+
resolveDeadline(a),
|
|
66
|
+
],
|
|
48
67
|
}, [a.baseOut ? a.key.quote : a.key.base]);
|
|
49
68
|
}
|
|
50
69
|
/**
|
|
@@ -59,10 +78,12 @@ export async function swapExactInPath(c, a) {
|
|
|
59
78
|
if (a.hops.length === 0)
|
|
60
79
|
throw new Error("logswap: empty path");
|
|
61
80
|
return sendRouterWrite(c, {
|
|
62
|
-
abi:
|
|
81
|
+
abi: routerAbi,
|
|
63
82
|
functionName: "swapExactInPath",
|
|
64
83
|
args: [
|
|
65
|
-
a.hops.map((h) => ({
|
|
84
|
+
a.hops.map((h) => ({ poolId: keyId(h.key), baseIn: h.baseIn })),
|
|
85
|
+
hopTokens(a.hops[0]).tokenIn,
|
|
86
|
+
hopTokens(a.hops[a.hops.length - 1]).tokenOut,
|
|
66
87
|
a.amountIn,
|
|
67
88
|
a.minOut,
|
|
68
89
|
resolveRecipient(c, a),
|