@logswap/sdk 0.5.102 → 0.5.103
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/fees.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* fees.ts — swap fees over a window, from `Swap` logs.
|
|
3
|
-
*
|
|
4
|
-
* `F` cannot answer "what did takers pay this week": it is per unit of L, it carries the mint-fee
|
|
5
|
-
* credits as well as swap fees, and it has no window. Each `Swap` event carries the quote leg and
|
|
6
|
-
* φ, and the fee is a fixed fraction of that leg, so the sum over a window is exact up to rounding:
|
|
7
|
-
*
|
|
8
|
-
* quote in (price up): the curve takes q/(1+φ), the fee is q·φ/(1+φ)
|
|
9
|
-
* quote out (price down): the curve releases q/(1−φ), the fee is q·φ/(1−φ)
|
|
10
|
-
*
|
|
11
|
-
* Both equal φ·L·|Δx| summed over the swap's segments — the same φ·|dx| per unit L that `F` books.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { managerC, type LogswapClient } from "./client.js";
|
|
15
|
-
import { managerCAbi } from "./generated.js";
|
|
16
|
-
import type { DiscoveredMarket } from "./pools.js";
|
|
17
|
-
import { getEventLogs } from "./history.js";
|
|
18
|
-
|
|
19
|
-
const WAD = 10n ** 18n;
|
|
20
|
-
const YEAR_SECONDS = 31_536_000;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The fee one swap paid, in quote, from its `Swap` event's fields.
|
|
24
|
-
*
|
|
25
|
-
* @deprecated The C `Swap` event carries its `fee` since C version 5 (decisions 050); read that.
|
|
26
|
-
* This reconstruction from φ rounds where the contract does not, and is kept for older logs.
|
|
27
|
-
*/
|
|
28
|
-
export function swapFeeOf(e: { baseIn: boolean; amountIn: bigint; amountOut: bigint }, swapFee: bigint): bigint {
|
|
29
|
-
return e.baseIn ? (e.amountOut * swapFee) / (WAD - swapFee) : (e.amountIn * swapFee) / (WAD + swapFee);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Swap fees paid over a window, in the market's quote units. */
|
|
33
|
-
export interface SwapFeeWindow {
|
|
34
|
-
fromBlock: bigint;
|
|
35
|
-
toBlock: bigint;
|
|
36
|
-
/** Window start, unix seconds — `since`, or the first block's timestamp, never before the market opened. */
|
|
37
|
-
from: bigint;
|
|
38
|
-
/** Window end, unix seconds — the last block's timestamp. */
|
|
39
|
-
to: bigint;
|
|
40
|
-
seconds: bigint;
|
|
41
|
-
swapFees: bigint;
|
|
42
|
-
swaps: number;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Probes per round of {@link blockAtOrAfter}: 15 splits the bracket 16 ways. */
|
|
46
|
-
const PROBES = 15n;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* The first block with timestamp ≥ `t` in `[lo, hi]`; `hi` when none is earlier.
|
|
50
|
-
*
|
|
51
|
-
* A PARALLEL search: each round reads {@link PROBES} evenly spaced blocks at once and keeps the
|
|
52
|
-
* sixteenth of the bracket that holds the answer, so 64k blocks take 4 round trips. Bisection read
|
|
53
|
-
* one block per round trip — ~16 sequential `getBlock`s, seconds apiece against a distant node,
|
|
54
|
-
* before a fee figure could render. Interpolating on timestamps was tried and did no better: a
|
|
55
|
-
* devnet mines on demand plus a heartbeat, so its blocks are far from evenly spaced in time.
|
|
56
|
-
* Exact for monotonic timestamps; a chain restored from a snapshot can carry a genesis stamped
|
|
57
|
-
* after its later blocks, so callers bound `lo` by the block they care about (a pool's own).
|
|
58
|
-
*/
|
|
59
|
-
export async function blockAtOrAfter(c: LogswapClient, t: bigint, lo: bigint, hi: bigint): Promise<bigint> {
|
|
60
|
-
if (lo >= hi) return hi;
|
|
61
|
-
const stamp = async (n: bigint) => (await c.public.getBlock({ blockNumber: n })).timestamp;
|
|
62
|
-
if ((await stamp(lo)) >= t) return lo;
|
|
63
|
-
// invariant from here: ts(lo) < t, and the answer is in (lo, hi]
|
|
64
|
-
while (hi - lo > 1n) {
|
|
65
|
-
const width = hi - lo;
|
|
66
|
-
const probes = [...new Set(Array.from({ length: Number(PROBES) }, (_, i) => lo + (width * BigInt(i + 1)) / (PROBES + 1n)))].filter(
|
|
67
|
-
(n) => n > lo && n < hi,
|
|
68
|
-
);
|
|
69
|
-
if (probes.length === 0) probes.push(lo + width / 2n);
|
|
70
|
-
const stamps = await Promise.all(probes.map(stamp));
|
|
71
|
-
const first = stamps.findIndex((ts) => ts >= t);
|
|
72
|
-
if (first === -1) lo = probes[probes.length - 1]!;
|
|
73
|
-
else {
|
|
74
|
-
hi = probes[first]!;
|
|
75
|
-
if (first > 0) lo = probes[first - 1]!;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return hi;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Sum the swap fees of market `m` over a window: from `since` (unix seconds) or `fromBlock`, to
|
|
83
|
-
* `toBlock` (default latest). Either start is clamped to the market's opening, so a period longer
|
|
84
|
-
* than the market's life measures its whole life and `seconds` says how long that was.
|
|
85
|
-
*/
|
|
86
|
-
export async function swapFees(
|
|
87
|
-
c: LogswapClient,
|
|
88
|
-
m: DiscoveredMarket,
|
|
89
|
-
opts: { since?: bigint; fromBlock?: bigint; toBlock?: bigint; /** The rindexer GraphQL endpoint: the window from its swap_flow view (the Swap rows as fallback), the node otherwise. */ indexer?: string } = {},
|
|
90
|
-
): Promise<SwapFeeWindow> {
|
|
91
|
-
const toBlock = opts.toBlock ?? (await c.public.getBlockNumber());
|
|
92
|
-
const [opened, last] = await Promise.all([
|
|
93
|
-
c.public.getBlock({ blockNumber: m.blockNumber }),
|
|
94
|
-
c.public.getBlock({ blockNumber: toBlock }),
|
|
95
|
-
]);
|
|
96
|
-
|
|
97
|
-
let fromBlock: bigint;
|
|
98
|
-
let from: bigint;
|
|
99
|
-
if (opts.since !== undefined && opts.since > opened.timestamp) {
|
|
100
|
-
fromBlock = await blockAtOrAfter(c, opts.since, m.blockNumber, toBlock);
|
|
101
|
-
from = opts.since;
|
|
102
|
-
} else if (opts.fromBlock !== undefined && opts.fromBlock > m.blockNumber) {
|
|
103
|
-
fromBlock = opts.fromBlock;
|
|
104
|
-
from = (await c.public.getBlock({ blockNumber: fromBlock })).timestamp;
|
|
105
|
-
} else {
|
|
106
|
-
fromBlock = m.blockNumber;
|
|
107
|
-
from = opened.timestamp;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
type Ev = Extract<(typeof managerCAbi)[number], { type: "event"; name: "Swap" }>;
|
|
111
|
-
const ev = managerCAbi.find((x): x is Ev => x.type === "event" && x.name === "Swap");
|
|
112
|
-
if (!ev) throw new Error("logswap: Swap event missing from the generated ABI");
|
|
113
|
-
|
|
114
|
-
// the event carries the fee the swap charged (decisions 050), exactly — no re-derivation from φ
|
|
115
|
-
let fees = 0n;
|
|
116
|
-
let swaps = 0;
|
|
117
|
-
const indexer = opts.indexer ?? c.indexerUrl;
|
|
118
|
-
if (fromBlock <= toBlock) {
|
|
119
|
-
const { logs } = await getEventLogs<{ fee: bigint }>(c, { indexer, address: managerC(c), event: ev, args: { poolId: m.poolId }, fromBlock, toBlock });
|
|
120
|
-
for (const l of logs) fees += l.args.fee;
|
|
121
|
-
swaps = logs.length;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const to = last.timestamp;
|
|
125
|
-
return { fromBlock, toBlock, from, to, seconds: to > from ? to - from : 0n, swapFees: fees, swaps };
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* The window's swap fees over the market's CURRENT active L, annualised linearly over the window.
|
|
130
|
-
* A fraction: 0.12 is 12%. Linear: fees are paid out, so none compound.
|
|
131
|
-
*
|
|
132
|
-
* Per unit of L. An uncapped position is worth L·(x − ξ + 1), so its return on capital is this
|
|
133
|
-
* divided by (x − ξ + 1).
|
|
134
|
-
*/
|
|
135
|
-
export function swapFeeApy(w: Pick<SwapFeeWindow, "swapFees" | "seconds">, lActive: bigint): number {
|
|
136
|
-
if (w.seconds <= 0n || lActive <= 0n) return 0;
|
|
137
|
-
return (Number(w.swapFees) / Number(lActive)) * (YEAR_SECONDS / Number(w.seconds));
|
|
138
|
-
}
|