four-flap-meme-sdk 1.5.83 → 1.5.85
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/flap/portal-bundle-merkle/encryption.d.ts +16 -0
- package/dist/flap/portal-bundle-merkle/encryption.js +146 -0
- package/dist/xlayer/aa-account.js +51 -43
- package/dist/xlayer/bundle.js +203 -179
- package/dist/xlayer/constants.d.ts +1 -1
- package/dist/xlayer/constants.js +3 -1
- package/dist/xlayer/index.d.ts +1 -1
- package/dist/xlayer/index.js +1 -1
- package/dist/xlayer/portal-ops.d.ts +14 -3
- package/dist/xlayer/portal-ops.js +26 -4
- package/dist/xlayer/types.d.ts +8 -2
- package/package.json +1 -1
|
@@ -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
|
+
}
|
|
@@ -637,54 +637,31 @@ export class AAAccountManager {
|
|
|
637
637
|
return [];
|
|
638
638
|
// 1) 批量预测 sender(优先 getAddress+multicall,失败回退 createAccount.staticCall)
|
|
639
639
|
const senders = await this.predictSendersByOwnersFast(owners);
|
|
640
|
-
// 2) 批量 getNonce
|
|
640
|
+
// 2) 批量 getNonce(multicall EntryPoint.getNonce)
|
|
641
641
|
const epIface = new Interface(ENTRYPOINT_ABI);
|
|
642
|
-
const
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
},
|
|
648
|
-
{
|
|
649
|
-
target: MULTICALL3,
|
|
650
|
-
allowFailure: true,
|
|
651
|
-
callData: '0x4d2301cc' + sender.slice(2).padStart(64, '0'), // getEthBalance(address)
|
|
652
|
-
},
|
|
653
|
-
]);
|
|
642
|
+
const nonceCalls = senders.map((sender) => ({
|
|
643
|
+
target: this.entryPointAddress,
|
|
644
|
+
allowFailure: true,
|
|
645
|
+
callData: epIface.encodeFunctionData('getNonce', [sender, 0]),
|
|
646
|
+
}));
|
|
654
647
|
const nonces = new Array(senders.length).fill(0n);
|
|
655
|
-
const
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
const
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
const
|
|
666
|
-
|
|
667
|
-
if (nonceRes?.success && nonceRes.returnData && nonceRes.returnData !== '0x') {
|
|
668
|
-
try {
|
|
669
|
-
const decoded = epIface.decodeFunctionResult('getNonce', nonceRes.returnData);
|
|
670
|
-
nonces[idx] = BigInt(decoded?.[0] ?? 0n);
|
|
671
|
-
}
|
|
672
|
-
catch { /* ignore */ }
|
|
673
|
-
}
|
|
674
|
-
// 解析 Balance
|
|
675
|
-
if (balanceRes?.success && balanceRes.returnData && balanceRes.returnData !== '0x') {
|
|
676
|
-
try {
|
|
677
|
-
balances[idx] = BigInt(balanceRes.returnData);
|
|
678
|
-
}
|
|
679
|
-
catch { /* ignore */ }
|
|
680
|
-
}
|
|
648
|
+
const BATCH = 350;
|
|
649
|
+
for (let cursor = 0; cursor < nonceCalls.length; cursor += BATCH) {
|
|
650
|
+
const sliceCalls = nonceCalls.slice(cursor, cursor + BATCH);
|
|
651
|
+
const res = await this.multicallAggregate3({ calls: sliceCalls });
|
|
652
|
+
for (let i = 0; i < res.length; i++) {
|
|
653
|
+
const r = res[i];
|
|
654
|
+
const idx = cursor + i;
|
|
655
|
+
if (!r?.success || !r.returnData || r.returnData === '0x')
|
|
656
|
+
continue;
|
|
657
|
+
try {
|
|
658
|
+
const decoded = epIface.decodeFunctionResult('getNonce', r.returnData);
|
|
659
|
+
nonces[idx] = BigInt(decoded?.[0] ?? 0n);
|
|
681
660
|
}
|
|
682
|
-
|
|
683
|
-
catch (err) {
|
|
684
|
-
console.warn(`[getMultipleAccountInfo] Multicall 失败,回退后续 logic 会处理缺失值:`, err);
|
|
661
|
+
catch { /* ignore */ }
|
|
685
662
|
}
|
|
686
663
|
}
|
|
687
|
-
// 3) deployed(getCode):小分片 + 并发上限 + 缓存 deployed=true
|
|
664
|
+
// 3) deployed(getCode):小分片 + 并发上限 + 缓存 deployed=true,避免 -32014 “batch request too many”
|
|
688
665
|
const deployed = new Array(senders.length).fill(false);
|
|
689
666
|
const needCode = [];
|
|
690
667
|
for (let i = 0; i < senders.length; i++) {
|
|
@@ -736,6 +713,37 @@ export class AAAccountManager {
|
|
|
736
713
|
}
|
|
737
714
|
}
|
|
738
715
|
}
|
|
716
|
+
// 4) balance(getEthBalance):用 Multicall3.getEthBalance 分片批量查询 OKB(减少 N 次 eth_getBalance)
|
|
717
|
+
const balances = new Array(senders.length).fill(0n);
|
|
718
|
+
const ethBalIface = new Interface(['function getEthBalance(address addr) view returns (uint256)']);
|
|
719
|
+
const balCalls = senders.map((sender) => ({
|
|
720
|
+
target: MULTICALL3,
|
|
721
|
+
allowFailure: true,
|
|
722
|
+
callData: ethBalIface.encodeFunctionData('getEthBalance', [sender]),
|
|
723
|
+
}));
|
|
724
|
+
const BAL_BATCH = 300;
|
|
725
|
+
try {
|
|
726
|
+
for (let cursor = 0; cursor < balCalls.length; cursor += BAL_BATCH) {
|
|
727
|
+
const sliceCalls = balCalls.slice(cursor, cursor + BAL_BATCH);
|
|
728
|
+
const res = await this.multicallAggregate3({ calls: sliceCalls });
|
|
729
|
+
for (let i = 0; i < res.length; i++) {
|
|
730
|
+
const r = res[i];
|
|
731
|
+
const idx = cursor + i;
|
|
732
|
+
if (!r?.success || !r.returnData || r.returnData === '0x')
|
|
733
|
+
continue;
|
|
734
|
+
try {
|
|
735
|
+
const decoded = ethBalIface.decodeFunctionResult('getEthBalance', r.returnData);
|
|
736
|
+
balances[idx] = BigInt(decoded?.[0] ?? 0n);
|
|
737
|
+
}
|
|
738
|
+
catch { /* ignore */ }
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
catch {
|
|
743
|
+
const fetched = await mapWithConcurrency(senders, 6, async (s) => await this.provider.getBalance(s));
|
|
744
|
+
for (let i = 0; i < fetched.length; i++)
|
|
745
|
+
balances[i] = fetched[i] ?? 0n;
|
|
746
|
+
}
|
|
739
747
|
return owners.map((owner, i) => ({
|
|
740
748
|
owner,
|
|
741
749
|
sender: senders[i],
|
package/dist/xlayer/bundle.js
CHANGED
|
@@ -10,10 +10,11 @@
|
|
|
10
10
|
import { Wallet, Interface, Contract, ethers } from 'ethers';
|
|
11
11
|
import { FLAP_PORTAL, ENTRYPOINT_ABI, PORTAL_ABI, DEFAULT_CALL_GAS_LIMIT_SELL, DEFAULT_WITHDRAW_RESERVE, WOKB, POTATOSWAP_V2_ROUTER, POTATOSWAP_V3_ROUTER, } from './constants.js';
|
|
12
12
|
import { AAAccountManager, encodeExecute, encodeExecuteBatch, createWallet } from './aa-account.js';
|
|
13
|
-
import { encodeBuyCall, encodeSellCall, encodeApproveCall, encodeTransferCall, encodeCreateCallV2, encodeCreateCallV3, encodeCreateCallV4, PortalQuery, parseOkb, formatOkb, lpFeeProfileToV3Fee, } from './portal-ops.js';
|
|
13
|
+
import { encodeBuyCall, encodeBuyCallV3, encodeSellCall, encodeApproveCall, encodeTransferCall, encodeCreateCallV2, encodeCreateCallV3, encodeCreateCallV4, PortalQuery, parseOkb, formatOkb, lpFeeProfileToV3Fee, } from './portal-ops.js';
|
|
14
14
|
import { mapWithConcurrency } from '../utils/concurrency.js';
|
|
15
15
|
import { PROFIT_CONFIG, ZERO_ADDRESS } from '../utils/constants.js';
|
|
16
|
-
import { DexQuery
|
|
16
|
+
import { DexQuery } from './dex.js';
|
|
17
|
+
import { encodeSwapExactETHForTokensSupportingFee, encodeSwapExactETHForTokensV3, } from './dex.js';
|
|
17
18
|
// ============================================================================
|
|
18
19
|
// AA Nonce(EntryPoint nonce)本地分配器
|
|
19
20
|
// ============================================================================
|
|
@@ -202,6 +203,14 @@ export class BundleExecutor {
|
|
|
202
203
|
feeData.gasPrice ??
|
|
203
204
|
100000000n;
|
|
204
205
|
const nonce = params.nonce ?? await provider.getTransactionCount(params.payerWallet.address, 'pending');
|
|
206
|
+
// ✅ 修复:计算所有 UserOp 的 Gas 需求总和作为绝对保底
|
|
207
|
+
// 理由:主交易的 gasLimit 必须大于等于内部所有 UserOp 的 gasLimit 之和 + EntryPoint 自身的执行开销
|
|
208
|
+
const sumOpsGasLimit = params.ops.reduce((sum, op) => {
|
|
209
|
+
return sum + BigInt(op.callGasLimit) + BigInt(op.verificationGasLimit) + BigInt(op.preVerificationGas);
|
|
210
|
+
}, 0n);
|
|
211
|
+
// EntryPoint 自身处理 UserOp 循环也有开销,通常每个 Op 增加 3-5w,再给主流程预留 10w
|
|
212
|
+
const handleOpsOverhead = BigInt(params.ops.length * 50000) + 100000n;
|
|
213
|
+
const minRequiredFromOps = sumOpsGasLimit + handleOpsOverhead;
|
|
205
214
|
let gasLimit = params.gasLimit ?? this.config.gasLimit;
|
|
206
215
|
if (!gasLimit) {
|
|
207
216
|
try {
|
|
@@ -211,13 +220,19 @@ export class BundleExecutor {
|
|
|
211
220
|
data,
|
|
212
221
|
value: 0n,
|
|
213
222
|
});
|
|
223
|
+
// 估算结果增加 20% 缓冲
|
|
214
224
|
gasLimit = BigInt(Math.ceil(Number(est) * 1.2));
|
|
215
225
|
}
|
|
216
226
|
catch {
|
|
217
|
-
//
|
|
218
|
-
gasLimit =
|
|
227
|
+
// 兜底值:如果估算失败,使用总和加上 10% 缓冲
|
|
228
|
+
gasLimit = BigInt(Math.ceil(Number(minRequiredFromOps) * 1.1));
|
|
219
229
|
}
|
|
220
230
|
}
|
|
231
|
+
// ✅ 强制检查:确保主交易 GasLimit 不低于子交易总和
|
|
232
|
+
if (gasLimit < minRequiredFromOps) {
|
|
233
|
+
console.log(`[signHandleOpsTx] GasLimit adjusted: ${gasLimit} -> ${minRequiredFromOps} (sum of ops)`);
|
|
234
|
+
gasLimit = BigInt(Math.ceil(Number(minRequiredFromOps) * 1.1)); // 给 10% 缓冲
|
|
235
|
+
}
|
|
221
236
|
return await params.payerWallet.signTransaction({
|
|
222
237
|
to: entryPointAddress,
|
|
223
238
|
data,
|
|
@@ -1362,8 +1377,6 @@ export class BundleExecutor {
|
|
|
1362
1377
|
const { tokenInfo, tokenAddress, payerPrivateKey, curveBuyerPrivateKeys, curveBuyAmounts, enableDexBuy = false, dexBuyerPrivateKeys = [], dexBuyAmounts = [], config = {} } = params;
|
|
1363
1378
|
const aaManager = this.getAAManager();
|
|
1364
1379
|
const provider = aaManager.getProvider();
|
|
1365
|
-
const feeData = await provider.getFeeData();
|
|
1366
|
-
const legacyGasPrice = feeData.gasPrice ?? 100000000n;
|
|
1367
1380
|
const payerWallet = createWallet(payerPrivateKey, config);
|
|
1368
1381
|
const payerAccount = await aaManager.getAccountInfo(payerWallet.address);
|
|
1369
1382
|
const nonceMap = new AANonceMap();
|
|
@@ -1415,8 +1428,7 @@ export class BundleExecutor {
|
|
|
1415
1428
|
// ✅ 预计算所有买入金额
|
|
1416
1429
|
const curveBuyData = curveBuyerWallets.map((wallet, i) => {
|
|
1417
1430
|
const buyWei = parseOkb(curveBuyAmounts[i]);
|
|
1418
|
-
const
|
|
1419
|
-
const gasLimit = 8000000n; // 统一给 800W,不再区分是否最后一笔
|
|
1431
|
+
const gasLimit = 6500000n; // 统一给 650W,不再区分是否最后一笔
|
|
1420
1432
|
return { wallet, info: curveBuyerInfos[i], buyWei, gasLimit };
|
|
1421
1433
|
});
|
|
1422
1434
|
totalCurveBuyWei = curveBuyData.reduce((sum, d) => sum + d.buyWei, 0n);
|
|
@@ -1469,6 +1481,7 @@ export class BundleExecutor {
|
|
|
1469
1481
|
// ✅ 并行构建和签名所有外盘买入 UserOps
|
|
1470
1482
|
const signedDexBuyOps = await mapWithConcurrency(dexBuyData, 5, async (data) => {
|
|
1471
1483
|
const { wallet, info, buyWei } = data;
|
|
1484
|
+
// ✅ 外盘买入:使用 Portal 的 swapExactInput 进行自动路由
|
|
1472
1485
|
let swapData;
|
|
1473
1486
|
let routerAddress;
|
|
1474
1487
|
if (dexType === 'V3') {
|
|
@@ -1551,34 +1564,34 @@ export class BundleExecutor {
|
|
|
1551
1564
|
* 自动计算剩余毕业容量,将钱包分为内盘和外盘组,生成签名的交易列表
|
|
1552
1565
|
*/
|
|
1553
1566
|
async bundleGraduateBuy(params) {
|
|
1554
|
-
const { tokenAddress, privateKeys, payerPrivateKey, amountMode, totalBuyAmount, minAmount, maxAmount, walletAmounts, enableDexBuy = false,
|
|
1567
|
+
const { tokenAddress, privateKeys, payerPrivateKey, amountMode, totalBuyAmount, minAmount, maxAmount, walletAmounts, enableDexBuy = false,
|
|
1568
|
+
// ✅ 新增:前端传递的分组信息
|
|
1569
|
+
curveAddresses, dexAddresses, graduationAmount, config = {} } = params;
|
|
1555
1570
|
const aaManager = this.getAAManager();
|
|
1556
1571
|
const provider = aaManager.getProvider();
|
|
1557
1572
|
// 1. 获取代币状态,动态计算毕业容量
|
|
1558
1573
|
const tokenState = await this.portalQuery.getTokenV7(tokenAddress);
|
|
1559
|
-
// ✅
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
const
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
const remainingStr = ethers.formatEther(remainingToGraduateWei);
|
|
1581
|
-
console.log(`[GraduateBuy] Status: ${tokenState.status}, Pool Reserve: ${poolReserveOkbStr} OKB, Graduation: ${graduationReserveStr} OKB, Remaining: ${remainingStr} OKB`);
|
|
1574
|
+
// ✅ 内联曲线公式计算毕业容量
|
|
1575
|
+
// 公式:graduationReserve = h * k(曲线参数从合约读取)
|
|
1576
|
+
const r = tokenState.r; // 衰减因子
|
|
1577
|
+
const h = tokenState.h; // 初始高度
|
|
1578
|
+
const k = tokenState.k; // 缩放系数
|
|
1579
|
+
// 毕业储备 = h * k (wei)
|
|
1580
|
+
const graduationReserveWei = h * k / (10n ** 18n);
|
|
1581
|
+
const graduationReserve = ethers.formatEther(graduationReserveWei);
|
|
1582
|
+
const poolReserveOkb = ethers.formatEther(tokenState.reserve);
|
|
1583
|
+
// // ✅ 优先使用前端传递的动态毕业阈值
|
|
1584
|
+
const graduateCapacity = graduationAmount ?? 73.38;
|
|
1585
|
+
const remainingToGraduate = Math.max(0, graduateCapacity - parseFloat(poolReserveOkb));
|
|
1586
|
+
// const graduateCapacity = parseFloat(graduationReserve);
|
|
1587
|
+
// 剩余毕业金额 = 毕业阈值 - 当前储备(加 0.5% 手续费缓冲)
|
|
1588
|
+
const remaining = Math.max(0, graduateCapacity - parseFloat(poolReserveOkb));
|
|
1589
|
+
const remainingWithFee = remaining * 1.005; // 0.5% 手续费缓冲
|
|
1590
|
+
// const remainingToGraduate = remainingWithFee;
|
|
1591
|
+
const remainingToGraduateWei = ethers.parseEther(remainingToGraduate.toFixed(18));
|
|
1592
|
+
//
|
|
1593
|
+
// console.log(`[GraduateBuy] Pool Reserve: ${poolReserveOkb} OKB, GraduateCapacity: ${graduateCapacity} OKB, Remaining: ${remainingToGraduate} OKB`);
|
|
1594
|
+
console.log(`[GraduateBuy] Pool Reserve: ${poolReserveOkb} OKB, Graduation: ${graduationReserve} OKB, Remaining: ${remainingToGraduate.toFixed(4)} OKB`);
|
|
1582
1595
|
// 2. 准备钱包
|
|
1583
1596
|
const sharedProvider = this.aaManager.getProvider();
|
|
1584
1597
|
const wallets = privateKeys.map(pk => new Wallet(pk, sharedProvider));
|
|
@@ -1586,7 +1599,21 @@ export class BundleExecutor {
|
|
|
1586
1599
|
const payerAccount = await aaManager.getAccountInfo(payerWallet.address);
|
|
1587
1600
|
const nonceMap = new AANonceMap();
|
|
1588
1601
|
nonceMap.init(payerAccount.sender, payerAccount.nonce);
|
|
1602
|
+
// ✅ 预获取所有钱包的 Sender 地址,用于双重地址匹配
|
|
1603
|
+
// 前端可能传递 Sender 地址(AA 模式下 wallet.address 被切换为 Sender)
|
|
1604
|
+
const walletInfos = await mapWithConcurrency(wallets, 5, async (w) => {
|
|
1605
|
+
const info = await aaManager.getAccountInfo(w.address);
|
|
1606
|
+
return { owner: w.address.toLowerCase(), sender: info.sender.toLowerCase() };
|
|
1607
|
+
});
|
|
1608
|
+
// 建立 Owner->Sender 映射,用于后续匹配
|
|
1609
|
+
const ownerToSender = new Map();
|
|
1610
|
+
const senderToOwner = new Map();
|
|
1611
|
+
for (const info of walletInfos) {
|
|
1612
|
+
ownerToSender.set(info.owner, info.sender);
|
|
1613
|
+
senderToOwner.set(info.sender, info.owner);
|
|
1614
|
+
}
|
|
1589
1615
|
// 3. 计算每个钱包的金额
|
|
1616
|
+
// ✅ 修复:同时支持 Owner 和 Sender 地址匹配
|
|
1590
1617
|
let finalAmounts = [];
|
|
1591
1618
|
if (amountMode === 'average') {
|
|
1592
1619
|
const avg = (totalBuyAmount || 0) / wallets.length;
|
|
@@ -1601,175 +1628,172 @@ export class BundleExecutor {
|
|
|
1601
1628
|
});
|
|
1602
1629
|
}
|
|
1603
1630
|
else if (amountMode === 'custom') {
|
|
1604
|
-
finalAmounts = wallets.map(w =>
|
|
1631
|
+
finalAmounts = wallets.map(w => {
|
|
1632
|
+
const ownerAddr = w.address.toLowerCase();
|
|
1633
|
+
const senderAddr = ownerToSender.get(ownerAddr) || '';
|
|
1634
|
+
// ✅ 双重匹配:先按 Owner 地址查找,再按 Sender 地址查找
|
|
1635
|
+
return walletAmounts?.[w.address] ||
|
|
1636
|
+
walletAmounts?.[w.address.toLowerCase()] ||
|
|
1637
|
+
walletAmounts?.[senderAddr] ||
|
|
1638
|
+
(senderAddr ? (walletAmounts?.[senderAddr.toLowerCase()] || 0) : 0);
|
|
1639
|
+
});
|
|
1605
1640
|
}
|
|
1606
|
-
// 4.
|
|
1607
|
-
const
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1641
|
+
// 4. 钱包分组
|
|
1642
|
+
const curveBuyers = [];
|
|
1643
|
+
const dexBuyers = [];
|
|
1644
|
+
// ✅ 优先使用前端传递的分组信息
|
|
1645
|
+
const hasFrontendGrouping = curveAddresses && curveAddresses.length > 0;
|
|
1646
|
+
if (hasFrontendGrouping) {
|
|
1647
|
+
// ✅ 使用前端计算好的分组,不再重新计算
|
|
1648
|
+
const curveSet = new Set(curveAddresses.map(a => a.toLowerCase()));
|
|
1649
|
+
const dexSet = dexAddresses ? new Set(dexAddresses.map(a => a.toLowerCase())) : new Set();
|
|
1650
|
+
console.log(`[GraduateBuy] Using frontend grouping: ${curveAddresses.length} curve, ${dexAddresses?.length || 0} dex`);
|
|
1651
|
+
for (let i = 0; i < wallets.length; i++) {
|
|
1652
|
+
const wallet = wallets[i];
|
|
1653
|
+
const ownerAddr = wallet.address.toLowerCase();
|
|
1654
|
+
const senderAddr = ownerToSender.get(ownerAddr) || '';
|
|
1655
|
+
const amountWei = ethers.parseEther(finalAmounts[i].toFixed(18));
|
|
1656
|
+
// ✅ 双重匹配:同时检查 Owner 和 Sender 地址
|
|
1657
|
+
const isInCurve = curveSet.has(ownerAddr) || curveSet.has(senderAddr);
|
|
1658
|
+
const isInDex = dexSet.has(ownerAddr) || dexSet.has(senderAddr);
|
|
1659
|
+
if (isInCurve) {
|
|
1660
|
+
curveBuyers.push({ wallet, amount: amountWei });
|
|
1626
1661
|
}
|
|
1627
|
-
else {
|
|
1628
|
-
|
|
1629
|
-
tasks.push({
|
|
1630
|
-
target: FLAP_PORTAL,
|
|
1631
|
-
amount: needed,
|
|
1632
|
-
data: encodeBuyCall(tokenAddress, needed, 0n)
|
|
1633
|
-
});
|
|
1634
|
-
isTrigger = true; // 肯定是毕业触发者
|
|
1635
|
-
currentCurveTotal = remainingToGraduateWei;
|
|
1636
|
-
if (enableDexBuy) {
|
|
1637
|
-
const excess = amountWei - needed;
|
|
1638
|
-
const dexType = (tokenState.lpFeeProfile >= 0 && tokenState.tokenVersion >= 4) ? 'V3' : 'V2';
|
|
1639
|
-
const deadline = Math.floor(Date.now() / 1000) + 1200;
|
|
1640
|
-
let swapData;
|
|
1641
|
-
let routerAddress;
|
|
1642
|
-
if (dexType === 'V3') {
|
|
1643
|
-
routerAddress = POTATOSWAP_V3_ROUTER;
|
|
1644
|
-
swapData = encodeSwapExactETHForTokensV3({
|
|
1645
|
-
tokenIn: WOKB, tokenOut: tokenAddress,
|
|
1646
|
-
fee: lpFeeProfileToV3Fee(tokenState.lpFeeProfile),
|
|
1647
|
-
recipient: ZERO_ADDRESS, // AA 内部 call,最终由 execute/executeBatch 决定 recipient
|
|
1648
|
-
deadline, amountIn: excess, amountOutMinimum: 0n, sqrtPriceLimitX96: 0n
|
|
1649
|
-
});
|
|
1650
|
-
}
|
|
1651
|
-
else {
|
|
1652
|
-
routerAddress = POTATOSWAP_V2_ROUTER;
|
|
1653
|
-
swapData = encodeSwapExactETHForTokensSupportingFee(0n, [WOKB, tokenAddress], ZERO_ADDRESS, deadline);
|
|
1654
|
-
}
|
|
1655
|
-
tasks.push({ target: routerAddress, amount: excess, data: swapData });
|
|
1656
|
-
}
|
|
1662
|
+
else if (isInDex && enableDexBuy) {
|
|
1663
|
+
dexBuyers.push({ wallet, amount: amountWei });
|
|
1657
1664
|
}
|
|
1665
|
+
// 如果钱包既不在 curveSet 也不在 dexSet,则跳过
|
|
1658
1666
|
}
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
}
|
|
1667
|
+
}
|
|
1668
|
+
else {
|
|
1669
|
+
// ✅ 原逻辑:SDK 自动分组(可能与前端不一致)
|
|
1670
|
+
console.log(`[GraduateBuy] No frontend grouping, using auto-split by remainingToGraduate=${remainingToGraduate} OKB`);
|
|
1671
|
+
let currentCurveTotal = 0n;
|
|
1672
|
+
for (let i = 0; i < wallets.length; i++) {
|
|
1673
|
+
const wallet = wallets[i];
|
|
1674
|
+
const amountWei = ethers.parseEther(finalAmounts[i].toFixed(18));
|
|
1675
|
+
if (currentCurveTotal < remainingToGraduateWei) {
|
|
1676
|
+
const needed = remainingToGraduateWei - currentCurveTotal;
|
|
1677
|
+
if (amountWei <= needed) {
|
|
1678
|
+
curveBuyers.push({ wallet, amount: amountWei });
|
|
1679
|
+
currentCurveTotal += amountWei;
|
|
1680
|
+
}
|
|
1681
|
+
else {
|
|
1682
|
+
// 当前钱包金额超过了剩余容量,将其限制为 needed
|
|
1683
|
+
curveBuyers.push({ wallet, amount: needed });
|
|
1684
|
+
currentCurveTotal = remainingToGraduateWei;
|
|
1685
|
+
// 超出部分暂不处理(遵循单钱包单属性逻辑)
|
|
1686
|
+
}
|
|
1673
1687
|
}
|
|
1674
|
-
else {
|
|
1675
|
-
|
|
1676
|
-
swapData = encodeSwapExactETHForTokensSupportingFee(0n, [WOKB, tokenAddress], ZERO_ADDRESS, deadline);
|
|
1688
|
+
else if (enableDexBuy) {
|
|
1689
|
+
dexBuyers.push({ wallet, amount: amountWei });
|
|
1677
1690
|
}
|
|
1678
|
-
tasks.push({ target: routerAddress, amount: amountWei, data: swapData });
|
|
1679
|
-
}
|
|
1680
|
-
if (tasks.length > 0) {
|
|
1681
|
-
buyerTasks.push({ wallet, calls: tasks, isGraduationTrigger: isTrigger });
|
|
1682
1691
|
}
|
|
1683
1692
|
}
|
|
1693
|
+
// ✅ 统一计算内盘买入总金额(无论使用哪种分组方式)
|
|
1694
|
+
const curveTotalWei = curveBuyers.reduce((sum, b) => sum + b.amount, 0n);
|
|
1684
1695
|
// 5. 构建 UserOps
|
|
1685
1696
|
const effConfig = { ...(this.config ?? {}), ...(config ?? {}) };
|
|
1697
|
+
const signedTransactions = [];
|
|
1686
1698
|
const allOps = [];
|
|
1687
|
-
//
|
|
1688
|
-
const
|
|
1699
|
+
// --- 内盘 Ops ---
|
|
1700
|
+
const curveBuyerInfos = await mapWithConcurrency(curveBuyers.map(b => b.wallet), 5, async (w) => {
|
|
1689
1701
|
const info = await aaManager.getAccountInfo(w.address);
|
|
1690
1702
|
nonceMap.init(info.sender, info.nonce);
|
|
1691
1703
|
return info;
|
|
1692
1704
|
});
|
|
1693
|
-
const
|
|
1694
|
-
const { wallet,
|
|
1695
|
-
const info =
|
|
1696
|
-
//
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
// 为了绝对安全,我们在此时重新编码带有正确 sender 的 swapData
|
|
1704
|
-
if (c.target === POTATOSWAP_V3_ROUTER || c.target === POTATOSWAP_V2_ROUTER) {
|
|
1705
|
-
const dexType = (tokenState.lpFeeProfile >= 0 && tokenState.tokenVersion >= 4) ? 'V3' : 'V2';
|
|
1706
|
-
const deadline = Math.floor(Date.now() / 1000) + 1200;
|
|
1707
|
-
if (dexType === 'V3') {
|
|
1708
|
-
callData = encodeSwapExactETHForTokensV3({
|
|
1709
|
-
tokenIn: WOKB, tokenOut: tokenAddress,
|
|
1710
|
-
fee: lpFeeProfileToV3Fee(tokenState.lpFeeProfile),
|
|
1711
|
-
recipient: info.sender,
|
|
1712
|
-
deadline, amountIn: c.amount, amountOutMinimum: 0n, sqrtPriceLimitX96: 0n
|
|
1713
|
-
});
|
|
1714
|
-
}
|
|
1715
|
-
else {
|
|
1716
|
-
callData = encodeSwapExactETHForTokensSupportingFee(0n, [WOKB, tokenAddress], info.sender, deadline);
|
|
1717
|
-
}
|
|
1718
|
-
}
|
|
1719
|
-
return { ...c, data: callData };
|
|
1720
|
-
});
|
|
1721
|
-
let mainCallData;
|
|
1722
|
-
if (finalCalls.length === 1) {
|
|
1723
|
-
mainCallData = encodeExecute(finalCalls[0].target, finalCalls[0].amount, finalCalls[0].data);
|
|
1724
|
-
}
|
|
1725
|
-
else {
|
|
1726
|
-
mainCallData = encodeExecuteBatch(finalCalls.map(c => c.target), finalCalls.map(c => c.amount), finalCalls.map(c => c.data));
|
|
1727
|
-
}
|
|
1705
|
+
const signedCurveBuyOps = await mapWithConcurrency(curveBuyers, 5, async (data, i) => {
|
|
1706
|
+
const { wallet, amount } = data;
|
|
1707
|
+
const info = curveBuyerInfos[i];
|
|
1708
|
+
// ✅ 使用 swapExactInputV3(支持 extensionData),与 BSC 版本保持一致
|
|
1709
|
+
// 参考 create-to-dex.ts 第 604-616 行
|
|
1710
|
+
const extensionData = params.extensionData ?? '0x';
|
|
1711
|
+
const buyData = encodeBuyCallV3(tokenAddress, amount, 0n, extensionData);
|
|
1712
|
+
const buyCallData = encodeExecute(FLAP_PORTAL, amount, buyData);
|
|
1713
|
+
// ✅ 判断是否是最后一笔内盘买入(触发毕业的那笔)
|
|
1714
|
+
const isGraduationTrigger = (i === curveBuyers.length - 1);
|
|
1728
1715
|
const callGasLimit = isGraduationTrigger
|
|
1729
|
-
?
|
|
1730
|
-
: (effConfig.fixedGas?.callGasLimit ??
|
|
1731
|
-
const
|
|
1716
|
+
? 6500000n // 毕业触发交易需要更高 gas
|
|
1717
|
+
: (effConfig.fixedGas?.callGasLimit ?? 500000n); // 其他买入使用前端配置或默认值
|
|
1718
|
+
const buyOpRes = await aaManager.buildUserOpWithFixedGas({
|
|
1732
1719
|
ownerWallet: wallet,
|
|
1733
1720
|
sender: info.sender,
|
|
1734
|
-
callData:
|
|
1721
|
+
callData: buyCallData,
|
|
1735
1722
|
nonce: nonceMap.next(info.sender),
|
|
1736
1723
|
initCode: info.deployed ? '0x' : (await aaManager.generateInitCode(wallet.address)),
|
|
1737
1724
|
deployed: info.deployed,
|
|
1738
1725
|
fixedGas: { callGasLimit }
|
|
1739
1726
|
});
|
|
1740
|
-
const
|
|
1741
|
-
return
|
|
1727
|
+
const signedBuyOp = await aaManager.signUserOp(buyOpRes.userOp, wallet);
|
|
1728
|
+
return signedBuyOp.userOp;
|
|
1742
1729
|
});
|
|
1743
|
-
allOps.push(...
|
|
1744
|
-
//
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
const hasGraduation = batchOps.some(op => BigInt(op.callGasLimit) >= 7000000n);
|
|
1755
|
-
const batchGasLimit = hasGraduation ? 8000000n : effConfig.gasLimit;
|
|
1756
|
-
const signedBatchTx = await this.signHandleOpsTx({
|
|
1757
|
-
ops: batchOps,
|
|
1758
|
-
payerWallet: payerWallet,
|
|
1759
|
-
beneficiary: params.beneficiary ?? payerWallet.address,
|
|
1760
|
-
nonce: currentPayerNonce,
|
|
1761
|
-
gasLimit: batchGasLimit,
|
|
1762
|
-
gasPrice: effConfig.minGasPriceGwei ? ethers.parseUnits(effConfig.minGasPriceGwei.toString(), 'gwei') : undefined
|
|
1730
|
+
allOps.push(...signedCurveBuyOps);
|
|
1731
|
+
// --- 外盘 Ops ---
|
|
1732
|
+
let totalDexBuyWei = 0n;
|
|
1733
|
+
if (enableDexBuy && dexBuyers.length > 0) {
|
|
1734
|
+
// ✅ 定义外盘买入所需的变量(参考 bundleCreateToDexSign 第 1860-1861 行)
|
|
1735
|
+
const deadline = Math.floor(Date.now() / 1000) + 1200; // 20 min
|
|
1736
|
+
const dexType = (tokenState.lpFeeProfile !== undefined && tokenState.lpFeeProfile >= 0) ? 'V3' : 'V2';
|
|
1737
|
+
const dexBuyerInfos = await mapWithConcurrency(dexBuyers.map(b => b.wallet), 5, async (w) => {
|
|
1738
|
+
const info = await aaManager.getAccountInfo(w.address);
|
|
1739
|
+
nonceMap.init(info.sender, info.nonce);
|
|
1740
|
+
return info;
|
|
1763
1741
|
});
|
|
1764
|
-
|
|
1765
|
-
|
|
1742
|
+
const signedDexBuyOps = await mapWithConcurrency(dexBuyers, 5, async (data, i) => {
|
|
1743
|
+
const { wallet, amount } = data;
|
|
1744
|
+
const info = dexBuyerInfos[i];
|
|
1745
|
+
totalDexBuyWei += amount;
|
|
1746
|
+
// ✅ 外盘买入:直接调用 DEX Router 进行 swap
|
|
1747
|
+
// 代币毕业后无法通过 Portal 买入(会报 0xe2fae90a 错误),必须走 DEX
|
|
1748
|
+
// 参考 BSC 版本 create-to-dex.ts 第 655-752 行
|
|
1749
|
+
let swapData;
|
|
1750
|
+
let routerAddress;
|
|
1751
|
+
if (dexType === 'V3') {
|
|
1752
|
+
routerAddress = POTATOSWAP_V3_ROUTER;
|
|
1753
|
+
swapData = encodeSwapExactETHForTokensV3({
|
|
1754
|
+
tokenIn: WOKB,
|
|
1755
|
+
tokenOut: tokenAddress,
|
|
1756
|
+
fee: lpFeeProfileToV3Fee(tokenState.lpFeeProfile),
|
|
1757
|
+
recipient: info.sender,
|
|
1758
|
+
deadline,
|
|
1759
|
+
amountIn: amount,
|
|
1760
|
+
amountOutMinimum: 0n,
|
|
1761
|
+
sqrtPriceLimitX96: 0n
|
|
1762
|
+
});
|
|
1763
|
+
}
|
|
1764
|
+
else {
|
|
1765
|
+
routerAddress = POTATOSWAP_V2_ROUTER;
|
|
1766
|
+
swapData = encodeSwapExactETHForTokensSupportingFee(0n, [WOKB, tokenAddress], info.sender, deadline);
|
|
1767
|
+
}
|
|
1768
|
+
const dexBuyCallData = encodeExecute(routerAddress, amount, swapData);
|
|
1769
|
+
const dexBuyOpRes = await aaManager.buildUserOpWithFixedGas({
|
|
1770
|
+
ownerWallet: wallet,
|
|
1771
|
+
sender: info.sender,
|
|
1772
|
+
callData: dexBuyCallData,
|
|
1773
|
+
nonce: nonceMap.next(info.sender),
|
|
1774
|
+
initCode: info.deployed ? '0x' : (await aaManager.generateInitCode(wallet.address)),
|
|
1775
|
+
deployed: info.deployed,
|
|
1776
|
+
fixedGas: { callGasLimit: effConfig.fixedGas?.callGasLimit ?? 500000n } // 外盘 swap 需要更多 gas
|
|
1777
|
+
});
|
|
1778
|
+
const signedDexBuyOp = await aaManager.signUserOp(dexBuyOpRes.userOp, wallet);
|
|
1779
|
+
return signedDexBuyOp.userOp;
|
|
1780
|
+
});
|
|
1781
|
+
allOps.push(...signedDexBuyOps);
|
|
1766
1782
|
}
|
|
1767
|
-
//
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1783
|
+
// 6. 签名唯一的 handleOps 交易
|
|
1784
|
+
const startNonce = params.payerStartNonce ?? (await provider.getTransactionCount(payerWallet.address, 'pending'));
|
|
1785
|
+
const signedMainTx = await this.signHandleOpsTx({
|
|
1786
|
+
ops: allOps,
|
|
1787
|
+
payerWallet: payerWallet,
|
|
1788
|
+
beneficiary: params.beneficiary ?? payerWallet.address,
|
|
1789
|
+
nonce: startNonce,
|
|
1790
|
+
gasLimit: effConfig.gasLimit,
|
|
1791
|
+
gasPrice: effConfig.minGasPriceGwei ? ethers.parseUnits(effConfig.minGasPriceGwei.toString(), 'gwei') : undefined
|
|
1792
|
+
});
|
|
1793
|
+
signedTransactions.push(signedMainTx);
|
|
1794
|
+
// 7. 计算并签利润提取交易 (EOA 转账,千分之 4)
|
|
1795
|
+
const totalBuyWei = curveTotalWei + totalDexBuyWei;
|
|
1796
|
+
const profitWei = (totalBuyWei * 4n) / 1000n; // 0.4%
|
|
1773
1797
|
if (profitWei > 0n) {
|
|
1774
1798
|
const profitRecipient = effConfig.profitRecipient || PROFIT_CONFIG.RECIPIENT;
|
|
1775
1799
|
const feeData = await provider.getFeeData();
|
|
@@ -1777,7 +1801,7 @@ export class BundleExecutor {
|
|
|
1777
1801
|
const profitTx = await payerWallet.signTransaction({
|
|
1778
1802
|
to: profitRecipient,
|
|
1779
1803
|
value: profitWei,
|
|
1780
|
-
nonce:
|
|
1804
|
+
nonce: startNonce + 1,
|
|
1781
1805
|
gasLimit: 21000n,
|
|
1782
1806
|
gasPrice,
|
|
1783
1807
|
chainId: effConfig.chainId ?? 196,
|
|
@@ -1788,9 +1812,9 @@ export class BundleExecutor {
|
|
|
1788
1812
|
return {
|
|
1789
1813
|
signedTransactions,
|
|
1790
1814
|
metadata: {
|
|
1791
|
-
curveCount:
|
|
1792
|
-
dexCount:
|
|
1793
|
-
curveTotalAmount: ethers.formatEther(
|
|
1815
|
+
curveCount: curveBuyers.length,
|
|
1816
|
+
dexCount: dexBuyers.length,
|
|
1817
|
+
curveTotalAmount: ethers.formatEther(curveTotalWei),
|
|
1794
1818
|
dexTotalAmount: ethers.formatEther(totalDexBuyWei),
|
|
1795
1819
|
tokenAddress
|
|
1796
1820
|
}
|
|
@@ -69,7 +69,7 @@ export declare const ENTRYPOINT_ABI: readonly ["function getNonce(address sender
|
|
|
69
69
|
/** SimpleAccount ABI */
|
|
70
70
|
export declare const SIMPLE_ACCOUNT_ABI: readonly ["function execute(address dest, uint256 value, bytes func) external", "function executeBatch(address[] calldata dest, bytes[] calldata func) external", "function executeBatch(address[] calldata dest, uint256[] calldata values, bytes[] calldata func) external"];
|
|
71
71
|
/** Flap Portal ABI */
|
|
72
|
-
export declare const PORTAL_ABI: readonly ["function swapExactInput((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData) params) payable returns (uint256)", "function buy(address token, address to, uint256 minAmount) external payable returns (uint256)", "function sell(address token, uint256 amount, uint256 minEth) external returns (uint256)", "function quoteExactInput((address inputToken,address outputToken,uint256 inputAmount)) external returns (uint256)", "function previewBuy(address token, uint256 ethAmount) external view returns (uint256)", "function previewSell(address token, uint256 tokenAmount) external view returns (uint256)", "function getTokenV5(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32))", "function getTokenV7(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32,
|
|
72
|
+
export declare const PORTAL_ABI: readonly ["function swapExactInput((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData) params) payable returns (uint256)", "function swapExactInputV3((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData,bytes extensionData)) external payable returns (uint256)", "function buy(address token, address to, uint256 minAmount) external payable returns (uint256)", "function sell(address token, uint256 amount, uint256 minEth) external returns (uint256)", "function quoteExactInput((address inputToken,address outputToken,uint256 inputAmount)) external returns (uint256)", "function previewBuy(address token, uint256 ethAmount) external view returns (uint256)", "function previewSell(address token, uint256 tokenAmount) external view returns (uint256)", "function getTokenV5(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32))", "function getTokenV7(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32,uint8,uint8))", "function newTokenV2((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData) params) external payable returns (address)", "function newTokenV3((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData,bytes32 extensionID,bytes extensionData) params) external payable returns (address)", "function newTokenV4((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData,bytes32 extensionID,bytes extensionData,uint8 dexId,uint8 lpFeeProfile) params) external payable returns (address)"];
|
|
73
73
|
/** ERC20 ABI */
|
|
74
74
|
export declare const ERC20_ABI: readonly ["function balanceOf(address account) view returns (uint256)", "function allowance(address owner, address spender) view returns (uint256)", "function approve(address spender, uint256 amount) returns (bool)", "function transfer(address to, uint256 amount) returns (bool)", "function decimals() view returns (uint8)", "function symbol() view returns (string)", "function name() view returns (string)"];
|
|
75
75
|
/** PotatoSwap V2 Router ABI */
|
package/dist/xlayer/constants.js
CHANGED
|
@@ -107,13 +107,15 @@ export const SIMPLE_ACCOUNT_ABI = [
|
|
|
107
107
|
/** Flap Portal ABI */
|
|
108
108
|
export const PORTAL_ABI = [
|
|
109
109
|
'function swapExactInput((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData) params) payable returns (uint256)',
|
|
110
|
+
// ✅ swapExactInputV3: 支持 extensionData 扩展,用于买到毕业等场景
|
|
111
|
+
'function swapExactInputV3((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData,bytes extensionData)) external payable returns (uint256)',
|
|
110
112
|
'function buy(address token, address to, uint256 minAmount) external payable returns (uint256)',
|
|
111
113
|
'function sell(address token, uint256 amount, uint256 minEth) external returns (uint256)',
|
|
112
114
|
'function quoteExactInput((address inputToken,address outputToken,uint256 inputAmount)) external returns (uint256)',
|
|
113
115
|
'function previewBuy(address token, uint256 ethAmount) external view returns (uint256)',
|
|
114
116
|
'function previewSell(address token, uint256 tokenAmount) external view returns (uint256)',
|
|
115
117
|
'function getTokenV5(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32))',
|
|
116
|
-
'function getTokenV7(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32,
|
|
118
|
+
'function getTokenV7(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32,uint8,uint8))',
|
|
117
119
|
'function newTokenV2((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData) params) external payable returns (address)',
|
|
118
120
|
'function newTokenV3((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData,bytes32 extensionID,bytes extensionData) params) external payable returns (address)',
|
|
119
121
|
'function newTokenV4((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData,bytes32 extensionID,bytes extensionData,uint8 dexId,uint8 lpFeeProfile) params) external payable returns (address)',
|
package/dist/xlayer/index.d.ts
CHANGED
|
@@ -61,7 +61,7 @@ export * from './types.js';
|
|
|
61
61
|
import type { BundleSwapSignParams, BundleSwapSignResult, BundleBatchSwapSignParams, BundleBatchSwapSignResult } from './types.js';
|
|
62
62
|
export { BundlerClient, createBundlerClient, type BundlerConfig, type BundlerReceipt, } from './bundler.js';
|
|
63
63
|
export { AAAccountManager, createAAAccountManager, predictSender, createWallet, encodeExecute, encodeExecuteBatch, generateAAWallets, generateAAWalletsFromMnemonic, predictSendersFromPrivateKeys, type GeneratedAAWallet, type GenerateAAWalletsParams, type GenerateAAWalletsResult, } from './aa-account.js';
|
|
64
|
-
export { encodeBuyCall, encodeSellCall, encodeApproveCall, encodeTransferCall, PortalQuery, createPortalQuery, applySlippage, formatOkb, parseOkb, formatTokenAmount, parseTokenAmount, type PortalQueryConfig, } from './portal-ops.js';
|
|
64
|
+
export { encodeBuyCall, encodeBuyCallV3, encodeSellCall, encodeApproveCall, encodeTransferCall, PortalQuery, createPortalQuery, applySlippage, formatOkb, parseOkb, formatTokenAmount, parseTokenAmount, type PortalQueryConfig, } from './portal-ops.js';
|
|
65
65
|
export { BundleExecutor, createBundleExecutor, bundleBuy, bundleSell, bundleBuySell, bundleCreateBuySign, bundleCreateToDexSign, bundleGraduateBuy, bundlePreApprove, checkApprovalStatus, } from './bundle.js';
|
|
66
66
|
export { DexBundleExecutor, createDexBundleExecutor, dexBundleBuySell, type DexBundleConfig, type DexBundleBuySellParams, } from './dex-bundle.js';
|
|
67
67
|
export { AAPortalSwapExecutor, } from './portal-bundle-swap.js';
|
package/dist/xlayer/index.js
CHANGED
|
@@ -77,7 +77,7 @@ generateAAWallets, generateAAWalletsFromMnemonic, predictSendersFromPrivateKeys,
|
|
|
77
77
|
// ============================================================================
|
|
78
78
|
// Portal 操作
|
|
79
79
|
// ============================================================================
|
|
80
|
-
export { encodeBuyCall, encodeSellCall, encodeApproveCall, encodeTransferCall, PortalQuery, createPortalQuery, applySlippage, formatOkb, parseOkb, formatTokenAmount, parseTokenAmount, } from './portal-ops.js';
|
|
80
|
+
export { encodeBuyCall, encodeBuyCallV3, encodeSellCall, encodeApproveCall, encodeTransferCall, PortalQuery, createPortalQuery, applySlippage, formatOkb, parseOkb, formatTokenAmount, parseTokenAmount, } from './portal-ops.js';
|
|
81
81
|
// ============================================================================
|
|
82
82
|
// 捆绑交易
|
|
83
83
|
// ============================================================================
|
|
@@ -16,6 +16,19 @@ import { JsonRpcProvider } from 'ethers';
|
|
|
16
16
|
* @param minOutputAmount 最小输出代币数量(默认 0)
|
|
17
17
|
*/
|
|
18
18
|
export declare function encodeBuyCall(tokenAddress: string, inputAmount: bigint, minOutputAmount?: bigint): string;
|
|
19
|
+
/**
|
|
20
|
+
* 编码买入调用 V3(swapExactInputV3: native -> token,支持 extensionData)
|
|
21
|
+
*
|
|
22
|
+
* 与 BSC 版本的 create-to-dex.ts 保持一致:
|
|
23
|
+
* - 统一优先使用 swapExactInputV3(支持 extensionData;无扩展时传 0x 即可)
|
|
24
|
+
* - 这里的 V3 指"交易接口版本(扩展支持)",不是"外盘 V3 池子"
|
|
25
|
+
*
|
|
26
|
+
* @param tokenAddress 目标代币地址
|
|
27
|
+
* @param inputAmount 输入 OKB 数量(wei)
|
|
28
|
+
* @param minOutputAmount 最小输出代币数量(默认 0)
|
|
29
|
+
* @param extensionData 扩展数据(默认 0x)
|
|
30
|
+
*/
|
|
31
|
+
export declare function encodeBuyCallV3(tokenAddress: string, inputAmount: bigint, minOutputAmount?: bigint, extensionData?: string): string;
|
|
19
32
|
/**
|
|
20
33
|
* 编码卖出调用(swapExactInput: token -> native)
|
|
21
34
|
*
|
|
@@ -181,9 +194,7 @@ export declare class PortalQuery {
|
|
|
181
194
|
quoteTokenAddress: string;
|
|
182
195
|
nativeToQuoteSwapEnabled: boolean;
|
|
183
196
|
extensionID: string;
|
|
184
|
-
|
|
185
|
-
pool: string;
|
|
186
|
-
progress: bigint;
|
|
197
|
+
dexId: number;
|
|
187
198
|
lpFeeProfile: number;
|
|
188
199
|
}>;
|
|
189
200
|
/**
|
|
@@ -33,6 +33,30 @@ export function encodeBuyCall(tokenAddress, inputAmount, minOutputAmount = 0n) {
|
|
|
33
33
|
},
|
|
34
34
|
]);
|
|
35
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* 编码买入调用 V3(swapExactInputV3: native -> token,支持 extensionData)
|
|
38
|
+
*
|
|
39
|
+
* 与 BSC 版本的 create-to-dex.ts 保持一致:
|
|
40
|
+
* - 统一优先使用 swapExactInputV3(支持 extensionData;无扩展时传 0x 即可)
|
|
41
|
+
* - 这里的 V3 指"交易接口版本(扩展支持)",不是"外盘 V3 池子"
|
|
42
|
+
*
|
|
43
|
+
* @param tokenAddress 目标代币地址
|
|
44
|
+
* @param inputAmount 输入 OKB 数量(wei)
|
|
45
|
+
* @param minOutputAmount 最小输出代币数量(默认 0)
|
|
46
|
+
* @param extensionData 扩展数据(默认 0x)
|
|
47
|
+
*/
|
|
48
|
+
export function encodeBuyCallV3(tokenAddress, inputAmount, minOutputAmount = 0n, extensionData = '0x') {
|
|
49
|
+
return portalIface.encodeFunctionData('swapExactInputV3', [
|
|
50
|
+
{
|
|
51
|
+
inputToken: ZERO_ADDRESS,
|
|
52
|
+
outputToken: tokenAddress,
|
|
53
|
+
inputAmount,
|
|
54
|
+
minOutputAmount,
|
|
55
|
+
permitData: '0x',
|
|
56
|
+
extensionData,
|
|
57
|
+
},
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
36
60
|
/**
|
|
37
61
|
* 编码卖出调用(swapExactInput: token -> native)
|
|
38
62
|
*
|
|
@@ -216,10 +240,8 @@ export class PortalQuery {
|
|
|
216
240
|
quoteTokenAddress: raw[9],
|
|
217
241
|
nativeToQuoteSwapEnabled: raw[10],
|
|
218
242
|
extensionID: raw[11],
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
progress: raw[14],
|
|
222
|
-
lpFeeProfile: Number(raw[15]),
|
|
243
|
+
dexId: Number(raw[12]),
|
|
244
|
+
lpFeeProfile: Number(raw[13]),
|
|
223
245
|
};
|
|
224
246
|
}
|
|
225
247
|
/**
|
package/dist/xlayer/types.d.ts
CHANGED
|
@@ -80,8 +80,6 @@ export interface XLayerConfig {
|
|
|
80
80
|
gasLimit?: bigint;
|
|
81
81
|
/** Payer handleOps 交易的最小 gasPrice (Gwei) */
|
|
82
82
|
minGasPriceGwei?: number;
|
|
83
|
-
/** 单个 handleOps 交易中最多包含的 UserOp 数量(默认 5) */
|
|
84
|
-
maxOpsPerHandleOps?: number;
|
|
85
83
|
/** 利润尾笔原生转账 gasLimit(默认 21000n) */
|
|
86
84
|
profitTailGasLimit?: bigint;
|
|
87
85
|
/**
|
|
@@ -862,8 +860,16 @@ export interface BundleGraduateBuyParams {
|
|
|
862
860
|
walletAmounts?: Record<string, number>;
|
|
863
861
|
/** 是否启用外盘买入(默认 false) */
|
|
864
862
|
enableDexBuy?: boolean;
|
|
863
|
+
/** 内盘钱包地址列表(可选,传入则使用前端分组,不传则 SDK 自动分组) */
|
|
864
|
+
curveAddresses?: string[];
|
|
865
|
+
/** 外盘钱包地址列表(可选) */
|
|
866
|
+
dexAddresses?: string[];
|
|
867
|
+
/** 动态毕业阈值(可选,不传则使用默认 73.38 OKB) */
|
|
868
|
+
graduationAmount?: number;
|
|
865
869
|
/** 配置覆盖 */
|
|
866
870
|
config?: Partial<XLayerConfig>;
|
|
871
|
+
/** 扩展数据(传递给 swapExactInputV3,默认 0x) */
|
|
872
|
+
extensionData?: string;
|
|
867
873
|
/** Payer 起始 nonce */
|
|
868
874
|
payerStartNonce?: number;
|
|
869
875
|
/** beneficiary(默认 payer 地址) */
|