@skate-org/amm-evm-v2 0.4.1
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 +26 -0
- package/README.md +33 -0
- package/dist/chain-clients.d.ts +42 -0
- package/dist/chain-clients.d.ts.map +1 -0
- package/dist/devConfig.d.ts +23 -0
- package/dist/devConfig.d.ts.map +1 -0
- package/dist/errors.d.ts +53 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/events/index.d.ts +4 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/kernel.d.ts +25 -0
- package/dist/events/kernel.d.ts.map +1 -0
- package/dist/events/periphery.d.ts +21 -0
- package/dist/events/periphery.d.ts.map +1 -0
- package/dist/events/watch.d.ts +23 -0
- package/dist/events/watch.d.ts.map +1 -0
- package/dist/index.cjs +997 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +952 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel/index.d.ts +3 -0
- package/dist/kernel/index.d.ts.map +1 -0
- package/dist/kernel/reader.d.ts +77 -0
- package/dist/kernel/reader.d.ts.map +1 -0
- package/dist/kernel/tickBitmap.d.ts +30 -0
- package/dist/kernel/tickBitmap.d.ts.map +1 -0
- package/dist/math/fullMath.d.ts +28 -0
- package/dist/math/fullMath.d.ts.map +1 -0
- package/dist/math/index.d.ts +5 -0
- package/dist/math/index.d.ts.map +1 -0
- package/dist/math/liquidityMath.d.ts +18 -0
- package/dist/math/liquidityMath.d.ts.map +1 -0
- package/dist/math/sqrtPriceMath.d.ts +62 -0
- package/dist/math/sqrtPriceMath.d.ts.map +1 -0
- package/dist/math/tickMath.d.ts +36 -0
- package/dist/math/tickMath.d.ts.map +1 -0
- package/dist/periphery/index.d.ts +3 -0
- package/dist/periphery/index.d.ts.map +1 -0
- package/dist/periphery/reader.d.ts +75 -0
- package/dist/periphery/reader.d.ts.map +1 -0
- package/dist/periphery/submit.d.ts +63 -0
- package/dist/periphery/submit.d.ts.map +1 -0
- package/package.json +41 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,952 @@
|
|
|
1
|
+
// src/chain-clients.ts
|
|
2
|
+
import {
|
|
3
|
+
createPublicClient,
|
|
4
|
+
defineChain,
|
|
5
|
+
http
|
|
6
|
+
} from "viem";
|
|
7
|
+
import { arbitrum } from "viem/chains";
|
|
8
|
+
import {
|
|
9
|
+
BLOCK_EXPLORER,
|
|
10
|
+
CHAIN,
|
|
11
|
+
VM,
|
|
12
|
+
vmTypeFromChain,
|
|
13
|
+
normalizeMode
|
|
14
|
+
} from "@skate-org/amm-core-v2";
|
|
15
|
+
var megaethChain = defineChain({
|
|
16
|
+
id: 4326,
|
|
17
|
+
name: "MegaETH",
|
|
18
|
+
network: "megaeth",
|
|
19
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
20
|
+
rpcUrls: {
|
|
21
|
+
default: { http: ["https://mainnet.megaeth.com/rpc"] },
|
|
22
|
+
public: { http: ["https://mainnet.megaeth.com/rpc"] }
|
|
23
|
+
},
|
|
24
|
+
blockExplorers: {
|
|
25
|
+
default: { name: "Blockscout", url: BLOCK_EXPLORER.MEGAETH }
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
var DEFAULT_RPC = {
|
|
29
|
+
[CHAIN.MEGAETH]: "https://mainnet.megaeth.com/rpc",
|
|
30
|
+
[CHAIN.ARBITRUM]: "https://arb1.arbitrum.io/rpc"
|
|
31
|
+
};
|
|
32
|
+
function chainDefFor(chain) {
|
|
33
|
+
switch (chain) {
|
|
34
|
+
case CHAIN.MEGAETH:
|
|
35
|
+
return megaethChain;
|
|
36
|
+
case CHAIN.ARBITRUM:
|
|
37
|
+
return arbitrum;
|
|
38
|
+
default:
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function resolveRpcUrl(chain, mode) {
|
|
43
|
+
const { config } = normalizeMode(mode);
|
|
44
|
+
return config?.rpcEndpoints?.[chain] ?? DEFAULT_RPC[chain];
|
|
45
|
+
}
|
|
46
|
+
function getKernelPublicClient(mode, transport) {
|
|
47
|
+
return createPublicClient({
|
|
48
|
+
chain: megaethChain,
|
|
49
|
+
transport: transport ?? http(resolveRpcUrl(CHAIN.MEGAETH, mode))
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function getSourcePublicClient(chain, mode, transport) {
|
|
53
|
+
if (vmTypeFromChain(chain) !== VM.EVM) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`getSourcePublicClient: chain ${chain} is not an EVM chain`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const chainDef = chainDefFor(chain);
|
|
59
|
+
if (!chainDef) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`getSourcePublicClient: no chain definition registered for chain ${chain}`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const rpc = resolveRpcUrl(chain, mode);
|
|
65
|
+
if (!rpc && !transport) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`getSourcePublicClient: no default or override RPC configured for chain ${chain}; pass a transport override or set DevModeConfig.rpcEndpoints[${chain}]`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return createPublicClient({
|
|
71
|
+
chain: chainDef,
|
|
72
|
+
transport: transport ?? http(rpc)
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/devConfig.ts
|
|
77
|
+
var DEFAULT_DEV_API_ENDPOINT = "https://dev.api.skatechain.org/amm-action-v2";
|
|
78
|
+
function createDevConfig(overrides = {}) {
|
|
79
|
+
return {
|
|
80
|
+
mode: "DEV",
|
|
81
|
+
config: {
|
|
82
|
+
apiEndpoint: overrides.apiEndpoint ?? DEFAULT_DEV_API_ENDPOINT,
|
|
83
|
+
...overrides.rpcEndpoints ? { rpcEndpoints: overrides.rpcEndpoints } : {}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/errors.ts
|
|
89
|
+
import { SdkError } from "@skate-org/amm-core-v2";
|
|
90
|
+
var EvmReadError = class extends SdkError {
|
|
91
|
+
name = "EvmReadError";
|
|
92
|
+
functionName;
|
|
93
|
+
constructor(functionName, message, options) {
|
|
94
|
+
super(`${functionName}: ${message}`, options);
|
|
95
|
+
this.functionName = functionName;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var EvmWriteError = class extends SdkError {
|
|
99
|
+
name = "EvmWriteError";
|
|
100
|
+
functionName;
|
|
101
|
+
constructor(functionName, message, options) {
|
|
102
|
+
super(`${functionName}: ${message}`, options);
|
|
103
|
+
this.functionName = functionName;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
function wrapReadError(err, functionName) {
|
|
107
|
+
const outerName = err?.name;
|
|
108
|
+
const causeName = err?.cause?.name;
|
|
109
|
+
const message = err?.shortMessage ?? err?.message ?? String(err);
|
|
110
|
+
const isViemContractError = outerName === "ContractFunctionExecutionError" || outerName === "ContractFunctionRevertedError" || outerName === "ContractFunctionZeroDataError" || causeName === "ContractFunctionRevertedError" || causeName === "ContractFunctionZeroDataError";
|
|
111
|
+
if (isViemContractError) {
|
|
112
|
+
return new EvmReadError(functionName, `reverted: ${message}`, {
|
|
113
|
+
cause: err
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return new EvmReadError(functionName, message, { cause: err });
|
|
117
|
+
}
|
|
118
|
+
function wrapWriteError(err, functionName) {
|
|
119
|
+
const message = err?.shortMessage ?? err?.message ?? String(err);
|
|
120
|
+
return new EvmWriteError(functionName, message, { cause: err });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/math/tickMath.ts
|
|
124
|
+
var MAX_UINT256 = (1n << 256n) - 1n;
|
|
125
|
+
var Q160_MASK = (1n << 160n) - 1n;
|
|
126
|
+
var Q256_MOD = 1n << 256n;
|
|
127
|
+
var MIN_TICK = -887272;
|
|
128
|
+
var MAX_TICK = 887272;
|
|
129
|
+
var MIN_SQRT_RATIO = 4295128739n;
|
|
130
|
+
var MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342n;
|
|
131
|
+
function getSqrtRatioAtTick(tick) {
|
|
132
|
+
if (!Number.isInteger(tick)) {
|
|
133
|
+
throw new Error("TickMath: tick must be an integer");
|
|
134
|
+
}
|
|
135
|
+
const absTick = BigInt(tick < 0 ? -tick : tick);
|
|
136
|
+
if (absTick > BigInt(MAX_TICK)) {
|
|
137
|
+
throw new Error("T");
|
|
138
|
+
}
|
|
139
|
+
let ratio = (absTick & 0x1n) !== 0n ? 0xfffcb933bd6fad37aa2d162d1a594001n : 0x100000000000000000000000000000000n;
|
|
140
|
+
if ((absTick & 0x2n) !== 0n)
|
|
141
|
+
ratio = (ratio * 0xfff97272373d413259a46990580e213an & MAX_UINT256) >> 128n;
|
|
142
|
+
if ((absTick & 0x4n) !== 0n)
|
|
143
|
+
ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdccn & MAX_UINT256) >> 128n;
|
|
144
|
+
if ((absTick & 0x8n) !== 0n)
|
|
145
|
+
ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0n & MAX_UINT256) >> 128n;
|
|
146
|
+
if ((absTick & 0x10n) !== 0n)
|
|
147
|
+
ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644n & MAX_UINT256) >> 128n;
|
|
148
|
+
if ((absTick & 0x20n) !== 0n)
|
|
149
|
+
ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0n & MAX_UINT256) >> 128n;
|
|
150
|
+
if ((absTick & 0x40n) !== 0n)
|
|
151
|
+
ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861n & MAX_UINT256) >> 128n;
|
|
152
|
+
if ((absTick & 0x80n) !== 0n)
|
|
153
|
+
ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053n & MAX_UINT256) >> 128n;
|
|
154
|
+
if ((absTick & 0x100n) !== 0n)
|
|
155
|
+
ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4n & MAX_UINT256) >> 128n;
|
|
156
|
+
if ((absTick & 0x200n) !== 0n)
|
|
157
|
+
ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54n & MAX_UINT256) >> 128n;
|
|
158
|
+
if ((absTick & 0x400n) !== 0n)
|
|
159
|
+
ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3n & MAX_UINT256) >> 128n;
|
|
160
|
+
if ((absTick & 0x800n) !== 0n)
|
|
161
|
+
ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9n & MAX_UINT256) >> 128n;
|
|
162
|
+
if ((absTick & 0x1000n) !== 0n)
|
|
163
|
+
ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825n & MAX_UINT256) >> 128n;
|
|
164
|
+
if ((absTick & 0x2000n) !== 0n)
|
|
165
|
+
ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5n & MAX_UINT256) >> 128n;
|
|
166
|
+
if ((absTick & 0x4000n) !== 0n)
|
|
167
|
+
ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7n & MAX_UINT256) >> 128n;
|
|
168
|
+
if ((absTick & 0x8000n) !== 0n)
|
|
169
|
+
ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6n & MAX_UINT256) >> 128n;
|
|
170
|
+
if ((absTick & 0x10000n) !== 0n)
|
|
171
|
+
ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9n & MAX_UINT256) >> 128n;
|
|
172
|
+
if ((absTick & 0x20000n) !== 0n)
|
|
173
|
+
ratio = (ratio * 0x5d6af8dedb81196699c329225ee604n & MAX_UINT256) >> 128n;
|
|
174
|
+
if ((absTick & 0x40000n) !== 0n)
|
|
175
|
+
ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98n & MAX_UINT256) >> 128n;
|
|
176
|
+
if ((absTick & 0x80000n) !== 0n)
|
|
177
|
+
ratio = (ratio * 0x48a170391f7dc42444e8fa2n & MAX_UINT256) >> 128n;
|
|
178
|
+
if (tick > 0) ratio = MAX_UINT256 / ratio;
|
|
179
|
+
const shifted = ratio >> 32n;
|
|
180
|
+
const remainder = ratio & (1n << 32n) - 1n;
|
|
181
|
+
const sqrtPriceX96 = remainder === 0n ? shifted : shifted + 1n;
|
|
182
|
+
return sqrtPriceX96 & Q160_MASK;
|
|
183
|
+
}
|
|
184
|
+
function getTickAtSqrtRatio(sqrtPriceX96) {
|
|
185
|
+
if (sqrtPriceX96 < MIN_SQRT_RATIO || sqrtPriceX96 >= MAX_SQRT_RATIO) {
|
|
186
|
+
throw new Error("R");
|
|
187
|
+
}
|
|
188
|
+
const ratio = sqrtPriceX96 << 32n;
|
|
189
|
+
let r = ratio;
|
|
190
|
+
let msb = 0n;
|
|
191
|
+
{
|
|
192
|
+
const f = r > 0xffffffffffffffffffffffffffffffffn ? 128n : 0n;
|
|
193
|
+
msb |= f;
|
|
194
|
+
r >>= f;
|
|
195
|
+
}
|
|
196
|
+
{
|
|
197
|
+
const f = r > 0xffffffffffffffffn ? 64n : 0n;
|
|
198
|
+
msb |= f;
|
|
199
|
+
r >>= f;
|
|
200
|
+
}
|
|
201
|
+
{
|
|
202
|
+
const f = r > 0xffffffffn ? 32n : 0n;
|
|
203
|
+
msb |= f;
|
|
204
|
+
r >>= f;
|
|
205
|
+
}
|
|
206
|
+
{
|
|
207
|
+
const f = r > 0xffffn ? 16n : 0n;
|
|
208
|
+
msb |= f;
|
|
209
|
+
r >>= f;
|
|
210
|
+
}
|
|
211
|
+
{
|
|
212
|
+
const f = r > 0xffn ? 8n : 0n;
|
|
213
|
+
msb |= f;
|
|
214
|
+
r >>= f;
|
|
215
|
+
}
|
|
216
|
+
{
|
|
217
|
+
const f = r > 0xfn ? 4n : 0n;
|
|
218
|
+
msb |= f;
|
|
219
|
+
r >>= f;
|
|
220
|
+
}
|
|
221
|
+
{
|
|
222
|
+
const f = r > 0x3n ? 2n : 0n;
|
|
223
|
+
msb |= f;
|
|
224
|
+
r >>= f;
|
|
225
|
+
}
|
|
226
|
+
{
|
|
227
|
+
const f = r > 0x1n ? 1n : 0n;
|
|
228
|
+
msb |= f;
|
|
229
|
+
}
|
|
230
|
+
if (msb >= 128n) r = ratio >> msb - 127n;
|
|
231
|
+
else r = ratio << 127n - msb & MAX_UINT256;
|
|
232
|
+
const offset = msb - 128n;
|
|
233
|
+
let log_2 = offset << 64n & MAX_UINT256;
|
|
234
|
+
for (let i = 0; i < 14; i++) {
|
|
235
|
+
r = r * r >> 127n;
|
|
236
|
+
const f = r >> 128n;
|
|
237
|
+
log_2 |= f << BigInt(63 - i);
|
|
238
|
+
r >>= f;
|
|
239
|
+
}
|
|
240
|
+
const log_2_signed = toInt256(log_2);
|
|
241
|
+
const log_sqrt10001 = log_2_signed * 255738958999603826347141n;
|
|
242
|
+
const tickLowBig = log_sqrt10001 - 3402992956809132418596140100660247210n >> 128n;
|
|
243
|
+
const tickHiBig = log_sqrt10001 + 291339464771989622907027621153398088495n >> 128n;
|
|
244
|
+
const tickLow = toInt24(tickLowBig);
|
|
245
|
+
const tickHi = toInt24(tickHiBig);
|
|
246
|
+
if (tickLow === tickHi) return tickLow;
|
|
247
|
+
return getSqrtRatioAtTick(tickHi) <= sqrtPriceX96 ? tickHi : tickLow;
|
|
248
|
+
}
|
|
249
|
+
function toInt256(x) {
|
|
250
|
+
const masked = x & MAX_UINT256;
|
|
251
|
+
return masked >= 1n << 255n ? masked - Q256_MOD : masked;
|
|
252
|
+
}
|
|
253
|
+
function toInt24(x) {
|
|
254
|
+
const mask = (1n << 24n) - 1n;
|
|
255
|
+
const truncated = x & mask;
|
|
256
|
+
const signed = truncated >= 1n << 23n ? truncated - (1n << 24n) : truncated;
|
|
257
|
+
return Number(signed);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// src/math/fullMath.ts
|
|
261
|
+
var MAX_UINT2562 = (1n << 256n) - 1n;
|
|
262
|
+
function mulDiv(a, b, denom) {
|
|
263
|
+
if (denom === 0n) {
|
|
264
|
+
throw new Error("FullMath: division by zero");
|
|
265
|
+
}
|
|
266
|
+
if (a < 0n || b < 0n || denom < 0n) {
|
|
267
|
+
throw new Error("FullMath: negative input");
|
|
268
|
+
}
|
|
269
|
+
const product = a * b;
|
|
270
|
+
const result = product / denom;
|
|
271
|
+
if (result > MAX_UINT2562) {
|
|
272
|
+
throw new Error("FullMath: result overflows uint256");
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
function mulDivRoundingUp(a, b, denom) {
|
|
277
|
+
if (denom === 0n) {
|
|
278
|
+
throw new Error("FullMath: division by zero");
|
|
279
|
+
}
|
|
280
|
+
if (a < 0n || b < 0n || denom < 0n) {
|
|
281
|
+
throw new Error("FullMath: negative input");
|
|
282
|
+
}
|
|
283
|
+
const product = a * b;
|
|
284
|
+
const quotient = product / denom;
|
|
285
|
+
if (quotient > MAX_UINT2562) {
|
|
286
|
+
throw new Error("FullMath: result overflows uint256");
|
|
287
|
+
}
|
|
288
|
+
if (product % denom !== 0n) {
|
|
289
|
+
if (quotient === MAX_UINT2562) {
|
|
290
|
+
throw new Error("FullMath: rounding up overflows uint256");
|
|
291
|
+
}
|
|
292
|
+
return quotient + 1n;
|
|
293
|
+
}
|
|
294
|
+
return quotient;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/math/sqrtPriceMath.ts
|
|
298
|
+
var MAX_UINT128 = (1n << 128n) - 1n;
|
|
299
|
+
var MAX_UINT160 = (1n << 160n) - 1n;
|
|
300
|
+
var MAX_UINT2563 = (1n << 256n) - 1n;
|
|
301
|
+
var Q96_RESOLUTION = 96n;
|
|
302
|
+
var Q96 = 1n << 96n;
|
|
303
|
+
function assertUint160(x, name) {
|
|
304
|
+
if (x < 0n || x > MAX_UINT160) {
|
|
305
|
+
throw new Error(`SqrtPriceMath: ${name} out of uint160 range`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function assertUint128(x, name) {
|
|
309
|
+
if (x < 0n || x > MAX_UINT128) {
|
|
310
|
+
throw new Error(`SqrtPriceMath: ${name} out of uint128 range`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function assertUint256(x, name) {
|
|
314
|
+
if (x < 0n || x > MAX_UINT2563) {
|
|
315
|
+
throw new Error(`SqrtPriceMath: ${name} out of uint256 range`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function divRoundingUp(x, y) {
|
|
319
|
+
if (y === 0n) throw new Error("SqrtPriceMath: div by zero");
|
|
320
|
+
return x / y + (x % y === 0n ? 0n : 1n);
|
|
321
|
+
}
|
|
322
|
+
function toUint160(x) {
|
|
323
|
+
if (x > MAX_UINT160) throw new Error("SqrtPriceMath: toUint160 overflow");
|
|
324
|
+
return x;
|
|
325
|
+
}
|
|
326
|
+
function getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amount, add) {
|
|
327
|
+
assertUint160(sqrtPX96, "sqrtPX96");
|
|
328
|
+
assertUint128(liquidity, "liquidity");
|
|
329
|
+
assertUint256(amount, "amount");
|
|
330
|
+
if (amount === 0n) return sqrtPX96;
|
|
331
|
+
const numerator1 = liquidity << Q96_RESOLUTION;
|
|
332
|
+
if (add) {
|
|
333
|
+
const product = amount * sqrtPX96 & MAX_UINT2563;
|
|
334
|
+
const noOverflow = amount === 0n || product / amount === sqrtPX96;
|
|
335
|
+
if (noOverflow) {
|
|
336
|
+
const denominator = numerator1 + product;
|
|
337
|
+
if (denominator >= numerator1) {
|
|
338
|
+
return toUint160(mulDivRoundingUp(numerator1, sqrtPX96, denominator));
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (sqrtPX96 === 0n) throw new Error("SqrtPriceMath: sqrtPX96 == 0");
|
|
342
|
+
const denom = numerator1 / sqrtPX96 + amount;
|
|
343
|
+
return toUint160(divRoundingUp(numerator1, denom));
|
|
344
|
+
} else {
|
|
345
|
+
const product = amount * sqrtPX96 & MAX_UINT2563;
|
|
346
|
+
const noOverflow = amount === 0n || product / amount === sqrtPX96;
|
|
347
|
+
if (!noOverflow || numerator1 <= product) {
|
|
348
|
+
throw new Error("SqrtPriceMath: getNextSqrtPriceFromAmount0 denom <= 0");
|
|
349
|
+
}
|
|
350
|
+
const denominator = numerator1 - product;
|
|
351
|
+
return toUint160(mulDivRoundingUp(numerator1, sqrtPX96, denominator));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
function getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amount, add) {
|
|
355
|
+
assertUint160(sqrtPX96, "sqrtPX96");
|
|
356
|
+
assertUint128(liquidity, "liquidity");
|
|
357
|
+
assertUint256(amount, "amount");
|
|
358
|
+
if (add) {
|
|
359
|
+
const quotient = amount <= MAX_UINT160 ? (amount << Q96_RESOLUTION) / liquidity : mulDiv(amount, Q96, liquidity);
|
|
360
|
+
return toUint160(sqrtPX96 + quotient);
|
|
361
|
+
} else {
|
|
362
|
+
const quotient = amount <= MAX_UINT160 ? divRoundingUp(amount << Q96_RESOLUTION, liquidity) : mulDivRoundingUp(amount, Q96, liquidity);
|
|
363
|
+
if (sqrtPX96 <= quotient) {
|
|
364
|
+
throw new Error("SqrtPriceMath: sqrtPX96 <= quotient");
|
|
365
|
+
}
|
|
366
|
+
return sqrtPX96 - quotient;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
function getNextSqrtPriceFromInput(sqrtPX96, liquidity, amountIn, zeroForOne) {
|
|
370
|
+
if (sqrtPX96 <= 0n) throw new Error("SqrtPriceMath: sqrtPX96 must be > 0");
|
|
371
|
+
if (liquidity <= 0n)
|
|
372
|
+
throw new Error("SqrtPriceMath: liquidity must be > 0");
|
|
373
|
+
return zeroForOne ? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true) : getNextSqrtPriceFromAmount1RoundingDown(
|
|
374
|
+
sqrtPX96,
|
|
375
|
+
liquidity,
|
|
376
|
+
amountIn,
|
|
377
|
+
true
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
function getNextSqrtPriceFromOutput(sqrtPX96, liquidity, amountOut, zeroForOne) {
|
|
381
|
+
if (sqrtPX96 <= 0n) throw new Error("SqrtPriceMath: sqrtPX96 must be > 0");
|
|
382
|
+
if (liquidity <= 0n)
|
|
383
|
+
throw new Error("SqrtPriceMath: liquidity must be > 0");
|
|
384
|
+
return zeroForOne ? getNextSqrtPriceFromAmount1RoundingDown(
|
|
385
|
+
sqrtPX96,
|
|
386
|
+
liquidity,
|
|
387
|
+
amountOut,
|
|
388
|
+
false
|
|
389
|
+
) : getNextSqrtPriceFromAmount0RoundingUp(
|
|
390
|
+
sqrtPX96,
|
|
391
|
+
liquidity,
|
|
392
|
+
amountOut,
|
|
393
|
+
false
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
function getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
397
|
+
assertUint160(sqrtRatioAX96, "sqrtRatioAX96");
|
|
398
|
+
assertUint160(sqrtRatioBX96, "sqrtRatioBX96");
|
|
399
|
+
assertUint128(liquidity, "liquidity");
|
|
400
|
+
let a = sqrtRatioAX96;
|
|
401
|
+
let b = sqrtRatioBX96;
|
|
402
|
+
if (a > b) {
|
|
403
|
+
[a, b] = [b, a];
|
|
404
|
+
}
|
|
405
|
+
if (a === 0n) throw new Error("SqrtPriceMath: sqrtRatio must be > 0");
|
|
406
|
+
const numerator1 = liquidity << Q96_RESOLUTION;
|
|
407
|
+
const numerator2 = b - a;
|
|
408
|
+
if (roundUp) {
|
|
409
|
+
return divRoundingUp(mulDivRoundingUp(numerator1, numerator2, b), a);
|
|
410
|
+
}
|
|
411
|
+
return mulDiv(numerator1, numerator2, b) / a;
|
|
412
|
+
}
|
|
413
|
+
function getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
414
|
+
assertUint160(sqrtRatioAX96, "sqrtRatioAX96");
|
|
415
|
+
assertUint160(sqrtRatioBX96, "sqrtRatioBX96");
|
|
416
|
+
assertUint128(liquidity, "liquidity");
|
|
417
|
+
let a = sqrtRatioAX96;
|
|
418
|
+
let b = sqrtRatioBX96;
|
|
419
|
+
if (a > b) {
|
|
420
|
+
[a, b] = [b, a];
|
|
421
|
+
}
|
|
422
|
+
return roundUp ? mulDivRoundingUp(liquidity, b - a, Q96) : mulDiv(liquidity, b - a, Q96);
|
|
423
|
+
}
|
|
424
|
+
function getAmount0DeltaSigned(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
|
|
425
|
+
const INT128_MIN2 = -(1n << 127n);
|
|
426
|
+
const INT128_MAX2 = (1n << 127n) - 1n;
|
|
427
|
+
if (liquidity < INT128_MIN2 || liquidity > INT128_MAX2) {
|
|
428
|
+
throw new Error("SqrtPriceMath: liquidity out of int128 range");
|
|
429
|
+
}
|
|
430
|
+
if (liquidity < 0n) {
|
|
431
|
+
return -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, -liquidity, false);
|
|
432
|
+
}
|
|
433
|
+
return getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, true);
|
|
434
|
+
}
|
|
435
|
+
function getAmount1DeltaSigned(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
|
|
436
|
+
const INT128_MIN2 = -(1n << 127n);
|
|
437
|
+
const INT128_MAX2 = (1n << 127n) - 1n;
|
|
438
|
+
if (liquidity < INT128_MIN2 || liquidity > INT128_MAX2) {
|
|
439
|
+
throw new Error("SqrtPriceMath: liquidity out of int128 range");
|
|
440
|
+
}
|
|
441
|
+
if (liquidity < 0n) {
|
|
442
|
+
return -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, -liquidity, false);
|
|
443
|
+
}
|
|
444
|
+
return getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, true);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// src/math/liquidityMath.ts
|
|
448
|
+
var MAX_UINT1282 = (1n << 128n) - 1n;
|
|
449
|
+
var INT128_MAX = (1n << 127n) - 1n;
|
|
450
|
+
var INT128_MIN = -(1n << 127n);
|
|
451
|
+
function addDelta(x, y) {
|
|
452
|
+
if (x < 0n || x > MAX_UINT1282) {
|
|
453
|
+
throw new Error("LiquidityMath: x out of uint128 range");
|
|
454
|
+
}
|
|
455
|
+
if (y < INT128_MIN || y > INT128_MAX) {
|
|
456
|
+
throw new Error("LiquidityMath: y out of int128 range");
|
|
457
|
+
}
|
|
458
|
+
const z = x + y;
|
|
459
|
+
if (y < 0n) {
|
|
460
|
+
if (z < 0n) throw new Error("LS");
|
|
461
|
+
} else {
|
|
462
|
+
if (z > MAX_UINT1282) throw new Error("LA");
|
|
463
|
+
}
|
|
464
|
+
return z;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// src/kernel/reader.ts
|
|
468
|
+
import {
|
|
469
|
+
KernelManagerABI,
|
|
470
|
+
KernelPoolABI
|
|
471
|
+
} from "@skate-org/amm-bindings";
|
|
472
|
+
import {
|
|
473
|
+
KernelManagerAddress,
|
|
474
|
+
normalizeMode as normalizeMode2
|
|
475
|
+
} from "@skate-org/amm-core-v2";
|
|
476
|
+
function resolveClient(mode, client) {
|
|
477
|
+
return client ?? getKernelPublicClient(mode);
|
|
478
|
+
}
|
|
479
|
+
async function readPool(kernelPool, mode, client) {
|
|
480
|
+
const c = resolveClient(mode, client);
|
|
481
|
+
try {
|
|
482
|
+
const [slot0, liquidity] = await Promise.all([
|
|
483
|
+
c.readContract({
|
|
484
|
+
address: kernelPool,
|
|
485
|
+
abi: KernelPoolABI,
|
|
486
|
+
functionName: "slot0"
|
|
487
|
+
}),
|
|
488
|
+
c.readContract({
|
|
489
|
+
address: kernelPool,
|
|
490
|
+
abi: KernelPoolABI,
|
|
491
|
+
functionName: "liquidity"
|
|
492
|
+
})
|
|
493
|
+
]);
|
|
494
|
+
const s0 = slot0;
|
|
495
|
+
return {
|
|
496
|
+
slot0: {
|
|
497
|
+
sqrtPriceX96: s0[0],
|
|
498
|
+
tick: s0[1],
|
|
499
|
+
observationIndex: s0[2],
|
|
500
|
+
observationCardinality: s0[3],
|
|
501
|
+
observationCardinalityNext: s0[4],
|
|
502
|
+
feeProtocol: s0[5],
|
|
503
|
+
unlocked: s0[6]
|
|
504
|
+
},
|
|
505
|
+
liquidity
|
|
506
|
+
};
|
|
507
|
+
} catch (err) {
|
|
508
|
+
throw wrapReadError(err, "readPool");
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
async function readPosition(tokenId, mode, client) {
|
|
512
|
+
const { mode: baseMode } = normalizeMode2(mode);
|
|
513
|
+
const manager = KernelManagerAddress(baseMode);
|
|
514
|
+
const c = resolveClient(mode, client);
|
|
515
|
+
try {
|
|
516
|
+
const raw = await c.readContract({
|
|
517
|
+
address: manager,
|
|
518
|
+
abi: KernelManagerABI,
|
|
519
|
+
functionName: "nftPositions",
|
|
520
|
+
args: [tokenId]
|
|
521
|
+
});
|
|
522
|
+
const asTuple = Array.isArray(raw) ? raw : null;
|
|
523
|
+
if (asTuple) {
|
|
524
|
+
return {
|
|
525
|
+
pool: asTuple[0],
|
|
526
|
+
tickLower: asTuple[1],
|
|
527
|
+
tickUpper: asTuple[2],
|
|
528
|
+
liquidity: asTuple[3],
|
|
529
|
+
feeGrowthInside0LastX128: asTuple[4],
|
|
530
|
+
feeGrowthInside1LastX128: asTuple[5],
|
|
531
|
+
tokensOwed0: asTuple[6],
|
|
532
|
+
tokensOwed1: asTuple[7]
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
return raw;
|
|
536
|
+
} catch (err) {
|
|
537
|
+
throw wrapReadError(err, "readPosition");
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
async function readTickBitmap(kernelPool, wordPos, mode, client) {
|
|
541
|
+
const c = resolveClient(mode, client);
|
|
542
|
+
try {
|
|
543
|
+
const word = await c.readContract({
|
|
544
|
+
address: kernelPool,
|
|
545
|
+
abi: KernelPoolABI,
|
|
546
|
+
functionName: "tickBitmap",
|
|
547
|
+
args: [wordPos]
|
|
548
|
+
});
|
|
549
|
+
return word;
|
|
550
|
+
} catch (err) {
|
|
551
|
+
throw wrapReadError(err, "readTickBitmap");
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
async function readTick(kernelPool, tick, mode, client) {
|
|
555
|
+
const c = resolveClient(mode, client);
|
|
556
|
+
try {
|
|
557
|
+
const raw = await c.readContract({
|
|
558
|
+
address: kernelPool,
|
|
559
|
+
abi: KernelPoolABI,
|
|
560
|
+
functionName: "ticks",
|
|
561
|
+
args: [tick]
|
|
562
|
+
});
|
|
563
|
+
return {
|
|
564
|
+
liquidityGross: raw[0],
|
|
565
|
+
liquidityNet: raw[1],
|
|
566
|
+
feeGrowthOutside0X128: raw[2],
|
|
567
|
+
feeGrowthOutside1X128: raw[3],
|
|
568
|
+
tickCumulativeOutside: raw[4],
|
|
569
|
+
secondsPerLiquidityOutsideX128: raw[5],
|
|
570
|
+
secondsOutside: raw[6],
|
|
571
|
+
initialized: raw[7]
|
|
572
|
+
};
|
|
573
|
+
} catch (err) {
|
|
574
|
+
throw wrapReadError(err, "readTick");
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
async function computePositionAmounts(tokenId, mode, client) {
|
|
578
|
+
const c = resolveClient(mode, client);
|
|
579
|
+
const position = await readPosition(tokenId, mode, c);
|
|
580
|
+
const { slot0 } = await readPool(position.pool, mode, c);
|
|
581
|
+
const sqrtLower = getSqrtRatioAtTick(position.tickLower);
|
|
582
|
+
const sqrtUpper = getSqrtRatioAtTick(position.tickUpper);
|
|
583
|
+
if (slot0.tick < position.tickLower) {
|
|
584
|
+
return {
|
|
585
|
+
amount0: getAmount0Delta(sqrtLower, sqrtUpper, position.liquidity, false),
|
|
586
|
+
amount1: 0n
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
if (slot0.tick >= position.tickUpper) {
|
|
590
|
+
return {
|
|
591
|
+
amount0: 0n,
|
|
592
|
+
amount1: getAmount1Delta(sqrtLower, sqrtUpper, position.liquidity, false)
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
return {
|
|
596
|
+
amount0: getAmount0Delta(
|
|
597
|
+
slot0.sqrtPriceX96,
|
|
598
|
+
sqrtUpper,
|
|
599
|
+
position.liquidity,
|
|
600
|
+
false
|
|
601
|
+
),
|
|
602
|
+
amount1: getAmount1Delta(
|
|
603
|
+
sqrtLower,
|
|
604
|
+
slot0.sqrtPriceX96,
|
|
605
|
+
position.liquidity,
|
|
606
|
+
false
|
|
607
|
+
)
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// src/kernel/tickBitmap.ts
|
|
612
|
+
function decodeTickBitmapWord(word, wordPos, tickSpacing) {
|
|
613
|
+
if (!Number.isSafeInteger(wordPos)) {
|
|
614
|
+
throw new RangeError(
|
|
615
|
+
`decodeTickBitmapWord: wordPos must be a safe integer, got ${wordPos}`
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
if (!Number.isSafeInteger(tickSpacing) || tickSpacing <= 0) {
|
|
619
|
+
throw new RangeError(
|
|
620
|
+
`decodeTickBitmapWord: tickSpacing must be a positive integer, got ${tickSpacing}`
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
const result = [];
|
|
624
|
+
const base = wordPos * 256;
|
|
625
|
+
for (let k = 0; k < 256; k++) {
|
|
626
|
+
if ((word >> BigInt(k) & 1n) === 1n) {
|
|
627
|
+
result.push((base + k) * tickSpacing);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return result;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// src/periphery/reader.ts
|
|
634
|
+
import { PeripheryPoolABI } from "@skate-org/amm-bindings";
|
|
635
|
+
import {
|
|
636
|
+
getPeripheryDetailsByKernelPoolAndChainId,
|
|
637
|
+
normalizeMode as normalizeMode3
|
|
638
|
+
} from "@skate-org/amm-core-v2";
|
|
639
|
+
function resolveClient2(chain, mode, client) {
|
|
640
|
+
return client ?? getSourcePublicClient(chain, mode);
|
|
641
|
+
}
|
|
642
|
+
async function readPeripheryPool(chain, kernelPool, mode, client) {
|
|
643
|
+
const { mode: baseMode } = normalizeMode3(mode);
|
|
644
|
+
const details = getPeripheryDetailsByKernelPoolAndChainId(
|
|
645
|
+
kernelPool,
|
|
646
|
+
chain,
|
|
647
|
+
baseMode
|
|
648
|
+
);
|
|
649
|
+
if (!details) {
|
|
650
|
+
throw new EvmReadError(
|
|
651
|
+
"readPeripheryPool",
|
|
652
|
+
`no periphery entry for kernelPool=${kernelPool} chain=${chain} mode=${baseMode}`
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
const address = details.address;
|
|
656
|
+
const c = resolveClient2(chain, mode, client);
|
|
657
|
+
try {
|
|
658
|
+
const [
|
|
659
|
+
balancesAvailableRaw,
|
|
660
|
+
dust0,
|
|
661
|
+
dust1,
|
|
662
|
+
token0,
|
|
663
|
+
token1,
|
|
664
|
+
fee,
|
|
665
|
+
kPool
|
|
666
|
+
] = await Promise.all([
|
|
667
|
+
c.readContract({
|
|
668
|
+
address,
|
|
669
|
+
abi: PeripheryPoolABI,
|
|
670
|
+
functionName: "balancesAvailable"
|
|
671
|
+
}),
|
|
672
|
+
c.readContract({
|
|
673
|
+
address,
|
|
674
|
+
abi: PeripheryPoolABI,
|
|
675
|
+
functionName: "dustAmount0"
|
|
676
|
+
}),
|
|
677
|
+
c.readContract({
|
|
678
|
+
address,
|
|
679
|
+
abi: PeripheryPoolABI,
|
|
680
|
+
functionName: "dustAmount1"
|
|
681
|
+
}),
|
|
682
|
+
c.readContract({
|
|
683
|
+
address,
|
|
684
|
+
abi: PeripheryPoolABI,
|
|
685
|
+
functionName: "token0"
|
|
686
|
+
}),
|
|
687
|
+
c.readContract({
|
|
688
|
+
address,
|
|
689
|
+
abi: PeripheryPoolABI,
|
|
690
|
+
functionName: "token1"
|
|
691
|
+
}),
|
|
692
|
+
c.readContract({
|
|
693
|
+
address,
|
|
694
|
+
abi: PeripheryPoolABI,
|
|
695
|
+
functionName: "fee"
|
|
696
|
+
}),
|
|
697
|
+
c.readContract({
|
|
698
|
+
address,
|
|
699
|
+
abi: PeripheryPoolABI,
|
|
700
|
+
functionName: "kernelPool"
|
|
701
|
+
})
|
|
702
|
+
]);
|
|
703
|
+
const bAvail = balancesAvailableRaw;
|
|
704
|
+
return {
|
|
705
|
+
address,
|
|
706
|
+
token0,
|
|
707
|
+
token1,
|
|
708
|
+
fee,
|
|
709
|
+
kernelPool: kPool,
|
|
710
|
+
balancesAvailable: {
|
|
711
|
+
amount0Available: bAvail[0],
|
|
712
|
+
amount1Available: bAvail[1]
|
|
713
|
+
},
|
|
714
|
+
dustAmount0: dust0,
|
|
715
|
+
dustAmount1: dust1
|
|
716
|
+
};
|
|
717
|
+
} catch (err) {
|
|
718
|
+
throw wrapReadError(err, "readPeripheryPool");
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
async function readUserPoolBalances(chain, kernelPool, user, mode, client) {
|
|
722
|
+
const { mode: baseMode } = normalizeMode3(mode);
|
|
723
|
+
const details = getPeripheryDetailsByKernelPoolAndChainId(
|
|
724
|
+
kernelPool,
|
|
725
|
+
chain,
|
|
726
|
+
baseMode
|
|
727
|
+
);
|
|
728
|
+
if (!details) {
|
|
729
|
+
throw new EvmReadError(
|
|
730
|
+
"readUserPoolBalances",
|
|
731
|
+
`no periphery entry for kernelPool=${kernelPool} chain=${chain} mode=${baseMode}`
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
const c = resolveClient2(chain, mode, client);
|
|
735
|
+
try {
|
|
736
|
+
const raw = await c.readContract({
|
|
737
|
+
address: details.address,
|
|
738
|
+
abi: PeripheryPoolABI,
|
|
739
|
+
functionName: "usersData",
|
|
740
|
+
args: [user]
|
|
741
|
+
});
|
|
742
|
+
if (Array.isArray(raw)) {
|
|
743
|
+
const tuple = raw;
|
|
744
|
+
return { amount0: tuple[0], amount1: tuple[1] };
|
|
745
|
+
}
|
|
746
|
+
const asObject = raw;
|
|
747
|
+
return { amount0: asObject.amount0, amount1: asObject.amount1 };
|
|
748
|
+
} catch (err) {
|
|
749
|
+
throw wrapReadError(err, "readUserPoolBalances");
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
// src/periphery/submit.ts
|
|
754
|
+
async function submitAction(wallet, params, _mode) {
|
|
755
|
+
if (wallet.chain?.id !== params.chain) {
|
|
756
|
+
throw new EvmWriteError(
|
|
757
|
+
"submitAction",
|
|
758
|
+
`wallet chain ${wallet.chain?.id ?? "undefined"} != params.chain ${params.chain}`
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
try {
|
|
762
|
+
const account = wallet.account;
|
|
763
|
+
if (!account) {
|
|
764
|
+
throw new EvmWriteError(
|
|
765
|
+
"submitAction",
|
|
766
|
+
"wallet has no account; callers must provide a wallet with wallet.account set"
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
return await wallet.sendTransaction({
|
|
770
|
+
to: params.to,
|
|
771
|
+
data: params.data,
|
|
772
|
+
value: params.value ?? 0n,
|
|
773
|
+
chain: wallet.chain,
|
|
774
|
+
account
|
|
775
|
+
});
|
|
776
|
+
} catch (err) {
|
|
777
|
+
if (err instanceof EvmWriteError) throw err;
|
|
778
|
+
throw wrapWriteError(err, "submitAction");
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
async function submitSwap(wallet, params, mode) {
|
|
782
|
+
try {
|
|
783
|
+
return await submitAction(wallet, params, mode);
|
|
784
|
+
} catch (err) {
|
|
785
|
+
if (err instanceof EvmWriteError) throw err;
|
|
786
|
+
throw wrapWriteError(err, "submitSwap");
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
async function submitApproval(wallet, params, mode) {
|
|
790
|
+
try {
|
|
791
|
+
return await submitAction(wallet, params, mode);
|
|
792
|
+
} catch (err) {
|
|
793
|
+
if (err instanceof EvmWriteError) throw err;
|
|
794
|
+
throw wrapWriteError(err, "submitApproval");
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// src/events/kernel.ts
|
|
799
|
+
import { decodeEventLog } from "viem";
|
|
800
|
+
import { KernelEventEmitterABI } from "@skate-org/amm-bindings";
|
|
801
|
+
var KERNEL_EVENT_NAMES = [
|
|
802
|
+
"Burn",
|
|
803
|
+
"Collect",
|
|
804
|
+
"CollectProtocol",
|
|
805
|
+
"IncreaseObservationCardinalityNext",
|
|
806
|
+
"Initialize",
|
|
807
|
+
"Mint",
|
|
808
|
+
"PeripheryPoolAdded",
|
|
809
|
+
"PeripheryPoolChanged",
|
|
810
|
+
"PoolCreated",
|
|
811
|
+
"PoolDescriptionUpdated",
|
|
812
|
+
"SetFeeProtocol",
|
|
813
|
+
"Swap"
|
|
814
|
+
];
|
|
815
|
+
function parseKernelEventLog(log) {
|
|
816
|
+
try {
|
|
817
|
+
const decoded = decodeEventLog({
|
|
818
|
+
abi: KernelEventEmitterABI,
|
|
819
|
+
data: log.data,
|
|
820
|
+
topics: log.topics
|
|
821
|
+
});
|
|
822
|
+
return {
|
|
823
|
+
eventName: decoded.eventName,
|
|
824
|
+
args: decoded.args ?? {},
|
|
825
|
+
log
|
|
826
|
+
};
|
|
827
|
+
} catch (err) {
|
|
828
|
+
const message = err.shortMessage ?? err.message ?? String(err);
|
|
829
|
+
throw new EvmReadError(
|
|
830
|
+
"parseKernelEventLog",
|
|
831
|
+
`failed to decode log: ${message}`,
|
|
832
|
+
{ cause: err }
|
|
833
|
+
);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
// src/events/periphery.ts
|
|
838
|
+
import { decodeEventLog as decodeEventLog2 } from "viem";
|
|
839
|
+
import { PeripheryEventEmitterABI } from "@skate-org/amm-bindings";
|
|
840
|
+
var PERIPHERY_EVENT_NAMES = [
|
|
841
|
+
"AmountSettled",
|
|
842
|
+
"Burned",
|
|
843
|
+
"LiquidityDecreased",
|
|
844
|
+
"LiquidityIncreased",
|
|
845
|
+
"MintSettled",
|
|
846
|
+
"Minted",
|
|
847
|
+
"PoolCreated",
|
|
848
|
+
"PoolDeployed",
|
|
849
|
+
"Swapped",
|
|
850
|
+
"TransferredTo"
|
|
851
|
+
];
|
|
852
|
+
function parsePeripheryEventLog(log) {
|
|
853
|
+
try {
|
|
854
|
+
const decoded = decodeEventLog2({
|
|
855
|
+
abi: PeripheryEventEmitterABI,
|
|
856
|
+
data: log.data,
|
|
857
|
+
topics: log.topics
|
|
858
|
+
});
|
|
859
|
+
return {
|
|
860
|
+
eventName: decoded.eventName,
|
|
861
|
+
args: decoded.args ?? {},
|
|
862
|
+
log
|
|
863
|
+
};
|
|
864
|
+
} catch (err) {
|
|
865
|
+
const message = err.shortMessage ?? err.message ?? String(err);
|
|
866
|
+
throw new EvmReadError(
|
|
867
|
+
"parsePeripheryEventLog",
|
|
868
|
+
`failed to decode log: ${message}`,
|
|
869
|
+
{ cause: err }
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// src/events/watch.ts
|
|
875
|
+
import {
|
|
876
|
+
KernelEventEmitterABI as KernelEventEmitterABI2,
|
|
877
|
+
PeripheryEventEmitterABI as PeripheryEventEmitterABI2
|
|
878
|
+
} from "@skate-org/amm-bindings";
|
|
879
|
+
function watchKernelEvents(client, address, opts) {
|
|
880
|
+
return client.watchContractEvent({
|
|
881
|
+
abi: KernelEventEmitterABI2,
|
|
882
|
+
address,
|
|
883
|
+
onLogs: (logs) => {
|
|
884
|
+
for (const log of logs) {
|
|
885
|
+
try {
|
|
886
|
+
opts.onEvent(parseKernelEventLog(log));
|
|
887
|
+
} catch (err) {
|
|
888
|
+
opts.onError?.(err);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
function watchPeripheryEvents(client, address, opts) {
|
|
895
|
+
return client.watchContractEvent({
|
|
896
|
+
abi: PeripheryEventEmitterABI2,
|
|
897
|
+
address,
|
|
898
|
+
onLogs: (logs) => {
|
|
899
|
+
for (const log of logs) {
|
|
900
|
+
try {
|
|
901
|
+
opts.onEvent(parsePeripheryEventLog(log));
|
|
902
|
+
} catch (err) {
|
|
903
|
+
opts.onError?.(err);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
export {
|
|
910
|
+
DEFAULT_DEV_API_ENDPOINT,
|
|
911
|
+
EvmReadError,
|
|
912
|
+
EvmWriteError,
|
|
913
|
+
KERNEL_EVENT_NAMES,
|
|
914
|
+
MAX_SQRT_RATIO,
|
|
915
|
+
MAX_TICK,
|
|
916
|
+
MIN_SQRT_RATIO,
|
|
917
|
+
MIN_TICK,
|
|
918
|
+
PERIPHERY_EVENT_NAMES,
|
|
919
|
+
addDelta,
|
|
920
|
+
computePositionAmounts,
|
|
921
|
+
createDevConfig,
|
|
922
|
+
decodeTickBitmapWord,
|
|
923
|
+
getAmount0Delta,
|
|
924
|
+
getAmount0DeltaSigned,
|
|
925
|
+
getAmount1Delta,
|
|
926
|
+
getAmount1DeltaSigned,
|
|
927
|
+
getKernelPublicClient,
|
|
928
|
+
getNextSqrtPriceFromAmount0RoundingUp,
|
|
929
|
+
getNextSqrtPriceFromAmount1RoundingDown,
|
|
930
|
+
getNextSqrtPriceFromInput,
|
|
931
|
+
getNextSqrtPriceFromOutput,
|
|
932
|
+
getSourcePublicClient,
|
|
933
|
+
getSqrtRatioAtTick,
|
|
934
|
+
getTickAtSqrtRatio,
|
|
935
|
+
megaethChain,
|
|
936
|
+
mulDiv,
|
|
937
|
+
mulDivRoundingUp,
|
|
938
|
+
parseKernelEventLog,
|
|
939
|
+
parsePeripheryEventLog,
|
|
940
|
+
readPeripheryPool,
|
|
941
|
+
readPool,
|
|
942
|
+
readPosition,
|
|
943
|
+
readTick,
|
|
944
|
+
readTickBitmap,
|
|
945
|
+
readUserPoolBalances,
|
|
946
|
+
submitAction,
|
|
947
|
+
submitApproval,
|
|
948
|
+
submitSwap,
|
|
949
|
+
watchKernelEvents,
|
|
950
|
+
watchPeripheryEvents
|
|
951
|
+
};
|
|
952
|
+
//# sourceMappingURL=index.js.map
|