four-flap-meme-sdk 1.3.49 → 1.3.50
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.
|
@@ -82,14 +82,14 @@ const V2_ROUTER_ABI = [
|
|
|
82
82
|
/**
|
|
83
83
|
* SwapRouter02 的 V2 方法 ABI (PotatoSwap)
|
|
84
84
|
*
|
|
85
|
-
*
|
|
85
|
+
* 重要:SwapRouter02 的 V2 方法只有:
|
|
86
86
|
* - swapExactTokensForTokens(amountIn, amountOutMin, path[], to)
|
|
87
87
|
* - swapTokensForExactTokens(amountOut, amountInMax, path[], to)
|
|
88
88
|
*
|
|
89
89
|
* 没有 swapExactETHForTokens / swapExactTokensForETH!
|
|
90
|
-
*
|
|
91
|
-
* - 买入(ETH
|
|
92
|
-
* - 卖出(Token
|
|
90
|
+
* 需要手动处理 ETH/WETH 转换:
|
|
91
|
+
* - 买入(ETH->Token): wrapETH + swapExactTokensForTokens
|
|
92
|
+
* - 卖出(Token->ETH): swapExactTokensForTokens(to=ADDRESS_THIS) + unwrapWETH9
|
|
93
93
|
*/
|
|
94
94
|
const SWAP_ROUTER02_V2_ABI = [
|
|
95
95
|
// V2 交换方法(只有 token-to-token)
|
|
@@ -97,15 +97,15 @@ const SWAP_ROUTER02_V2_ABI = [
|
|
|
97
97
|
'function swapTokensForExactTokens(uint256 amountOut, uint256 amountInMax, address[] calldata path, address to) external payable returns (uint256 amountIn)',
|
|
98
98
|
// Multicall - 多种重载
|
|
99
99
|
'function multicall(uint256 deadline, bytes[] calldata data) external payable returns (bytes[] memory results)',
|
|
100
|
-
'function multicall(bytes32 previousBlockhash, bytes[] calldata data) external payable returns (bytes[] memory results)',
|
|
101
100
|
'function multicall(bytes[] calldata data) external payable returns (bytes[] memory results)',
|
|
102
101
|
// 辅助方法 - ETH/WETH 转换
|
|
103
102
|
'function wrapETH(uint256 value) external payable',
|
|
104
103
|
'function unwrapWETH9(uint256 amountMinimum, address recipient) external payable',
|
|
105
104
|
'function unwrapWETH9(uint256 amountMinimum) external payable',
|
|
106
105
|
'function refundETH() external payable',
|
|
107
|
-
//
|
|
106
|
+
// pull 方法 - 从用户拉取代币到 Router
|
|
108
107
|
'function pull(address token, uint256 value) external payable',
|
|
108
|
+
// sweepToken - 将 Router 中的代币发送给用户
|
|
109
109
|
'function sweepToken(address token, uint256 amountMinimum, address recipient) external payable',
|
|
110
110
|
'function sweepToken(address token, uint256 amountMinimum) external payable',
|
|
111
111
|
];
|
|
@@ -269,24 +269,20 @@ async function buildProfitTransaction(wallet, profitAmountWei, nonce, gasPrice,
|
|
|
269
269
|
// V2 直接交易
|
|
270
270
|
// ============================================================================
|
|
271
271
|
/**
|
|
272
|
-
* 判断是否是 SwapRouter02 (
|
|
273
|
-
*
|
|
274
|
-
* 只有 XLayer PotatoSwap 使用 SwapRouter02 进行 V2 交易
|
|
275
|
-
* BSC/Monad 的 V2 交易使用传统的 V2 Router(带 deadline 参数)
|
|
276
|
-
*
|
|
277
|
-
* SwapRouter02 的 V2 方法签名不同:
|
|
278
|
-
* - swapExactETHForTokens(amountOutMin, path[], to) - 没有 deadline
|
|
279
|
-
* - swapExactTokensForETH(amountIn, amountOutMin, path[], to) - 没有 deadline
|
|
272
|
+
* 判断是否是 SwapRouter02 (PotatoSwap, PancakeSwap V3 等)
|
|
273
|
+
* SwapRouter02 的 V2 方法签名不同,需要特殊处理
|
|
280
274
|
*/
|
|
281
275
|
function isSwapRouter02(chain, routerAddress) {
|
|
282
276
|
const chainUpper = chain.toUpperCase();
|
|
283
277
|
const addrLower = routerAddress.toLowerCase();
|
|
284
|
-
//
|
|
278
|
+
// XLayer PotatoSwap SwapRouter02
|
|
285
279
|
if (chainUpper === 'XLAYER' && addrLower === DIRECT_ROUTERS.XLAYER.POTATOSWAP_V2.toLowerCase()) {
|
|
286
280
|
return true;
|
|
287
281
|
}
|
|
288
|
-
//
|
|
289
|
-
|
|
282
|
+
// BSC PancakeSwap V3 SwapRouter02
|
|
283
|
+
if (chainUpper === 'BSC' && addrLower === DIRECT_ROUTERS.BSC.PANCAKESWAP_V3.toLowerCase()) {
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
290
286
|
return false;
|
|
291
287
|
}
|
|
292
288
|
/**
|
|
@@ -330,39 +326,42 @@ export async function directV2BatchBuy(params) {
|
|
|
330
326
|
let txData;
|
|
331
327
|
let txValue;
|
|
332
328
|
if (useSwapRouter02) {
|
|
333
|
-
// ✅ SwapRouter02:
|
|
334
|
-
//
|
|
329
|
+
// ✅ SwapRouter02: 只有 swapExactTokensForTokens,需要手动处理 ETH wrap
|
|
330
|
+
// 文档:没有 swapExactETHForTokens / swapExactTokensForETH
|
|
331
|
+
const multicallData = [];
|
|
335
332
|
if (useNative) {
|
|
336
|
-
// ETH ->
|
|
337
|
-
//
|
|
338
|
-
//
|
|
333
|
+
// ETH -> 代币:
|
|
334
|
+
// 1. wrapETH(msg.value) - 将 ETH 转为 WETH
|
|
335
|
+
// 2. swapExactTokensForTokens(WETH -> Token)
|
|
336
|
+
const wrapData = routerIface.encodeFunctionData('wrapETH', [amountWei]);
|
|
337
|
+
multicallData.push(wrapData);
|
|
339
338
|
const swapData = routerIface.encodeFunctionData('swapExactTokensForTokens', [
|
|
340
339
|
amountWei,
|
|
341
340
|
0n, // amountOutMin
|
|
342
341
|
path, // path: [WETH, ..., tokenOut]
|
|
343
342
|
wallet.address, // to
|
|
344
343
|
]);
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
]);
|
|
350
|
-
txValue = amountWei;
|
|
344
|
+
multicallData.push(swapData);
|
|
345
|
+
// refundETH - 退回多余的 ETH(如果有)
|
|
346
|
+
const refundData = routerIface.encodeFunctionData('refundETH', []);
|
|
347
|
+
multicallData.push(refundData);
|
|
351
348
|
}
|
|
352
349
|
else {
|
|
353
|
-
// 代币 ->
|
|
350
|
+
// 代币 -> 代币:直接 swapExactTokensForTokens
|
|
354
351
|
const swapData = routerIface.encodeFunctionData('swapExactTokensForTokens', [
|
|
355
352
|
amountWei,
|
|
356
353
|
0n, // amountOutMin
|
|
357
354
|
path,
|
|
358
355
|
wallet.address, // to
|
|
359
356
|
]);
|
|
360
|
-
|
|
361
|
-
deadline,
|
|
362
|
-
[swapData],
|
|
363
|
-
]);
|
|
364
|
-
txValue = 0n;
|
|
357
|
+
multicallData.push(swapData);
|
|
365
358
|
}
|
|
359
|
+
// 使用 multicall 包装,传递 deadline
|
|
360
|
+
txData = routerIface.encodeFunctionData('multicall(uint256,bytes[])', [
|
|
361
|
+
deadline,
|
|
362
|
+
multicallData,
|
|
363
|
+
]);
|
|
364
|
+
txValue = useNative ? amountWei : 0n;
|
|
366
365
|
}
|
|
367
366
|
else if (useNative) {
|
|
368
367
|
// 传统 V2 Router: 原生币 → Token
|
|
@@ -489,49 +488,46 @@ export async function directV2BatchSell(params) {
|
|
|
489
488
|
// 卖出交易
|
|
490
489
|
let txData;
|
|
491
490
|
if (useSwapRouter02) {
|
|
492
|
-
// ✅ SwapRouter02:
|
|
493
|
-
|
|
491
|
+
// ✅ SwapRouter02: 只有 swapExactTokensForTokens,需要手动处理 ETH unwrap
|
|
492
|
+
const router02Iface = new ethers.Interface(SWAP_ROUTER02_V2_ABI);
|
|
493
|
+
const multicallData = [];
|
|
494
|
+
// SwapRouter02 特殊地址约定:
|
|
495
|
+
// address(1) = ADDRESS_THIS = Router 合约自己
|
|
496
|
+
// address(2) = MSG_SENDER = 调用者
|
|
497
|
+
const ADDRESS_THIS = '0x0000000000000000000000000000000000000001'; // Router 自己
|
|
494
498
|
if (useNativeOutput) {
|
|
495
|
-
// 代币 -> ETH:
|
|
496
|
-
//
|
|
497
|
-
//
|
|
498
|
-
|
|
499
|
-
// address(2) = ADDRESS_THIS = Router 合约自己
|
|
500
|
-
const ADDRESS_THIS = '0x0000000000000000000000000000000000000002';
|
|
501
|
-
const multicallData = [];
|
|
502
|
-
// 1. swapExactTokensForTokens - Token -> WETH,发送到 Router 自己
|
|
503
|
-
const swapData = routerIface.encodeFunctionData('swapExactTokensForTokens', [
|
|
499
|
+
// 代币 -> ETH:
|
|
500
|
+
// 1. swapExactTokensForTokens(token -> WETH, to = ADDRESS_THIS)
|
|
501
|
+
// 2. unwrapWETH9(WETH -> ETH, to = 用户)
|
|
502
|
+
const swapData = router02Iface.encodeFunctionData('swapExactTokensForTokens', [
|
|
504
503
|
sellAmount,
|
|
505
504
|
0n, // amountOutMin
|
|
506
505
|
path, // path: [token, ..., WETH]
|
|
507
|
-
ADDRESS_THIS, // to =
|
|
506
|
+
ADDRESS_THIS, // to = Router 自己,WETH 留在 Router 中
|
|
508
507
|
]);
|
|
509
508
|
multicallData.push(swapData);
|
|
510
|
-
//
|
|
511
|
-
const unwrapData =
|
|
509
|
+
// unwrap WETH -> ETH 发送给用户
|
|
510
|
+
const unwrapData = router02Iface.encodeFunctionData('unwrapWETH9(uint256,address)', [
|
|
512
511
|
0n, // amountMinimum
|
|
513
512
|
wallet.address, // recipient = 用户
|
|
514
513
|
]);
|
|
515
514
|
multicallData.push(unwrapData);
|
|
516
|
-
// 使用 multicall(uint256 deadline, bytes[]) - 带 deadline 的版本
|
|
517
|
-
txData = routerIface.encodeFunctionData('multicall(uint256,bytes[])', [
|
|
518
|
-
deadline,
|
|
519
|
-
multicallData,
|
|
520
|
-
]);
|
|
521
515
|
}
|
|
522
516
|
else {
|
|
523
|
-
// 代币 ->
|
|
524
|
-
const swapData =
|
|
517
|
+
// 代币 -> 代币:直接 swapExactTokensForTokens
|
|
518
|
+
const swapData = router02Iface.encodeFunctionData('swapExactTokensForTokens', [
|
|
525
519
|
sellAmount,
|
|
526
520
|
0n, // amountOutMin
|
|
527
521
|
path,
|
|
528
522
|
wallet.address, // to
|
|
529
523
|
]);
|
|
530
|
-
|
|
531
|
-
deadline,
|
|
532
|
-
[swapData],
|
|
533
|
-
]);
|
|
524
|
+
multicallData.push(swapData);
|
|
534
525
|
}
|
|
526
|
+
// 使用 multicall 包装,传递 deadline
|
|
527
|
+
txData = router02Iface.encodeFunctionData('multicall(uint256,bytes[])', [
|
|
528
|
+
deadline,
|
|
529
|
+
multicallData,
|
|
530
|
+
]);
|
|
535
531
|
}
|
|
536
532
|
else if (useNativeOutput) {
|
|
537
533
|
txData = routerIface.encodeFunctionData('swapExactTokensForETHSupportingFeeOnTransferTokens', [
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ECDH + AES-GCM 加密工具(浏览器兼容)
|
|
3
|
+
* 用于将签名交易用服务器公钥加密
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 用服务器公钥加密签名交易(ECDH + AES-GCM)
|
|
7
|
+
*
|
|
8
|
+
* @param signedTransactions 签名后的交易数组
|
|
9
|
+
* @param publicKeyBase64 服务器提供的公钥(Base64 格式)
|
|
10
|
+
* @returns JSON 字符串 {e: 临时公钥, i: IV, d: 密文}
|
|
11
|
+
*/
|
|
12
|
+
export declare function encryptWithPublicKey(signedTransactions: string[], publicKeyBase64: string): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* 验证公钥格式(Base64)
|
|
15
|
+
*/
|
|
16
|
+
export declare function validatePublicKey(publicKeyBase64: string): boolean;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ECDH + AES-GCM 加密工具(浏览器兼容)
|
|
3
|
+
* 用于将签名交易用服务器公钥加密
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 获取全局 crypto 对象(最简单直接的方式)
|
|
7
|
+
*/
|
|
8
|
+
function getCryptoAPI() {
|
|
9
|
+
// 尝试所有可能的全局对象,优先浏览器环境
|
|
10
|
+
const cryptoObj = (typeof window !== 'undefined' && window.crypto) ||
|
|
11
|
+
(typeof self !== 'undefined' && self.crypto) ||
|
|
12
|
+
(typeof global !== 'undefined' && global.crypto) ||
|
|
13
|
+
(typeof globalThis !== 'undefined' && globalThis.crypto);
|
|
14
|
+
if (!cryptoObj) {
|
|
15
|
+
const env = typeof window !== 'undefined' ? 'Browser' : 'Node.js';
|
|
16
|
+
const protocol = typeof location !== 'undefined' ? location.protocol : 'unknown';
|
|
17
|
+
throw new Error(`❌ Crypto API 不可用。环境: ${env}, 协议: ${protocol}. ` +
|
|
18
|
+
'请确保在 HTTPS 或 localhost 下运行');
|
|
19
|
+
}
|
|
20
|
+
return cryptoObj;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 获取 SubtleCrypto(用于加密操作)
|
|
24
|
+
*/
|
|
25
|
+
function getSubtleCrypto() {
|
|
26
|
+
const crypto = getCryptoAPI();
|
|
27
|
+
if (!crypto.subtle) {
|
|
28
|
+
const protocol = typeof location !== 'undefined' ? location.protocol : 'unknown';
|
|
29
|
+
const hostname = typeof location !== 'undefined' ? location.hostname : 'unknown';
|
|
30
|
+
throw new Error(`❌ SubtleCrypto API 不可用。协议: ${protocol}, 主机: ${hostname}. ` +
|
|
31
|
+
'请确保:1) 使用 HTTPS (或 localhost);2) 浏览器支持 Web Crypto API;' +
|
|
32
|
+
'3) 不在无痕/隐私浏览模式下');
|
|
33
|
+
}
|
|
34
|
+
return crypto.subtle;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Base64 转 ArrayBuffer(优先使用浏览器 API)
|
|
38
|
+
*/
|
|
39
|
+
function base64ToArrayBuffer(base64) {
|
|
40
|
+
// 浏览器环境(优先)
|
|
41
|
+
if (typeof atob !== 'undefined') {
|
|
42
|
+
const binaryString = atob(base64);
|
|
43
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
44
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
45
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
46
|
+
}
|
|
47
|
+
return bytes.buffer;
|
|
48
|
+
}
|
|
49
|
+
// Node.js 环境(fallback)
|
|
50
|
+
if (typeof Buffer !== 'undefined') {
|
|
51
|
+
return Buffer.from(base64, 'base64').buffer;
|
|
52
|
+
}
|
|
53
|
+
throw new Error('❌ Base64 解码不可用');
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* ArrayBuffer 转 Base64(优先使用浏览器 API)
|
|
57
|
+
*/
|
|
58
|
+
function arrayBufferToBase64(buffer) {
|
|
59
|
+
// 浏览器环境(优先)
|
|
60
|
+
if (typeof btoa !== 'undefined') {
|
|
61
|
+
const bytes = new Uint8Array(buffer);
|
|
62
|
+
let binary = '';
|
|
63
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
64
|
+
binary += String.fromCharCode(bytes[i]);
|
|
65
|
+
}
|
|
66
|
+
return btoa(binary);
|
|
67
|
+
}
|
|
68
|
+
// Node.js 环境(fallback)
|
|
69
|
+
if (typeof Buffer !== 'undefined') {
|
|
70
|
+
return Buffer.from(buffer).toString('base64');
|
|
71
|
+
}
|
|
72
|
+
throw new Error('❌ Base64 编码不可用');
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 生成随机 Hex 字符串
|
|
76
|
+
*/
|
|
77
|
+
function randomHex(length) {
|
|
78
|
+
const crypto = getCryptoAPI();
|
|
79
|
+
const array = new Uint8Array(length);
|
|
80
|
+
crypto.getRandomValues(array);
|
|
81
|
+
return Array.from(array)
|
|
82
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
83
|
+
.join('');
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 用服务器公钥加密签名交易(ECDH + AES-GCM)
|
|
87
|
+
*
|
|
88
|
+
* @param signedTransactions 签名后的交易数组
|
|
89
|
+
* @param publicKeyBase64 服务器提供的公钥(Base64 格式)
|
|
90
|
+
* @returns JSON 字符串 {e: 临时公钥, i: IV, d: 密文}
|
|
91
|
+
*/
|
|
92
|
+
export async function encryptWithPublicKey(signedTransactions, publicKeyBase64) {
|
|
93
|
+
try {
|
|
94
|
+
// 0. 获取 SubtleCrypto 和 Crypto API
|
|
95
|
+
const subtle = getSubtleCrypto();
|
|
96
|
+
const crypto = getCryptoAPI();
|
|
97
|
+
// 1. 准备数据
|
|
98
|
+
const payload = {
|
|
99
|
+
signedTransactions,
|
|
100
|
+
timestamp: Date.now(),
|
|
101
|
+
nonce: randomHex(8)
|
|
102
|
+
};
|
|
103
|
+
const plaintext = JSON.stringify(payload);
|
|
104
|
+
// 2. 生成临时 ECDH 密钥对
|
|
105
|
+
const ephemeralKeyPair = await subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveKey']);
|
|
106
|
+
// 3. 导入服务器公钥
|
|
107
|
+
const publicKeyBuffer = base64ToArrayBuffer(publicKeyBase64);
|
|
108
|
+
const publicKey = await subtle.importKey('raw', publicKeyBuffer, { name: 'ECDH', namedCurve: 'P-256' }, false, []);
|
|
109
|
+
// 4. 派生共享密钥(AES-256)
|
|
110
|
+
const sharedKey = await subtle.deriveKey({ name: 'ECDH', public: publicKey }, ephemeralKeyPair.privateKey, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);
|
|
111
|
+
// 5. AES-GCM 加密
|
|
112
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
113
|
+
const encrypted = await subtle.encrypt({ name: 'AES-GCM', iv }, sharedKey, new TextEncoder().encode(plaintext));
|
|
114
|
+
// 6. 导出临时公钥
|
|
115
|
+
const ephemeralPublicKeyRaw = await subtle.exportKey('raw', ephemeralKeyPair.publicKey);
|
|
116
|
+
// 7. 返回加密包(JSON 格式)
|
|
117
|
+
return JSON.stringify({
|
|
118
|
+
e: arrayBufferToBase64(ephemeralPublicKeyRaw), // 临时公钥
|
|
119
|
+
i: arrayBufferToBase64(iv.buffer), // IV
|
|
120
|
+
d: arrayBufferToBase64(encrypted) // 密文
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
throw new Error(`加密失败: ${error?.message || String(error)}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 验证公钥格式(Base64)
|
|
129
|
+
*/
|
|
130
|
+
export function validatePublicKey(publicKeyBase64) {
|
|
131
|
+
try {
|
|
132
|
+
if (!publicKeyBase64)
|
|
133
|
+
return false;
|
|
134
|
+
// Base64 字符集验证
|
|
135
|
+
if (!/^[A-Za-z0-9+/=]+$/.test(publicKeyBase64))
|
|
136
|
+
return false;
|
|
137
|
+
// ECDH P-256 公钥固定长度 65 字节(未压缩)
|
|
138
|
+
// Base64 编码后约 88 字符
|
|
139
|
+
if (publicKeyBase64.length < 80 || publicKeyBase64.length > 100)
|
|
140
|
+
return false;
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -29,6 +29,8 @@ export interface PoolPairInfo {
|
|
|
29
29
|
reserveToken: string;
|
|
30
30
|
reserveQuote: string;
|
|
31
31
|
reserveQuoteRaw?: bigint;
|
|
32
|
+
reserveTokenRaw?: bigint;
|
|
33
|
+
poolRatio?: string;
|
|
32
34
|
}
|
|
33
35
|
/** V3 池子信息(带 fee) */
|
|
34
36
|
export interface V3PoolInfo extends PoolPairInfo {
|
|
@@ -53,6 +55,9 @@ export interface BestPoolInfo {
|
|
|
53
55
|
pairAddress: string;
|
|
54
56
|
liquidity: string;
|
|
55
57
|
liquidityRaw: bigint;
|
|
58
|
+
reserveToken?: string;
|
|
59
|
+
reserveTokenRaw?: bigint;
|
|
60
|
+
poolRatio?: string;
|
|
56
61
|
}
|
|
57
62
|
/** LP 平台类型 */
|
|
58
63
|
export type LPPlatform = 'FOUR' | 'FLAP' | 'PANCAKESWAP_V2' | 'PANCAKESWAP_V3' | 'UNISWAP_V2' | 'UNISWAP_V3' | 'MULTI_DEX' | 'UNKNOWN';
|
|
@@ -60,6 +65,8 @@ export type LPPlatform = 'FOUR' | 'FLAP' | 'PANCAKESWAP_V2' | 'PANCAKESWAP_V3' |
|
|
|
60
65
|
export interface LPInfo {
|
|
61
66
|
platform: LPPlatform;
|
|
62
67
|
decimals?: number;
|
|
68
|
+
totalSupply?: string;
|
|
69
|
+
totalSupplyRaw?: bigint;
|
|
63
70
|
four?: {
|
|
64
71
|
helper: string;
|
|
65
72
|
reserveNative?: string;
|
package/dist/utils/lp-inspect.js
CHANGED
|
@@ -222,11 +222,25 @@ export async function inspectTokenLP(token, opts) {
|
|
|
222
222
|
if (opts.customDexConfig) {
|
|
223
223
|
chainConfig = { ...chainConfig, ...opts.customDexConfig };
|
|
224
224
|
}
|
|
225
|
-
//
|
|
225
|
+
// 查询代币精度和总供应量
|
|
226
226
|
let tokenDecimals;
|
|
227
|
+
let totalSupplyRaw;
|
|
227
228
|
try {
|
|
228
|
-
|
|
229
|
+
const tokenContract = new Contract(token, [
|
|
230
|
+
'function decimals() view returns (uint8)',
|
|
231
|
+
'function totalSupply() view returns (uint256)'
|
|
232
|
+
], provider);
|
|
233
|
+
const [decimals, supply] = await Promise.all([
|
|
234
|
+
tokenContract.decimals().catch(() => 18),
|
|
235
|
+
tokenContract.totalSupply().catch(() => undefined)
|
|
236
|
+
]);
|
|
237
|
+
tokenDecimals = Number(decimals);
|
|
238
|
+
totalSupplyRaw = supply ? BigInt(supply) : undefined;
|
|
229
239
|
result.decimals = tokenDecimals;
|
|
240
|
+
if (totalSupplyRaw !== undefined) {
|
|
241
|
+
result.totalSupplyRaw = totalSupplyRaw;
|
|
242
|
+
result.totalSupply = formatBalance(totalSupplyRaw, shouldFormat, tokenDecimals);
|
|
243
|
+
}
|
|
230
244
|
}
|
|
231
245
|
catch {
|
|
232
246
|
tokenDecimals = 18;
|
|
@@ -328,6 +342,8 @@ export async function inspectTokenLP(token, opts) {
|
|
|
328
342
|
pairAddress: pair.pairAddress,
|
|
329
343
|
liquidity: pair.reserveQuote,
|
|
330
344
|
liquidityRaw: normalizeLiquidity(pair.reserveQuoteRaw, pair.quoteDecimals),
|
|
345
|
+
reserveToken: pair.reserveToken, // ✅ 新增
|
|
346
|
+
reserveTokenRaw: pair.reserveTokenRaw, // ✅ 新增
|
|
331
347
|
});
|
|
332
348
|
}
|
|
333
349
|
}
|
|
@@ -356,6 +372,8 @@ export async function inspectTokenLP(token, opts) {
|
|
|
356
372
|
pairAddress: pool.pairAddress,
|
|
357
373
|
liquidity: pool.reserveQuote,
|
|
358
374
|
liquidityRaw: normalizeLiquidity(pool.reserveQuoteRaw, pool.quoteDecimals),
|
|
375
|
+
reserveToken: pool.reserveToken, // ✅ 新增
|
|
376
|
+
reserveTokenRaw: pool.reserveTokenRaw, // ✅ 新增
|
|
359
377
|
});
|
|
360
378
|
}
|
|
361
379
|
}
|
|
@@ -371,7 +389,7 @@ export async function inspectTokenLP(token, opts) {
|
|
|
371
389
|
const dexResults = await Promise.all(dexPromises);
|
|
372
390
|
// 过滤出有流动性的 DEX
|
|
373
391
|
result.allPools = dexResults.filter(dex => dex.v2Pairs.length > 0 || dex.v3Pools.length > 0);
|
|
374
|
-
//
|
|
392
|
+
// 按流动性排序最佳池子,并计算 poolRatio
|
|
375
393
|
result.bestPools = allBestPoolCandidates
|
|
376
394
|
.sort((a, b) => {
|
|
377
395
|
// 降序排列
|
|
@@ -381,7 +399,37 @@ export async function inspectTokenLP(token, opts) {
|
|
|
381
399
|
return -1;
|
|
382
400
|
return 0;
|
|
383
401
|
})
|
|
384
|
-
.slice(0, 10)
|
|
402
|
+
.slice(0, 10) // 最多返回 10 个
|
|
403
|
+
.map(pool => {
|
|
404
|
+
// ✅ 计算池子代币占比
|
|
405
|
+
if (pool.reserveTokenRaw && totalSupplyRaw && totalSupplyRaw > 0n) {
|
|
406
|
+
// 计算百分比:(reserveTokenRaw / totalSupplyRaw) * 100
|
|
407
|
+
// 为了保持精度,先乘以 10000,再除以 totalSupply,得到万分比
|
|
408
|
+
const ratioBps = (pool.reserveTokenRaw * 10000n) / totalSupplyRaw;
|
|
409
|
+
const ratioPercent = Number(ratioBps) / 100; // 转为百分比
|
|
410
|
+
pool.poolRatio = `${ratioPercent.toFixed(2)}%`;
|
|
411
|
+
}
|
|
412
|
+
return pool;
|
|
413
|
+
});
|
|
414
|
+
// ✅ 同时为 allPools 中的每个池子计算 poolRatio
|
|
415
|
+
if (totalSupplyRaw && totalSupplyRaw > 0n) {
|
|
416
|
+
for (const dexPool of result.allPools) {
|
|
417
|
+
for (const pair of dexPool.v2Pairs) {
|
|
418
|
+
if (pair.reserveTokenRaw) {
|
|
419
|
+
const ratioBps = (pair.reserveTokenRaw * 10000n) / totalSupplyRaw;
|
|
420
|
+
const ratioPercent = Number(ratioBps) / 100;
|
|
421
|
+
pair.poolRatio = `${ratioPercent.toFixed(2)}%`;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
for (const pool of dexPool.v3Pools) {
|
|
425
|
+
if (pool.reserveTokenRaw) {
|
|
426
|
+
const ratioBps = (pool.reserveTokenRaw * 10000n) / totalSupplyRaw;
|
|
427
|
+
const ratioPercent = Number(ratioBps) / 100;
|
|
428
|
+
pool.poolRatio = `${ratioPercent.toFixed(2)}%`;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
385
433
|
// 确定平台类型
|
|
386
434
|
result.platform = determinePlatform(result.allPools);
|
|
387
435
|
return result;
|
|
@@ -450,6 +498,7 @@ async function queryV2Pairs(token, factoryAddress, quoteTokens, provider, multic
|
|
|
450
498
|
reserveToken: formatBalance(reserveToken, shouldFormat, tokenDecimals),
|
|
451
499
|
reserveQuote: formatBalance(reserveQuote, shouldFormat, p.quoteToken.decimals),
|
|
452
500
|
reserveQuoteRaw: reserveQuote,
|
|
501
|
+
reserveTokenRaw: reserveToken, // ✅ 新增
|
|
453
502
|
});
|
|
454
503
|
}
|
|
455
504
|
catch (err) {
|
|
@@ -487,6 +536,7 @@ async function queryV2PairsFallback(token, factoryAddress, quoteTokens, provider
|
|
|
487
536
|
reserveToken: formatBalance(reserveToken, shouldFormat, tokenDecimals),
|
|
488
537
|
reserveQuote: formatBalance(reserveQuote, shouldFormat, qt.decimals),
|
|
489
538
|
reserveQuoteRaw: reserveQuote,
|
|
539
|
+
reserveTokenRaw: reserveToken, // ✅ 新增
|
|
490
540
|
});
|
|
491
541
|
}
|
|
492
542
|
}
|
|
@@ -572,6 +622,7 @@ async function queryV3Pools(token, factoryAddress, quoteTokens, feeTiers, provid
|
|
|
572
622
|
reserveToken: formatBalance(tokenBalance, shouldFormat, tokenDecimals),
|
|
573
623
|
reserveQuote: formatBalance(quoteBalance, shouldFormat, p.quoteToken.decimals),
|
|
574
624
|
reserveQuoteRaw: quoteBalance,
|
|
625
|
+
reserveTokenRaw: tokenBalance, // ✅ 新增
|
|
575
626
|
fee: p.fee,
|
|
576
627
|
});
|
|
577
628
|
}
|
|
@@ -616,6 +667,7 @@ async function queryV3PoolsFallback(token, factoryAddress, quoteTokens, feeTiers
|
|
|
616
667
|
reserveToken: formatBalance(tokenBalance, shouldFormat, tokenDecimals),
|
|
617
668
|
reserveQuote: formatBalance(quoteBalance, shouldFormat, qt.decimals),
|
|
618
669
|
reserveQuoteRaw: quoteBalance,
|
|
670
|
+
reserveTokenRaw: tokenBalance, // ✅ 新增
|
|
619
671
|
fee,
|
|
620
672
|
});
|
|
621
673
|
}
|