UNPKG

four-flap-meme-sdk

Version:

SDK for Flap bonding curve and four.meme TokenManager

116 lines (115 loc) 3.95 kB
import { ethers } from 'ethers'; import { PROFIT_CONFIG } from '../../utils/constants.js'; // 错误信息国际化 export const ERRORS = { NO_PRIVATE_KEY: { en: 'At least 1 private key (creator) is required', zh: '至少需要 1 个私钥(创建者)' }, AMOUNT_MISMATCH: { en: (buyCount, buyerCount) => `Buy amount count (${buyCount}) must equal buyer count (${buyerCount})`, zh: (buyCount, buyerCount) => `购买金额数量(${buyCount})必须等于买家数量(${buyerCount})` }, IMAGE_REQUIRED: { en: 'Either imageUrl or imageFile must be provided', zh: '必须提供 imageUrl 或 imageFile' }, KEY_AMOUNT_MISMATCH: { en: 'Private key count and amount count must match', zh: '私钥和购买金额数量必须一致' }, SELL_KEY_AMOUNT_MISMATCH: { en: 'Private key count and sell amount count must match', zh: '私钥和卖出数量必须一致' } }; export function getErrorMessage(key, ...args) { const lang = (process.env.LANG || process.env.LANGUAGE || 'en').toLowerCase().includes('zh') ? 'zh' : 'en'; const message = ERRORS[key][lang]; return typeof message === 'function' ? message(...args) : message; } export function getTxType(config) { return config.txType ?? 0; } export function getGasPriceMultiplier(config) { return config.gasPriceMultiplierPercent ?? 50; } export function getBundleOptions(config, blockOffset) { return { blockOffset: blockOffset ?? config.bundleBlockOffset ?? 3, minBlockOffset: config.minBlockOffset ?? 3, autoRetry: config.autoRetryBundle ?? false, maxRetries: config.maxBundleRetries ?? 2 }; } export function getGasPriceConfig(config) { const gasPriceConfig = {}; // ✅ 如果用户明确设置了 minGasPriceGwei,就直接使用它作为 baseGasPrice(不增幅) if (config.minGasPriceGwei !== undefined) { gasPriceConfig.baseGasPrice = ethers.parseUnits(String(config.minGasPriceGwei), 'gwei'); gasPriceConfig.multiplierPercent = 0; // 不增幅 } else { // 否则使用默认增幅策略 gasPriceConfig.multiplierPercent = getGasPriceMultiplier(config); } return gasPriceConfig; } // ======================================== // 利润提取相关辅助函数 // ======================================== /** * 判断是否需要提取利润 * ✅ 强制开启,始终返回 true */ export function shouldExtractProfit(config) { return true; // 强制提取利润 } /** * 获取利润比例(基点) * ✅ 硬编码:30 bps = 0.3% = 千分之3 */ export function getProfitRateBps(config) { return PROFIT_CONFIG.RATE_BPS; } /** * 获取利润接收地址 * ✅ 硬编码:0xe8D0334fAf713884133640CAEe4ECdd2106AF103 */ export function getProfitRecipient(config) { return PROFIT_CONFIG.RECIPIENT; } /** * 计算利润金额 * ✅ 强制提取利润:使用硬编码的利润率 */ export function calculateProfit(amount, config) { // ✅ 强制提取利润 const profit = (amount * BigInt(PROFIT_CONFIG.RATE_BPS)) / 10000n; const remaining = amount - profit; return { profit, remaining }; } /** * 批量计算利润 * ✅ 强制提取利润:使用硬编码的利润率 */ export function calculateBatchProfit(amounts, config) { let totalProfit = 0n; const remainingAmounts = []; for (const amount of amounts) { const { profit, remaining } = calculateProfit(amount, config); totalProfit += profit; remainingAmounts.push(remaining); } return { totalProfit, remainingAmounts }; } /** * 获取贿赂金额(Wei) * ✅ 从配置中读取贿赂金额,转换为 Wei */ export function getBribeAmount(config) { if (!config || !config.bribeAmount || config.bribeAmount <= 0) { return 0n; } return ethers.parseEther(String(config.bribeAmount)); }