four-flap-meme-sdk 1.4.97 → 1.4.99
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.
|
@@ -158,13 +158,43 @@ function splitAmount(totalAmount, count) {
|
|
|
158
158
|
allocated += amount;
|
|
159
159
|
}
|
|
160
160
|
amounts.push(totalAmount - allocated);
|
|
161
|
-
// 随机打乱
|
|
162
|
-
for (let i = amounts.length - 1; i > 0; i--) {
|
|
163
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
164
|
-
[amounts[i], amounts[j]] = [amounts[j], amounts[i]];
|
|
165
|
-
}
|
|
166
161
|
return amounts;
|
|
167
162
|
}
|
|
163
|
+
/** 解析输入金额字符串为 bigint(按是否原生币选择 parseEther / parseUnits) */
|
|
164
|
+
function parseQuoteAmount(amount, useNativeToken, quoteTokenDecimals) {
|
|
165
|
+
const v = String(amount ?? '').trim();
|
|
166
|
+
if (!v)
|
|
167
|
+
return 0n;
|
|
168
|
+
return useNativeToken ? ethers.parseEther(v) : ethers.parseUnits(v, quoteTokenDecimals);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* 计算每个买家的买入金额(优先使用 params 传入的 buyAmount;否则 fallback 到按总额随机拆分)
|
|
172
|
+
* 说明:memeweb 前端已根据 average/random/custom 计算出每个钱包 buyAmount;
|
|
173
|
+
* 若 SDK 这里再次 splitAmount,会导致实际 buyAmount 与 UI/预期不一致,并引发余额不足(insufficient funds)。
|
|
174
|
+
*/
|
|
175
|
+
function resolveBuyerAmounts(buyers, totalAmountStr, useNativeToken, quoteTokenDecimals) {
|
|
176
|
+
const provided = buyers.map(b => b?.buyAmount).filter(v => v != null && String(v).trim() !== '');
|
|
177
|
+
const allProvided = provided.length === buyers.length;
|
|
178
|
+
if (allProvided) {
|
|
179
|
+
// ✅ 所有买家都显式提供了 buyAmount:按传入金额构建,但要修正“浮点/四舍五入导致总和偏差”
|
|
180
|
+
const amounts = buyers.map(b => parseQuoteAmount(String(b.buyAmount), useNativeToken, quoteTokenDecimals));
|
|
181
|
+
const totalWei = parseQuoteAmount(totalAmountStr, useNativeToken, quoteTokenDecimals);
|
|
182
|
+
const sum = amounts.reduce((a, b) => a + b, 0n);
|
|
183
|
+
const delta = totalWei - sum;
|
|
184
|
+
if (delta !== 0n && amounts.length > 0) {
|
|
185
|
+
// 优先修正最后一个金额(对应 create-to-dex 里“最后一单可能触发毕业”的假设)
|
|
186
|
+
const lastIdx = amounts.length - 1;
|
|
187
|
+
const next = amounts[lastIdx] + delta;
|
|
188
|
+
if (next <= 0n) {
|
|
189
|
+
throw new Error(`买入金额总和与总额不一致且无法修正:sum=${sum} total=${totalWei} delta=${delta}`);
|
|
190
|
+
}
|
|
191
|
+
amounts[lastIdx] = next;
|
|
192
|
+
}
|
|
193
|
+
return amounts;
|
|
194
|
+
}
|
|
195
|
+
const totalWei = parseQuoteAmount(totalAmountStr, useNativeToken, quoteTokenDecimals);
|
|
196
|
+
return splitAmount(totalWei, buyers.length);
|
|
197
|
+
}
|
|
168
198
|
// ==================== 主函数 ====================
|
|
169
199
|
/**
|
|
170
200
|
* Flap 发币 + 一键买到外盘捆绑交易
|
|
@@ -231,17 +261,11 @@ export async function flapBundleCreateToDex(params) {
|
|
|
231
261
|
const bribeAmount = getBribeAmount(config);
|
|
232
262
|
const needBribeTx = bribeAmount > 0n;
|
|
233
263
|
// ✅ 计算内盘买入金额
|
|
234
|
-
const
|
|
235
|
-
? ethers.parseEther(curveTotalBuyAmount)
|
|
236
|
-
: ethers.parseUnits(curveTotalBuyAmount, quoteTokenDecimals);
|
|
237
|
-
const curveBuyAmounts = splitAmount(curveTotalWei, curveBuyers.length);
|
|
264
|
+
const curveBuyAmounts = resolveBuyerAmounts(curveBuyers, curveTotalBuyAmount, useNativeToken, quoteTokenDecimals);
|
|
238
265
|
// ✅ 计算外盘买入金额
|
|
239
266
|
let dexBuyAmounts = [];
|
|
240
267
|
if (enableDexBuy && dexBuyers.length > 0 && dexTotalBuyAmount) {
|
|
241
|
-
|
|
242
|
-
? ethers.parseEther(dexTotalBuyAmount)
|
|
243
|
-
: ethers.parseUnits(dexTotalBuyAmount, quoteTokenDecimals);
|
|
244
|
-
dexBuyAmounts = splitAmount(dexTotalWei, dexBuyers.length);
|
|
268
|
+
dexBuyAmounts = resolveBuyerAmounts(dexBuyers, dexTotalBuyAmount, useNativeToken, quoteTokenDecimals);
|
|
245
269
|
}
|
|
246
270
|
// ✅ 计算利润
|
|
247
271
|
const totalBuyAmount = curveBuyAmounts.reduce((a, b) => a + b, 0n)
|