four-flap-meme-sdk 1.4.97 → 1.4.98

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,29 @@ 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
+ return buyers.map(b => parseQuoteAmount(String(b.buyAmount), useNativeToken, quoteTokenDecimals));
180
+ }
181
+ const totalWei = parseQuoteAmount(totalAmountStr, useNativeToken, quoteTokenDecimals);
182
+ return splitAmount(totalWei, buyers.length);
183
+ }
168
184
  // ==================== 主函数 ====================
169
185
  /**
170
186
  * Flap 发币 + 一键买到外盘捆绑交易
@@ -231,17 +247,11 @@ export async function flapBundleCreateToDex(params) {
231
247
  const bribeAmount = getBribeAmount(config);
232
248
  const needBribeTx = bribeAmount > 0n;
233
249
  // ✅ 计算内盘买入金额
234
- const curveTotalWei = useNativeToken
235
- ? ethers.parseEther(curveTotalBuyAmount)
236
- : ethers.parseUnits(curveTotalBuyAmount, quoteTokenDecimals);
237
- const curveBuyAmounts = splitAmount(curveTotalWei, curveBuyers.length);
250
+ const curveBuyAmounts = resolveBuyerAmounts(curveBuyers, curveTotalBuyAmount, useNativeToken, quoteTokenDecimals);
238
251
  // ✅ 计算外盘买入金额
239
252
  let dexBuyAmounts = [];
240
253
  if (enableDexBuy && dexBuyers.length > 0 && dexTotalBuyAmount) {
241
- const dexTotalWei = useNativeToken
242
- ? ethers.parseEther(dexTotalBuyAmount)
243
- : ethers.parseUnits(dexTotalBuyAmount, quoteTokenDecimals);
244
- dexBuyAmounts = splitAmount(dexTotalWei, dexBuyers.length);
254
+ dexBuyAmounts = resolveBuyerAmounts(dexBuyers, dexTotalBuyAmount, useNativeToken, quoteTokenDecimals);
245
255
  }
246
256
  // ✅ 计算利润
247
257
  const totalBuyAmount = curveBuyAmounts.reduce((a, b) => a + b, 0n)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.4.97",
3
+ "version": "1.4.98",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",