@clonegod/ttd-bsc-common 3.1.68 → 3.1.70
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/dist/quote/index.d.ts +0 -1
- package/dist/quote/index.js +0 -1
- package/dist/quote/pricing/fee_helpers.d.ts +13 -0
- package/dist/quote/pricing/fee_helpers.js +68 -0
- package/dist/quote/pricing/index.d.ts +1 -0
- package/dist/quote/pricing/index.js +1 -0
- package/dist/quote/pricing/pool_state_initializer.js +15 -21
- package/dist/types/pool_state.d.ts +115 -30
- package/package.json +2 -2
package/dist/quote/index.d.ts
CHANGED
package/dist/quote/index.js
CHANGED
|
@@ -22,4 +22,3 @@ __exportStar(require("./verify"), exports);
|
|
|
22
22
|
__exportStar(require("./price_feed_handler"), exports);
|
|
23
23
|
__exportStar(require("./quote_amount"), exports);
|
|
24
24
|
__exportStar(require("./preload_token_prices"), exports);
|
|
25
|
-
__exportStar(require("./hook_fee_monitor"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AmmPoolState, ClmmPoolState, InfinityPoolState } from '../../types/pool_state';
|
|
2
|
+
export declare const V4_DYNAMIC_FEE_FLAG = 8388608;
|
|
3
|
+
export declare function calculateSwapFeePpm(protocolFeePpm: number, lpFeePpm: number): number;
|
|
4
|
+
export declare function decodeV4ProtocolFee(encodedProtocolFee: number, zeroForOne: boolean): number;
|
|
5
|
+
export declare function v2EffectiveFeeBps(state: AmmPoolState): number;
|
|
6
|
+
export declare function v3EffectiveFeeBps(state: ClmmPoolState): number;
|
|
7
|
+
export declare function v4EffectiveFeeBps(state: InfinityPoolState): number;
|
|
8
|
+
export declare function v4EffectiveFeePpm(state: InfinityPoolState): number;
|
|
9
|
+
export declare function v4DirectionalFeePpm(state: InfinityPoolState, zeroForOne: boolean): number;
|
|
10
|
+
export declare function buildInfinityPoolSdkFee(state: InfinityPoolState): {
|
|
11
|
+
fee: number;
|
|
12
|
+
protocolFee: number;
|
|
13
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.V4_DYNAMIC_FEE_FLAG = void 0;
|
|
4
|
+
exports.calculateSwapFeePpm = calculateSwapFeePpm;
|
|
5
|
+
exports.decodeV4ProtocolFee = decodeV4ProtocolFee;
|
|
6
|
+
exports.v2EffectiveFeeBps = v2EffectiveFeeBps;
|
|
7
|
+
exports.v3EffectiveFeeBps = v3EffectiveFeeBps;
|
|
8
|
+
exports.v4EffectiveFeeBps = v4EffectiveFeeBps;
|
|
9
|
+
exports.v4EffectiveFeePpm = v4EffectiveFeePpm;
|
|
10
|
+
exports.v4DirectionalFeePpm = v4DirectionalFeePpm;
|
|
11
|
+
exports.buildInfinityPoolSdkFee = buildInfinityPoolSdkFee;
|
|
12
|
+
exports.V4_DYNAMIC_FEE_FLAG = 0x800000;
|
|
13
|
+
function calculateSwapFeePpm(protocolFeePpm, lpFeePpm) {
|
|
14
|
+
return protocolFeePpm + lpFeePpm - Math.floor(protocolFeePpm * lpFeePpm / 1000000);
|
|
15
|
+
}
|
|
16
|
+
function decodeV4ProtocolFee(encodedProtocolFee, zeroForOne) {
|
|
17
|
+
if (zeroForOne)
|
|
18
|
+
return encodedProtocolFee & 0xFFF;
|
|
19
|
+
return (encodedProtocolFee >> 12) & 0xFFF;
|
|
20
|
+
}
|
|
21
|
+
function v2EffectiveFeeBps(state) {
|
|
22
|
+
return state.protocolFeeBps;
|
|
23
|
+
}
|
|
24
|
+
function v3EffectiveFeeBps(state) {
|
|
25
|
+
return state.poolFee / 100;
|
|
26
|
+
}
|
|
27
|
+
function v4EffectiveFeeBps(state) {
|
|
28
|
+
return v4EffectiveFeePpm(state) / 100;
|
|
29
|
+
}
|
|
30
|
+
function v4EffectiveFeePpm(state) {
|
|
31
|
+
if (state.lastSwapFee != null) {
|
|
32
|
+
return state.lastSwapFee;
|
|
33
|
+
}
|
|
34
|
+
if (state.poolKeyFee !== exports.V4_DYNAMIC_FEE_FLAG) {
|
|
35
|
+
return state.poolKeyFee;
|
|
36
|
+
}
|
|
37
|
+
const lp = state.slot0LpFee ?? 0;
|
|
38
|
+
const proto = state.slot0ProtocolFee ?? 0;
|
|
39
|
+
const proto0to1 = proto & 0xFFF;
|
|
40
|
+
const proto1to0 = (proto >> 12) & 0xFFF;
|
|
41
|
+
const fee0to1 = calculateSwapFeePpm(proto0to1, lp);
|
|
42
|
+
const fee1to0 = calculateSwapFeePpm(proto1to0, lp);
|
|
43
|
+
return Math.max(fee0to1, fee1to0);
|
|
44
|
+
}
|
|
45
|
+
function v4DirectionalFeePpm(state, zeroForOne) {
|
|
46
|
+
if (state.lastSwapFee != null) {
|
|
47
|
+
return state.lastSwapFee;
|
|
48
|
+
}
|
|
49
|
+
if (state.poolKeyFee !== exports.V4_DYNAMIC_FEE_FLAG) {
|
|
50
|
+
return state.poolKeyFee;
|
|
51
|
+
}
|
|
52
|
+
const lp = state.slot0LpFee ?? 0;
|
|
53
|
+
const proto = state.slot0ProtocolFee ?? 0;
|
|
54
|
+
const directionalProto = decodeV4ProtocolFee(proto, zeroForOne);
|
|
55
|
+
return calculateSwapFeePpm(directionalProto, lp);
|
|
56
|
+
}
|
|
57
|
+
function buildInfinityPoolSdkFee(state) {
|
|
58
|
+
if (state.lastSwapFee != null) {
|
|
59
|
+
return { fee: state.lastSwapFee, protocolFee: 0 };
|
|
60
|
+
}
|
|
61
|
+
if (state.poolKeyFee !== exports.V4_DYNAMIC_FEE_FLAG) {
|
|
62
|
+
return { fee: state.poolKeyFee, protocolFee: 0 };
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
fee: state.slot0LpFee ?? 0,
|
|
66
|
+
protocolFee: state.slot0ProtocolFee ?? 0,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./pool"), exports);
|
|
18
18
|
__exportStar(require("./sdk_token_factory"), exports);
|
|
19
19
|
__exportStar(require("./pool_state_initializer"), exports);
|
|
20
|
+
__exportStar(require("./fee_helpers"), exports);
|
|
@@ -55,17 +55,17 @@ class PoolStateInitializer {
|
|
|
55
55
|
poolContract.token0(),
|
|
56
56
|
poolContract.token1(),
|
|
57
57
|
]);
|
|
58
|
-
const
|
|
58
|
+
const protocolFeeBps = AMM_FEE_MAP[dexId] || AMM_FEE_MAP[dexId.replace('-', '_')] || 30;
|
|
59
59
|
const state = {
|
|
60
60
|
address: poolAddress,
|
|
61
61
|
token0: token0.toLowerCase(),
|
|
62
62
|
token1: token1.toLowerCase(),
|
|
63
|
-
|
|
63
|
+
protocolFeeBps,
|
|
64
64
|
reserve0: reserves[0].toString(),
|
|
65
65
|
reserve1: reserves[1].toString(),
|
|
66
66
|
};
|
|
67
67
|
(0, ttd_core_1.log_info)(`[PoolStateInitializer] AMM pool initialized: ${poolAddress}`, {
|
|
68
|
-
token0: state.token0, token1: state.token1,
|
|
68
|
+
token0: state.token0, token1: state.token1, protocolFeeBps: state.protocolFeeBps,
|
|
69
69
|
});
|
|
70
70
|
return state;
|
|
71
71
|
}
|
|
@@ -87,13 +87,11 @@ class PoolStateInitializer {
|
|
|
87
87
|
poolContract.liquidity(),
|
|
88
88
|
]);
|
|
89
89
|
const [sqrtPriceX96, tick] = slot0;
|
|
90
|
-
const feeRatePpm = Number(fee);
|
|
91
90
|
const state = {
|
|
92
91
|
address: poolAddress,
|
|
93
92
|
token0: token0.toLowerCase(),
|
|
94
93
|
token1: token1.toLowerCase(),
|
|
95
|
-
|
|
96
|
-
feeRatePpm,
|
|
94
|
+
poolFee: Number(fee),
|
|
97
95
|
tickSpacing: Number(tickSpacing),
|
|
98
96
|
tick: Number(tick),
|
|
99
97
|
sqrtPriceX96: sqrtPriceX96.toString(),
|
|
@@ -101,7 +99,7 @@ class PoolStateInitializer {
|
|
|
101
99
|
};
|
|
102
100
|
(0, ttd_core_1.log_info)(`[PoolStateInitializer] CLMM pool initialized: ${poolAddress}`, {
|
|
103
101
|
token0: state.token0, token1: state.token1,
|
|
104
|
-
|
|
102
|
+
poolFee: state.poolFee,
|
|
105
103
|
tickSpacing: state.tickSpacing, tick: state.tick,
|
|
106
104
|
});
|
|
107
105
|
return state;
|
|
@@ -122,32 +120,28 @@ class PoolStateInitializer {
|
|
|
122
120
|
const [sqrtPriceX96, tick, protocolFee, lpFee] = slot0Result;
|
|
123
121
|
const [currency0, currency1, hooks, poolManager, fee, parameters] = poolKeyInfo;
|
|
124
122
|
const { tickSpacing } = decodeParameters(parameters);
|
|
125
|
-
const DYNAMIC_FEE_FLAG = 0x800000;
|
|
126
|
-
const isDynamic = Number(fee) === DYNAMIC_FEE_FLAG;
|
|
127
|
-
const effectiveLpFeePpm = isDynamic ? Number(lpFee) : Number(fee);
|
|
128
123
|
const state = {
|
|
129
124
|
address: poolKey,
|
|
130
125
|
token0: currency0.toLowerCase ? currency0.toLowerCase() : currency0,
|
|
131
126
|
token1: currency1.toLowerCase ? currency1.toLowerCase() : currency1,
|
|
132
|
-
|
|
133
|
-
|
|
127
|
+
currency0,
|
|
128
|
+
currency1,
|
|
129
|
+
hooks,
|
|
130
|
+
poolManager,
|
|
131
|
+
parameters,
|
|
134
132
|
tickSpacing,
|
|
133
|
+
poolKeyFee: Number(fee),
|
|
135
134
|
tick: Number(tick),
|
|
136
135
|
sqrtPriceX96: sqrtPriceX96.toString(),
|
|
137
136
|
liquidity: liquidity.toString(),
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
parameters,
|
|
141
|
-
currency0,
|
|
142
|
-
currency1,
|
|
143
|
-
lpFee: Number(lpFee),
|
|
144
|
-
protocolFee: Number(protocolFee),
|
|
137
|
+
slot0LpFee: Number(lpFee),
|
|
138
|
+
slot0ProtocolFee: Number(protocolFee),
|
|
145
139
|
};
|
|
146
140
|
(0, ttd_core_1.log_info)(`[PoolStateInitializer] Infinity pool initialized: ${poolKey}`, {
|
|
147
141
|
currency0: state.currency0, currency1: state.currency1,
|
|
148
|
-
|
|
142
|
+
poolKeyFee: state.poolKeyFee,
|
|
143
|
+
slot0LpFee: state.slot0LpFee, slot0ProtocolFee: state.slot0ProtocolFee,
|
|
149
144
|
tickSpacing: state.tickSpacing, tick: state.tick,
|
|
150
|
-
lpFee: state.lpFee, protocolFee: state.protocolFee,
|
|
151
145
|
});
|
|
152
146
|
return state;
|
|
153
147
|
}
|
|
@@ -14,7 +14,7 @@ export interface AmmPoolState {
|
|
|
14
14
|
address: string;
|
|
15
15
|
token0: string;
|
|
16
16
|
token1: string;
|
|
17
|
-
|
|
17
|
+
protocolFeeBps: number;
|
|
18
18
|
reserve0: string;
|
|
19
19
|
reserve1: string;
|
|
20
20
|
}
|
|
@@ -22,47 +22,132 @@ export interface ClmmPoolState {
|
|
|
22
22
|
address: string;
|
|
23
23
|
token0: string;
|
|
24
24
|
token1: string;
|
|
25
|
-
|
|
26
|
-
feeRatePpm: number;
|
|
25
|
+
poolFee: number;
|
|
27
26
|
tickSpacing: number;
|
|
28
27
|
tick: number;
|
|
29
28
|
sqrtPriceX96: string;
|
|
30
29
|
liquidity: string;
|
|
31
30
|
}
|
|
32
|
-
export interface InfinityPoolState
|
|
31
|
+
export interface InfinityPoolState {
|
|
32
|
+
address: string;
|
|
33
|
+
token0: string;
|
|
34
|
+
token1: string;
|
|
35
|
+
currency0: string;
|
|
36
|
+
currency1: string;
|
|
33
37
|
hooks: string;
|
|
34
38
|
poolManager: string;
|
|
35
39
|
parameters: string;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
tickSpacing: number;
|
|
41
|
+
poolKeyFee: number;
|
|
42
|
+
tick: number;
|
|
43
|
+
sqrtPriceX96: string;
|
|
44
|
+
liquidity: string;
|
|
45
|
+
slot0LpFee?: number;
|
|
46
|
+
slot0ProtocolFee?: number;
|
|
47
|
+
lastSwapFee?: number;
|
|
48
|
+
lastSwapProtocolFee?: number;
|
|
40
49
|
}
|
|
41
50
|
export interface OnchainPoolChangeEvent {
|
|
42
51
|
provider_id?: string;
|
|
43
52
|
pool_address: string;
|
|
44
53
|
type: string;
|
|
45
54
|
event_time: number;
|
|
46
|
-
data:
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
55
|
+
data: OnchainEventDataMerged;
|
|
56
|
+
}
|
|
57
|
+
export interface OnchainEventDataMerged {
|
|
58
|
+
blockNumber: number;
|
|
59
|
+
transactionIndex: number;
|
|
60
|
+
transactionHash: string;
|
|
61
|
+
reserve0?: string;
|
|
62
|
+
reserve1?: string;
|
|
63
|
+
amount0?: string;
|
|
64
|
+
amount1?: string;
|
|
65
|
+
sqrtPriceX96?: string;
|
|
66
|
+
liquidity?: string;
|
|
67
|
+
tick?: number;
|
|
68
|
+
protocolFeesToken0?: string;
|
|
69
|
+
protocolFeesToken1?: string;
|
|
70
|
+
fee?: string;
|
|
71
|
+
protocolFee?: string;
|
|
72
|
+
tickLower?: number;
|
|
73
|
+
tickUpper?: number;
|
|
74
|
+
amount?: string;
|
|
75
|
+
liquidityDelta?: string;
|
|
76
|
+
token0Symbol?: string;
|
|
77
|
+
token1Symbol?: string;
|
|
78
|
+
askToken0InToken1?: string;
|
|
79
|
+
bidToken0InToken1?: string;
|
|
80
|
+
askToken1InToken0?: string;
|
|
81
|
+
bidToken1InToken0?: string;
|
|
82
|
+
}
|
|
83
|
+
export interface V2SwapEventData {
|
|
84
|
+
blockNumber: number;
|
|
85
|
+
transactionIndex: number;
|
|
86
|
+
transactionHash: string;
|
|
87
|
+
reserve0: string;
|
|
88
|
+
reserve1: string;
|
|
89
|
+
amount0?: string;
|
|
90
|
+
amount1?: string;
|
|
91
|
+
}
|
|
92
|
+
export interface V2LiquidityEventData {
|
|
93
|
+
blockNumber: number;
|
|
94
|
+
transactionIndex: number;
|
|
95
|
+
transactionHash: string;
|
|
96
|
+
amount0: string;
|
|
97
|
+
amount1: string;
|
|
98
|
+
}
|
|
99
|
+
export interface V3SwapEventData {
|
|
100
|
+
blockNumber: number;
|
|
101
|
+
transactionIndex: number;
|
|
102
|
+
transactionHash: string;
|
|
103
|
+
amount0: string;
|
|
104
|
+
amount1: string;
|
|
105
|
+
sqrtPriceX96: string;
|
|
106
|
+
liquidity: string;
|
|
107
|
+
tick: number;
|
|
108
|
+
protocolFeesToken0: string;
|
|
109
|
+
protocolFeesToken1: string;
|
|
110
|
+
}
|
|
111
|
+
export interface V3LiquidityEventData {
|
|
112
|
+
blockNumber: number;
|
|
113
|
+
transactionIndex: number;
|
|
114
|
+
transactionHash: string;
|
|
115
|
+
tickLower: number;
|
|
116
|
+
tickUpper: number;
|
|
117
|
+
amount: string;
|
|
118
|
+
amount0: string;
|
|
119
|
+
amount1: string;
|
|
120
|
+
}
|
|
121
|
+
export interface V4SwapEventData {
|
|
122
|
+
blockNumber: number;
|
|
123
|
+
transactionIndex: number;
|
|
124
|
+
transactionHash: string;
|
|
125
|
+
amount0: string;
|
|
126
|
+
amount1: string;
|
|
127
|
+
sqrtPriceX96: string;
|
|
128
|
+
liquidity: string;
|
|
129
|
+
tick: number;
|
|
130
|
+
fee: string;
|
|
131
|
+
protocolFee: string;
|
|
132
|
+
}
|
|
133
|
+
export interface V4LiquidityEventData {
|
|
134
|
+
blockNumber: number;
|
|
135
|
+
transactionIndex: number;
|
|
136
|
+
transactionHash: string;
|
|
137
|
+
tickLower: number;
|
|
138
|
+
tickUpper: number;
|
|
139
|
+
amount: string;
|
|
140
|
+
amount0: string;
|
|
141
|
+
amount1: string;
|
|
142
|
+
}
|
|
143
|
+
export interface PriceFeedEventData {
|
|
144
|
+
blockNumber: number;
|
|
145
|
+
transactionIndex: number;
|
|
146
|
+
transactionHash: string;
|
|
147
|
+
token0Symbol: string;
|
|
148
|
+
token1Symbol: string;
|
|
149
|
+
askToken0InToken1: string;
|
|
150
|
+
bidToken0InToken1: string;
|
|
151
|
+
askToken1InToken0: string;
|
|
152
|
+
bidToken1InToken0: string;
|
|
68
153
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clonegod/ttd-bsc-common",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.70",
|
|
4
4
|
"description": "BSC common library",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"push": "npm run build && npm publish"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@clonegod/ttd-core": "3.1.
|
|
17
|
+
"@clonegod/ttd-core": "3.1.61",
|
|
18
18
|
"axios": "1.15.0",
|
|
19
19
|
"dotenv": "^16.4.7",
|
|
20
20
|
"ethers": "^5.8.0",
|