@paraswap/dex-lib 4.3.11 → 4.3.12
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/build/abi/PendleOracle.json +413 -0
- package/build/abi/algebra-integral/AlgebraFactory.abi.json +947 -0
- package/build/abi/algebra-integral/Quoter.abi.json +231 -0
- package/build/abi/algebra-integral/SwapRouter.abi.json +810 -0
- package/build/abi/ekubo/twamm-data-fetcher.json +216 -0
- package/build/abi/ekubo/twamm.json +720 -0
- package/build/abi/ring-v2/few-wrapped-token.json +587 -0
- package/build/abi/ring-v2/ring-v2-factory.json +125 -0
- package/build/abi/ring-v2/ring-v2-pool.json +461 -0
- package/build/abi/ring-v2/ring-v2-router.json +332 -0
- package/build/abi/usdc-transmuter/usdc-transmuter.abi.json +156 -0
- package/build/dex/aave-v3-pt-roll-over/aave-v3-pt-roll-over.d.ts +58 -0
- package/build/dex/aave-v3-pt-roll-over/aave-v3-pt-roll-over.js +363 -0
- package/build/dex/aave-v3-pt-roll-over/aave-v3-pt-roll-over.js.map +1 -0
- package/build/dex/aave-v3-pt-roll-over/config.d.ts +3 -0
- package/build/dex/aave-v3-pt-roll-over/config.js +42 -0
- package/build/dex/aave-v3-pt-roll-over/config.js.map +1 -0
- package/build/dex/aave-v3-pt-roll-over/constants.d.ts +3 -0
- package/build/dex/aave-v3-pt-roll-over/constants.js +7 -0
- package/build/dex/aave-v3-pt-roll-over/constants.js.map +1 -0
- package/build/dex/aave-v3-pt-roll-over/types.d.ts +30 -0
- package/build/dex/aave-v3-pt-roll-over/types.js +3 -0
- package/build/dex/aave-v3-pt-roll-over/types.js.map +1 -0
- package/build/dex/aave-v3-pt-roll-over/utils.d.ts +4 -0
- package/build/dex/aave-v3-pt-roll-over/utils.js +12 -0
- package/build/dex/aave-v3-pt-roll-over/utils.js.map +1 -0
- package/build/dex/algebra-integral/algebra-integral-factory.d.ts +29 -0
- package/build/dex/algebra-integral/algebra-integral-factory.js +159 -0
- package/build/dex/algebra-integral/algebra-integral-factory.js.map +1 -0
- package/build/dex/algebra-integral/algebra-integral.d.ts +43 -0
- package/build/dex/algebra-integral/algebra-integral.js +425 -0
- package/build/dex/algebra-integral/algebra-integral.js.map +1 -0
- package/build/dex/algebra-integral/config.d.ts +3 -0
- package/build/dex/algebra-integral/config.js +17 -0
- package/build/dex/algebra-integral/config.js.map +1 -0
- package/build/dex/algebra-integral/types.d.ts +35 -0
- package/build/dex/algebra-integral/types.js +10 -0
- package/build/dex/algebra-integral/types.js.map +1 -0
- package/build/dex/balancer-v1/balancer-v1.js +18 -1
- package/build/dex/balancer-v1/balancer-v1.js.map +1 -1
- package/build/dex/balancer-v3/reClammPool.d.ts +23 -0
- package/build/dex/balancer-v3/reClammPool.js +82 -0
- package/build/dex/balancer-v3/reClammPool.js.map +1 -0
- package/build/dex/ekubo/pools/base.d.ts +36 -0
- package/build/dex/ekubo/pools/base.js +289 -0
- package/build/dex/ekubo/pools/base.js.map +1 -0
- package/build/dex/ekubo/pools/full-range.d.ts +23 -0
- package/build/dex/ekubo/pools/full-range.js +99 -0
- package/build/dex/ekubo/pools/full-range.js.map +1 -0
- package/build/dex/ekubo/pools/iface.d.ts +46 -0
- package/build/dex/ekubo/pools/iface.js +75 -0
- package/build/dex/ekubo/pools/iface.js.map +1 -0
- package/build/dex/ekubo/pools/math/twamm/exp2.d.ts +1 -0
- package/build/dex/ekubo/pools/math/twamm/exp2.js +86 -0
- package/build/dex/ekubo/pools/math/twamm/exp2.js.map +1 -0
- package/build/dex/ekubo/pools/math/twamm/sqrt-ratio.d.ts +1 -0
- package/build/dex/ekubo/pools/math/twamm/sqrt-ratio.js +56 -0
- package/build/dex/ekubo/pools/math/twamm/sqrt-ratio.js.map +1 -0
- package/build/dex/ekubo/pools/oracle.d.ts +8 -0
- package/build/dex/ekubo/pools/oracle.js +19 -0
- package/build/dex/ekubo/pools/oracle.js.map +1 -0
- package/build/dex/ekubo/pools/twamm.d.ts +38 -0
- package/build/dex/ekubo/pools/twamm.js +253 -0
- package/build/dex/ekubo/pools/twamm.js.map +1 -0
- package/build/dex/ekubo/pools/utils.d.ts +28 -0
- package/build/dex/ekubo/pools/utils.js +80 -0
- package/build/dex/ekubo/pools/utils.js.map +1 -0
- package/build/dex/miro-migrator/constants.d.ts +1 -1
- package/build/dex/miro-migrator/constants.js +1 -1
- package/build/dex/miro-migrator/miro-migrator-pool.d.ts +5 -5
- package/build/dex/miro-migrator/miro-migrator-pool.js +13 -22
- package/build/dex/miro-migrator/miro-migrator-pool.js.map +1 -1
- package/build/dex/miro-migrator/miro-migrator-state.d.ts +27 -0
- package/build/dex/miro-migrator/miro-migrator-state.js +89 -0
- package/build/dex/miro-migrator/miro-migrator-state.js.map +1 -0
- package/build/dex/miro-migrator/miro-migrator.js +4 -2
- package/build/dex/miro-migrator/miro-migrator.js.map +1 -1
- package/build/dex/sky-converter/config.js +0 -1
- package/build/dex/sky-converter/config.js.map +1 -1
- package/build/dex/sky-converter/sky-converter.js +22 -11
- package/build/dex/sky-converter/sky-converter.js.map +1 -1
- package/build/dex/sky-converter/types.d.ts +1 -1
- package/build/dex/usdc-transmuter/config.d.ts +3 -0
- package/build/dex/usdc-transmuter/config.js +20 -0
- package/build/dex/usdc-transmuter/config.js.map +1 -0
- package/build/dex/usdc-transmuter/constants.d.ts +1 -0
- package/build/dex/usdc-transmuter/constants.js +5 -0
- package/build/dex/usdc-transmuter/constants.js.map +1 -0
- package/build/dex/usdc-transmuter/types.d.ts +20 -0
- package/build/dex/usdc-transmuter/types.js +9 -0
- package/build/dex/usdc-transmuter/types.js.map +1 -0
- package/build/dex/usdc-transmuter/usdc-transmuter-pool.d.ts +26 -0
- package/build/dex/usdc-transmuter/usdc-transmuter-pool.js +75 -0
- package/build/dex/usdc-transmuter/usdc-transmuter-pool.js.map +1 -0
- package/build/dex/usdc-transmuter/usdc-transmuter.d.ts +37 -0
- package/build/dex/usdc-transmuter/usdc-transmuter.js +148 -0
- package/build/dex/usdc-transmuter/usdc-transmuter.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DeepReadonly } from 'ts-essentials';
|
|
2
|
+
import { IDexHelper } from '../../../dex-helper/idex-helper';
|
|
3
|
+
import { Logger } from '../../../types';
|
|
4
|
+
import { BasicQuoteData, EkuboContracts } from '../types';
|
|
5
|
+
import { EkuboPool, PoolKeyed, Quote, QuoteFn } from './iface';
|
|
6
|
+
import { PoolKey, SwappedEvent } from './utils';
|
|
7
|
+
export declare class FullRangePool extends EkuboPool<FullRangePoolState.Object> {
|
|
8
|
+
private readonly dataFetcher;
|
|
9
|
+
constructor(parentName: string, dexHelper: IDexHelper, logger: Logger, contracts: EkuboContracts, key: PoolKey, quoteFn?: QuoteFn<FullRangePoolState.Object>);
|
|
10
|
+
generateState(blockNumber?: number | 'latest'): Promise<DeepReadonly<FullRangePoolState.Object>>;
|
|
11
|
+
}
|
|
12
|
+
export declare namespace FullRangePoolState {
|
|
13
|
+
type Object = {
|
|
14
|
+
sqrtRatio: bigint;
|
|
15
|
+
liquidity: bigint;
|
|
16
|
+
};
|
|
17
|
+
function fromQuoter(data: BasicQuoteData): DeepReadonly<Object>;
|
|
18
|
+
function fromPositionUpdatedEvent(oldState: DeepReadonly<Object>, liquidityDelta: bigint): Object | null;
|
|
19
|
+
function fromSwappedEvent(ev: SwappedEvent): Object;
|
|
20
|
+
}
|
|
21
|
+
export declare function quote(this: PoolKeyed, amount: bigint, isToken1: boolean, state: DeepReadonly<FullRangePoolState.Object>, sqrtRatioLimit?: bigint): Quote & {
|
|
22
|
+
stateAfter: typeof state;
|
|
23
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FullRangePoolState = exports.FullRangePool = void 0;
|
|
7
|
+
exports.quote = quote;
|
|
8
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
9
|
+
const iface_1 = require("./iface");
|
|
10
|
+
const price_1 = require("./math/price");
|
|
11
|
+
const swap_1 = require("./math/swap");
|
|
12
|
+
const tick_1 = require("./math/tick");
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
const GAS_COST_OF_ONE_FULL_RANGE_SWAP = 20_000;
|
|
15
|
+
class FullRangePool extends iface_1.EkuboPool {
|
|
16
|
+
dataFetcher;
|
|
17
|
+
constructor(parentName, dexHelper, logger, contracts, key, quoteFn) {
|
|
18
|
+
const { contract: { address }, interface: iface, dataFetcher, } = contracts.core;
|
|
19
|
+
super(parentName, dexHelper, logger, key, {
|
|
20
|
+
[address]: new iface_1.NamedEventHandlers(iface, {
|
|
21
|
+
PositionUpdated: (args, oldState) => {
|
|
22
|
+
if (key.num_id !== BigInt(args.poolId)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return FullRangePoolState.fromPositionUpdatedEvent(oldState, args.params.liquidityDelta.toBigInt());
|
|
26
|
+
},
|
|
27
|
+
}),
|
|
28
|
+
}, {
|
|
29
|
+
[address]: data => {
|
|
30
|
+
const ev = (0, utils_1.parseSwappedEvent)(data);
|
|
31
|
+
if (key.num_id !== ev.poolId) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return FullRangePoolState.fromSwappedEvent(ev);
|
|
35
|
+
},
|
|
36
|
+
}, quoteFn ?? quote);
|
|
37
|
+
this.dataFetcher = dataFetcher;
|
|
38
|
+
}
|
|
39
|
+
async generateState(blockNumber) {
|
|
40
|
+
const data = await this.dataFetcher.getQuoteData([this.key.toAbi()], 0, {
|
|
41
|
+
blockTag: blockNumber,
|
|
42
|
+
});
|
|
43
|
+
return FullRangePoolState.fromQuoter(data[0]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.FullRangePool = FullRangePool;
|
|
47
|
+
var FullRangePoolState;
|
|
48
|
+
(function (FullRangePoolState) {
|
|
49
|
+
function fromQuoter(data) {
|
|
50
|
+
const liquidity = data.liquidity.toBigInt();
|
|
51
|
+
const sqrtRatioFloat = data.sqrtRatio.toBigInt();
|
|
52
|
+
return {
|
|
53
|
+
sqrtRatio: (0, price_1.floatSqrtRatioToFixed)(sqrtRatioFloat),
|
|
54
|
+
liquidity,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
FullRangePoolState.fromQuoter = fromQuoter;
|
|
58
|
+
function fromPositionUpdatedEvent(oldState, liquidityDelta) {
|
|
59
|
+
if (liquidityDelta === 0n) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const clonedState = lodash_1.default.cloneDeep(oldState);
|
|
63
|
+
clonedState.liquidity += liquidityDelta;
|
|
64
|
+
return clonedState;
|
|
65
|
+
}
|
|
66
|
+
FullRangePoolState.fromPositionUpdatedEvent = fromPositionUpdatedEvent;
|
|
67
|
+
function fromSwappedEvent(ev) {
|
|
68
|
+
return {
|
|
69
|
+
liquidity: ev.liquidityAfter,
|
|
70
|
+
sqrtRatio: ev.sqrtRatioAfter,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
FullRangePoolState.fromSwappedEvent = fromSwappedEvent;
|
|
74
|
+
})(FullRangePoolState || (exports.FullRangePoolState = FullRangePoolState = {}));
|
|
75
|
+
function quote(amount, isToken1, state, sqrtRatioLimit) {
|
|
76
|
+
const isIncreasing = (0, swap_1.isPriceIncreasing)(amount, isToken1);
|
|
77
|
+
let sqrtRatio = state.sqrtRatio;
|
|
78
|
+
const liquidity = state.liquidity;
|
|
79
|
+
sqrtRatioLimit ??= isIncreasing ? tick_1.MAX_SQRT_RATIO : tick_1.MIN_SQRT_RATIO;
|
|
80
|
+
const step = (0, swap_1.computeStep)({
|
|
81
|
+
fee: this.key.config.fee,
|
|
82
|
+
sqrtRatio,
|
|
83
|
+
liquidity,
|
|
84
|
+
isToken1,
|
|
85
|
+
sqrtRatioLimit,
|
|
86
|
+
amount,
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
consumedAmount: step.consumedAmount,
|
|
90
|
+
calculatedAmount: step.calculatedAmount,
|
|
91
|
+
gasConsumed: GAS_COST_OF_ONE_FULL_RANGE_SWAP,
|
|
92
|
+
skipAhead: 0,
|
|
93
|
+
stateAfter: {
|
|
94
|
+
sqrtRatio: step.sqrtRatioNext,
|
|
95
|
+
liquidity,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=full-range.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"full-range.js","sourceRoot":"","sources":["../../../../src/dex/ekubo/pools/full-range.ts"],"names":[],"mappings":";;;;;;AA0HA,sBAiCC;AA3JD,oDAAuB;AAKvB,mCAMiB;AACjB,wCAAqD;AACrD,sCAA6D;AAC7D,sCAA6D;AAC7D,mCAAmE;AAEnE,MAAM,+BAA+B,GAAG,MAAM,CAAC;AAE/C,MAAa,aAAc,SAAQ,iBAAoC;IACpD,WAAW,CAAC;IAE7B,YACE,UAAkB,EAClB,SAAqB,EACrB,MAAc,EACd,SAAyB,EACzB,GAAY,EACZ,OAA4C;QAE5C,MAAM,EACJ,QAAQ,EAAE,EAAE,OAAO,EAAE,EACrB,SAAS,EAAE,KAAK,EAChB,WAAW,GACZ,GAAG,SAAS,CAAC,IAAI,CAAC;QAEnB,KAAK,CACH,UAAU,EACV,SAAS,EACT,MAAM,EACN,GAAG,EACH;YACE,CAAC,OAAO,CAAC,EAAE,IAAI,0BAAkB,CAAC,KAAK,EAAE;gBACvC,eAAe,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE;oBAClC,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;wBACvC,OAAO,IAAI,CAAC;oBACd,CAAC;oBAED,OAAO,kBAAkB,CAAC,wBAAwB,CA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Interface, Result } from '@ethersproject/abi';
|
|
2
|
+
import { Logger } from 'log4js';
|
|
3
|
+
import { DeepReadonly } from 'ts-essentials';
|
|
4
|
+
import { IDexHelper } from '../../../dex-helper/idex-helper';
|
|
5
|
+
import { StatefulEventSubscriber } from '../../../stateful-event-subscriber';
|
|
6
|
+
import { BlockHeader, Log } from '../../../types';
|
|
7
|
+
import { PoolKey } from './utils';
|
|
8
|
+
export interface Quote {
|
|
9
|
+
consumedAmount: bigint;
|
|
10
|
+
calculatedAmount: bigint;
|
|
11
|
+
gasConsumed: number;
|
|
12
|
+
skipAhead: number;
|
|
13
|
+
}
|
|
14
|
+
export interface PoolKeyed {
|
|
15
|
+
key: PoolKey;
|
|
16
|
+
}
|
|
17
|
+
export interface IEkuboPool extends PoolKeyed {
|
|
18
|
+
quote(amount: bigint, token: bigint, blockNumber: number): Quote;
|
|
19
|
+
}
|
|
20
|
+
export type QuoteFn<State> = (amount: bigint, isToken1: boolean, state: DeepReadonly<State>) => Quote;
|
|
21
|
+
export type NamedEventHandler<State> = (args: Result, oldState: DeepReadonly<State>, blockHeader: Readonly<BlockHeader>) => DeepReadonly<State> | null;
|
|
22
|
+
export type AnonymousEventHandler<State> = (data: string, oldState: DeepReadonly<State>, blockHeader: Readonly<BlockHeader>) => DeepReadonly<State> | null;
|
|
23
|
+
export declare class NamedEventHandlers<State> {
|
|
24
|
+
private readonly iface;
|
|
25
|
+
private readonly handlers;
|
|
26
|
+
constructor(iface: Interface, handlers: Record<string, NamedEventHandler<State>>);
|
|
27
|
+
parseLog(log: Readonly<Log>, oldState: DeepReadonly<State>, blockHeader: Readonly<BlockHeader>): DeepReadonly<State> | null;
|
|
28
|
+
}
|
|
29
|
+
export declare abstract class EkuboPool<State> extends StatefulEventSubscriber<State> implements IEkuboPool {
|
|
30
|
+
readonly key: PoolKey;
|
|
31
|
+
private readonly namedEventHandlers;
|
|
32
|
+
private readonly anonymousEventHandlers;
|
|
33
|
+
private readonly quoteFn;
|
|
34
|
+
protected constructor(parentName: string, dexHelper: IDexHelper, logger: Logger, key: PoolKey, namedEventHandlers: Record<string, NamedEventHandlers<State>>, anonymousEventHandlers: Record<string, AnonymousEventHandler<State>>, quoteFn: QuoteFn<State>);
|
|
35
|
+
/**
|
|
36
|
+
* The function is called every time any of the subscribed
|
|
37
|
+
* addresses release log. The function accepts the current
|
|
38
|
+
* state, updates the state according to the log, and returns
|
|
39
|
+
* the updated state.
|
|
40
|
+
* @param state - Current state of event subscriber
|
|
41
|
+
* @param log - Log released by one of the subscribed addresses
|
|
42
|
+
* @returns Updates state of the event subscriber after the log
|
|
43
|
+
*/
|
|
44
|
+
protected processLog(state: DeepReadonly<State>, log: Readonly<Log>, blockHeader: Readonly<BlockHeader>): DeepReadonly<State> | null;
|
|
45
|
+
quote(amount: bigint, token: bigint, blockNumber: number): Quote;
|
|
46
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EkuboPool = exports.NamedEventHandlers = void 0;
|
|
4
|
+
const stateful_event_subscriber_1 = require("../../../stateful-event-subscriber");
|
|
5
|
+
class NamedEventHandlers {
|
|
6
|
+
iface;
|
|
7
|
+
handlers;
|
|
8
|
+
constructor(iface, handlers) {
|
|
9
|
+
this.iface = iface;
|
|
10
|
+
this.handlers = handlers;
|
|
11
|
+
}
|
|
12
|
+
parseLog(log, oldState, blockHeader) {
|
|
13
|
+
const event = this.iface.parseLog(log);
|
|
14
|
+
return this.handlers[event.name]?.(event.args, oldState, blockHeader);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.NamedEventHandlers = NamedEventHandlers;
|
|
18
|
+
const BASE_GAS_COST = 22_000;
|
|
19
|
+
class EkuboPool extends stateful_event_subscriber_1.StatefulEventSubscriber {
|
|
20
|
+
key;
|
|
21
|
+
namedEventHandlers;
|
|
22
|
+
anonymousEventHandlers;
|
|
23
|
+
quoteFn;
|
|
24
|
+
constructor(parentName, dexHelper, logger, key, namedEventHandlers, anonymousEventHandlers, quoteFn) {
|
|
25
|
+
super(parentName, key.string_id, dexHelper, logger);
|
|
26
|
+
this.key = key;
|
|
27
|
+
this.namedEventHandlers = namedEventHandlers;
|
|
28
|
+
this.anonymousEventHandlers = anonymousEventHandlers;
|
|
29
|
+
this.quoteFn = quoteFn;
|
|
30
|
+
this.addressesSubscribed = [
|
|
31
|
+
...new Set(Object.keys(namedEventHandlers).concat(Object.keys(anonymousEventHandlers))),
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The function is called every time any of the subscribed
|
|
36
|
+
* addresses release log. The function accepts the current
|
|
37
|
+
* state, updates the state according to the log, and returns
|
|
38
|
+
* the updated state.
|
|
39
|
+
* @param state - Current state of event subscriber
|
|
40
|
+
* @param log - Log released by one of the subscribed addresses
|
|
41
|
+
* @returns Updates state of the event subscriber after the log
|
|
42
|
+
*/
|
|
43
|
+
processLog(state, log, blockHeader) {
|
|
44
|
+
const emitter = log.address;
|
|
45
|
+
if (log.topics.length === 0) {
|
|
46
|
+
return this.anonymousEventHandlers[emitter]?.(log.data, state, blockHeader);
|
|
47
|
+
}
|
|
48
|
+
return this.namedEventHandlers[emitter]?.parseLog(log, state, blockHeader);
|
|
49
|
+
}
|
|
50
|
+
quote(amount, token, blockNumber) {
|
|
51
|
+
const isToken1 = token === this.key.token1;
|
|
52
|
+
if (!isToken1 && this.key.token0 !== token) {
|
|
53
|
+
throw new Error('Invalid token');
|
|
54
|
+
}
|
|
55
|
+
if (amount === 0n) {
|
|
56
|
+
return {
|
|
57
|
+
consumedAmount: 0n,
|
|
58
|
+
calculatedAmount: 0n,
|
|
59
|
+
gasConsumed: 0,
|
|
60
|
+
skipAhead: 0,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const state = this.getState(blockNumber);
|
|
64
|
+
if (state === null) {
|
|
65
|
+
throw new Error(`Quote for block number ${blockNumber} requested but state is not recent enough`);
|
|
66
|
+
}
|
|
67
|
+
const quote = this.quoteFn(amount, isToken1, state);
|
|
68
|
+
if (quote.calculatedAmount !== 0n) {
|
|
69
|
+
quote.gasConsumed += BASE_GAS_COST;
|
|
70
|
+
}
|
|
71
|
+
return quote;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.EkuboPool = EkuboPool;
|
|
75
|
+
//# sourceMappingURL=iface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iface.js","sourceRoot":"","sources":["../../../../src/dex/ekubo/pools/iface.ts"],"names":[],"mappings":";;;AAIA,kFAA6E;AAoC7E,MAAa,kBAAkB;IAEV;IACA;IAFnB,YACmB,KAAgB,EAChB,QAAkD;QADlD,UAAK,GAAL,KAAK,CAAW;QAChB,aAAQ,GAAR,QAAQ,CAA0C;IAClE,CAAC;IAEG,QAAQ,CACb,GAAkB,EAClB,QAA6B,EAC7B,WAAkC;QAElC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;IACxE,CAAC;CACF;AAdD,gDAcC;AAED,MAAM,aAAa,GAAG,MAAM,CAAC;AAE7B,MAAsB,SACpB,SAAQ,mDAA8B;IAOpB;IACC;IAIA;IAIA;IAbnB,YACE,UAAkB,EAClB,SAAqB,EACrB,MAAc,EACE,GAAY,EACX,kBAGhB,EACgB,sBAGhB,EACgB,OAAuB;QAExC,KAAK,CAAC,UAAU,EAAE,GAAG,CAAC,SAAS,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;QAXpC,QAAG,GAAH,GAAG,CAAS;QACX,uBAAkB,GAAlB,kBAAkB,CAGlC;QACgB,2BAAsB,GAAtB,sBAAsB,CAGtC;QACgB,YAAO,GAAP,OAAO,CAAg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AW,2CAA2C,CACjF,CAAC;QACJ,CAAC;QAED,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAEpD,IAAI,KAAK,CAAC,gBAAgB,KAAK,EAAE,EAAE,CAAC;YAClC,KAAK,CAAC,WAAW,IAAI,aAAa,CAAC;QACrC,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAxFD,8BAwFC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function exp2(x: bigint): bigint;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exp2 = exp2;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const MASKS_AND_FACTORS = [
|
|
6
|
+
[0x8000000000000000n, 481231938336009023090067544955250113854n],
|
|
7
|
+
[0x4000000000000000n, 404666211852346594250993303657235475948n],
|
|
8
|
+
[0x2000000000000000n, 371080552416919877990254144423618836767n],
|
|
9
|
+
[0x1000000000000000n, 355347954397881497469693820312941593443n],
|
|
10
|
+
[0x800000000000000n, 347733580493780928808815525413232318461n],
|
|
11
|
+
[0x400000000000000n, 343987798952690256687074238090730651112n],
|
|
12
|
+
[0x200000000000000n, 342130066523749645191881555545647086143n],
|
|
13
|
+
[0x100000000000000n, 341204966012395051463589306197117539651n],
|
|
14
|
+
[0x80000000000000n, 340743354212339922144397487283364652955n],
|
|
15
|
+
[0x40000000000000n, 340512782555889898808859563671008026639n],
|
|
16
|
+
[0x20000000000000n, 340397555242326998647385072673097901535n],
|
|
17
|
+
[0x10000000000000n, 340339956208435708755752659506489956133n],
|
|
18
|
+
[0x8000000000000n, 340311160346490911934870813363085081661n],
|
|
19
|
+
[0x4000000000000n, 340296763329178528376528243588334151603n],
|
|
20
|
+
[0x2000000000000n, 340289565048926066557319684044576333862n],
|
|
21
|
+
[0x1000000000000n, 340285965965899358974465315064323671036n],
|
|
22
|
+
[0x800000000000n, 340284166438660709872813645066166128555n],
|
|
23
|
+
[0x400000000000n, 340283266678610039476911010529773336521n],
|
|
24
|
+
[0x200000000000n, 340282816799476865065514053322893021549n],
|
|
25
|
+
[0x100000000000n, 340282591860133317712432962519222523747n],
|
|
26
|
+
[0x80000000000n, 340282479390517303956044167089727739432n],
|
|
27
|
+
[0x40000000000n, 340282423155723237052512385577070742059n],
|
|
28
|
+
[0x20000000000n, 340282395038329688593740233918090740389n],
|
|
29
|
+
[0x10000000000n, 340282380979633785612518603506803612670n],
|
|
30
|
+
[0x8000000000n, 340282373950286051933938400987007267567n],
|
|
31
|
+
[0x4000000000n, 340282370435612239547654640565033792378n],
|
|
32
|
+
[0x2000000000n, 340282368678275346967764181521839267590n],
|
|
33
|
+
[0x1000000000n, 340282367799606904081131786786979136761n],
|
|
34
|
+
[0x800000000n, 340282367360272683488643795553082001443n],
|
|
35
|
+
[0x400000000n, 340282367140605573405106851149122747984n],
|
|
36
|
+
[0x200000000n, 340282367030772018416515141710341210063n],
|
|
37
|
+
[0x100000000n, 340282366975855240935513477676743808340n],
|
|
38
|
+
[0x80000000n, 340282366948396852198336193330767679917n],
|
|
39
|
+
[0x40000000n, 340282366934667657830578438075407037644n],
|
|
40
|
+
[0x20000000n, 340282366927803060646907282177123794346n],
|
|
41
|
+
[0x10000000n, 340282366924370762055123634660330219950n],
|
|
42
|
+
[0x8000000n, 340282366922654612759244793510020291790n],
|
|
43
|
+
[0x4000000n, 340282366921796538111308618586887023373n],
|
|
44
|
+
[0x2000000n, 340282366921367500787341342538325810693n],
|
|
45
|
+
[0x1000000n, 340282366921152982125357907367296559436n],
|
|
46
|
+
[0x800000n, 340282366921045722794366240495094772541n],
|
|
47
|
+
[0x400000n, 340282366920992093128870419737322088773n],
|
|
48
|
+
[0x200000n, 340282366920965278296122512528017799308n],
|
|
49
|
+
[0x100000n, 340282366920951870879748559715761167680n],
|
|
50
|
+
[0x80000n, 340282366920945167171561583507731730142n],
|
|
51
|
+
[0x40000n, 340282366920941815317468095453241730942n],
|
|
52
|
+
[0x20000n, 340282366920940139390421351438377911234n],
|
|
53
|
+
[0x10000n, 340282366920939301426897979434041296353n],
|
|
54
|
+
[0x8000n, 340282366920938882445136293432646812656n],
|
|
55
|
+
[0x4000n, 340282366920938672954255450432143026744n],
|
|
56
|
+
[0x2000n, 340282366920938568208815028931939497772n],
|
|
57
|
+
[0x1000n, 340282366920938515836094818181849824282n],
|
|
58
|
+
[0x800n, 340282366920938489649734712806808010286n],
|
|
59
|
+
[0x400n, 340282366920938476556554660119287858975n],
|
|
60
|
+
[0x200n, 340282366920938470009964633775527972241n],
|
|
61
|
+
[0x100n, 340282366920938466736669620603648076105n],
|
|
62
|
+
[0x80n, 340282366920938465100022114017708139844n],
|
|
63
|
+
[0x40n, 340282366920938464281698360724738174666n],
|
|
64
|
+
[0x20n, 340282366920938463872536484078253192815n],
|
|
65
|
+
[0x10n, 340282366920938463667955545755010702074n],
|
|
66
|
+
[0x8n, 340282366920938463565665076593389456749n],
|
|
67
|
+
[0x4n, 340282366920938463514519842012578834098n],
|
|
68
|
+
[0x2n, 340282366920938463488947224722173522776n],
|
|
69
|
+
[0x1n, 340282366920938463476160916076970867115n],
|
|
70
|
+
];
|
|
71
|
+
function exp2(x) {
|
|
72
|
+
// x must be less than 0x400000000000000000 == 64 << 64 (overflow check)
|
|
73
|
+
if (x >= 0x400000000000000000n) {
|
|
74
|
+
throw new Error('Overflow');
|
|
75
|
+
}
|
|
76
|
+
// Start with 0x80000000000000000000000000000000 (i.e. 2^127)
|
|
77
|
+
let result = constants_1.TWO_POW_127;
|
|
78
|
+
for (const [mask, factor] of MASKS_AND_FACTORS) {
|
|
79
|
+
if ((x & mask) !== 0n) {
|
|
80
|
+
result = (result * factor) >> 128n;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Final adjustment: shift right by 63 - (x >> 64). (x >> 64) is the integer part.
|
|
84
|
+
return result >> (63n - (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,CAAC;IAClD,CAAC,KAAK,EAAE,wCAAwC,CAAC;IACjD,CAAC,KAAK,EAAE,wCAAwC,CAAC;IACjD,CAAC,KAAK,EAAE,wCAAwC,CAAC;IACjD,CAAC,KAAK,EAAE,wCAAwC,CAAC;IACjD,CAAC,IAAI,EAAE,wCAAwC,CAAC;IAChD,CAAC,IAAI,EAAE,wCAAwC,CAAC;IAChD,CAAC,IAAI,EAAE,wCAAwC,CAAC;IAChD,CAAC,IAAI,EAAE,wCAAwC,CAAC;CACjD,CAAC;AAEF,SAAgB,IAAI,CAAC,CAAS;IAC5B,wEAAwE;IACxE,IAAI,CAAC,IAAI,qBAAqB,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,UAAU,CAAC,CAAC;IAC9B,CAAC;IAED,6DAA6D;IAC7D,IAAI,MAAM,GAAG,uBAAW,CAAC;IAEzB,KAAK,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,iBAAiB,EAAE,CAAC;QAC/C,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;YACtB,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,IAAI,IAAI,CAAC;QACrC,CAAC;IACH,CAAC;IAED,kFAAkF;IAClF,OAAO,MAAM,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function calculateNextSqrtRatio(sqrtRatio: bigint, liquidity: bigint, saleRateToken0: bigint, saleRateToken1: bigint, timeElapsed: number, fee: bigint): bigint;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateNextSqrtRatio = calculateNextSqrtRatio;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const exp2_1 = require("./exp2");
|
|
6
|
+
const EXPONENT_LIMIT = 0x400000000000000000n;
|
|
7
|
+
function sqrt(x) {
|
|
8
|
+
if (x < 0n) {
|
|
9
|
+
throw new Error('Square root of negative numbers is not supported.');
|
|
10
|
+
}
|
|
11
|
+
if (x < 2n) {
|
|
12
|
+
return x;
|
|
13
|
+
}
|
|
14
|
+
let x0 = x / 2n;
|
|
15
|
+
let x1 = (x0 + x / x0) / 2n;
|
|
16
|
+
while (x0 > x1) {
|
|
17
|
+
x0 = x1;
|
|
18
|
+
x1 = (x0 + x / x0) / 2n;
|
|
19
|
+
}
|
|
20
|
+
return x0;
|
|
21
|
+
}
|
|
22
|
+
function computeSqrtSaleRatioX128(saleRateToken0, saleRateToken1) {
|
|
23
|
+
const saleRatio = (saleRateToken1 << 128n) / saleRateToken0;
|
|
24
|
+
if (saleRatio < constants_1.TWO_POW_128) {
|
|
25
|
+
// Full precision
|
|
26
|
+
return sqrt(saleRatio << 128n);
|
|
27
|
+
}
|
|
28
|
+
else if (saleRatio < constants_1.TWO_POW_192) {
|
|
29
|
+
// We know it only has 192 bits, so we can lsh it 64 before rooting to get more precision
|
|
30
|
+
return sqrt(saleRatio << 64n) << 32n;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return sqrt(saleRatio << 16n) << 56n;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function calculateNextSqrtRatio(sqrtRatio, liquidity, saleRateToken0, saleRateToken1, timeElapsed, fee) {
|
|
37
|
+
const sqrtSaleRatio = computeSqrtSaleRatioX128(saleRateToken0, saleRateToken1);
|
|
38
|
+
if (liquidity === 0n) {
|
|
39
|
+
return sqrtSaleRatio;
|
|
40
|
+
}
|
|
41
|
+
const saleRate = (sqrt(saleRateToken0 * saleRateToken1) * (constants_1.TWO_POW_64 - fee)) / constants_1.TWO_POW_64;
|
|
42
|
+
const exponent = (saleRate * BigInt(timeElapsed) * 12392656037n) / liquidity;
|
|
43
|
+
if (exponent >= EXPONENT_LIMIT) {
|
|
44
|
+
return sqrtSaleRatio;
|
|
45
|
+
}
|
|
46
|
+
const twoPowExponentX128 = (0, exp2_1.exp2)(exponent) << 64n;
|
|
47
|
+
const [num, sign] = sqrtRatio > sqrtSaleRatio
|
|
48
|
+
? [sqrtRatio - sqrtSaleRatio, true]
|
|
49
|
+
: [sqrtSaleRatio - sqrtRatio, false];
|
|
50
|
+
const c = (num << 128n) / (sqrtSaleRatio + sqrtRatio);
|
|
51
|
+
const [term1, term2] = [twoPowExponentX128 - c, twoPowExponentX128 + c];
|
|
52
|
+
return sign
|
|
53
|
+
? (sqrtSaleRatio * term2) / term1
|
|
54
|
+
: (sqrtSaleRatio * term1) / term2;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=sqrt-ratio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqrt-ratio.js","sourceRoot":"","sources":["../../../../../../src/dex/ekubo/pools/math/twamm/sqrt-ratio.ts"],"names":[],"mappings":";;AAyCA,wDAuCC;AAhFD,4CAAoE;AACpE,iCAA8B;AAE9B,MAAM,cAAc,GAAW,qBAAqB,CAAC;AAErD,SAAS,IAAI,CAAC,CAAS;IACrB,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IACvE,CAAC;IACD,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;QACX,OAAO,CAAC,CAAC;IACX,CAAC;IAED,IAAI,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC;IAChB,IAAI,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;IAE5B,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC;QACf,EAAE,GAAG,EAAE,CAAC;QACR,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;IAC1B,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,SAAS,wBAAwB,C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AC,CAAC,CAAC,aAAa,GAAG,SAAS,EAAE,KAAK,CAAC,CAAC;IAEzC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,aAAa,GAAG,SAAS,CAAC,CAAC;IAEtD,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,kBAAkB,GAAG,CAAC,EAAE,kBAAkB,GAAG,CAAC,CAAC,CAAC;IAExE,OAAO,IAAI;QACT,CAAC,CAAC,CAAC,aAAa,GAAG,KAAK,CAAC,GAAG,KAAK;QACjC,CAAC,CAAC,CAAC,aAAa,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Logger } from 'log4js';
|
|
2
|
+
import { IDexHelper } from '../../../dex-helper/idex-helper';
|
|
3
|
+
import { EkuboContracts } from '../types';
|
|
4
|
+
import { FullRangePool } from './full-range';
|
|
5
|
+
import { PoolKey } from './utils';
|
|
6
|
+
export declare class OraclePool extends FullRangePool {
|
|
7
|
+
constructor(parentName: string, dexHelper: IDexHelper, logger: Logger, contracts: EkuboContracts, key: PoolKey);
|
|
8
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OraclePool = void 0;
|
|
4
|
+
const full_range_1 = require("./full-range");
|
|
5
|
+
const GAS_COST_OF_UPDATING_ORACLE_SNAPSHOT = 10_000;
|
|
6
|
+
class OraclePool extends full_range_1.FullRangePool {
|
|
7
|
+
constructor(parentName, dexHelper, logger, contracts, key) {
|
|
8
|
+
super(parentName, dexHelper, logger, contracts, key, quote);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.OraclePool = OraclePool;
|
|
12
|
+
function quote(amount, isToken1, state) {
|
|
13
|
+
const fullRangeQuote = full_range_1.quote.bind(this)(amount, isToken1, state);
|
|
14
|
+
if (fullRangeQuote.calculatedAmount !== 0n) {
|
|
15
|
+
fullRangeQuote.gasConsumed += GAS_COST_OF_UPDATING_ORACLE_SNAPSHOT;
|
|
16
|
+
}
|
|
17
|
+
return fullRangeQuote;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=oracle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oracle.js","sourceRoot":"","sources":["../../../../src/dex/ekubo/pools/oracle.ts"],"names":[],"mappings":";;;AAIA,6CAIsB;AAItB,MAAM,oCAAoC,GAAG,MAAM,CAAC;AAEpD,MAAa,UAAW,SAAQ,0BAAa;IAC3C,YACE,UAAkB,EAClB,SAAqB,EACrB,MAAc,EACd,SAAyB,EACzB,GAAY;QAEZ,KAAK,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;CACF;AAVD,gCAUC;AAED,SAAS,KAAK,CAEZ,MAAc,EACd,QAAiB,EACjB,KAA8C;IAE9C,MAAM,cAAc,GAAG,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;IAE9E,IAAI,cAAc,CAAC,gBAAgB,KAAK,EAAE,EAAE,CAAC;QAC3C,cAAc,CAAC,WAAW,IAAI,oCAAoC,CAAC;IACrE,CAAC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Logger } from 'log4js';
|
|
2
|
+
import { DeepReadonly } from 'ts-essentials';
|
|
3
|
+
import { IDexHelper } from '../../../dex-helper/idex-helper';
|
|
4
|
+
import { EkuboContracts, TwammQuoteData } from '../types';
|
|
5
|
+
import { FullRangePoolState } from './full-range';
|
|
6
|
+
import { EkuboPool, PoolKeyed, Quote } from './iface';
|
|
7
|
+
import { PoolKey, SwappedEvent } from './utils';
|
|
8
|
+
export declare class TwammPool extends EkuboPool<TwammPoolState.Object> {
|
|
9
|
+
private readonly dataFetcher;
|
|
10
|
+
constructor(parentName: string, dexHelper: IDexHelper, logger: Logger, contracts: EkuboContracts, key: PoolKey);
|
|
11
|
+
generateState(blockNumber?: number | 'latest'): Promise<DeepReadonly<TwammPoolState.Object>>;
|
|
12
|
+
}
|
|
13
|
+
interface VirtualOrdersExecutedEvent {
|
|
14
|
+
poolId: bigint;
|
|
15
|
+
token0SaleRate: bigint;
|
|
16
|
+
token1SaleRate: bigint;
|
|
17
|
+
}
|
|
18
|
+
export declare namespace TwammPoolState {
|
|
19
|
+
interface SaleRateDelta {
|
|
20
|
+
time: number;
|
|
21
|
+
saleRateDelta0: bigint;
|
|
22
|
+
saleRateDelta1: bigint;
|
|
23
|
+
}
|
|
24
|
+
interface Object {
|
|
25
|
+
fullRangePoolState: FullRangePoolState.Object;
|
|
26
|
+
token0SaleRate: bigint;
|
|
27
|
+
token1SaleRate: bigint;
|
|
28
|
+
lastExecutionTime: number;
|
|
29
|
+
virtualOrderDeltas: SaleRateDelta[];
|
|
30
|
+
}
|
|
31
|
+
function fromQuoter(data: TwammQuoteData): DeepReadonly<Object>;
|
|
32
|
+
function fromPositionUpdatedEvent(oldState: DeepReadonly<Object>, liquidityDelta: bigint): Object | null;
|
|
33
|
+
function fromSwappedEvent(oldState: DeepReadonly<Object>, ev: SwappedEvent): Object;
|
|
34
|
+
function fromVirtualOrdersExecutedEvent(oldState: DeepReadonly<Object>, ev: VirtualOrdersExecutedEvent, timestamp: number): Object;
|
|
35
|
+
function fromOrderUpdatedEvent(oldState: DeepReadonly<Object>, [startTime, endTime]: [number, number], orderSaleRateDelta: bigint, isToken1: boolean): Object;
|
|
36
|
+
}
|
|
37
|
+
export declare function quote(this: PoolKeyed, amount: bigint, isToken1: boolean, state: DeepReadonly<TwammPoolState.Object>, overrideTime?: number): Quote;
|
|
38
|
+
export {};
|