@skate-org/amm-evm-v2 2.0.0-beta.2 → 2.0.0-beta.21
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/CHANGELOG.md +45 -119
- package/README.md +56 -4
- package/dist/chain-clients.d.ts.map +1 -1
- package/dist/client.d.ts +7361 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/devConfig.d.ts +0 -2
- package/dist/devConfig.d.ts.map +1 -1
- package/dist/index.cjs +513 -116
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +513 -116
- package/dist/index.js.map +1 -1
- package/dist/kernel/lens.d.ts +20 -4
- package/dist/kernel/lens.d.ts.map +1 -1
- package/dist/kernel/reader.d.ts +42 -8
- package/dist/kernel/reader.d.ts.map +1 -1
- package/dist/periphery/calldata.d.ts +87 -0
- package/dist/periphery/calldata.d.ts.map +1 -0
- package/dist/periphery/index.d.ts +1 -0
- package/dist/periphery/index.d.ts.map +1 -1
- package/dist/periphery/reader.d.ts +29 -28
- package/dist/periphery/reader.d.ts.map +1 -1
- package/dist/skate-amm.d.ts +7373 -0
- package/dist/skate-amm.d.ts.map +1 -0
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
defineChain,
|
|
5
5
|
http
|
|
6
6
|
} from "viem";
|
|
7
|
-
import { arbitrum } from "viem/chains";
|
|
7
|
+
import { arbitrum, mainnet } from "viem/chains";
|
|
8
8
|
import {
|
|
9
9
|
BLOCK_EXPLORER,
|
|
10
10
|
CHAIN,
|
|
@@ -28,7 +28,8 @@ var megaethChain = defineChain({
|
|
|
28
28
|
});
|
|
29
29
|
var DEFAULT_RPC = {
|
|
30
30
|
[CHAIN.MEGAETH]: "https://mainnet.megaeth.com/rpc",
|
|
31
|
-
[CHAIN.ARBITRUM]: "https://arb1.arbitrum.io/rpc"
|
|
31
|
+
[CHAIN.ARBITRUM]: "https://arb1.arbitrum.io/rpc",
|
|
32
|
+
[CHAIN.ETHEREUM]: "https://ethereum-rpc.publicnode.com"
|
|
32
33
|
};
|
|
33
34
|
function chainDefFor(chain) {
|
|
34
35
|
switch (chain) {
|
|
@@ -36,6 +37,8 @@ function chainDefFor(chain) {
|
|
|
36
37
|
return megaethChain;
|
|
37
38
|
case CHAIN.ARBITRUM:
|
|
38
39
|
return arbitrum;
|
|
40
|
+
case CHAIN.ETHEREUM:
|
|
41
|
+
return mainnet;
|
|
39
42
|
default:
|
|
40
43
|
return void 0;
|
|
41
44
|
}
|
|
@@ -82,17 +85,18 @@ function getSourcePublicClient(chain, mode, transport) {
|
|
|
82
85
|
});
|
|
83
86
|
}
|
|
84
87
|
|
|
85
|
-
// src/
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
88
|
+
// src/kernel/reader.ts
|
|
89
|
+
import {
|
|
90
|
+
KernelManagerABI,
|
|
91
|
+
KernelPoolABI
|
|
92
|
+
} from "@skate-org/amm-bindings";
|
|
93
|
+
import {
|
|
94
|
+
KernelManagerAddress,
|
|
95
|
+
normalizeMode as normalizeMode2,
|
|
96
|
+
getAmount0Delta,
|
|
97
|
+
getAmount1Delta,
|
|
98
|
+
getSqrtRatioAtTick
|
|
99
|
+
} from "@skate-org/amm-core-v2";
|
|
96
100
|
|
|
97
101
|
// src/errors.ts
|
|
98
102
|
import { SdkError } from "@skate-org/amm-core-v2";
|
|
@@ -129,36 +133,23 @@ function wrapWriteError(err, functionName) {
|
|
|
129
133
|
return new EvmWriteError(functionName, message, { cause: err });
|
|
130
134
|
}
|
|
131
135
|
|
|
132
|
-
// src/kernel/reader.ts
|
|
133
|
-
import {
|
|
134
|
-
KernelManagerABI,
|
|
135
|
-
KernelPoolABI
|
|
136
|
-
} from "@skate-org/amm-bindings";
|
|
137
|
-
import {
|
|
138
|
-
KernelManagerAddress,
|
|
139
|
-
normalizeMode as normalizeMode2,
|
|
140
|
-
getAmount0Delta,
|
|
141
|
-
getAmount1Delta,
|
|
142
|
-
getSqrtRatioAtTick
|
|
143
|
-
} from "@skate-org/amm-core-v2";
|
|
144
|
-
|
|
145
136
|
// src/kernel/_client.ts
|
|
146
137
|
function resolveKernelClient(mode, client) {
|
|
147
138
|
return client ?? getKernelPublicClient(mode);
|
|
148
139
|
}
|
|
149
140
|
|
|
150
141
|
// src/kernel/reader.ts
|
|
151
|
-
async function readPool(
|
|
152
|
-
const c = resolveKernelClient(mode, client);
|
|
142
|
+
async function readPool(p) {
|
|
143
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
153
144
|
try {
|
|
154
145
|
const [slot0, liquidity] = await Promise.all([
|
|
155
146
|
c.readContract({
|
|
156
|
-
address: kernelPool,
|
|
147
|
+
address: p.kernelPool,
|
|
157
148
|
abi: KernelPoolABI,
|
|
158
149
|
functionName: "slot0"
|
|
159
150
|
}),
|
|
160
151
|
c.readContract({
|
|
161
|
-
address: kernelPool,
|
|
152
|
+
address: p.kernelPool,
|
|
162
153
|
abi: KernelPoolABI,
|
|
163
154
|
functionName: "liquidity"
|
|
164
155
|
})
|
|
@@ -180,16 +171,36 @@ async function readPool(kernelPool, mode, client) {
|
|
|
180
171
|
throw wrapReadError(err, "readPool");
|
|
181
172
|
}
|
|
182
173
|
}
|
|
183
|
-
async function
|
|
184
|
-
const
|
|
174
|
+
async function readKernelBalances(p) {
|
|
175
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
176
|
+
try {
|
|
177
|
+
const [balance0, balance1] = await Promise.all([
|
|
178
|
+
c.readContract({
|
|
179
|
+
address: p.kernelPool,
|
|
180
|
+
abi: KernelPoolABI,
|
|
181
|
+
functionName: "balance0"
|
|
182
|
+
}),
|
|
183
|
+
c.readContract({
|
|
184
|
+
address: p.kernelPool,
|
|
185
|
+
abi: KernelPoolABI,
|
|
186
|
+
functionName: "balance1"
|
|
187
|
+
})
|
|
188
|
+
]);
|
|
189
|
+
return { balance0, balance1 };
|
|
190
|
+
} catch (err) {
|
|
191
|
+
throw wrapReadError(err, "readKernelBalances");
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async function readPosition(p) {
|
|
195
|
+
const { mode: baseMode } = normalizeMode2(p.mode);
|
|
185
196
|
const manager = KernelManagerAddress(baseMode);
|
|
186
|
-
const c = resolveKernelClient(mode, client);
|
|
197
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
187
198
|
try {
|
|
188
199
|
const raw = await c.readContract({
|
|
189
200
|
address: manager,
|
|
190
201
|
abi: KernelManagerABI,
|
|
191
202
|
functionName: "nftPositions",
|
|
192
|
-
args: [tokenId]
|
|
203
|
+
args: [p.tokenId]
|
|
193
204
|
});
|
|
194
205
|
const asTuple = Array.isArray(raw) ? raw : null;
|
|
195
206
|
if (asTuple) {
|
|
@@ -209,28 +220,28 @@ async function readPosition(tokenId, mode, client) {
|
|
|
209
220
|
throw wrapReadError(err, "readPosition");
|
|
210
221
|
}
|
|
211
222
|
}
|
|
212
|
-
async function readTickBitmap(
|
|
213
|
-
const c = resolveKernelClient(mode, client);
|
|
223
|
+
async function readTickBitmap(p) {
|
|
224
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
214
225
|
try {
|
|
215
226
|
const word = await c.readContract({
|
|
216
|
-
address: kernelPool,
|
|
227
|
+
address: p.kernelPool,
|
|
217
228
|
abi: KernelPoolABI,
|
|
218
229
|
functionName: "tickBitmap",
|
|
219
|
-
args: [wordPos]
|
|
230
|
+
args: [p.wordPos]
|
|
220
231
|
});
|
|
221
232
|
return word;
|
|
222
233
|
} catch (err) {
|
|
223
234
|
throw wrapReadError(err, "readTickBitmap");
|
|
224
235
|
}
|
|
225
236
|
}
|
|
226
|
-
async function readTick(
|
|
227
|
-
const c = resolveKernelClient(mode, client);
|
|
237
|
+
async function readTick(p) {
|
|
238
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
228
239
|
try {
|
|
229
240
|
const raw = await c.readContract({
|
|
230
|
-
address: kernelPool,
|
|
241
|
+
address: p.kernelPool,
|
|
231
242
|
abi: KernelPoolABI,
|
|
232
243
|
functionName: "ticks",
|
|
233
|
-
args: [tick]
|
|
244
|
+
args: [p.tick]
|
|
234
245
|
});
|
|
235
246
|
return {
|
|
236
247
|
liquidityGross: raw[0],
|
|
@@ -246,10 +257,10 @@ async function readTick(kernelPool, tick, mode, client) {
|
|
|
246
257
|
throw wrapReadError(err, "readTick");
|
|
247
258
|
}
|
|
248
259
|
}
|
|
249
|
-
async function computePositionAmounts(
|
|
250
|
-
const c = resolveKernelClient(mode, client);
|
|
251
|
-
const position = await readPosition(tokenId, mode, c);
|
|
252
|
-
const { slot0 } = await readPool(position.pool, mode, c);
|
|
260
|
+
async function computePositionAmounts(p) {
|
|
261
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
262
|
+
const position = await readPosition({ tokenId: p.tokenId, mode: p.mode, client: c });
|
|
263
|
+
const { slot0 } = await readPool({ kernelPool: position.pool, mode: p.mode, client: c });
|
|
253
264
|
const sqrtLower = getSqrtRatioAtTick(position.tickLower);
|
|
254
265
|
const sqrtUpper = getSqrtRatioAtTick(position.tickUpper);
|
|
255
266
|
if (slot0.tick < position.tickLower) {
|
|
@@ -280,48 +291,26 @@ async function computePositionAmounts(tokenId, mode, client) {
|
|
|
280
291
|
};
|
|
281
292
|
}
|
|
282
293
|
|
|
283
|
-
// src/kernel/tickBitmap.ts
|
|
284
|
-
function decodeTickBitmapWord(word, wordPos, tickSpacing) {
|
|
285
|
-
if (!Number.isSafeInteger(wordPos)) {
|
|
286
|
-
throw new RangeError(
|
|
287
|
-
`decodeTickBitmapWord: wordPos must be a safe integer, got ${wordPos}`
|
|
288
|
-
);
|
|
289
|
-
}
|
|
290
|
-
if (!Number.isSafeInteger(tickSpacing) || tickSpacing <= 0) {
|
|
291
|
-
throw new RangeError(
|
|
292
|
-
`decodeTickBitmapWord: tickSpacing must be a positive integer, got ${tickSpacing}`
|
|
293
|
-
);
|
|
294
|
-
}
|
|
295
|
-
const result = [];
|
|
296
|
-
const base = wordPos * 256;
|
|
297
|
-
for (let k = 0; k < 256; k++) {
|
|
298
|
-
if ((word >> BigInt(k) & 1n) === 1n) {
|
|
299
|
-
result.push((base + k) * tickSpacing);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
return result;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
294
|
// src/kernel/lens.ts
|
|
306
295
|
import { KernelManagerABI as KernelManagerABI2 } from "@skate-org/amm-bindings";
|
|
307
296
|
import {
|
|
308
297
|
KernelManagerAddress as KernelManagerAddress2,
|
|
309
298
|
normalizeMode as normalizeMode3
|
|
310
299
|
} from "@skate-org/amm-core-v2";
|
|
311
|
-
async function simulateSwap(
|
|
312
|
-
const { mode: baseMode } = normalizeMode3(mode);
|
|
313
|
-
const c = resolveKernelClient(mode, client);
|
|
300
|
+
async function simulateSwap(p) {
|
|
301
|
+
const { mode: baseMode } = normalizeMode3(p.mode);
|
|
302
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
314
303
|
try {
|
|
315
304
|
const out = await c.simulateContract({
|
|
316
305
|
address: KernelManagerAddress2(baseMode),
|
|
317
306
|
abi: KernelManagerABI2,
|
|
318
307
|
functionName: "lensSwap",
|
|
319
308
|
args: [
|
|
320
|
-
params.pool,
|
|
321
|
-
params.recipient,
|
|
322
|
-
params.zeroForOne,
|
|
323
|
-
params.amountSpecified,
|
|
324
|
-
params.sqrtPriceLimitX96
|
|
309
|
+
p.params.pool,
|
|
310
|
+
p.params.recipient,
|
|
311
|
+
p.params.zeroForOne,
|
|
312
|
+
p.params.amountSpecified,
|
|
313
|
+
p.params.sqrtPriceLimitX96
|
|
325
314
|
]
|
|
326
315
|
});
|
|
327
316
|
const [amount0, amount1, sqrtPriceX96After, returnData] = out.result;
|
|
@@ -330,22 +319,22 @@ async function simulateSwap(params, mode, client) {
|
|
|
330
319
|
throw wrapReadError(err, "simulateSwap");
|
|
331
320
|
}
|
|
332
321
|
}
|
|
333
|
-
async function simulateMint(
|
|
334
|
-
const { mode: baseMode } = normalizeMode3(mode);
|
|
335
|
-
const c = resolveKernelClient(mode, client);
|
|
322
|
+
async function simulateMint(p) {
|
|
323
|
+
const { mode: baseMode } = normalizeMode3(p.mode);
|
|
324
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
336
325
|
try {
|
|
337
326
|
const out = await c.simulateContract({
|
|
338
327
|
address: KernelManagerAddress2(baseMode),
|
|
339
328
|
abi: KernelManagerABI2,
|
|
340
329
|
functionName: "lensMint",
|
|
341
330
|
args: [
|
|
342
|
-
params.pool,
|
|
343
|
-
params.recipient,
|
|
344
|
-
params.tickLower,
|
|
345
|
-
params.tickUpper,
|
|
346
|
-
params.amount0,
|
|
347
|
-
params.amount1,
|
|
348
|
-
params.data
|
|
331
|
+
p.params.pool,
|
|
332
|
+
p.params.recipient,
|
|
333
|
+
p.params.tickLower,
|
|
334
|
+
p.params.tickUpper,
|
|
335
|
+
p.params.amount0,
|
|
336
|
+
p.params.amount1,
|
|
337
|
+
p.params.data
|
|
349
338
|
]
|
|
350
339
|
});
|
|
351
340
|
const [amount0Used, amount1Used, liquidityAmount, returnData] = out.result;
|
|
@@ -354,20 +343,20 @@ async function simulateMint(params, mode, client) {
|
|
|
354
343
|
throw wrapReadError(err, "simulateMint");
|
|
355
344
|
}
|
|
356
345
|
}
|
|
357
|
-
async function simulateBurn(
|
|
358
|
-
const { mode: baseMode } = normalizeMode3(mode);
|
|
359
|
-
const c = resolveKernelClient(mode, client);
|
|
346
|
+
async function simulateBurn(p) {
|
|
347
|
+
const { mode: baseMode } = normalizeMode3(p.mode);
|
|
348
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
360
349
|
try {
|
|
361
350
|
const out = await c.simulateContract({
|
|
362
351
|
address: KernelManagerAddress2(baseMode),
|
|
363
352
|
abi: KernelManagerABI2,
|
|
364
353
|
functionName: "lensBurn",
|
|
365
354
|
args: [
|
|
366
|
-
params.pool,
|
|
367
|
-
params.recipient,
|
|
368
|
-
params.tickLower,
|
|
369
|
-
params.tickUpper,
|
|
370
|
-
params.amount
|
|
355
|
+
p.params.pool,
|
|
356
|
+
p.params.recipient,
|
|
357
|
+
p.params.tickLower,
|
|
358
|
+
p.params.tickUpper,
|
|
359
|
+
p.params.amount
|
|
371
360
|
]
|
|
372
361
|
});
|
|
373
362
|
const [amount0, amount1, returnData] = out.result;
|
|
@@ -376,15 +365,15 @@ async function simulateBurn(params, mode, client) {
|
|
|
376
365
|
throw wrapReadError(err, "simulateBurn");
|
|
377
366
|
}
|
|
378
367
|
}
|
|
379
|
-
async function simulateDecreaseLiquidity(
|
|
380
|
-
const { mode: baseMode } = normalizeMode3(mode);
|
|
381
|
-
const c = resolveKernelClient(mode, client);
|
|
368
|
+
async function simulateDecreaseLiquidity(p) {
|
|
369
|
+
const { mode: baseMode } = normalizeMode3(p.mode);
|
|
370
|
+
const c = resolveKernelClient(p.mode, p.client);
|
|
382
371
|
try {
|
|
383
372
|
const out = await c.simulateContract({
|
|
384
373
|
address: KernelManagerAddress2(baseMode),
|
|
385
374
|
abi: KernelManagerABI2,
|
|
386
375
|
functionName: "lensDecreaseLiquidity",
|
|
387
|
-
args: [params.tokenId, params.liquidity]
|
|
376
|
+
args: [p.params.tokenId, p.params.liquidity]
|
|
388
377
|
});
|
|
389
378
|
const [amount0, amount1, returnData] = out.result;
|
|
390
379
|
return { amount0, amount1, returnData };
|
|
@@ -396,27 +385,28 @@ async function simulateDecreaseLiquidity(params, mode, client) {
|
|
|
396
385
|
// src/periphery/reader.ts
|
|
397
386
|
import { PeripheryPoolABI } from "@skate-org/amm-bindings";
|
|
398
387
|
import {
|
|
388
|
+
AmmCore,
|
|
399
389
|
getPeripheryDetailsByKernelPoolAndChainId,
|
|
400
390
|
normalizeMode as normalizeMode4
|
|
401
391
|
} from "@skate-org/amm-core-v2";
|
|
402
392
|
function resolveClient(chain, mode, client) {
|
|
403
393
|
return client ?? getSourcePublicClient(chain, mode);
|
|
404
394
|
}
|
|
405
|
-
async function readPeripheryPool(
|
|
406
|
-
const { mode: baseMode } = normalizeMode4(mode);
|
|
395
|
+
async function readPeripheryPool(p) {
|
|
396
|
+
const { mode: baseMode } = normalizeMode4(p.mode);
|
|
407
397
|
const details = getPeripheryDetailsByKernelPoolAndChainId(
|
|
408
|
-
kernelPool,
|
|
409
|
-
chain,
|
|
398
|
+
p.kernelPool,
|
|
399
|
+
p.chain,
|
|
410
400
|
baseMode
|
|
411
401
|
);
|
|
412
402
|
if (!details) {
|
|
413
403
|
throw new EvmReadError(
|
|
414
404
|
"readPeripheryPool",
|
|
415
|
-
`no periphery entry for kernelPool=${kernelPool} chain=${chain} mode=${baseMode}`
|
|
405
|
+
`no periphery entry for kernelPool=${p.kernelPool} chain=${p.chain} mode=${baseMode}`
|
|
416
406
|
);
|
|
417
407
|
}
|
|
418
408
|
const address = details.address;
|
|
419
|
-
const c = resolveClient(chain, mode, client);
|
|
409
|
+
const c = resolveClient(p.chain, p.mode, p.client);
|
|
420
410
|
try {
|
|
421
411
|
const [
|
|
422
412
|
balancesAvailableRaw,
|
|
@@ -481,26 +471,24 @@ async function readPeripheryPool(chain, kernelPool, mode, client) {
|
|
|
481
471
|
throw wrapReadError(err, "readPeripheryPool");
|
|
482
472
|
}
|
|
483
473
|
}
|
|
484
|
-
async function readUserPoolBalances(
|
|
485
|
-
const { mode: baseMode } = normalizeMode4(mode);
|
|
486
|
-
const
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
);
|
|
491
|
-
if (!details) {
|
|
474
|
+
async function readUserPoolBalances(p) {
|
|
475
|
+
const { mode: baseMode } = normalizeMode4(p.mode);
|
|
476
|
+
const core = new AmmCore(p.mode);
|
|
477
|
+
const info = core.getPoolInfoByKey(p.poolKey);
|
|
478
|
+
const periphery = info?.peripheryInfo[p.chain];
|
|
479
|
+
if (!periphery?.address) {
|
|
492
480
|
throw new EvmReadError(
|
|
493
481
|
"readUserPoolBalances",
|
|
494
|
-
`no periphery entry for
|
|
482
|
+
`no periphery entry for poolKey=${p.poolKey} chain=${p.chain} mode=${baseMode}`
|
|
495
483
|
);
|
|
496
484
|
}
|
|
497
|
-
const c = resolveClient(chain, mode, client);
|
|
485
|
+
const c = resolveClient(p.chain, p.mode, p.client);
|
|
498
486
|
try {
|
|
499
487
|
const raw = await c.readContract({
|
|
500
|
-
address:
|
|
488
|
+
address: periphery.address,
|
|
501
489
|
abi: PeripheryPoolABI,
|
|
502
490
|
functionName: "usersData",
|
|
503
|
-
args: [user]
|
|
491
|
+
args: [p.user]
|
|
504
492
|
});
|
|
505
493
|
if (Array.isArray(raw)) {
|
|
506
494
|
const tuple = raw;
|
|
@@ -558,6 +546,407 @@ async function submitApproval(wallet, params, mode) {
|
|
|
558
546
|
}
|
|
559
547
|
}
|
|
560
548
|
|
|
549
|
+
// src/periphery/calldata.ts
|
|
550
|
+
import {
|
|
551
|
+
encodeAbiParameters,
|
|
552
|
+
encodeFunctionData,
|
|
553
|
+
maxUint256,
|
|
554
|
+
parseAbiParameters
|
|
555
|
+
} from "viem";
|
|
556
|
+
import { PeripheryPoolABI as PeripheryPoolABI2 } from "@skate-org/amm-bindings";
|
|
557
|
+
import {
|
|
558
|
+
AmmCore as AmmCore2
|
|
559
|
+
} from "@skate-org/amm-core-v2";
|
|
560
|
+
function resolvePeripheryTarget(poolKey, chain, mode) {
|
|
561
|
+
const core = new AmmCore2(mode);
|
|
562
|
+
const info = core.getPoolInfoByKey(poolKey);
|
|
563
|
+
if (!info) throw new Error(`unknown poolKey: ${poolKey}`);
|
|
564
|
+
const periphery = info.peripheryInfo[chain];
|
|
565
|
+
if (!periphery?.address) {
|
|
566
|
+
throw new Error(`pool ${poolKey} has no periphery on chain ${chain}`);
|
|
567
|
+
}
|
|
568
|
+
return periphery.address;
|
|
569
|
+
}
|
|
570
|
+
function encodeSwap(params) {
|
|
571
|
+
const {
|
|
572
|
+
recipient,
|
|
573
|
+
zeroForOne,
|
|
574
|
+
amount,
|
|
575
|
+
sqrtPriceLimitX96,
|
|
576
|
+
destVmType,
|
|
577
|
+
destChainId,
|
|
578
|
+
minAmountOut,
|
|
579
|
+
deadline
|
|
580
|
+
} = params;
|
|
581
|
+
const extraData = encodeAbiParameters(
|
|
582
|
+
parseAbiParameters("uint256,uint256,uint256,uint256"),
|
|
583
|
+
[
|
|
584
|
+
minAmountOut ?? 0n,
|
|
585
|
+
BigInt(destChainId),
|
|
586
|
+
BigInt(destVmType),
|
|
587
|
+
deadline ?? maxUint256
|
|
588
|
+
]
|
|
589
|
+
);
|
|
590
|
+
return encodeFunctionData({
|
|
591
|
+
abi: PeripheryPoolABI2,
|
|
592
|
+
functionName: "swap",
|
|
593
|
+
args: [recipient, zeroForOne, amount, sqrtPriceLimitX96, extraData]
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
function encodeMint(params) {
|
|
597
|
+
const {
|
|
598
|
+
recipient,
|
|
599
|
+
tickLower,
|
|
600
|
+
tickUpper,
|
|
601
|
+
amount0,
|
|
602
|
+
amount1,
|
|
603
|
+
amount0Min,
|
|
604
|
+
amount1Min,
|
|
605
|
+
deadline
|
|
606
|
+
} = params;
|
|
607
|
+
const extraData = encodeAbiParameters(
|
|
608
|
+
parseAbiParameters("uint256,uint256,uint256"),
|
|
609
|
+
[amount0Min ?? 0n, amount1Min ?? 0n, deadline ?? maxUint256]
|
|
610
|
+
);
|
|
611
|
+
return encodeFunctionData({
|
|
612
|
+
abi: PeripheryPoolABI2,
|
|
613
|
+
functionName: "mint",
|
|
614
|
+
args: [recipient, tickLower, tickUpper, amount0, amount1, extraData]
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
function encodeBurn(params) {
|
|
618
|
+
const { tokenId, amount0Min, amount1Min, deadline } = params;
|
|
619
|
+
const extraData = encodeAbiParameters(
|
|
620
|
+
parseAbiParameters("uint256,uint256,uint256"),
|
|
621
|
+
[amount0Min ?? 0n, amount1Min ?? 0n, deadline ?? maxUint256]
|
|
622
|
+
);
|
|
623
|
+
return encodeFunctionData({
|
|
624
|
+
abi: PeripheryPoolABI2,
|
|
625
|
+
functionName: "burn",
|
|
626
|
+
args: [tokenId, extraData]
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
function encodeIncreaseLiquidity(params) {
|
|
630
|
+
const { tokenId, amount0, amount1, amount0Min, amount1Min, deadline } = params;
|
|
631
|
+
const extraData = encodeAbiParameters(
|
|
632
|
+
parseAbiParameters("uint256,uint256,uint256"),
|
|
633
|
+
[amount0Min ?? 0n, amount1Min ?? 0n, deadline ?? maxUint256]
|
|
634
|
+
);
|
|
635
|
+
return encodeFunctionData({
|
|
636
|
+
abi: PeripheryPoolABI2,
|
|
637
|
+
functionName: "increaseLiquidity",
|
|
638
|
+
args: [tokenId, amount0, amount1, extraData]
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
function encodeDecreaseLiquidity(params) {
|
|
642
|
+
const { tokenId, liquidityAmount, amount0Min, amount1Min, deadline } = params;
|
|
643
|
+
const extraData = encodeAbiParameters(
|
|
644
|
+
parseAbiParameters("uint256,uint256,uint256"),
|
|
645
|
+
[amount0Min ?? 0n, amount1Min ?? 0n, deadline ?? maxUint256]
|
|
646
|
+
);
|
|
647
|
+
return encodeFunctionData({
|
|
648
|
+
abi: PeripheryPoolABI2,
|
|
649
|
+
functionName: "decreaseLiquidity",
|
|
650
|
+
args: [tokenId, liquidityAmount, extraData]
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
async function buildSwap(params) {
|
|
654
|
+
const { poolKey, chain, mode, ...encodeParams } = params;
|
|
655
|
+
return {
|
|
656
|
+
target: resolvePeripheryTarget(poolKey, chain, mode),
|
|
657
|
+
calldata: encodeSwap(encodeParams)
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
async function buildMint(params) {
|
|
661
|
+
const { poolKey, chain, mode, ...encodeParams } = params;
|
|
662
|
+
return {
|
|
663
|
+
target: resolvePeripheryTarget(poolKey, chain, mode),
|
|
664
|
+
calldata: encodeMint(encodeParams)
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
async function buildBurn(params) {
|
|
668
|
+
const { poolKey, chain, mode, ...encodeParams } = params;
|
|
669
|
+
return {
|
|
670
|
+
target: resolvePeripheryTarget(poolKey, chain, mode),
|
|
671
|
+
calldata: encodeBurn(encodeParams)
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
async function buildIncreaseLiquidity(params) {
|
|
675
|
+
const { poolKey, chain, mode, ...encodeParams } = params;
|
|
676
|
+
return {
|
|
677
|
+
target: resolvePeripheryTarget(poolKey, chain, mode),
|
|
678
|
+
calldata: encodeIncreaseLiquidity(encodeParams)
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
async function buildDecreaseLiquidity(params) {
|
|
682
|
+
const { poolKey, chain, mode, ...encodeParams } = params;
|
|
683
|
+
return {
|
|
684
|
+
target: resolvePeripheryTarget(poolKey, chain, mode),
|
|
685
|
+
calldata: encodeDecreaseLiquidity(encodeParams)
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// src/client.ts
|
|
690
|
+
var AmmEvm = class {
|
|
691
|
+
env;
|
|
692
|
+
constructor(env) {
|
|
693
|
+
this.env = env;
|
|
694
|
+
}
|
|
695
|
+
// ── Kernel readers ────────────────────────────────────────────────────────
|
|
696
|
+
readPool(p) {
|
|
697
|
+
return readPool({ ...p, mode: this.env, client: p.client });
|
|
698
|
+
}
|
|
699
|
+
readKernelBalances(p) {
|
|
700
|
+
return readKernelBalances({ ...p, mode: this.env, client: p.client });
|
|
701
|
+
}
|
|
702
|
+
readPosition(p) {
|
|
703
|
+
return readPosition({ ...p, mode: this.env, client: p.client });
|
|
704
|
+
}
|
|
705
|
+
readTickBitmap(p) {
|
|
706
|
+
return readTickBitmap({ ...p, mode: this.env, client: p.client });
|
|
707
|
+
}
|
|
708
|
+
readTick(p) {
|
|
709
|
+
return readTick({ ...p, mode: this.env, client: p.client });
|
|
710
|
+
}
|
|
711
|
+
computePositionAmounts(p) {
|
|
712
|
+
return computePositionAmounts({ ...p, mode: this.env, client: p.client });
|
|
713
|
+
}
|
|
714
|
+
// ── Kernel lens (simulate) ────────────────────────────────────────────────
|
|
715
|
+
simulateSwap(p) {
|
|
716
|
+
return simulateSwap({ ...p, mode: this.env, client: p.client });
|
|
717
|
+
}
|
|
718
|
+
simulateMint(p) {
|
|
719
|
+
return simulateMint({ ...p, mode: this.env, client: p.client });
|
|
720
|
+
}
|
|
721
|
+
simulateBurn(p) {
|
|
722
|
+
return simulateBurn({ ...p, mode: this.env, client: p.client });
|
|
723
|
+
}
|
|
724
|
+
simulateDecreaseLiquidity(p) {
|
|
725
|
+
return simulateDecreaseLiquidity({ ...p, mode: this.env, client: p.client });
|
|
726
|
+
}
|
|
727
|
+
// ── Periphery readers ─────────────────────────────────────────────────────
|
|
728
|
+
readPeripheryPool(p) {
|
|
729
|
+
return readPeripheryPool({ ...p, mode: this.env, client: p.client });
|
|
730
|
+
}
|
|
731
|
+
readUserPoolBalances(p) {
|
|
732
|
+
return readUserPoolBalances({ ...p, mode: this.env, client: p.client });
|
|
733
|
+
}
|
|
734
|
+
// ── Periphery calldata builders ───────────────────────────────────────────
|
|
735
|
+
buildSwap(p) {
|
|
736
|
+
return buildSwap({ ...p, mode: this.env });
|
|
737
|
+
}
|
|
738
|
+
buildMint(p) {
|
|
739
|
+
return buildMint({ ...p, mode: this.env });
|
|
740
|
+
}
|
|
741
|
+
buildBurn(p) {
|
|
742
|
+
return buildBurn({ ...p, mode: this.env });
|
|
743
|
+
}
|
|
744
|
+
buildIncreaseLiquidity(p) {
|
|
745
|
+
return buildIncreaseLiquidity({ ...p, mode: this.env });
|
|
746
|
+
}
|
|
747
|
+
buildDecreaseLiquidity(p) {
|
|
748
|
+
return buildDecreaseLiquidity({ ...p, mode: this.env });
|
|
749
|
+
}
|
|
750
|
+
// ── Periphery submitters ──────────────────────────────────────────────────
|
|
751
|
+
submitAction(wallet, params) {
|
|
752
|
+
return submitAction(wallet, params, this.env);
|
|
753
|
+
}
|
|
754
|
+
submitSwap(wallet, params) {
|
|
755
|
+
return submitSwap(wallet, params, this.env);
|
|
756
|
+
}
|
|
757
|
+
submitApproval(wallet, params) {
|
|
758
|
+
return submitApproval(wallet, params, this.env);
|
|
759
|
+
}
|
|
760
|
+
/** EVM submits any action via the same path — alias of `submitAction`. */
|
|
761
|
+
submitMint(wallet, params) {
|
|
762
|
+
return submitAction(wallet, params, this.env);
|
|
763
|
+
}
|
|
764
|
+
/** EVM submits any action via the same path — alias of `submitAction`. */
|
|
765
|
+
submitBurn(wallet, params) {
|
|
766
|
+
return submitAction(wallet, params, this.env);
|
|
767
|
+
}
|
|
768
|
+
/** EVM submits any action via the same path — alias of `submitAction`. */
|
|
769
|
+
submitIncreaseLiquidity(wallet, params) {
|
|
770
|
+
return submitAction(wallet, params, this.env);
|
|
771
|
+
}
|
|
772
|
+
/** EVM submits any action via the same path — alias of `submitAction`. */
|
|
773
|
+
submitDecreaseLiquidity(wallet, params) {
|
|
774
|
+
return submitAction(wallet, params, this.env);
|
|
775
|
+
}
|
|
776
|
+
// ── Chain-client factories ────────────────────────────────────────────────
|
|
777
|
+
getKernelPublicClient(transport) {
|
|
778
|
+
return getKernelPublicClient(this.env, transport);
|
|
779
|
+
}
|
|
780
|
+
getSourcePublicClient(chain, transport) {
|
|
781
|
+
return getSourcePublicClient(chain, this.env, transport);
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
// src/skate-amm.ts
|
|
786
|
+
import { AmmCore as AmmCore3 } from "@skate-org/amm-core-v2";
|
|
787
|
+
import { AmmApi } from "@skate-org/amm-api-v2";
|
|
788
|
+
var SkateAmm = class {
|
|
789
|
+
env;
|
|
790
|
+
core;
|
|
791
|
+
api;
|
|
792
|
+
vm;
|
|
793
|
+
quote;
|
|
794
|
+
actions;
|
|
795
|
+
pools;
|
|
796
|
+
constructor(env) {
|
|
797
|
+
this.env = env;
|
|
798
|
+
this.core = new AmmCore3(env);
|
|
799
|
+
this.api = new AmmApi(env);
|
|
800
|
+
this.vm = new AmmEvm(env);
|
|
801
|
+
const api = this.api;
|
|
802
|
+
const core = this.core;
|
|
803
|
+
this.quote = {
|
|
804
|
+
swap: api.swapQuote.bind(api),
|
|
805
|
+
mint: api.mintQuote.bind(api),
|
|
806
|
+
burn: api.burnQuote.bind(api),
|
|
807
|
+
increaseLiquidity: api.increaseLiquidityQuote.bind(api),
|
|
808
|
+
decreaseLiquidity: api.decreaseLiquidityQuote.bind(api),
|
|
809
|
+
boostedMint: api.boostedMintQuote.bind(api),
|
|
810
|
+
boostedRange: api.boostedRangeQuote.bind(api),
|
|
811
|
+
streamSwap: api.streamSwapQuote.bind(api),
|
|
812
|
+
subscribeSwap: api.subscribeSwapQuote.bind(api)
|
|
813
|
+
};
|
|
814
|
+
this.actions = {
|
|
815
|
+
get: api.getAction.bind(api),
|
|
816
|
+
byUser: api.getActionsByUser.bind(api),
|
|
817
|
+
byUserTimeRange: api.getActionsByUserTimeRange.bind(api),
|
|
818
|
+
waitForExecuted: api.waitForExecuted.bind(api),
|
|
819
|
+
position: api.getPosition.bind(api)
|
|
820
|
+
};
|
|
821
|
+
this.pools = {
|
|
822
|
+
all: core.getAllPoolInfo.bind(core),
|
|
823
|
+
keys: core.getAllPoolKeys.bind(core),
|
|
824
|
+
byKey: core.getPoolInfoByKey.bind(core),
|
|
825
|
+
supportedChains: core.getSupportedChains.bind(core),
|
|
826
|
+
supportedPairs: core.getSupportedPairs.bind(core),
|
|
827
|
+
supportedTokens: core.getSupportedTokens.bind(core),
|
|
828
|
+
keyByKernelPool: core.kernelPoolToPoolKey.bind(core)
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
// ── AmmVmClient: builders ────────────────────────────────────────────────────
|
|
832
|
+
buildSwap(p) {
|
|
833
|
+
return this.vm.buildSwap(p);
|
|
834
|
+
}
|
|
835
|
+
buildMint(p) {
|
|
836
|
+
return this.vm.buildMint(p);
|
|
837
|
+
}
|
|
838
|
+
buildBurn(p) {
|
|
839
|
+
return this.vm.buildBurn(p);
|
|
840
|
+
}
|
|
841
|
+
buildIncreaseLiquidity(p) {
|
|
842
|
+
return this.vm.buildIncreaseLiquidity(p);
|
|
843
|
+
}
|
|
844
|
+
buildDecreaseLiquidity(p) {
|
|
845
|
+
return this.vm.buildDecreaseLiquidity(p);
|
|
846
|
+
}
|
|
847
|
+
// ── AmmVmClient: submitters ──────────────────────────────────────────────────
|
|
848
|
+
submitSwap(w, p) {
|
|
849
|
+
return this.vm.submitSwap(w, p);
|
|
850
|
+
}
|
|
851
|
+
submitMint(w, p) {
|
|
852
|
+
return this.vm.submitMint(w, p);
|
|
853
|
+
}
|
|
854
|
+
submitBurn(w, p) {
|
|
855
|
+
return this.vm.submitBurn(w, p);
|
|
856
|
+
}
|
|
857
|
+
submitIncreaseLiquidity(w, p) {
|
|
858
|
+
return this.vm.submitIncreaseLiquidity(w, p);
|
|
859
|
+
}
|
|
860
|
+
submitDecreaseLiquidity(w, p) {
|
|
861
|
+
return this.vm.submitDecreaseLiquidity(w, p);
|
|
862
|
+
}
|
|
863
|
+
// ── AmmVmClient: readers ─────────────────────────────────────────────────────
|
|
864
|
+
readPeripheryPool(p) {
|
|
865
|
+
return this.vm.readPeripheryPool(p);
|
|
866
|
+
}
|
|
867
|
+
readUserPoolBalances(p) {
|
|
868
|
+
return this.vm.readUserPoolBalances(p);
|
|
869
|
+
}
|
|
870
|
+
// ── EVM extras ───────────────────────────────────────────────────────────────
|
|
871
|
+
/** Read raw kernel pool slot. */
|
|
872
|
+
readPool(p) {
|
|
873
|
+
return this.vm.readPool(p);
|
|
874
|
+
}
|
|
875
|
+
readKernelBalances(p) {
|
|
876
|
+
return this.vm.readKernelBalances(p);
|
|
877
|
+
}
|
|
878
|
+
readPosition(p) {
|
|
879
|
+
return this.vm.readPosition(p);
|
|
880
|
+
}
|
|
881
|
+
readTickBitmap(p) {
|
|
882
|
+
return this.vm.readTickBitmap(p);
|
|
883
|
+
}
|
|
884
|
+
readTick(p) {
|
|
885
|
+
return this.vm.readTick(p);
|
|
886
|
+
}
|
|
887
|
+
computePositionAmounts(p) {
|
|
888
|
+
return this.vm.computePositionAmounts(p);
|
|
889
|
+
}
|
|
890
|
+
simulateSwap(p) {
|
|
891
|
+
return this.vm.simulateSwap(p);
|
|
892
|
+
}
|
|
893
|
+
simulateMint(p) {
|
|
894
|
+
return this.vm.simulateMint(p);
|
|
895
|
+
}
|
|
896
|
+
simulateBurn(p) {
|
|
897
|
+
return this.vm.simulateBurn(p);
|
|
898
|
+
}
|
|
899
|
+
simulateDecreaseLiquidity(p) {
|
|
900
|
+
return this.vm.simulateDecreaseLiquidity(p);
|
|
901
|
+
}
|
|
902
|
+
submitAction(w, p) {
|
|
903
|
+
return this.vm.submitAction(w, p);
|
|
904
|
+
}
|
|
905
|
+
submitApproval(w, p) {
|
|
906
|
+
return this.vm.submitApproval(w, p);
|
|
907
|
+
}
|
|
908
|
+
getKernelPublicClient(...args) {
|
|
909
|
+
return this.vm.getKernelPublicClient(...args);
|
|
910
|
+
}
|
|
911
|
+
getSourcePublicClient(...args) {
|
|
912
|
+
return this.vm.getSourcePublicClient(...args);
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
// src/devConfig.ts
|
|
917
|
+
var DEFAULT_DEV_API_ENDPOINT = "https://dev.api.skatechain.org/amm-action-v2";
|
|
918
|
+
function createDevConfig(overrides = {}) {
|
|
919
|
+
return {
|
|
920
|
+
mode: "DEV",
|
|
921
|
+
config: {
|
|
922
|
+
apiEndpoint: overrides.apiEndpoint ?? DEFAULT_DEV_API_ENDPOINT,
|
|
923
|
+
...overrides.rpcEndpoints ? { rpcEndpoints: overrides.rpcEndpoints } : {}
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// src/kernel/tickBitmap.ts
|
|
929
|
+
function decodeTickBitmapWord(word, wordPos, tickSpacing) {
|
|
930
|
+
if (!Number.isSafeInteger(wordPos)) {
|
|
931
|
+
throw new RangeError(
|
|
932
|
+
`decodeTickBitmapWord: wordPos must be a safe integer, got ${wordPos}`
|
|
933
|
+
);
|
|
934
|
+
}
|
|
935
|
+
if (!Number.isSafeInteger(tickSpacing) || tickSpacing <= 0) {
|
|
936
|
+
throw new RangeError(
|
|
937
|
+
`decodeTickBitmapWord: tickSpacing must be a positive integer, got ${tickSpacing}`
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
const result = [];
|
|
941
|
+
const base = wordPos * 256;
|
|
942
|
+
for (let k = 0; k < 256; k++) {
|
|
943
|
+
if ((word >> BigInt(k) & 1n) === 1n) {
|
|
944
|
+
result.push((base + k) * tickSpacing);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
return result;
|
|
948
|
+
}
|
|
949
|
+
|
|
561
950
|
// src/events/kernel.ts
|
|
562
951
|
import { decodeEventLog } from "viem";
|
|
563
952
|
import { KernelEventEmitterABI } from "@skate-org/amm-bindings";
|
|
@@ -670,10 +1059,17 @@ function watchPeripheryEvents(client, address, opts) {
|
|
|
670
1059
|
});
|
|
671
1060
|
}
|
|
672
1061
|
export {
|
|
1062
|
+
AmmEvm,
|
|
673
1063
|
EvmReadError,
|
|
674
1064
|
EvmWriteError,
|
|
675
1065
|
KERNEL_EVENT_NAMES,
|
|
676
1066
|
PERIPHERY_EVENT_NAMES,
|
|
1067
|
+
SkateAmm,
|
|
1068
|
+
buildBurn,
|
|
1069
|
+
buildDecreaseLiquidity,
|
|
1070
|
+
buildIncreaseLiquidity,
|
|
1071
|
+
buildMint,
|
|
1072
|
+
buildSwap,
|
|
677
1073
|
computePositionAmounts,
|
|
678
1074
|
createDevConfig,
|
|
679
1075
|
decodeTickBitmapWord,
|
|
@@ -682,6 +1078,7 @@ export {
|
|
|
682
1078
|
megaethChain,
|
|
683
1079
|
parseKernelEventLog,
|
|
684
1080
|
parsePeripheryEventLog,
|
|
1081
|
+
readKernelBalances,
|
|
685
1082
|
readPeripheryPool,
|
|
686
1083
|
readPool,
|
|
687
1084
|
readPosition,
|