@byreal-io/byreal-clmm-sdk 0.2.0
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/LICENSE +21 -0
- package/README.md +341 -0
- package/dist/esm/client/chain.d.ts +227 -0
- package/dist/esm/client/chain.d.ts.map +1 -0
- package/dist/esm/client/chain.js +1123 -0
- package/dist/esm/client/chain.js.map +1 -0
- package/dist/esm/client/index.d.ts +4 -0
- package/dist/esm/client/index.d.ts.map +1 -0
- package/dist/esm/client/index.js +4 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/client/models.d.ts +160 -0
- package/dist/esm/client/models.d.ts.map +1 -0
- package/dist/esm/client/models.js +2 -0
- package/dist/esm/client/models.js.map +1 -0
- package/dist/esm/client/utils.d.ts +103 -0
- package/dist/esm/client/utils.d.ts.map +1 -0
- package/dist/esm/client/utils.js +238 -0
- package/dist/esm/client/utils.js.map +1 -0
- package/dist/esm/constants.d.ts +19 -0
- package/dist/esm/constants.d.ts.map +1 -0
- package/dist/esm/constants.js +27 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/instructions/baseInstruction.d.ts +74 -0
- package/dist/esm/instructions/baseInstruction.d.ts.map +1 -0
- package/dist/esm/instructions/baseInstruction.js +577 -0
- package/dist/esm/instructions/baseInstruction.js.map +1 -0
- package/dist/esm/instructions/constants.d.ts +29 -0
- package/dist/esm/instructions/constants.d.ts.map +1 -0
- package/dist/esm/instructions/constants.js +32 -0
- package/dist/esm/instructions/constants.js.map +1 -0
- package/dist/esm/instructions/getRawData.d.ts +60 -0
- package/dist/esm/instructions/getRawData.d.ts.map +1 -0
- package/dist/esm/instructions/getRawData.js +105 -0
- package/dist/esm/instructions/getRawData.js.map +1 -0
- package/dist/esm/instructions/index.d.ts +9 -0
- package/dist/esm/instructions/index.d.ts.map +1 -0
- package/dist/esm/instructions/index.js +9 -0
- package/dist/esm/instructions/index.js.map +1 -0
- package/dist/esm/instructions/instruction.d.ts +137 -0
- package/dist/esm/instructions/instruction.d.ts.map +1 -0
- package/dist/esm/instructions/instruction.js +152 -0
- package/dist/esm/instructions/instruction.js.map +1 -0
- package/dist/esm/instructions/layout.d.ts +217 -0
- package/dist/esm/instructions/layout.d.ts.map +1 -0
- package/dist/esm/instructions/layout.js +203 -0
- package/dist/esm/instructions/layout.js.map +1 -0
- package/dist/esm/instructions/libs/marshmallow/bufferLayout.d.ts +107 -0
- package/dist/esm/instructions/libs/marshmallow/bufferLayout.d.ts.map +1 -0
- package/dist/esm/instructions/libs/marshmallow/bufferLayout.js +49 -0
- package/dist/esm/instructions/libs/marshmallow/bufferLayout.js.map +1 -0
- package/dist/esm/instructions/libs/marshmallow/index.d.ts +88 -0
- package/dist/esm/instructions/libs/marshmallow/index.d.ts.map +1 -0
- package/dist/esm/instructions/libs/marshmallow/index.js +256 -0
- package/dist/esm/instructions/libs/marshmallow/index.js.map +1 -0
- package/dist/esm/instructions/models.d.ts +39 -0
- package/dist/esm/instructions/models.d.ts.map +1 -0
- package/dist/esm/instructions/models.js +14 -0
- package/dist/esm/instructions/models.js.map +1 -0
- package/dist/esm/instructions/pda.d.ts +52 -0
- package/dist/esm/instructions/pda.d.ts.map +1 -0
- package/dist/esm/instructions/pda.js +47 -0
- package/dist/esm/instructions/pda.js.map +1 -0
- package/dist/esm/instructions/target/idl/byreal_amm_v3.json +7242 -0
- package/dist/esm/instructions/target/idl/byreal_amm_v3_test.json +7680 -0
- package/dist/esm/instructions/target/types/byreal_amm_v3.d.ts +5329 -0
- package/dist/esm/instructions/target/types/byreal_amm_v3.d.ts.map +1 -0
- package/dist/esm/instructions/target/types/byreal_amm_v3.js +2 -0
- package/dist/esm/instructions/target/types/byreal_amm_v3.js.map +1 -0
- package/dist/esm/instructions/target/types/raydium_amm_v3.d.ts +4149 -0
- package/dist/esm/instructions/target/types/raydium_amm_v3.d.ts.map +1 -0
- package/dist/esm/instructions/target/types/raydium_amm_v3.js +2 -0
- package/dist/esm/instructions/target/types/raydium_amm_v3.js.map +1 -0
- package/dist/esm/instructions/utils/binaryUtils.d.ts +11 -0
- package/dist/esm/instructions/utils/binaryUtils.d.ts.map +1 -0
- package/dist/esm/instructions/utils/binaryUtils.js +77 -0
- package/dist/esm/instructions/utils/binaryUtils.js.map +1 -0
- package/dist/esm/instructions/utils/fetchWalletTokenAccounts.d.ts +42 -0
- package/dist/esm/instructions/utils/fetchWalletTokenAccounts.d.ts.map +1 -0
- package/dist/esm/instructions/utils/fetchWalletTokenAccounts.js +63 -0
- package/dist/esm/instructions/utils/fetchWalletTokenAccounts.js.map +1 -0
- package/dist/esm/instructions/utils/getTickArrayBitmapExtension.d.ts +8 -0
- package/dist/esm/instructions/utils/getTickArrayBitmapExtension.d.ts.map +1 -0
- package/dist/esm/instructions/utils/getTickArrayBitmapExtension.js +22 -0
- package/dist/esm/instructions/utils/getTickArrayBitmapExtension.js.map +1 -0
- package/dist/esm/instructions/utils/getTickArrayInfo.d.ts +16 -0
- package/dist/esm/instructions/utils/getTickArrayInfo.d.ts.map +1 -0
- package/dist/esm/instructions/utils/getTickArrayInfo.js +75 -0
- package/dist/esm/instructions/utils/getTickArrayInfo.js.map +1 -0
- package/dist/esm/instructions/utils/index.d.ts +21 -0
- package/dist/esm/instructions/utils/index.d.ts.map +1 -0
- package/dist/esm/instructions/utils/index.js +21 -0
- package/dist/esm/instructions/utils/index.js.map +1 -0
- package/dist/esm/instructions/utils/liquidityMath.d.ts +30 -0
- package/dist/esm/instructions/utils/liquidityMath.d.ts.map +1 -0
- package/dist/esm/instructions/utils/liquidityMath.js +138 -0
- package/dist/esm/instructions/utils/liquidityMath.js.map +1 -0
- package/dist/esm/instructions/utils/mathUtils.d.ts +11 -0
- package/dist/esm/instructions/utils/mathUtils.d.ts.map +1 -0
- package/dist/esm/instructions/utils/mathUtils.js +36 -0
- package/dist/esm/instructions/utils/mathUtils.js.map +1 -0
- package/dist/esm/instructions/utils/models.d.ts +93 -0
- package/dist/esm/instructions/utils/models.d.ts.map +1 -0
- package/dist/esm/instructions/utils/models.js +13 -0
- package/dist/esm/instructions/utils/models.js.map +1 -0
- package/dist/esm/instructions/utils/poolStateUtils.d.ts +134 -0
- package/dist/esm/instructions/utils/poolStateUtils.d.ts.map +1 -0
- package/dist/esm/instructions/utils/poolStateUtils.js +137 -0
- package/dist/esm/instructions/utils/poolStateUtils.js.map +1 -0
- package/dist/esm/instructions/utils/poolUtils.d.ts +136 -0
- package/dist/esm/instructions/utils/poolUtils.d.ts.map +1 -0
- package/dist/esm/instructions/utils/poolUtils.js +219 -0
- package/dist/esm/instructions/utils/poolUtils.js.map +1 -0
- package/dist/esm/instructions/utils/position.d.ts +36 -0
- package/dist/esm/instructions/utils/position.d.ts.map +1 -0
- package/dist/esm/instructions/utils/position.js +86 -0
- package/dist/esm/instructions/utils/position.js.map +1 -0
- package/dist/esm/instructions/utils/sqrtPriceMath.d.ts +14 -0
- package/dist/esm/instructions/utils/sqrtPriceMath.d.ts.map +1 -0
- package/dist/esm/instructions/utils/sqrtPriceMath.js +168 -0
- package/dist/esm/instructions/utils/sqrtPriceMath.js.map +1 -0
- package/dist/esm/instructions/utils/swapMath.d.ts +48 -0
- package/dist/esm/instructions/utils/swapMath.d.ts.map +1 -0
- package/dist/esm/instructions/utils/swapMath.js +263 -0
- package/dist/esm/instructions/utils/swapMath.js.map +1 -0
- package/dist/esm/instructions/utils/tick.d.ts +106 -0
- package/dist/esm/instructions/utils/tick.d.ts.map +1 -0
- package/dist/esm/instructions/utils/tick.js +390 -0
- package/dist/esm/instructions/utils/tick.js.map +1 -0
- package/dist/esm/instructions/utils/tickArrayUtils.d.ts +50 -0
- package/dist/esm/instructions/utils/tickArrayUtils.d.ts.map +1 -0
- package/dist/esm/instructions/utils/tickArrayUtils.js +157 -0
- package/dist/esm/instructions/utils/tickArrayUtils.js.map +1 -0
- package/dist/esm/instructions/utils/tickMath.d.ts +18 -0
- package/dist/esm/instructions/utils/tickMath.d.ts.map +1 -0
- package/dist/esm/instructions/utils/tickMath.js +34 -0
- package/dist/esm/instructions/utils/tickMath.js.map +1 -0
- package/dist/esm/instructions/utils/tickarrayBitmap.d.ts +38 -0
- package/dist/esm/instructions/utils/tickarrayBitmap.d.ts.map +1 -0
- package/dist/esm/instructions/utils/tickarrayBitmap.js +157 -0
- package/dist/esm/instructions/utils/tickarrayBitmap.js.map +1 -0
- package/dist/esm/instructions/utils/transfer.d.ts +7 -0
- package/dist/esm/instructions/utils/transfer.d.ts.map +1 -0
- package/dist/esm/instructions/utils/transfer.js +78 -0
- package/dist/esm/instructions/utils/transfer.js.map +1 -0
- package/dist/esm/utils/accountInfo.d.ts +27 -0
- package/dist/esm/utils/accountInfo.d.ts.map +1 -0
- package/dist/esm/utils/accountInfo.js +80 -0
- package/dist/esm/utils/accountInfo.js.map +1 -0
- package/dist/esm/utils/checkV0TxSize.d.ts +8 -0
- package/dist/esm/utils/checkV0TxSize.d.ts.map +1 -0
- package/dist/esm/utils/checkV0TxSize.js +18 -0
- package/dist/esm/utils/checkV0TxSize.js.map +1 -0
- package/dist/esm/utils/estimateComputeUnits.d.ts +11 -0
- package/dist/esm/utils/estimateComputeUnits.d.ts.map +1 -0
- package/dist/esm/utils/estimateComputeUnits.js +48 -0
- package/dist/esm/utils/estimateComputeUnits.js.map +1 -0
- package/dist/esm/utils/generatePubKey.d.ts +10 -0
- package/dist/esm/utils/generatePubKey.d.ts.map +1 -0
- package/dist/esm/utils/generatePubKey.js +14 -0
- package/dist/esm/utils/generatePubKey.js.map +1 -0
- package/dist/esm/utils/index.d.ts +9 -0
- package/dist/esm/utils/index.d.ts.map +1 -0
- package/dist/esm/utils/index.js +11 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/token.d.ts +9 -0
- package/dist/esm/utils/token.d.ts.map +1 -0
- package/dist/esm/utils/token.js +16 -0
- package/dist/esm/utils/token.js.map +1 -0
- package/dist/esm/utils/transactionUtils.d.ts +84 -0
- package/dist/esm/utils/transactionUtils.d.ts.map +1 -0
- package/dist/esm/utils/transactionUtils.js +138 -0
- package/dist/esm/utils/transactionUtils.js.map +1 -0
- package/dist/esm/utils/validateAndParsePublicKey.d.ts +15 -0
- package/dist/esm/utils/validateAndParsePublicKey.d.ts.map +1 -0
- package/dist/esm/utils/validateAndParsePublicKey.js +42 -0
- package/dist/esm/utils/validateAndParsePublicKey.js.map +1 -0
- package/package.json +66 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
|
+
import BN from 'bn.js';
|
|
3
|
+
import { IAmmConfigLayout } from '../layout.js';
|
|
4
|
+
import { IPoolLayoutWithId } from '../models.js';
|
|
5
|
+
import { TickArrayContainer, TickArrayBitmapExtensionType } from './models.js';
|
|
6
|
+
export declare class PoolUtils {
|
|
7
|
+
static isOverflowDefaultTickarrayBitmap(tickSpacing: number, tickarrayStartIndexs: number[]): boolean;
|
|
8
|
+
static _tickRange(tickSpacing: number): {
|
|
9
|
+
maxTickBoundary: number;
|
|
10
|
+
minTickBoundary: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Calculate the maximum and minimum ticks selectable by users in the UI
|
|
14
|
+
* Unlike _tickRange, this method directly returns the available tick value range, not the tickarray boundaries
|
|
15
|
+
*
|
|
16
|
+
* @param tickSpacing tick spacing
|
|
17
|
+
* @returns Maximum and minimum tick values selectable by users
|
|
18
|
+
*/
|
|
19
|
+
static tickRange(tickSpacing: number): {
|
|
20
|
+
maxTickBoundary: number;
|
|
21
|
+
minTickBoundary: number;
|
|
22
|
+
};
|
|
23
|
+
static nextInitializedTickArrayStartIndex(poolInfo: {
|
|
24
|
+
tickCurrent: number;
|
|
25
|
+
tickSpacing: number;
|
|
26
|
+
tickArrayBitmap: BN[];
|
|
27
|
+
exBitmapInfo: TickArrayBitmapExtensionType;
|
|
28
|
+
}, lastTickArrayStartIndex: number, zeroForOne: boolean): {
|
|
29
|
+
isExist: boolean;
|
|
30
|
+
nextStartIndex: number;
|
|
31
|
+
};
|
|
32
|
+
static getFirstInitializedTickArray(poolInfo: {
|
|
33
|
+
programId: PublicKey;
|
|
34
|
+
poolId: PublicKey;
|
|
35
|
+
tickCurrent: number;
|
|
36
|
+
tickSpacing: number;
|
|
37
|
+
tickArrayBitmap: BN[];
|
|
38
|
+
exBitmapInfo: TickArrayBitmapExtensionType;
|
|
39
|
+
}, zeroForOne: boolean): {
|
|
40
|
+
isExist: true;
|
|
41
|
+
startIndex: number;
|
|
42
|
+
nextAccountMeta: PublicKey;
|
|
43
|
+
} | {
|
|
44
|
+
isExist: false;
|
|
45
|
+
startIndex: undefined;
|
|
46
|
+
nextAccountMeta: undefined;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Calculate the output amount and required account list for token swap
|
|
50
|
+
*
|
|
51
|
+
* This function is the core of CLMM (Concentrated Liquidity Market Maker) swap logic, used to calculate the expected output amount for a given input amount,
|
|
52
|
+
* and determine all tick array accounts that need to be accessed during trade execution.
|
|
53
|
+
*
|
|
54
|
+
* @param params Swap calculation parameter object
|
|
55
|
+
* @param params.poolInfo - Complete information of the liquidity pool, including current price, liquidity, tick and other states
|
|
56
|
+
* @param params.exBitmapInfo - Tick array bitmap extension information, used to handle ticks beyond the default range
|
|
57
|
+
* @param params.ammConfig - AMM configuration information, including transaction fee rates and other parameters
|
|
58
|
+
* @param params.tickArrayInfo - Loaded tick array information cache, with keys as tick array start indices
|
|
59
|
+
* @param params.inputTokenMint - Input token mint address, used to determine trade direction
|
|
60
|
+
* @param params.inputAmount - Input token amount (using minimum units)
|
|
61
|
+
* @param params.sqrtPriceLimitX64 - Optional price limit, representing the worst price for the trade (Q64.64 format)
|
|
62
|
+
* @param params.catchLiquidityInsufficient - Whether to catch liquidity insufficient situations, default false
|
|
63
|
+
*
|
|
64
|
+
* @returns Swap calculation result object
|
|
65
|
+
* @returns allTrade - Boolean value indicating whether the specified input amount can be fully traded
|
|
66
|
+
* true: All amount can be traded
|
|
67
|
+
* false: Only partial trading due to insufficient liquidity or price limit
|
|
68
|
+
* @returns expectedAmountOut - Expected output token amount to be obtained (using minimum units)
|
|
69
|
+
* @returns remainingAccounts - List of all tick array account addresses that need to be accessed during trading
|
|
70
|
+
* These accounts need to be passed as remaining accounts in the trade instruction
|
|
71
|
+
* @returns executionPrice - Final price after trade execution (square root price in Q64.64 format)
|
|
72
|
+
* @returns feeAmount - Total fees generated by the trade (using input token minimum units)
|
|
73
|
+
*
|
|
74
|
+
* @throws Error Throws 'Invalid tick array' error when no valid tick array is found
|
|
75
|
+
*/
|
|
76
|
+
static getOutputAmountAndRemainAccounts(params: {
|
|
77
|
+
poolInfo: IPoolLayoutWithId;
|
|
78
|
+
exBitmapInfo: TickArrayBitmapExtensionType;
|
|
79
|
+
ammConfig: IAmmConfigLayout;
|
|
80
|
+
tickArrayInfo: {
|
|
81
|
+
[key: string]: TickArrayContainer;
|
|
82
|
+
};
|
|
83
|
+
inputTokenMint: PublicKey;
|
|
84
|
+
inputAmount: BN;
|
|
85
|
+
sqrtPriceLimitX64?: BN;
|
|
86
|
+
catchLiquidityInsufficient?: boolean;
|
|
87
|
+
}): {
|
|
88
|
+
allTrade: boolean;
|
|
89
|
+
expectedAmountOut: BN;
|
|
90
|
+
remainingAccounts: PublicKey[];
|
|
91
|
+
executionPrice: BN;
|
|
92
|
+
feeAmount: BN;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Calculate the required input token amount and account list for exact output
|
|
96
|
+
*
|
|
97
|
+
* This function is used for "exact output" scenarios, where the user specifies the desired output token amount,
|
|
98
|
+
* and the function calculates how many tokens need to be input to obtain the specified output amount.
|
|
99
|
+
*
|
|
100
|
+
* @param params Swap calculation parameter object
|
|
101
|
+
* @param params.poolInfo - Complete information of the liquidity pool
|
|
102
|
+
* @param params.exBitmapInfo - Tick array bitmap extension information
|
|
103
|
+
* @param params.ammConfig - AMM configuration information
|
|
104
|
+
* @param params.tickArrayInfo - Loaded tick array information cache
|
|
105
|
+
* @param params.outputTokenMint - Output token mint address
|
|
106
|
+
* @param params.outputAmount - Expected output token amount (using minimum units)
|
|
107
|
+
* @param params.sqrtPriceLimitX64 - Optional price limit (Q64.64 format)
|
|
108
|
+
* @param params.catchLiquidityInsufficient - Whether to catch liquidity insufficient situations
|
|
109
|
+
*
|
|
110
|
+
* @returns Swap calculation result object
|
|
111
|
+
* @returns allTrade - Whether the specified output amount can be fully obtained
|
|
112
|
+
* @returns expectedAmountIn - Required input token amount (including fees)
|
|
113
|
+
* @returns remainingAccounts - List of tick array accounts that need to be accessed during trading
|
|
114
|
+
* @returns executionPrice - Final price after trade execution
|
|
115
|
+
* @returns feeAmount - Total fees generated by the trade
|
|
116
|
+
*/
|
|
117
|
+
static getInputAmountAndRemainAccounts(params: {
|
|
118
|
+
poolInfo: IPoolLayoutWithId;
|
|
119
|
+
exBitmapInfo: TickArrayBitmapExtensionType;
|
|
120
|
+
ammConfig: IAmmConfigLayout;
|
|
121
|
+
tickArrayInfo: {
|
|
122
|
+
[key: string]: TickArrayContainer;
|
|
123
|
+
};
|
|
124
|
+
outputTokenMint: PublicKey;
|
|
125
|
+
outputAmount: BN;
|
|
126
|
+
sqrtPriceLimitX64?: BN;
|
|
127
|
+
catchLiquidityInsufficient?: boolean;
|
|
128
|
+
}): {
|
|
129
|
+
allTrade: boolean;
|
|
130
|
+
expectedAmountIn: BN;
|
|
131
|
+
remainingAccounts: PublicKey[];
|
|
132
|
+
executionPrice: BN;
|
|
133
|
+
feeAmount: BN;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=poolUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"poolUtils.d.ts","sourceRoot":"","sources":["../../../../src/instructions/utils/poolUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,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KACf;CAoFF"}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { MAX_TICK, MIN_TICK, NEGATIVE_ONE } from '../constants.js';
|
|
2
|
+
import { getPdaTickArrayAddress } from '../pda.js';
|
|
3
|
+
import { SwapMath } from './swapMath.js';
|
|
4
|
+
import { TickQuery, TickUtils } from './tick.js';
|
|
5
|
+
import { TickArrayBitmap, TickArrayBitmapExtensionUtils } from './tickarrayBitmap.js';
|
|
6
|
+
export class PoolUtils {
|
|
7
|
+
// Used to check if a set of tickarray start indices exceed the boundary range of the default bitmap
|
|
8
|
+
static isOverflowDefaultTickarrayBitmap(tickSpacing, tickarrayStartIndexs) {
|
|
9
|
+
const { maxTickBoundary, minTickBoundary } = this._tickRange(tickSpacing);
|
|
10
|
+
for (const tickIndex of tickarrayStartIndexs) {
|
|
11
|
+
const tickarrayStartIndex = TickUtils.getTickArrayStartIndexByTick(tickIndex, tickSpacing);
|
|
12
|
+
if (tickarrayStartIndex >= maxTickBoundary || tickarrayStartIndex < minTickBoundary) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
static _tickRange(tickSpacing) {
|
|
19
|
+
let maxTickBoundary = TickArrayBitmap.maxTickInTickarrayBitmap(tickSpacing);
|
|
20
|
+
let minTickBoundary = -maxTickBoundary;
|
|
21
|
+
if (maxTickBoundary > MAX_TICK) {
|
|
22
|
+
maxTickBoundary = TickQuery.getArrayStartIndex(MAX_TICK, tickSpacing) + TickQuery.tickCount(tickSpacing);
|
|
23
|
+
}
|
|
24
|
+
if (minTickBoundary < MIN_TICK) {
|
|
25
|
+
minTickBoundary = TickQuery.getArrayStartIndex(MIN_TICK, tickSpacing);
|
|
26
|
+
}
|
|
27
|
+
return { maxTickBoundary, minTickBoundary };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Calculate the maximum and minimum ticks selectable by users in the UI
|
|
31
|
+
* Unlike _tickRange, this method directly returns the available tick value range, not the tickarray boundaries
|
|
32
|
+
*
|
|
33
|
+
* @param tickSpacing tick spacing
|
|
34
|
+
* @returns Maximum and minimum tick values selectable by users
|
|
35
|
+
*/
|
|
36
|
+
static tickRange(tickSpacing) {
|
|
37
|
+
// Use protocol-defined hard boundaries
|
|
38
|
+
let maxTickBoundary = MAX_TICK;
|
|
39
|
+
let minTickBoundary = MIN_TICK;
|
|
40
|
+
// Ensure returned tick values are divisible by tickSpacing to meet UI selection requirements
|
|
41
|
+
maxTickBoundary = Math.floor(maxTickBoundary / tickSpacing) * tickSpacing;
|
|
42
|
+
minTickBoundary = Math.ceil(minTickBoundary / tickSpacing) * tickSpacing;
|
|
43
|
+
return { maxTickBoundary, minTickBoundary };
|
|
44
|
+
}
|
|
45
|
+
static nextInitializedTickArrayStartIndex(poolInfo, lastTickArrayStartIndex, zeroForOne) {
|
|
46
|
+
// Start scanning from the provided lastTickArrayStartIndex instead of recomputing from tickCurrent
|
|
47
|
+
// This avoids skipping the immediate adjacent array when tickCurrent happens to equal a start index.
|
|
48
|
+
while (true) {
|
|
49
|
+
const { isInit: startIsInit, tickIndex: startIndex } = TickArrayBitmap.nextInitializedTickArrayStartIndex(TickUtils.mergeTickArrayBitmap(poolInfo.tickArrayBitmap), lastTickArrayStartIndex, poolInfo.tickSpacing, zeroForOne);
|
|
50
|
+
if (startIsInit) {
|
|
51
|
+
return { isExist: true, nextStartIndex: startIndex };
|
|
52
|
+
}
|
|
53
|
+
lastTickArrayStartIndex = startIndex;
|
|
54
|
+
const { isInit, tickIndex } = TickArrayBitmapExtensionUtils.nextInitializedTickArrayFromOneBitmap(lastTickArrayStartIndex, poolInfo.tickSpacing, zeroForOne, poolInfo.exBitmapInfo);
|
|
55
|
+
if (isInit)
|
|
56
|
+
return { isExist: true, nextStartIndex: tickIndex };
|
|
57
|
+
lastTickArrayStartIndex = tickIndex;
|
|
58
|
+
if (lastTickArrayStartIndex < MIN_TICK || lastTickArrayStartIndex > MAX_TICK)
|
|
59
|
+
return { isExist: false, nextStartIndex: 0 };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
static getFirstInitializedTickArray(poolInfo, zeroForOne) {
|
|
63
|
+
const { isInitialized, startIndex } = PoolUtils.isOverflowDefaultTickarrayBitmap(poolInfo.tickSpacing, [
|
|
64
|
+
poolInfo.tickCurrent,
|
|
65
|
+
])
|
|
66
|
+
? TickArrayBitmapExtensionUtils.checkTickArrayIsInit(TickQuery.getArrayStartIndex(poolInfo.tickCurrent, poolInfo.tickSpacing), poolInfo.tickSpacing, poolInfo.exBitmapInfo)
|
|
67
|
+
: TickUtils.checkTickArrayIsInitialized(TickUtils.mergeTickArrayBitmap(poolInfo.tickArrayBitmap), poolInfo.tickCurrent, poolInfo.tickSpacing);
|
|
68
|
+
if (isInitialized) {
|
|
69
|
+
const { publicKey: address } = getPdaTickArrayAddress(poolInfo.programId, poolInfo.poolId, startIndex);
|
|
70
|
+
return {
|
|
71
|
+
isExist: true,
|
|
72
|
+
startIndex,
|
|
73
|
+
nextAccountMeta: address,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const { isExist, nextStartIndex } = this.nextInitializedTickArrayStartIndex(poolInfo, TickQuery.getArrayStartIndex(poolInfo.tickCurrent, poolInfo.tickSpacing), zeroForOne);
|
|
77
|
+
if (isExist) {
|
|
78
|
+
const { publicKey: address } = getPdaTickArrayAddress(poolInfo.programId, poolInfo.poolId, nextStartIndex);
|
|
79
|
+
return {
|
|
80
|
+
isExist: true,
|
|
81
|
+
startIndex: nextStartIndex,
|
|
82
|
+
nextAccountMeta: address,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return { isExist: false, nextAccountMeta: undefined, startIndex: undefined };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Calculate the output amount and required account list for token swap
|
|
89
|
+
*
|
|
90
|
+
* This function is the core of CLMM (Concentrated Liquidity Market Maker) swap logic, used to calculate the expected output amount for a given input amount,
|
|
91
|
+
* and determine all tick array accounts that need to be accessed during trade execution.
|
|
92
|
+
*
|
|
93
|
+
* @param params Swap calculation parameter object
|
|
94
|
+
* @param params.poolInfo - Complete information of the liquidity pool, including current price, liquidity, tick and other states
|
|
95
|
+
* @param params.exBitmapInfo - Tick array bitmap extension information, used to handle ticks beyond the default range
|
|
96
|
+
* @param params.ammConfig - AMM configuration information, including transaction fee rates and other parameters
|
|
97
|
+
* @param params.tickArrayInfo - Loaded tick array information cache, with keys as tick array start indices
|
|
98
|
+
* @param params.inputTokenMint - Input token mint address, used to determine trade direction
|
|
99
|
+
* @param params.inputAmount - Input token amount (using minimum units)
|
|
100
|
+
* @param params.sqrtPriceLimitX64 - Optional price limit, representing the worst price for the trade (Q64.64 format)
|
|
101
|
+
* @param params.catchLiquidityInsufficient - Whether to catch liquidity insufficient situations, default false
|
|
102
|
+
*
|
|
103
|
+
* @returns Swap calculation result object
|
|
104
|
+
* @returns allTrade - Boolean value indicating whether the specified input amount can be fully traded
|
|
105
|
+
* true: All amount can be traded
|
|
106
|
+
* false: Only partial trading due to insufficient liquidity or price limit
|
|
107
|
+
* @returns expectedAmountOut - Expected output token amount to be obtained (using minimum units)
|
|
108
|
+
* @returns remainingAccounts - List of all tick array account addresses that need to be accessed during trading
|
|
109
|
+
* These accounts need to be passed as remaining accounts in the trade instruction
|
|
110
|
+
* @returns executionPrice - Final price after trade execution (square root price in Q64.64 format)
|
|
111
|
+
* @returns feeAmount - Total fees generated by the trade (using input token minimum units)
|
|
112
|
+
*
|
|
113
|
+
* @throws Error Throws 'Invalid tick array' error when no valid tick array is found
|
|
114
|
+
*/
|
|
115
|
+
static getOutputAmountAndRemainAccounts(params) {
|
|
116
|
+
const { poolInfo, exBitmapInfo, ammConfig, tickArrayInfo, inputTokenMint, inputAmount, sqrtPriceLimitX64, catchLiquidityInsufficient = false, } = params;
|
|
117
|
+
// Step 1: Determine trade direction
|
|
118
|
+
// zeroForOne = true: means trading tokenA (token0) for tokenB (token1), price decreases
|
|
119
|
+
// zeroForOne = false: means trading tokenB (token1) for tokenA (token0), price increases
|
|
120
|
+
const zeroForOne = inputTokenMint.toBase58() === poolInfo.mintA.toBase58();
|
|
121
|
+
// Step 2: Initialize account list to store all tick array accounts needed during trading
|
|
122
|
+
const allNeededAccounts = [];
|
|
123
|
+
// Step 3: Find the first tick array that needs to be accessed
|
|
124
|
+
// Based on current tick position and trade direction, find the array containing current tick or next initialized tick
|
|
125
|
+
const { isExist, startIndex: firstTickArrayStartIndex, nextAccountMeta, } = this.getFirstInitializedTickArray({
|
|
126
|
+
programId: poolInfo.programId,
|
|
127
|
+
poolId: poolInfo.poolId,
|
|
128
|
+
tickCurrent: poolInfo.tickCurrent,
|
|
129
|
+
tickSpacing: poolInfo.tickSpacing,
|
|
130
|
+
tickArrayBitmap: poolInfo.tickArrayBitmap,
|
|
131
|
+
exBitmapInfo,
|
|
132
|
+
}, zeroForOne);
|
|
133
|
+
// If no valid tick array is found, it indicates abnormal pool state or no liquidity
|
|
134
|
+
if (!isExist || firstTickArrayStartIndex === undefined || !nextAccountMeta)
|
|
135
|
+
throw new Error('Invalid tick array');
|
|
136
|
+
// Step 4: Add the first tick array account to the list
|
|
137
|
+
allNeededAccounts.push(nextAccountMeta);
|
|
138
|
+
// Step 5: Execute the core logic of swap calculation
|
|
139
|
+
// SwapMath.swapCompute simulates the entire swap process, calculating:
|
|
140
|
+
// - Actual tradable amount
|
|
141
|
+
// - Output token amount
|
|
142
|
+
// - Other tick array accounts needed on the trade path
|
|
143
|
+
// - Final execution price and fees
|
|
144
|
+
const { allTrade, amountCalculated: outputAmount, accounts: reaminAccounts, sqrtPriceX64: executionPrice, feeAmount, } = SwapMath.swapCompute(poolInfo.programId, poolInfo.poolId, tickArrayInfo, poolInfo.tickArrayBitmap, exBitmapInfo, zeroForOne, ammConfig.tradeFeeRate, poolInfo.liquidity, poolInfo.tickCurrent, poolInfo.tickSpacing, poolInfo.sqrtPriceX64, inputAmount, firstTickArrayStartIndex, sqrtPriceLimitX64, catchLiquidityInsufficient);
|
|
145
|
+
// Step 6: Add other necessary accounts discovered during swap to the list
|
|
146
|
+
allNeededAccounts.push(...reaminAccounts);
|
|
147
|
+
// Step 7: Return calculation results
|
|
148
|
+
// Note: outputAmount is negative (indicating outflow), needs to be multiplied by -1 to convert to positive
|
|
149
|
+
return {
|
|
150
|
+
allTrade,
|
|
151
|
+
expectedAmountOut: outputAmount.mul(NEGATIVE_ONE),
|
|
152
|
+
remainingAccounts: allNeededAccounts,
|
|
153
|
+
executionPrice,
|
|
154
|
+
feeAmount,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Calculate the required input token amount and account list for exact output
|
|
159
|
+
*
|
|
160
|
+
* This function is used for "exact output" scenarios, where the user specifies the desired output token amount,
|
|
161
|
+
* and the function calculates how many tokens need to be input to obtain the specified output amount.
|
|
162
|
+
*
|
|
163
|
+
* @param params Swap calculation parameter object
|
|
164
|
+
* @param params.poolInfo - Complete information of the liquidity pool
|
|
165
|
+
* @param params.exBitmapInfo - Tick array bitmap extension information
|
|
166
|
+
* @param params.ammConfig - AMM configuration information
|
|
167
|
+
* @param params.tickArrayInfo - Loaded tick array information cache
|
|
168
|
+
* @param params.outputTokenMint - Output token mint address
|
|
169
|
+
* @param params.outputAmount - Expected output token amount (using minimum units)
|
|
170
|
+
* @param params.sqrtPriceLimitX64 - Optional price limit (Q64.64 format)
|
|
171
|
+
* @param params.catchLiquidityInsufficient - Whether to catch liquidity insufficient situations
|
|
172
|
+
*
|
|
173
|
+
* @returns Swap calculation result object
|
|
174
|
+
* @returns allTrade - Whether the specified output amount can be fully obtained
|
|
175
|
+
* @returns expectedAmountIn - Required input token amount (including fees)
|
|
176
|
+
* @returns remainingAccounts - List of tick array accounts that need to be accessed during trading
|
|
177
|
+
* @returns executionPrice - Final price after trade execution
|
|
178
|
+
* @returns feeAmount - Total fees generated by the trade
|
|
179
|
+
*/
|
|
180
|
+
static getInputAmountAndRemainAccounts(params) {
|
|
181
|
+
const { poolInfo, exBitmapInfo, ammConfig, tickArrayInfo, outputTokenMint, outputAmount, sqrtPriceLimitX64, catchLiquidityInsufficient = false, } = params;
|
|
182
|
+
// 步骤1: 确定交易方向
|
|
183
|
+
// 注意:对于精确输出,交易方向的判断基于输出代币
|
|
184
|
+
// 如果输出 tokenB,则需要输入 tokenA(zeroForOne = true)
|
|
185
|
+
// 如果输出 tokenA,则需要输入 tokenB(zeroForOne = false)
|
|
186
|
+
const zeroForOne = outputTokenMint.toBase58() === poolInfo.mintB.toBase58();
|
|
187
|
+
// 步骤2: 初始化账户列表
|
|
188
|
+
const allNeededAccounts = [];
|
|
189
|
+
// 步骤3: 查找第一个需要访问的 tick 数组
|
|
190
|
+
const { isExist, startIndex: firstTickArrayStartIndex, nextAccountMeta, } = this.getFirstInitializedTickArray({
|
|
191
|
+
programId: poolInfo.programId,
|
|
192
|
+
poolId: poolInfo.poolId,
|
|
193
|
+
tickCurrent: poolInfo.tickCurrent,
|
|
194
|
+
tickSpacing: poolInfo.tickSpacing,
|
|
195
|
+
tickArrayBitmap: poolInfo.tickArrayBitmap,
|
|
196
|
+
exBitmapInfo,
|
|
197
|
+
}, zeroForOne);
|
|
198
|
+
if (!isExist || firstTickArrayStartIndex === undefined || !nextAccountMeta)
|
|
199
|
+
throw new Error('Invalid tick array');
|
|
200
|
+
// Step 4: Add the first tick array account to the list
|
|
201
|
+
allNeededAccounts.push(nextAccountMeta);
|
|
202
|
+
// 步骤5: 执行交换计算
|
|
203
|
+
// 对于精确输出,amountSpecified 需要是负数
|
|
204
|
+
const amountSpecified = outputAmount.mul(NEGATIVE_ONE);
|
|
205
|
+
const { allTrade, amountCalculated: inputAmount, accounts: reaminAccounts, sqrtPriceX64: executionPrice, feeAmount, } = SwapMath.swapCompute(poolInfo.programId, poolInfo.poolId, tickArrayInfo, poolInfo.tickArrayBitmap, exBitmapInfo, zeroForOne, ammConfig.tradeFeeRate, poolInfo.liquidity, poolInfo.tickCurrent, poolInfo.tickSpacing, poolInfo.sqrtPriceX64, amountSpecified, firstTickArrayStartIndex, sqrtPriceLimitX64, catchLiquidityInsufficient);
|
|
206
|
+
// 步骤6: 将其他必需账户添加到列表中
|
|
207
|
+
allNeededAccounts.push(...reaminAccounts);
|
|
208
|
+
// 步骤7: 返回计算结果
|
|
209
|
+
// 注意:对于精确输出,inputAmount 包含了手续费
|
|
210
|
+
return {
|
|
211
|
+
allTrade,
|
|
212
|
+
expectedAmountIn: inputAmount,
|
|
213
|
+
remainingAccounts: allNeededAccounts,
|
|
214
|
+
executionPrice,
|
|
215
|
+
feeAmount,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
//# sourceMappingURL=poolUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"poolUtils.js","sourceRoot":"","sources":["../../../../src/instructions/utils/poolUtils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGnE,OAAO,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAGnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,6BAA6B,EAAE,MAAM,sBAAsB,CAAC;AAEtF,MAAM,OAAO,SAAS;IACpB,oGAAoG;IAC7F,MAAM,CAAC,gCAAgC,CAAC,WAAmB,EAAE,oBAA8B;QAChG,MAAM,EAAE,eAAe,EAAE,eAAe,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;QAE1E,KAAK,MAAM,SAAS,IAAI,oBAAoB,EAAE,CAAC;YAC7C,MAAM,mBAAmB,GAAG,SAAS,CAAC,4BAA4B,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;YAE3F,IAAI,mBAAmB,IAAI,eAAe,IAA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gB,EAAE,WAAW,EAC7B,QAAQ,EAAE,cAAc,EACxB,YAAY,EAAE,cAAc,EAC5B,SAAS,GACV,GAAG,QAAQ,CAAC,WAAW,CACtB,QAAQ,CAAC,SAAS,EAClB,QAAQ,CAAC,MAAM,EACf,aAAa,EACb,QAAQ,CAAC,eAAe,EACxB,YAAY,EACZ,UAAU,EACV,SAAS,CAAC,YAAY,EACtB,QAAQ,CAAC,SAAS,EAClB,QAAQ,CAAC,WAAW,EACpB,QAAQ,CAAC,WAAW,EACpB,QAAQ,CAAC,YAAY,EACrB,eAAe,EACf,wBAAwB,EACxB,iBAAiB,EACjB,0BAA0B,CAC3B,CAAC;QAEF,qBAAqB;QACrB,iBAAiB,CAAC,IAAI,CAAC,GAAG,cAAc,CAAC,CAAC;QAE1C,cAAc;QACd,+BAA+B;QAC/B,OAAO;YACL,QAAQ;YACR,gBAAgB,EAAE,WAAW;YAC7B,iBAAiB,EAAE,iBAAiB;YACpC,cAAc;YACd,SAAS;SACV,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { EpochInfo } from '@solana/web3.js';
|
|
2
|
+
import BN from 'bn.js';
|
|
3
|
+
import { IPersonalPositionLayout, IPoolLayout } from '../layout.js';
|
|
4
|
+
import { Tick } from './models.js';
|
|
5
|
+
export interface GetTransferAmountFee {
|
|
6
|
+
amount: BN;
|
|
7
|
+
fee: BN | undefined;
|
|
8
|
+
expirationTime: number | undefined;
|
|
9
|
+
}
|
|
10
|
+
export interface ReturnTypeGetLiquidityAmountOut {
|
|
11
|
+
liquidity: BN;
|
|
12
|
+
amountSlippageA: GetTransferAmountFee;
|
|
13
|
+
amountSlippageB: GetTransferAmountFee;
|
|
14
|
+
amountA: GetTransferAmountFee;
|
|
15
|
+
amountB: GetTransferAmountFee;
|
|
16
|
+
expirationTime: number | undefined;
|
|
17
|
+
}
|
|
18
|
+
export declare class PositionUtils {
|
|
19
|
+
static getfeeGrowthInside(poolInfo: Pick<IPoolLayout, 'tickCurrent' | 'feeGrowthGlobalX64A' | 'feeGrowthGlobalX64B'>, tickLowerState: Tick, tickUpperState: Tick): {
|
|
20
|
+
feeGrowthInsideX64A: BN;
|
|
21
|
+
feeGrowthInsideBX64: BN;
|
|
22
|
+
};
|
|
23
|
+
static getPositionFees(poolInfo: Pick<IPoolLayout, 'tickCurrent' | 'feeGrowthGlobalX64A' | 'feeGrowthGlobalX64B'>, positionState: IPersonalPositionLayout, tickLowerState: Tick, tickUpperState: Tick): {
|
|
24
|
+
tokenFeeAmountA: BN;
|
|
25
|
+
tokenFeeAmountB: BN;
|
|
26
|
+
};
|
|
27
|
+
static getAmountsFromLiquidity(params: {
|
|
28
|
+
poolInfo: IPoolLayout;
|
|
29
|
+
ownerPosition: IPersonalPositionLayout;
|
|
30
|
+
liquidity: BN;
|
|
31
|
+
slippage: number;
|
|
32
|
+
add: boolean;
|
|
33
|
+
epochInfo: EpochInfo;
|
|
34
|
+
}): ReturnTypeGetLiquidityAmountOut;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=position.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../../../../src/instructions/utils/position.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,MAAM,OAAO,CAAC;AAIvB,OAAO,EAAE,uBAAuB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAMpE,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEnC,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,EAAE,CAAC;IACX,GAAG,EAAE,EAAE,GAAG,SAAS,CAAC;IACpB,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC;AAED,MAAM,WAAW,+BAA+B;IAC9C,SAAS,EAAE,EAAE,CAAC;IACd,eAAe,EAAE,oBAAoB,CAAC;IACtC,eAAe,EAAE,oBAAoB,CAAC;IACtC,OAAO,EAAE,oBAAoB,CAAC;IAC9B,OAAO,EAAE,oBAAoB,CAAC;IAC9B,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC;AAED,qBAAa,aAAa;IACxB,MAAM,CAAC,kBAAkB,CACvB,QAAQ,EAAE,IAAI,CAAC,WAAW,EAAE,aAAa,GAAG,qBAAqB,GAAG,qBAAqB,CAAC,EAC1F,cAAc,EAAE,IAAI,EACpB,cAAc,EAAE,IAAI,GACnB;QAAE,mBAAmB,EAAE,EAAE,CAAC;QAAC,mBAAmB,EAAE,EAAE,CAAA;KAAE;IAgCvD,MAAM,CAAC,eAAe,CACpB,QAAQ,EAAE,IAAI,CAAC,WAAW,EAAE,aAAa,GAAG,qBAAqB,GAAG,qBAAqB,CAAC,EAC1F,aAAa,EAAE,uBAAuB,EACtC,cAAc,EAAE,IAAI,EACpB,cAAc,EAAE,IAAI,GACnB;QAAE,eAAe,EAAE,EAAE,CAAC;QAAC,eAAe,EAAE,EAAE,CAAA;KAAE;IAwB/C,MAAM,CAAC,uBAAuB,CAAC,MAAM,EAAE;QACrC,QAAQ,EAAE,WAAW,CAAC;QACtB,aAAa,EAAE,uBAAuB,CAAC;QACvC,SAAS,EAAE,EAAE,CAAC;QACd,QAAQ,EAAE,MAAM,CAAC;QACjB,GAAG,EAAE,OAAO,CAAC;QACb,SAAS,EAAE,SAAS,CAAC;KACtB,GAAG,+BAA+B;CA0DpC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import BN from 'bn.js';
|
|
2
|
+
import { Decimal } from 'decimal.js';
|
|
3
|
+
import { Q64 } from '../constants.js';
|
|
4
|
+
import { LiquidityMath } from './liquidityMath.js';
|
|
5
|
+
import { MathUtils } from './mathUtils.js';
|
|
6
|
+
import { SqrtPriceMath } from './sqrtPriceMath.js';
|
|
7
|
+
import { getTransferAmountFee, minExpirationTime } from './transfer.js';
|
|
8
|
+
export class PositionUtils {
|
|
9
|
+
static getfeeGrowthInside(poolInfo, tickLowerState, tickUpperState) {
|
|
10
|
+
let feeGrowthBelowX64A = new BN(0);
|
|
11
|
+
let feeGrowthBelowX64B = new BN(0);
|
|
12
|
+
if (poolInfo.tickCurrent >= tickLowerState.tick) {
|
|
13
|
+
feeGrowthBelowX64A = tickLowerState.feeGrowthOutsideX64A;
|
|
14
|
+
feeGrowthBelowX64B = tickLowerState.feeGrowthOutsideX64B;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
feeGrowthBelowX64A = poolInfo.feeGrowthGlobalX64A.sub(tickLowerState.feeGrowthOutsideX64A);
|
|
18
|
+
feeGrowthBelowX64B = poolInfo.feeGrowthGlobalX64B.sub(tickLowerState.feeGrowthOutsideX64B);
|
|
19
|
+
}
|
|
20
|
+
let feeGrowthAboveX64A = new BN(0);
|
|
21
|
+
let feeGrowthAboveX64B = new BN(0);
|
|
22
|
+
if (poolInfo.tickCurrent < tickUpperState.tick) {
|
|
23
|
+
feeGrowthAboveX64A = tickUpperState.feeGrowthOutsideX64A;
|
|
24
|
+
feeGrowthAboveX64B = tickUpperState.feeGrowthOutsideX64B;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
feeGrowthAboveX64A = poolInfo.feeGrowthGlobalX64A.sub(tickUpperState.feeGrowthOutsideX64A);
|
|
28
|
+
feeGrowthAboveX64B = poolInfo.feeGrowthGlobalX64B.sub(tickUpperState.feeGrowthOutsideX64B);
|
|
29
|
+
}
|
|
30
|
+
const feeGrowthInsideX64A = MathUtils.wrappingSubU128(MathUtils.wrappingSubU128(poolInfo.feeGrowthGlobalX64A, feeGrowthBelowX64A), feeGrowthAboveX64A);
|
|
31
|
+
const feeGrowthInsideBX64 = MathUtils.wrappingSubU128(MathUtils.wrappingSubU128(poolInfo.feeGrowthGlobalX64B, feeGrowthBelowX64B), feeGrowthAboveX64B);
|
|
32
|
+
return { feeGrowthInsideX64A, feeGrowthInsideBX64 };
|
|
33
|
+
}
|
|
34
|
+
static getPositionFees(poolInfo, positionState, tickLowerState, tickUpperState) {
|
|
35
|
+
const { feeGrowthInsideX64A, feeGrowthInsideBX64 } = this.getfeeGrowthInside(poolInfo, tickLowerState, tickUpperState);
|
|
36
|
+
const feeGrowthdeltaA = MathUtils.mulDivFloor(MathUtils.wrappingSubU128(feeGrowthInsideX64A, positionState.feeGrowthInsideLastX64A), positionState.liquidity, Q64);
|
|
37
|
+
const tokenFeeAmountA = positionState.tokenFeesOwedA.add(feeGrowthdeltaA);
|
|
38
|
+
const feeGrowthdelta1 = MathUtils.mulDivFloor(MathUtils.wrappingSubU128(feeGrowthInsideBX64, positionState.feeGrowthInsideLastX64B), positionState.liquidity, Q64);
|
|
39
|
+
const tokenFeeAmountB = positionState.tokenFeesOwedB.add(feeGrowthdelta1);
|
|
40
|
+
return { tokenFeeAmountA, tokenFeeAmountB };
|
|
41
|
+
}
|
|
42
|
+
static getAmountsFromLiquidity(params) {
|
|
43
|
+
const { poolInfo, ownerPosition, liquidity, slippage, add, epochInfo } = params;
|
|
44
|
+
const sqrtPriceX64 = poolInfo.sqrtPriceX64;
|
|
45
|
+
const sqrtPriceX64A = SqrtPriceMath.getSqrtPriceX64FromTick(ownerPosition.tickLower);
|
|
46
|
+
const sqrtPriceX64B = SqrtPriceMath.getSqrtPriceX64FromTick(ownerPosition.tickUpper);
|
|
47
|
+
const coefficientRe = add ? 1 + slippage : 1 - slippage;
|
|
48
|
+
const amounts = LiquidityMath.getAmountsFromLiquidity(sqrtPriceX64, sqrtPriceX64A, sqrtPriceX64B, liquidity, add);
|
|
49
|
+
// TODO: Temporarily not considering Token 2022 transfer fee scenarios. If needed in the future, this should be added here;
|
|
50
|
+
const [amountA, amountB] = [
|
|
51
|
+
getTransferAmountFee(amounts.amountA, undefined, epochInfo, true),
|
|
52
|
+
getTransferAmountFee(amounts.amountB, undefined, epochInfo, true),
|
|
53
|
+
];
|
|
54
|
+
const [amountSlippageA, amountSlippageB] = [
|
|
55
|
+
getTransferAmountFee(new BN(new Decimal(amounts.amountA.toString()).mul(coefficientRe).toFixed(0)), undefined, epochInfo, true),
|
|
56
|
+
getTransferAmountFee(new BN(new Decimal(amounts.amountB.toString()).mul(coefficientRe).toFixed(0)), undefined, epochInfo, true),
|
|
57
|
+
];
|
|
58
|
+
// const [amountA, amountB] = [
|
|
59
|
+
// getTransferAmountFee(amounts.amountA, poolInfo.mintA.extensions?.feeConfig, epochInfo, true),
|
|
60
|
+
// getTransferAmountFee(amounts.amountB, poolInfo.mintB.extensions?.feeConfig, epochInfo, true),
|
|
61
|
+
// ];
|
|
62
|
+
// const [amountSlippageA, amountSlippageB] = [
|
|
63
|
+
// getTransferAmountFee(
|
|
64
|
+
// new BN(new Decimal(amounts.amountA.toString()).mul(coefficientRe).toFixed(0)),
|
|
65
|
+
// poolInfo.mintA.extensions?.feeConfig,
|
|
66
|
+
// epochInfo,
|
|
67
|
+
// true,
|
|
68
|
+
// ),
|
|
69
|
+
// getTransferAmountFee(
|
|
70
|
+
// new BN(new Decimal(amounts.amountB.toString()).mul(coefficientRe).toFixed(0)),
|
|
71
|
+
// poolInfo.mintB.extensions?.feeConfig,
|
|
72
|
+
// epochInfo,
|
|
73
|
+
// true,
|
|
74
|
+
// ),
|
|
75
|
+
// ];
|
|
76
|
+
return {
|
|
77
|
+
liquidity,
|
|
78
|
+
amountA,
|
|
79
|
+
amountB,
|
|
80
|
+
amountSlippageA,
|
|
81
|
+
amountSlippageB,
|
|
82
|
+
expirationTime: minExpirationTime(amountA.expirationTime, amountB.expirationTime),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=position.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"position.js","sourceRoot":"","sources":["../../../../src/instructions/utils/position.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC,OAAO,EAAE,GAAG,EA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E,SAAS,EACT,SAAS,EACT,IAAI,CACL;SACF,CAAC;QAEF,+BAA+B;QAC/B,kGAAkG;QAClG,kGAAkG;QAClG,KAAK;QACL,+CAA+C;QAC/C,0BAA0B;QAC1B,qFAAqF;QACrF,4CAA4C;QAC5C,iBAAiB;QACjB,YAAY;QACZ,OAAO;QACP,0BAA0B;QAC1B,qFAAqF;QACrF,4CAA4C;QAC5C,iBAAiB;QACjB,YAAY;QACZ,OAAO;QACP,KAAK;QAEL,OAAO;YACL,SAAS;YACT,OAAO;YACP,OAAO;YACP,eAAe;YACf,eAAe;YACf,cAAc,EAAE,iBAAiB,CAAC,OAAO,CAAC,cAAc,EAAE,OAAO,CAAC,cAAc,CAAC;SAClF,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import BN from 'bn.js';
|
|
2
|
+
import { Decimal } from 'decimal.js';
|
|
3
|
+
export declare class SqrtPriceMath {
|
|
4
|
+
static sqrtPriceX64ToPrice(sqrtPriceX64: BN, decimalsA: number, decimalsB: number): Decimal;
|
|
5
|
+
static priceToSqrtPriceX64(price: Decimal, decimalsA: number, decimalsB: number): BN;
|
|
6
|
+
static getNextSqrtPriceX64FromInput(sqrtPriceX64: BN, liquidity: BN, amountIn: BN, zeroForOne: boolean): BN;
|
|
7
|
+
static getNextSqrtPriceX64FromOutput(sqrtPriceX64: BN, liquidity: BN, amountOut: BN, zeroForOne: boolean): BN;
|
|
8
|
+
private static getNextSqrtPriceFromTokenAmountARoundingUp;
|
|
9
|
+
private static getNextSqrtPriceFromTokenAmountBRoundingDown;
|
|
10
|
+
static getSqrtPriceX64FromTick(tick: number): BN;
|
|
11
|
+
static getTickFromPrice(price: Decimal, decimalsA: number, decimalsB: number): number;
|
|
12
|
+
static getTickFromSqrtPriceX64(sqrtPriceX64: BN): number;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=sqrtPriceMath.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqrtPriceMath.d.ts","sourceRoot":"","sources":["../../../../src/instructions/utils/sqrtPriceMath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAmCrC,qBAAa,aAAa;WACV,mBAAmB,CAAC,YAAY,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO;WAMpF,mBAAmB,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,EAAE;WAI7E,4BAA4B,CAAC,YAAY,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,UAAU,EAAE,OAAO,GAAG,EAAE;WAapG,6BAA6B,CAAC,YAAY,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,UAAU,EAAE,OAAO,GAAG,EAAE;IAapH,OAAO,CAAC,MAAM,CAAC,0CAA0C;IA0BzD,OAAO,CAAC,MAAM,CAAC,4CAA4C;WAkB7C,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,EAAE;WAiCzC,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM;WAI9E,uBAAuB,CAAC,YAAY,EAAE,EAAE,GAAG,MAAM;CAuChE"}
|