four-flap-meme-sdk 1.4.90 → 1.4.91
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.
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { type FlapSignConfig } from './config.js';
|
|
11
11
|
import type { MerkleSignedResult } from './types.js';
|
|
12
|
-
export type CreateToDexChain = 'bsc';
|
|
12
|
+
export type CreateToDexChain = 'bsc' | 'xlayer';
|
|
13
13
|
export type DexPoolType = 'v2' | 'v3';
|
|
14
14
|
/** 签名配置 */
|
|
15
15
|
export interface CreateToDexSignConfig extends FlapSignConfig {
|
|
@@ -26,9 +26,39 @@ const ERC20_ABI = [
|
|
|
26
26
|
}
|
|
27
27
|
];
|
|
28
28
|
// ==================== 链常量 ====================
|
|
29
|
+
// BSC
|
|
29
30
|
const BSC_PANCAKE_V2_ROUTER = ADDRESSES.BSC.PancakeV2Router;
|
|
30
31
|
const BSC_PANCAKE_V3_ROUTER = ADDRESSES.BSC.PancakeV3Router;
|
|
31
32
|
const BSC_WBNB = ADDRESSES.BSC.WBNB;
|
|
33
|
+
// XLAYER
|
|
34
|
+
const XLAYER_POTATOSWAP_V2_ROUTER = '0x881fb2f98c13d521009464e7d1cbf16e1b394e8e';
|
|
35
|
+
const XLAYER_POTATOSWAP_V3_ROUTER = '0xB45D0149249488333E3F3f9F359807F4b810C1FC';
|
|
36
|
+
const XLAYER_WOKB = '0xe538905cf8410324e03a5a23c1c177a474d59b2b';
|
|
37
|
+
// 链 ID 映射
|
|
38
|
+
const CHAIN_ID_MAP = {
|
|
39
|
+
bsc: 56,
|
|
40
|
+
xlayer: 196,
|
|
41
|
+
};
|
|
42
|
+
// 获取链配置
|
|
43
|
+
function getChainConfig(chain) {
|
|
44
|
+
if (chain === 'xlayer') {
|
|
45
|
+
return {
|
|
46
|
+
chainId: 196,
|
|
47
|
+
v2Router: XLAYER_POTATOSWAP_V2_ROUTER,
|
|
48
|
+
v3Router: XLAYER_POTATOSWAP_V3_ROUTER,
|
|
49
|
+
wrappedNative: XLAYER_WOKB,
|
|
50
|
+
nativeSymbol: 'OKB',
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// 默认 BSC
|
|
54
|
+
return {
|
|
55
|
+
chainId: 56,
|
|
56
|
+
v2Router: BSC_PANCAKE_V2_ROUTER,
|
|
57
|
+
v3Router: BSC_PANCAKE_V3_ROUTER,
|
|
58
|
+
wrappedNative: BSC_WBNB,
|
|
59
|
+
nativeSymbol: 'BNB',
|
|
60
|
+
};
|
|
61
|
+
}
|
|
32
62
|
// PancakeSwap Router ABI
|
|
33
63
|
const PANCAKE_V3_ROUTER_ABI = [
|
|
34
64
|
{
|
|
@@ -78,7 +108,8 @@ const USD1_V3_FEE = 100;
|
|
|
78
108
|
const DEFAULT_V3_FEE = 2500;
|
|
79
109
|
// ==================== 工具函数 ====================
|
|
80
110
|
/** 构建 ERC20 approve 交易 */
|
|
81
|
-
async function buildApproveTransaction(wallet, tokenAddress, spenderAddress, amount, nonce, gasPrice, txType
|
|
111
|
+
async function buildApproveTransaction(wallet, tokenAddress, spenderAddress, amount, nonce, gasPrice, txType, chainId = 56 // ✅ 添加 chainId 参数
|
|
112
|
+
) {
|
|
82
113
|
const erc20Interface = new ethers.Interface(ERC20_ABI);
|
|
83
114
|
const data = erc20Interface.encodeFunctionData('approve', [spenderAddress, amount]);
|
|
84
115
|
const tx = {
|
|
@@ -87,7 +118,7 @@ async function buildApproveTransaction(wallet, tokenAddress, spenderAddress, amo
|
|
|
87
118
|
from: wallet.address,
|
|
88
119
|
nonce,
|
|
89
120
|
gasLimit: BigInt(100000), // approve 交易 gas 较低
|
|
90
|
-
chainId
|
|
121
|
+
chainId,
|
|
91
122
|
type: txType
|
|
92
123
|
};
|
|
93
124
|
if (txType === 2) {
|
|
@@ -154,9 +185,12 @@ export async function flapBundleCreateToDex(params) {
|
|
|
154
185
|
// 判断是否使用原生代币
|
|
155
186
|
const useNativeToken = !quoteToken || quoteToken === ZERO_ADDRESS;
|
|
156
187
|
const inputToken = useNativeToken ? ZERO_ADDRESS : quoteToken;
|
|
188
|
+
// ✅ 获取链配置
|
|
189
|
+
const chainConfig = getChainConfig(chain);
|
|
190
|
+
const chainId = chainConfig.chainId;
|
|
157
191
|
// 创建 Provider
|
|
158
192
|
const provider = new ethers.JsonRpcProvider(config.rpcUrl, {
|
|
159
|
-
chainId
|
|
193
|
+
chainId,
|
|
160
194
|
name: chain.toUpperCase()
|
|
161
195
|
});
|
|
162
196
|
const portalAddress = FLAP_PORTAL_ADDRESSES[chain.toUpperCase()];
|
|
@@ -228,7 +262,7 @@ export async function flapBundleCreateToDex(params) {
|
|
|
228
262
|
nonce: bribeNonce,
|
|
229
263
|
gasPrice,
|
|
230
264
|
gasLimit: 21000n,
|
|
231
|
-
chainId
|
|
265
|
+
chainId,
|
|
232
266
|
type: txType
|
|
233
267
|
});
|
|
234
268
|
allTransactions.push(bribeTx);
|
|
@@ -305,7 +339,7 @@ export async function flapBundleCreateToDex(params) {
|
|
|
305
339
|
from: devWallet.address,
|
|
306
340
|
nonce: devNonce,
|
|
307
341
|
gasLimit: finalGasLimit,
|
|
308
|
-
chainId
|
|
342
|
+
chainId,
|
|
309
343
|
type: txType
|
|
310
344
|
};
|
|
311
345
|
if (txType === 2) {
|
|
@@ -328,7 +362,8 @@ export async function flapBundleCreateToDex(params) {
|
|
|
328
362
|
if (!useNativeToken) {
|
|
329
363
|
const approveNonce = noncesMap.get(addr);
|
|
330
364
|
noncesMap.set(addr, approveNonce + 1);
|
|
331
|
-
const approveTx = await buildApproveTransaction(wallet, quoteToken, portalAddress, curveBuyAmounts[i], approveNonce, gasPrice, txType
|
|
365
|
+
const approveTx = await buildApproveTransaction(wallet, quoteToken, portalAddress, curveBuyAmounts[i], approveNonce, gasPrice, txType, chainId // ✅ 传递 chainId
|
|
366
|
+
);
|
|
332
367
|
signedTxs.push(approveTx);
|
|
333
368
|
}
|
|
334
369
|
// 构建买入交易
|
|
@@ -350,7 +385,7 @@ export async function flapBundleCreateToDex(params) {
|
|
|
350
385
|
from: wallet.address,
|
|
351
386
|
nonce: buyNonce,
|
|
352
387
|
gasLimit: buyGasLimit,
|
|
353
|
-
chainId
|
|
388
|
+
chainId,
|
|
354
389
|
type: txType,
|
|
355
390
|
value: useNativeToken ? curveBuyAmounts[i] : 0n
|
|
356
391
|
};
|
|
@@ -375,14 +410,17 @@ export async function flapBundleCreateToDex(params) {
|
|
|
375
410
|
const addr = wallet.address.toLowerCase();
|
|
376
411
|
const signedTxs = [];
|
|
377
412
|
const buyAmount = dexBuyAmounts[i];
|
|
378
|
-
|
|
413
|
+
// ✅ 使用动态 Router 地址
|
|
414
|
+
const smartRouter = dexPoolType === 'v3' ? chainConfig.v3Router : chainConfig.v2Router;
|
|
415
|
+
const wrappedNative = chainConfig.wrappedNative;
|
|
379
416
|
// ✅ 根据 quoteToken 选择 V3 费率
|
|
380
417
|
const actualV3Fee = v3Fee || (useNativeToken ? DEFAULT_V3_FEE : USD1_V3_FEE);
|
|
381
418
|
// ✅ ERC20: 先构建 approve 交易
|
|
382
419
|
if (!useNativeToken) {
|
|
383
420
|
const approveNonce = noncesMap.get(addr);
|
|
384
421
|
noncesMap.set(addr, approveNonce + 1);
|
|
385
|
-
const approveTx = await buildApproveTransaction(wallet, quoteToken, smartRouter, buyAmount, approveNonce, gasPrice, txType
|
|
422
|
+
const approveTx = await buildApproveTransaction(wallet, quoteToken, smartRouter, buyAmount, approveNonce, gasPrice, txType, chainId // ✅ 传递 chainId
|
|
423
|
+
);
|
|
386
424
|
signedTxs.push(approveTx);
|
|
387
425
|
}
|
|
388
426
|
// 构建买入交易
|
|
@@ -394,7 +432,7 @@ export async function flapBundleCreateToDex(params) {
|
|
|
394
432
|
if (dexPoolType === 'v3') {
|
|
395
433
|
// V3: exactInputSingle
|
|
396
434
|
const params = {
|
|
397
|
-
tokenIn: useNativeToken ?
|
|
435
|
+
tokenIn: useNativeToken ? wrappedNative : quoteToken,
|
|
398
436
|
tokenOut: tokenAddress,
|
|
399
437
|
fee: actualV3Fee,
|
|
400
438
|
recipient: wallet.address,
|
|
@@ -407,7 +445,7 @@ export async function flapBundleCreateToDex(params) {
|
|
|
407
445
|
}
|
|
408
446
|
else {
|
|
409
447
|
// V2: swapExactTokensForTokens
|
|
410
|
-
const path = useNativeToken ? [
|
|
448
|
+
const path = useNativeToken ? [wrappedNative, tokenAddress] : [quoteToken, tokenAddress];
|
|
411
449
|
const swapData = routerInterface.encodeFunctionData('swapExactTokensForTokens', [buyAmount, 0n, path, wallet.address]);
|
|
412
450
|
multicallData = [swapData];
|
|
413
451
|
value = useNativeToken ? buyAmount : 0n;
|
|
@@ -419,7 +457,7 @@ export async function flapBundleCreateToDex(params) {
|
|
|
419
457
|
from: wallet.address,
|
|
420
458
|
nonce: buyNonce,
|
|
421
459
|
gasLimit: BigInt(500000),
|
|
422
|
-
chainId
|
|
460
|
+
chainId,
|
|
423
461
|
type: txType,
|
|
424
462
|
value
|
|
425
463
|
};
|
|
@@ -450,7 +488,7 @@ export async function flapBundleCreateToDex(params) {
|
|
|
450
488
|
profitRecipient: getProfitRecipient(),
|
|
451
489
|
hopCount: PROFIT_HOP_COUNT,
|
|
452
490
|
gasPrice,
|
|
453
|
-
chainId
|
|
491
|
+
chainId,
|
|
454
492
|
txType,
|
|
455
493
|
startNonce: profitNonce
|
|
456
494
|
});
|