four-flap-meme-sdk 1.3.94 → 1.3.95
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/clients/blockrazor.js +0 -1
- package/dist/contracts/tm-bundle-merkle/config.d.ts +5 -0
- package/dist/contracts/tm-bundle-merkle/config.js +10 -0
- package/dist/contracts/tm-bundle-merkle/core.js +92 -24
- package/dist/contracts/tm-bundle-merkle/internal.d.ts +1 -1
- package/dist/contracts/tm-bundle-merkle/internal.js +4 -3
- package/dist/contracts/tm-bundle-merkle/pancake-proxy.js +103 -54
- package/dist/contracts/tm-bundle-merkle/swap-buy-first.d.ts +0 -1
- package/dist/contracts/tm-bundle-merkle/swap-buy-first.js +36 -6
- package/dist/contracts/tm-bundle-merkle/swap.d.ts +0 -3
- package/dist/contracts/tm-bundle-merkle/swap.js +59 -6
- package/dist/contracts/tm-bundle-merkle/utils.js +7 -7
- package/dist/flap/portal-bundle-merkle/config.d.ts +8 -0
- package/dist/flap/portal-bundle-merkle/config.js +17 -0
- package/dist/flap/portal-bundle-merkle/core.js +120 -68
- package/dist/flap/portal-bundle-merkle/encryption.d.ts +16 -0
- package/dist/flap/portal-bundle-merkle/encryption.js +146 -0
- package/dist/flap/portal-bundle-merkle/pancake-proxy.js +136 -78
- package/dist/flap/portal-bundle-merkle/swap-buy-first.d.ts +0 -2
- package/dist/flap/portal-bundle-merkle/swap-buy-first.js +49 -30
- package/dist/flap/portal-bundle-merkle/swap.d.ts +0 -2
- package/dist/flap/portal-bundle-merkle/swap.js +75 -47
- package/dist/flap/portal-bundle-merkle/types.d.ts +1 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +0 -1
- package/dist/pancake/bundle-buy-first.d.ts +1 -1
- package/dist/pancake/bundle-buy-first.js +49 -17
- package/dist/pancake/bundle-swap.d.ts +1 -4
- package/dist/pancake/bundle-swap.js +98 -33
- package/dist/utils/erc20.d.ts +108 -2
- package/dist/utils/erc20.js +65 -17
- package/package.json +4 -39
- package/dist/sol/constants.d.ts +0 -126
- package/dist/sol/constants.js +0 -145
- package/dist/sol/dex/index.d.ts +0 -8
- package/dist/sol/dex/index.js +0 -12
- package/dist/sol/dex/meteora/client.d.ts +0 -76
- package/dist/sol/dex/meteora/client.js +0 -219
- package/dist/sol/dex/meteora/damm-v1-bundle.d.ts +0 -61
- package/dist/sol/dex/meteora/damm-v1-bundle.js +0 -112
- package/dist/sol/dex/meteora/damm-v1.d.ts +0 -118
- package/dist/sol/dex/meteora/damm-v1.js +0 -315
- package/dist/sol/dex/meteora/damm-v2-bundle.d.ts +0 -82
- package/dist/sol/dex/meteora/damm-v2-bundle.js +0 -242
- package/dist/sol/dex/meteora/damm-v2.d.ts +0 -172
- package/dist/sol/dex/meteora/damm-v2.js +0 -632
- package/dist/sol/dex/meteora/dbc-bundle.d.ts +0 -123
- package/dist/sol/dex/meteora/dbc-bundle.js +0 -304
- package/dist/sol/dex/meteora/dbc.d.ts +0 -192
- package/dist/sol/dex/meteora/dbc.js +0 -619
- package/dist/sol/dex/meteora/dlmm-bundle.d.ts +0 -39
- package/dist/sol/dex/meteora/dlmm-bundle.js +0 -189
- package/dist/sol/dex/meteora/dlmm.d.ts +0 -157
- package/dist/sol/dex/meteora/dlmm.js +0 -671
- package/dist/sol/dex/meteora/index.d.ts +0 -25
- package/dist/sol/dex/meteora/index.js +0 -65
- package/dist/sol/dex/meteora/types.d.ts +0 -787
- package/dist/sol/dex/meteora/types.js +0 -110
- package/dist/sol/dex/orca/index.d.ts +0 -10
- package/dist/sol/dex/orca/index.js +0 -16
- package/dist/sol/dex/orca/orca-bundle.d.ts +0 -41
- package/dist/sol/dex/orca/orca-bundle.js +0 -173
- package/dist/sol/dex/orca/orca.d.ts +0 -65
- package/dist/sol/dex/orca/orca.js +0 -474
- package/dist/sol/dex/orca/types.d.ts +0 -263
- package/dist/sol/dex/orca/types.js +0 -38
- package/dist/sol/dex/orca/wavebreak-bundle.d.ts +0 -34
- package/dist/sol/dex/orca/wavebreak-bundle.js +0 -198
- package/dist/sol/dex/orca/wavebreak-types.d.ts +0 -227
- package/dist/sol/dex/orca/wavebreak-types.js +0 -23
- package/dist/sol/dex/orca/wavebreak.d.ts +0 -78
- package/dist/sol/dex/orca/wavebreak.js +0 -497
- package/dist/sol/dex/pump/index.d.ts +0 -9
- package/dist/sol/dex/pump/index.js +0 -14
- package/dist/sol/dex/pump/pump-bundle.d.ts +0 -92
- package/dist/sol/dex/pump/pump-bundle.js +0 -383
- package/dist/sol/dex/pump/pump-swap-bundle.d.ts +0 -103
- package/dist/sol/dex/pump/pump-swap-bundle.js +0 -380
- package/dist/sol/dex/pump/pump-swap.d.ts +0 -46
- package/dist/sol/dex/pump/pump-swap.js +0 -199
- package/dist/sol/dex/pump/pump.d.ts +0 -35
- package/dist/sol/dex/pump/pump.js +0 -352
- package/dist/sol/dex/pump/types.d.ts +0 -215
- package/dist/sol/dex/pump/types.js +0 -5
- package/dist/sol/dex/raydium/index.d.ts +0 -8
- package/dist/sol/dex/raydium/index.js +0 -12
- package/dist/sol/dex/raydium/launchlab.d.ts +0 -68
- package/dist/sol/dex/raydium/launchlab.js +0 -210
- package/dist/sol/dex/raydium/raydium-bundle.d.ts +0 -64
- package/dist/sol/dex/raydium/raydium-bundle.js +0 -324
- package/dist/sol/dex/raydium/raydium.d.ts +0 -40
- package/dist/sol/dex/raydium/raydium.js +0 -366
- package/dist/sol/dex/raydium/types.d.ts +0 -240
- package/dist/sol/dex/raydium/types.js +0 -5
- package/dist/sol/index.d.ts +0 -10
- package/dist/sol/index.js +0 -16
- package/dist/sol/jito/bundle.d.ts +0 -90
- package/dist/sol/jito/bundle.js +0 -263
- package/dist/sol/jito/index.d.ts +0 -7
- package/dist/sol/jito/index.js +0 -7
- package/dist/sol/jito/tip.d.ts +0 -51
- package/dist/sol/jito/tip.js +0 -83
- package/dist/sol/jito/types.d.ts +0 -100
- package/dist/sol/jito/types.js +0 -5
- package/dist/sol/token/create-complete.d.ts +0 -115
- package/dist/sol/token/create-complete.js +0 -235
- package/dist/sol/token/create-token.d.ts +0 -57
- package/dist/sol/token/create-token.js +0 -230
- package/dist/sol/token/index.d.ts +0 -9
- package/dist/sol/token/index.js +0 -14
- package/dist/sol/token/metadata-upload.d.ts +0 -86
- package/dist/sol/token/metadata-upload.js +0 -173
- package/dist/sol/token/metadata.d.ts +0 -92
- package/dist/sol/token/metadata.js +0 -274
- package/dist/sol/token/types.d.ts +0 -153
- package/dist/sol/token/types.js +0 -5
- package/dist/sol/types.d.ts +0 -176
- package/dist/sol/types.js +0 -7
- package/dist/sol/utils/balance.d.ts +0 -160
- package/dist/sol/utils/balance.js +0 -638
- package/dist/sol/utils/connection.d.ts +0 -78
- package/dist/sol/utils/connection.js +0 -168
- package/dist/sol/utils/index.d.ts +0 -9
- package/dist/sol/utils/index.js +0 -9
- package/dist/sol/utils/lp-inspect.d.ts +0 -129
- package/dist/sol/utils/lp-inspect.js +0 -900
- package/dist/sol/utils/transfer.d.ts +0 -125
- package/dist/sol/utils/transfer.js +0 -220
- package/dist/sol/utils/wallet.d.ts +0 -107
- package/dist/sol/utils/wallet.js +0 -210
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ethers, Wallet, Contract, Interface } from 'ethers';
|
|
2
2
|
import { NonceManager, getOptimizedGasPrice } from '../../utils/bundle-helpers.js';
|
|
3
3
|
import { FLAP_PORTAL_ADDRESSES, FLAP_ORIGINAL_PORTAL_ADDRESSES } from '../constants.js';
|
|
4
|
-
import { CHAIN_ID_MAP, PORTAL_ABI, getErrorMessage, getTxType, getGasPriceConfig, shouldExtractProfit, calculateProfit, getProfitRecipient } from './config.js';
|
|
4
|
+
import { CHAIN_ID_MAP, PORTAL_ABI, getErrorMessage, getTxType, getGasPriceConfig, shouldExtractProfit, calculateProfit, getProfitRecipient, getBribeAmount, BLOCKRAZOR_BUILDER_EOA } from './config.js';
|
|
5
5
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
6
6
|
const MULTICALL3_ADDRESS = '0xcA11bde05977b3631167028862bE2a173976CA11';
|
|
7
7
|
const MULTICALL3_ABI = [
|
|
@@ -153,6 +153,25 @@ export async function createTokenWithBundleBuyMerkle(params) {
|
|
|
153
153
|
populateBuyTransactionsWithQuote(buyers, portalAddr, tokenAddress, adjustedFundsList, inputToken, useNativeToken),
|
|
154
154
|
allocateBuyerNonces(buyers, extractProfit, maxFundsIndex, totalProfit, nonceManager)
|
|
155
155
|
]);
|
|
156
|
+
// ✅ 贿赂交易放在首位(提高 BlockRazor 打包优先级)
|
|
157
|
+
const bribeAmount = getBribeAmount(config);
|
|
158
|
+
const bribeTxs = [];
|
|
159
|
+
if (bribeAmount > 0n && maxFundsIndex >= 0 && buyers.length > 0) {
|
|
160
|
+
// 贿赂交易使用 maxFundsIndex 钱包的第一个 nonce
|
|
161
|
+
const bribeNonce = buyerNonces[maxFundsIndex];
|
|
162
|
+
const bribeTx = await buyers[maxFundsIndex].signTransaction({
|
|
163
|
+
to: BLOCKRAZOR_BUILDER_EOA,
|
|
164
|
+
value: bribeAmount,
|
|
165
|
+
nonce: bribeNonce,
|
|
166
|
+
gasPrice,
|
|
167
|
+
gasLimit: 21000n,
|
|
168
|
+
chainId,
|
|
169
|
+
type: getTxType(config)
|
|
170
|
+
});
|
|
171
|
+
bribeTxs.push(bribeTx);
|
|
172
|
+
// 调整 maxFundsIndex 钱包的 nonce(买入交易 +1)
|
|
173
|
+
buyerNonces[maxFundsIndex] = bribeNonce + 1;
|
|
174
|
+
}
|
|
156
175
|
const signedBuys = await signBuyTransactions({
|
|
157
176
|
unsignedBuys,
|
|
158
177
|
buyers,
|
|
@@ -164,21 +183,25 @@ export async function createTokenWithBundleBuyMerkle(params) {
|
|
|
164
183
|
fundsList: adjustedFundsList, // ✅ 使用调整后的金额
|
|
165
184
|
useNativeToken // ✅ 传递是否使用原生代币
|
|
166
185
|
});
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
buyers
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
186
|
+
// ✅ 利润交易放在末尾
|
|
187
|
+
const profitTxs = [];
|
|
188
|
+
if (extractProfit && totalProfit > 0n && maxFundsIndex >= 0) {
|
|
189
|
+
const profitNonce = buyerNonces[maxFundsIndex] + 1;
|
|
190
|
+
const profitTx = await buyers[maxFundsIndex].signTransaction({
|
|
191
|
+
to: getProfitRecipient(),
|
|
192
|
+
value: totalProfit,
|
|
193
|
+
nonce: profitNonce,
|
|
194
|
+
gasPrice,
|
|
195
|
+
gasLimit: 23000n,
|
|
196
|
+
chainId,
|
|
197
|
+
type: getTxType(config)
|
|
198
|
+
});
|
|
199
|
+
profitTxs.push(profitTx);
|
|
200
|
+
}
|
|
179
201
|
nonceManager.clearTemp();
|
|
202
|
+
// ✅ 组装顺序:贿赂 → 创建代币 → 买入 → 利润
|
|
180
203
|
return {
|
|
181
|
-
signedTransactions: signedTxs,
|
|
204
|
+
signedTransactions: [...bribeTxs, ...signedTxs, ...signedBuys, ...profitTxs],
|
|
182
205
|
tokenAddress,
|
|
183
206
|
metadata: buildProfitMetadata(extractProfit, totalBuyAmount, totalProfit, buyers.length)
|
|
184
207
|
};
|
|
@@ -234,6 +257,24 @@ export async function batchBuyWithBundleMerkle(params) {
|
|
|
234
257
|
]);
|
|
235
258
|
if (presetNonces) {
|
|
236
259
|
}
|
|
260
|
+
// ✅ 贿赂交易放在首位(提高 BlockRazor 打包优先级)
|
|
261
|
+
const bribeAmount = getBribeAmount(config);
|
|
262
|
+
const bribeTxs = [];
|
|
263
|
+
if (bribeAmount > 0n && maxFundsIndex >= 0 && buyers.length > 0) {
|
|
264
|
+
const bribeNonce = buyerNonces[maxFundsIndex];
|
|
265
|
+
const bribeTx = await buyers[maxFundsIndex].signTransaction({
|
|
266
|
+
to: BLOCKRAZOR_BUILDER_EOA,
|
|
267
|
+
value: bribeAmount,
|
|
268
|
+
nonce: bribeNonce,
|
|
269
|
+
gasPrice,
|
|
270
|
+
gasLimit: 21000n,
|
|
271
|
+
chainId,
|
|
272
|
+
type: getTxType(config)
|
|
273
|
+
});
|
|
274
|
+
bribeTxs.push(bribeTx);
|
|
275
|
+
// 调整 maxFundsIndex 钱包的 nonce(买入交易 +1)
|
|
276
|
+
buyerNonces[maxFundsIndex] = bribeNonce + 1;
|
|
277
|
+
}
|
|
237
278
|
const signedBuys = await signBuyTransactions({
|
|
238
279
|
unsignedBuys,
|
|
239
280
|
buyers,
|
|
@@ -245,21 +286,25 @@ export async function batchBuyWithBundleMerkle(params) {
|
|
|
245
286
|
fundsList: adjustedFundsList, // ✅ 使用调整后的金额
|
|
246
287
|
useNativeToken // ✅ USDT 购买时 value=0,BNB 购买时 value=金额
|
|
247
288
|
});
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
buyers
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
289
|
+
// ✅ 利润交易放在末尾
|
|
290
|
+
const profitTxs = [];
|
|
291
|
+
if (extractProfit && nativeProfitAmount > 0n && maxFundsIndex >= 0) {
|
|
292
|
+
const profitNonce = buyerNonces[maxFundsIndex] + 1;
|
|
293
|
+
const profitTx = await buyers[maxFundsIndex].signTransaction({
|
|
294
|
+
to: getProfitRecipient(),
|
|
295
|
+
value: nativeProfitAmount,
|
|
296
|
+
nonce: profitNonce,
|
|
297
|
+
gasPrice,
|
|
298
|
+
gasLimit: 23000n,
|
|
299
|
+
chainId,
|
|
300
|
+
type: getTxType(config)
|
|
301
|
+
});
|
|
302
|
+
profitTxs.push(profitTx);
|
|
303
|
+
}
|
|
260
304
|
nonceManager.clearTemp();
|
|
305
|
+
// ✅ 组装顺序:贿赂 → 买入 → 利润
|
|
261
306
|
return {
|
|
262
|
-
signedTransactions:
|
|
307
|
+
signedTransactions: [...bribeTxs, ...signedBuys, ...profitTxs],
|
|
263
308
|
metadata: buildProfitMetadata(extractProfit, totalBuyAmount, nativeProfitAmount, buyers.length)
|
|
264
309
|
};
|
|
265
310
|
}
|
|
@@ -310,18 +355,27 @@ export async function batchSellWithBundleMerkle(params) {
|
|
|
310
355
|
}
|
|
311
356
|
}
|
|
312
357
|
}
|
|
313
|
-
// ✅
|
|
358
|
+
// ✅ 修复:根据是否需要贿赂/利润交易,统一分配 nonces
|
|
359
|
+
const bribeAmount = getBribeAmount(config);
|
|
360
|
+
const needBribeTx = bribeAmount > 0n && maxRevenueIndex >= 0;
|
|
314
361
|
const needProfitTx = extractProfit && totalTokenProfit > 0n && maxRevenueIndex >= 0;
|
|
362
|
+
// 计算 maxRevenueIndex 钱包需要的 nonce 数量:贿赂(可选) + 卖出 + 利润(可选)
|
|
363
|
+
const maxRevenueNonceCount = 1 + (needBribeTx ? 1 : 0) + (needProfitTx ? 1 : 0);
|
|
315
364
|
let nonces;
|
|
365
|
+
let bribeNonce;
|
|
316
366
|
let profitNonce;
|
|
317
367
|
if (presetNonces && presetNonces.length === wallets.length) {
|
|
318
|
-
// ✅ 使用前端传入的 nonces
|
|
319
|
-
nonces =
|
|
320
|
-
|
|
368
|
+
// ✅ 使用前端传入的 nonces
|
|
369
|
+
nonces = [...presetNonces];
|
|
370
|
+
if (needBribeTx) {
|
|
371
|
+
bribeNonce = nonces[maxRevenueIndex];
|
|
372
|
+
nonces[maxRevenueIndex] = bribeNonce + 1; // 卖出交易 nonce +1
|
|
373
|
+
}
|
|
374
|
+
profitNonce = needProfitTx ? nonces[maxRevenueIndex] + 1 : undefined;
|
|
321
375
|
}
|
|
322
|
-
else if (
|
|
323
|
-
// maxRevenueIndex
|
|
324
|
-
const maxRevenueNonces = await nonceManager.getNextNonceBatch(wallets[maxRevenueIndex],
|
|
376
|
+
else if (maxRevenueNonceCount > 1 && maxRevenueIndex >= 0) {
|
|
377
|
+
// maxRevenueIndex 钱包需要多个连续 nonce
|
|
378
|
+
const maxRevenueNonces = await nonceManager.getNextNonceBatch(wallets[maxRevenueIndex], maxRevenueNonceCount);
|
|
325
379
|
// 其他钱包各需要 1 个 nonce
|
|
326
380
|
const otherWallets = wallets.filter((_, i) => i !== maxRevenueIndex);
|
|
327
381
|
const otherNonces = otherWallets.length > 0
|
|
@@ -330,18 +384,24 @@ export async function batchSellWithBundleMerkle(params) {
|
|
|
330
384
|
// 组装最终的 nonces 数组(保持原顺序)
|
|
331
385
|
nonces = [];
|
|
332
386
|
let otherIdx = 0;
|
|
387
|
+
let nonceIdx = 0;
|
|
388
|
+
if (needBribeTx) {
|
|
389
|
+
bribeNonce = maxRevenueNonces[nonceIdx++]; // 贿赂交易用第一个 nonce
|
|
390
|
+
}
|
|
333
391
|
for (let i = 0; i < wallets.length; i++) {
|
|
334
392
|
if (i === maxRevenueIndex) {
|
|
335
|
-
nonces.push(maxRevenueNonces[
|
|
393
|
+
nonces.push(maxRevenueNonces[nonceIdx++]); // 卖出交易
|
|
336
394
|
}
|
|
337
395
|
else {
|
|
338
396
|
nonces.push(otherNonces[otherIdx++]);
|
|
339
397
|
}
|
|
340
398
|
}
|
|
341
|
-
|
|
399
|
+
if (needProfitTx) {
|
|
400
|
+
profitNonce = maxRevenueNonces[nonceIdx]; // 利润交易用最后一个 nonce
|
|
401
|
+
}
|
|
342
402
|
}
|
|
343
403
|
else {
|
|
344
|
-
//
|
|
404
|
+
// 不需要额外交易,所有钱包各 1 个 nonce
|
|
345
405
|
nonces = await nonceManager.getNextNoncesForWallets(wallets);
|
|
346
406
|
}
|
|
347
407
|
const minOuts = resolveMinOutputs(minOutputAmounts, wallets.length, quotedOutputs);
|
|
@@ -353,7 +413,21 @@ export async function batchSellWithBundleMerkle(params) {
|
|
|
353
413
|
minOutputAmount: minOuts[i],
|
|
354
414
|
permitData: '0x'
|
|
355
415
|
})));
|
|
356
|
-
// ✅
|
|
416
|
+
// ✅ 贿赂交易放在首位
|
|
417
|
+
const bribeTxs = [];
|
|
418
|
+
if (needBribeTx && bribeNonce !== undefined) {
|
|
419
|
+
const bribeTx = await wallets[maxRevenueIndex].signTransaction({
|
|
420
|
+
to: BLOCKRAZOR_BUILDER_EOA,
|
|
421
|
+
value: bribeAmount,
|
|
422
|
+
nonce: bribeNonce,
|
|
423
|
+
gasPrice,
|
|
424
|
+
gasLimit: 21000n,
|
|
425
|
+
chainId,
|
|
426
|
+
type: getTxType(config)
|
|
427
|
+
});
|
|
428
|
+
bribeTxs.push(bribeTx);
|
|
429
|
+
}
|
|
430
|
+
// ✅ 签名所有卖出交易(并行)
|
|
357
431
|
// ✅ 卖出交易 value 必须为 0,不能发送原生代币
|
|
358
432
|
const signedList = await Promise.all(unsignedList.map((unsigned, i) => wallets[i].signTransaction({
|
|
359
433
|
...unsigned,
|
|
@@ -365,16 +439,13 @@ export async function batchSellWithBundleMerkle(params) {
|
|
|
365
439
|
type: getTxType(config),
|
|
366
440
|
value: 0n // ✅ 卖出交易不发送原生代币
|
|
367
441
|
})));
|
|
368
|
-
|
|
369
|
-
|
|
442
|
+
// ✅ 利润交易放在末尾
|
|
443
|
+
const profitTxs = [];
|
|
370
444
|
if (needProfitTx && profitNonce !== undefined) {
|
|
371
445
|
// ERC20 输出时:获取代币利润等值的原生代币(BNB)报价
|
|
372
446
|
let nativeProfitAmount = totalTokenProfit;
|
|
373
447
|
if (!useNativeOutput && outputToken) {
|
|
374
448
|
nativeProfitAmount = await getTokenToNativeQuote(provider, outputToken, totalTokenProfit, chainId);
|
|
375
|
-
// 如果报价失败(返回 0),跳过利润提取
|
|
376
|
-
if (nativeProfitAmount === 0n) {
|
|
377
|
-
}
|
|
378
449
|
}
|
|
379
450
|
if (nativeProfitAmount > 0n) {
|
|
380
451
|
const profitTx = await wallets[maxRevenueIndex].signTransaction({
|
|
@@ -386,12 +457,13 @@ export async function batchSellWithBundleMerkle(params) {
|
|
|
386
457
|
chainId,
|
|
387
458
|
type: getTxType(config)
|
|
388
459
|
});
|
|
389
|
-
|
|
460
|
+
profitTxs.push(profitTx);
|
|
390
461
|
}
|
|
391
462
|
}
|
|
392
463
|
nonceManager.clearTemp();
|
|
464
|
+
// ✅ 组装顺序:贿赂 → 卖出 → 利润
|
|
393
465
|
return {
|
|
394
|
-
signedTransactions:
|
|
466
|
+
signedTransactions: [...bribeTxs, ...signedList, ...profitTxs]
|
|
395
467
|
};
|
|
396
468
|
}
|
|
397
469
|
// ✅ Provider 缓存(复用连接,减少初始化开销)
|
|
@@ -535,22 +607,6 @@ async function signBuyTransactions({ unsignedBuys, buyers, nonces, gasLimits, ga
|
|
|
535
607
|
value: useNativeToken ? fundsList[i] : 0n // ✅ 非原生代币时 value 为 0
|
|
536
608
|
})));
|
|
537
609
|
}
|
|
538
|
-
async function appendProfitTransaction({ extractProfit, totalProfit, buyers, maxIndex, nonces, gasPrice, chainId, config, signedTxs }) {
|
|
539
|
-
if (!extractProfit || totalProfit === 0n || buyers.length === 0 || maxIndex < 0) {
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
const profitNonce = (nonces[maxIndex] ?? 0) + 1;
|
|
543
|
-
const profitTx = await buyers[maxIndex].signTransaction({
|
|
544
|
-
to: getProfitRecipient(),
|
|
545
|
-
value: totalProfit,
|
|
546
|
-
nonce: profitNonce,
|
|
547
|
-
gasPrice,
|
|
548
|
-
gasLimit: 23000n,
|
|
549
|
-
chainId,
|
|
550
|
-
type: getTxType(config)
|
|
551
|
-
});
|
|
552
|
-
signedTxs.push(profitTx);
|
|
553
|
-
}
|
|
554
610
|
function buildProfitMetadata(extractProfit, totalBuyAmount, totalProfit, buyerCount) {
|
|
555
611
|
if (!extractProfit) {
|
|
556
612
|
return undefined;
|
|
@@ -628,12 +684,8 @@ async function quoteSellOutputsWithQuote(portal, tokenAddress, amountsWei, outpu
|
|
|
628
684
|
}));
|
|
629
685
|
}
|
|
630
686
|
}
|
|
631
|
-
function resolveMinOutputs(
|
|
632
|
-
|
|
633
|
-
return provided.map(m => typeof m === 'string' ? ethers.parseEther(m) : BigInt(m));
|
|
634
|
-
}
|
|
635
|
-
// ✅ 默认 minOutput = 0,不设置滑点限制
|
|
636
|
-
// 原因:大额交易时 5% 滑点可能不够,导致交易失败
|
|
687
|
+
function resolveMinOutputs(_provided, walletCount, _quotedOutputs) {
|
|
688
|
+
// ✅ 已移除滑点保护:minOutput 固定为 0
|
|
637
689
|
return Array(walletCount).fill(0n);
|
|
638
690
|
}
|
|
639
|
-
// ✅
|
|
691
|
+
// ✅ 贿赂交易和利润交易已内联到各函数中,确保正确的顺序:贿赂 → 主交易 → 利润
|
|
@@ -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
|
+
}
|