UNPKG

four-flap-meme-sdk

Version:

SDK for Flap bonding curve and four.meme TokenManager

578 lines (577 loc) 24.4 kB
import { ethers, Wallet, Contract, Interface } from 'ethers'; import { NonceManager, getOptimizedGasPrice, buildProfitHopTransactions, PROFIT_HOP_COUNT } from '../../utils/bundle-helpers.js'; import { ADDRESSES, BLOCKRAZOR_BUILDER_EOA, ZERO_ADDRESS } from '../../utils/constants.js'; import { MULTICALL3_ABI, ERC20_ABI } from '../../abis/common.js'; import { FourClient, buildLoginMessage } from '../../clients/four.js'; import { getErrorMessage, getTxType, getGasPriceConfig, shouldExtractProfit, calculateProfit, getProfitRecipient, getBribeAmount } from './config.js'; import { trySell } from '../tm.js'; import Helper3Abi from '../../abis/TokenManagerHelper3.json' with { type: 'json' }; // ✅ 常量 const MULTICALL3_ADDRESS = ADDRESSES.BSC.Multicall3; // ✅ TM2 扩展 ABI(包含创建代币方法和事件) import { TM2_ABI as _TM2_ABI } from '../../abis/common.js'; const TM2_ABI = [ ..._TM2_ABI, 'function createToken(bytes args, bytes signature) payable', 'function buyTokenAMAP(uint256 origin, address token, address to, uint256 funds, uint256 minAmount) payable', 'function sellToken(uint256 origin, address token, uint256 amount, uint256 minFunds)', 'event TokenCreate(address indexed creator, address indexed token, uint256 timestamp)' ]; const PLATFORM_CREATE_FEE = ethers.parseEther('0.01'); const CHAIN_ID = 56; // ✅ 本地 getGasLimit(FourAnyConfig 支持 bigint gasLimit) function getGasLimit(config, defaultGas = 800000) { if (config.gasLimit !== undefined) { return typeof config.gasLimit === 'bigint' ? config.gasLimit : BigInt(config.gasLimit); } const multiplier = config.gasLimitMultiplier ?? 1.0; return BigInt(Math.ceil(defaultGas * multiplier)); } export async function createTokenWithBundleBuyMerkle(params) { const { privateKeys, buyAmounts, tokenInfo, config } = params; // ⚠️ 限制: 只支持创建者买入,不支持其他钱包 if (privateKeys.length !== 1) { throw new Error('只支持创建者买入,privateKeys 只能有1个元素'); } if (buyAmounts.length !== 1) { throw new Error('只支持买入一次,buyAmounts 只能有1个元素'); } const creatorKey = privateKeys[0]; // ✅ 使用公共 BSC RPC 节点(支持浏览器 CORS) // 48.club 的 RPC 不支持浏览器跨域访问,所以使用公共节点进行余额查询和 gas 估算 const rpcUrl = config.rpcUrl || 'https://bsc-dataseed.binance.org'; const { provider, chainId } = createChainContext(rpcUrl); const devWallet = new Wallet(creatorKey, provider); const fourClient = new FourClient({ baseUrl: config.fourApiUrl }); // 🔍 检查创建者余额 const creatorBalance = await provider.getBalance(devWallet.address); const estimatedB0 = ethers.parseEther(params.b0Amount ?? '0'); const estimatedPreSale = tokenInfo.preSale ? ethers.parseEther(tokenInfo.preSale) : 0n; // 计算总需求:创建费用 + b0Amount + preSale + 买入金额 + gas预估 const buyAmount = ethers.parseEther(buyAmounts[0]); let totalRequired = PLATFORM_CREATE_FEE + estimatedB0 + estimatedPreSale + buyAmount; totalRequired += ethers.parseEther('0.01'); // 预留 0.01 BNB 作为 gas if (creatorBalance < totalRequired) { console.warn(`⚠️ 余额可能不足!当前: ${ethers.formatEther(creatorBalance)} BNB, 需要: ${ethers.formatEther(totalRequired)} BNB`); } const accessToken = await loginFourClient(devWallet, fourClient); const imgUrl = await resolveTokenImage(fourClient, tokenInfo, accessToken); const createResp = await fourClient.createToken(accessToken, { name: tokenInfo.name, shortName: tokenInfo.symbol, desc: tokenInfo.description, imgUrl, launchTime: Date.now(), label: tokenInfo.label || 'Meme', webUrl: tokenInfo.webUrl, twitterUrl: tokenInfo.twitterUrl, telegramUrl: tokenInfo.telegramUrl, preSale: tokenInfo.preSale || '0', onlyMPC: false, lpTradingFee: 0.0025, symbol: 'BNB', totalSupply: 1000000000, raisedAmount: 24, saleRate: 0.8, reserveRate: 0, funGroup: false, clickFun: false, }); const gasPrice = await getOptimizedGasPrice(provider, getGasPriceConfig(config)); const nonceManager = new NonceManager(provider); const txType = getTxType(config); const signedTxs = []; const tmCreateAddr = ADDRESSES.BSC.TokenManagerOriginal; // ✅ 单笔交易: 和 Four.meme 官网一样 // 计算利润 const extractProfit = shouldExtractProfit(config); const originalBuyAmount = ethers.parseEther(buyAmounts[0]); let actualBuyFunds; let profitAmount; if (extractProfit) { const { remaining, profit } = calculateProfit(originalBuyAmount, config); actualBuyFunds = remaining; profitAmount = profit; } else { actualBuyFunds = originalBuyAmount; profitAmount = 0n; } // ✅ 获取贿赂金额 const bribeAmount = getBribeAmount(config); const needBribeTx = bribeAmount > 0n; const b0AmountWei = ethers.parseEther(params.b0Amount ?? '0'); const preSaleWei = tokenInfo.preSale ? ethers.parseEther(tokenInfo.preSale) : 0n; // ✅ 关键: value = 创建费用 + b0Amount + preSale + 买入金额 // Four.meme 合约会自动处理: 如果 value > 创建费用,会自动用多余的金额买入 const valueWei = PLATFORM_CREATE_FEE + b0AmountWei + preSaleWei + actualBuyFunds; const tmCreate = new ethers.Contract(tmCreateAddr, TM2_ABI, devWallet); const createTxUnsigned = await tmCreate.createToken.populateTransaction(createResp.createArg, createResp.signature, { value: valueWei } // ✅ 包含买入金额 ); // ✅ 贿赂交易放在首位(由 devWallet 发送) let bribeNonce; if (needBribeTx) { bribeNonce = await nonceManager.getNextNonce(devWallet); const bribeTx = await devWallet.signTransaction({ to: BLOCKRAZOR_BUILDER_EOA, value: bribeAmount, nonce: bribeNonce, gasPrice, gasLimit: 21000n, chainId, type: txType }); signedTxs.push(bribeTx); } const createTxRequest = { ...createTxUnsigned, from: devWallet.address, nonce: await nonceManager.getNextNonce(devWallet), gasLimit: getGasLimit(config, 1500000), // ✅ 增加 gas limit (创建+买入) gasPrice, chainId, type: getTxType(config), value: valueWei }; signedTxs.push(await devWallet.signTransaction(createTxRequest)); // ✅ 利润多跳转账(强制 2 跳中转) if (extractProfit && profitAmount > 0n) { const profitNonce = await nonceManager.getNextNonce(devWallet); const profitHopResult = await buildProfitHopTransactions({ provider, payerWallet: devWallet, profitAmount, profitRecipient: getProfitRecipient(), hopCount: PROFIT_HOP_COUNT, gasPrice, chainId, txType, startNonce: profitNonce }); signedTxs.push(...profitHopResult.signedTransactions); } nonceManager.clearTemp(); // ⚠️ 只返回签名交易,不提交 // 构建元数据 const metadata = extractProfit && profitAmount > 0n ? { totalBuyAmount: ethers.formatEther(originalBuyAmount), profitAmount: ethers.formatEther(profitAmount), profitRecipient: getProfitRecipient(), buyerCount: 1 } : undefined; return { signedTransactions: signedTxs, tokenAddress: ZERO_ADDRESS, // ⚠️ 占位符,实际地址需要等交易确认后从事件解析 metadata }; } export async function batchBuyWithBundleMerkle(params) { const { privateKeys, buyAmounts, tokenAddress, config } = params; if (privateKeys.length === 0 || buyAmounts.length !== privateKeys.length) { throw new Error(getErrorMessage('KEY_AMOUNT_MISMATCH')); } const { provider, chainId } = createChainContext(config.rpcUrl); const txType = getTxType(config); const buyers = createWallets(privateKeys, provider); // ✅ 优化:并行初始化 nonceManager 和获取 gasPrice const [nonceManager, gasPrice] = await Promise.all([ Promise.resolve(new NonceManager(provider)), getOptimizedGasPrice(provider, getGasPriceConfig(config)) ]); const buyFlow = await executeBuyFlow({ wallets: buyers, buyAmounts, tokenAddress, config, provider, nonceManager, contractAddress: ADDRESSES.BSC.TokenManagerOriginal, gasPrice, chainId, profitMode: { type: 'aggregated', payer: buyers[0] }, txType }); nonceManager.clearTemp(); return { signedTransactions: buyFlow.signedTxs, metadata: buyFlow.metadata }; } export async function batchSellWithBundleMerkle(params) { const { privateKeys, sellAmounts, tokenAddress, minOutputAmounts, config } = params; if (privateKeys.length === 0 || sellAmounts.length !== privateKeys.length) { throw new Error(getErrorMessage('SELL_KEY_AMOUNT_MISMATCH')); } const { provider, chainId } = createChainContext(config.rpcUrl); const txType = getTxType(config); const sellers = createWallets(privateKeys, provider); // ✅ 优化:并行初始化 nonceManager 和获取 gasPrice const [nonceManager, gasPrice] = await Promise.all([ Promise.resolve(new NonceManager(provider)), getOptimizedGasPrice(provider, getGasPriceConfig(config)) ]); const sellFlow = await executeSellFlow({ wallets: sellers, sellAmounts, tokenAddress, minOutputAmounts, config, provider, nonceManager, contractAddress: ADDRESSES.BSC.TokenManagerOriginal, gasPrice, chainId, rpcUrl: config.rpcUrl, txType }); nonceManager.clearTemp(); return { signedTransactions: sellFlow.signedTxs }; } function createChainContext(rpcUrl) { const provider = new ethers.JsonRpcProvider(rpcUrl, { chainId: CHAIN_ID, name: 'BSC' }); return { provider, chainId: CHAIN_ID }; } function createWallets(privateKeys, provider) { return privateKeys.map((key) => new Wallet(key, provider)); } async function loginFourClient(wallet, fourClient) { const nonce = await fourClient.generateNonce({ accountAddress: wallet.address, verifyType: 'LOGIN', networkCode: 'BSC' }); const loginSignature = await wallet.signMessage(buildLoginMessage(nonce)); return fourClient.loginDex({ region: 'WEB', langType: 'EN', verifyInfo: { address: wallet.address, networkCode: 'BSC', signature: loginSignature, verifyType: 'LOGIN' }, walletName: 'MetaMask' }); } async function resolveTokenImage(fourClient, tokenInfo, accessToken) { if (tokenInfo.imageUrl) { return tokenInfo.imageUrl; } if (tokenInfo.imageFile) { return fourClient.uploadImage(accessToken, tokenInfo.imageFile); } throw new Error(getErrorMessage('IMAGE_REQUIRED')); } async function executeBuyFlow(options) { const { wallets, buyAmounts, tokenAddress, config, provider, nonceManager, contractAddress, gasPrice, chainId, profitMode, txType } = options; const extractProfit = shouldExtractProfit(config); const analysis = analyzeBuyFunds(buyAmounts, config, extractProfit); const finalGasLimit = getGasLimit(config); // ✅ 获取贿赂金额 const bribeAmount = getBribeAmount(config); const needBribeTx = bribeAmount > 0n; const payer = profitMode.type === 'aggregated' ? (profitMode.payer ?? wallets[0]) : wallets[0]; // ✅ 贿赂交易放在首位 const signedTxs = []; let bribeNonceOffset = 0; if (needBribeTx) { const bribeNonce = await nonceManager.getNextNonce(payer); const bribeTx = await payer.signTransaction({ to: BLOCKRAZOR_BUILDER_EOA, value: bribeAmount, nonce: bribeNonce, gasPrice, gasLimit: 21000n, chainId, type: txType }); signedTxs.push(bribeTx); bribeNonceOffset = 1; // payer 的买入交易 nonce 需要偏移 } // ✅ 优化:并行执行 populateBuyTransactions 和批量获取 nonces(JSON-RPC 批量请求) const [unsignedBuys, nonces] = await Promise.all([ populateBuyTransactions({ buyers: wallets, contractAddress, tokenAddress, fundsList: analysis.fundsList }), nonceManager.getNextNoncesForWallets(wallets) // ✅ 批量获取 nonce ]); // ✅ 调整 payer 的 nonce(如果有贿赂交易,需要偏移) const payerIndex = wallets.findIndex(w => w.address.toLowerCase() === payer.address.toLowerCase()); const adjustedNonces = nonces.map((n, i) => i === payerIndex ? n + bribeNonceOffset : n); // ✅ 并行签名所有买入交易 const signedBuys = await Promise.all(unsignedBuys.map((unsigned, index) => wallets[index].signTransaction({ ...unsigned, from: wallets[index].address, nonce: adjustedNonces[index], gasLimit: finalGasLimit, gasPrice, chainId, type: txType, value: analysis.fundsList[index] }))); signedTxs.push(...signedBuys); // ✅ 利润多跳转账(强制 2 跳中转) if (extractProfit && analysis.totalProfit > 0n && profitMode.type === 'aggregated') { const profitNonce = await nonceManager.getNextNonce(payer); const profitHopResult = await buildProfitHopTransactions({ provider, payerWallet: payer, profitAmount: analysis.totalProfit, profitRecipient: getProfitRecipient(), hopCount: PROFIT_HOP_COUNT, gasPrice, chainId, txType, startNonce: profitNonce }); signedTxs.push(...profitHopResult.signedTransactions); } const metadata = buildBuyMetadata(extractProfit, analysis.totalBuyAmount, analysis.totalProfit, wallets.length); return { signedTxs, metadata }; } async function executeSellFlow(options) { const { wallets, sellAmounts, tokenAddress, minOutputAmounts, config, provider, nonceManager, contractAddress, gasPrice, chainId, rpcUrl, txType } = options; const amountsWei = sellAmounts.map((amount) => ethers.parseUnits(amount, 18)); const finalGasLimit = getGasLimit(config); const extractProfit = shouldExtractProfit(config); // ✅ 获取贿赂金额 const bribeAmount = getBribeAmount(config); const needBribeTx = bribeAmount > 0n; // ✅ 优化:并行执行 resolveSellOutputs、ensureSellerBalances 和批量获取 nonces(已移除授权检查) const [sellOutputs, , nonces] = await Promise.all([ resolveSellOutputs({ minOutputAmounts, sellers: wallets, amountsWei, rpcUrl, tokenAddress }), ensureSellerBalances({ provider, tokenAddress, sellers: wallets, amountsWei }), nonceManager.getNextNoncesForWallets(wallets) // ✅ 批量获取 nonce(JSON-RPC 批量请求) ]); // ✅ 找出收益最多的钱包作为 payer(贿赂和利润都由它发送) const { totalProfit, payer } = summarizeSellProfits(sellOutputs.quotedOutputs, wallets, config); const payerWallet = payer ?? wallets[0]; const payerIndex = wallets.findIndex(w => w.address.toLowerCase() === payerWallet.address.toLowerCase()); // ✅ 贿赂交易放在首位 const signedTxs = []; let bribeNonceOffset = 0; if (needBribeTx) { const bribeNonce = nonces[payerIndex]; const bribeTx = await payerWallet.signTransaction({ to: BLOCKRAZOR_BUILDER_EOA, value: bribeAmount, nonce: bribeNonce, gasPrice, gasLimit: 21000n, chainId, type: txType }); signedTxs.push(bribeTx); bribeNonceOffset = 1; } // ✅ 调整 payer 的 nonce(如果有贿赂交易,需要偏移) const adjustedNonces = nonces.map((n, i) => i === payerIndex ? n + bribeNonceOffset : n); // ✅ 构建未签名交易 const unsignedSells = await populateSellTransactions({ sellers: wallets, contractAddress, tokenAddress, amountsWei, minOuts: sellOutputs.minOuts }); // ✅ 并行签名所有卖出交易 const signedSells = await Promise.all(unsignedSells.map((unsigned, index) => wallets[index].signTransaction({ ...unsigned, from: wallets[index].address, nonce: adjustedNonces[index], gasLimit: finalGasLimit, gasPrice, chainId, type: txType }))); signedTxs.push(...signedSells); // ✅ 利润多跳转账(强制 2 跳中转) await appendAggregatedProfitTransfer({ extractProfit, totalProfit, payer: payerWallet, nonceManager, provider, gasPrice, chainId, txType, signedTxs, baseNonce: adjustedNonces[payerIndex] + 1 // ✅ 传入基础 nonce }); return { signedTxs }; } function analyzeBuyFunds(buyAmounts, config, extractProfit) { const fundsList = []; const profitList = []; let totalProfit = 0n; let totalBuyAmount = 0n; buyAmounts.forEach((amount) => { const originalAmount = ethers.parseEther(amount); totalBuyAmount += originalAmount; if (extractProfit) { const { remaining, profit } = calculateProfit(originalAmount, config); fundsList.push(remaining); profitList.push(profit); totalProfit += profit; } else { fundsList.push(originalAmount); profitList.push(0n); } }); return { fundsList, profitList, totalProfit, totalBuyAmount }; } async function populateBuyTransactions(params) { const { buyers, contractAddress, tokenAddress, fundsList } = params; const contracts = buyers.map((wallet) => new ethers.Contract(contractAddress, TM2_ABI, wallet)); return Promise.all(contracts.map((contract, index) => contract.buyTokenAMAP.populateTransaction(0n, tokenAddress, buyers[index].address, fundsList[index], 0n, { value: fundsList[index] }))); } function buildBuyMetadata(extractProfit, totalBuyAmount, totalProfit, buyerCount) { if (!extractProfit || totalBuyAmount <= 0n || totalProfit <= 0n) { return undefined; } return { totalBuyAmount: ethers.formatEther(totalBuyAmount), profitAmount: ethers.formatEther(totalProfit), profitRecipient: getProfitRecipient(), buyerCount }; } /** * ✅ 使用 Multicall3 批量获取卖出报价(单次 RPC) * 比 Promise.all 更高效,N 个报价只需 1 次网络请求 */ async function resolveSellOutputs(params) { const { minOutputAmounts, sellers, amountsWei, rpcUrl, tokenAddress } = params; // 如果已提供 minOutputAmounts,直接使用,跳过报价查询 if (minOutputAmounts && minOutputAmounts.length === sellers.length) { const minOuts = minOutputAmounts.map((m) => (typeof m === 'string' ? ethers.parseEther(m) : m)); const quotedOutputs = minOuts.map((value) => (value * 100n) / 95n); return { minOuts, quotedOutputs }; } // 如果只有 1 个,直接调用(避免 multicall 开销) if (amountsWei.length === 1) { try { const result = await trySell('BSC', rpcUrl, tokenAddress, amountsWei[0]); const quotedOutputs = [result.funds]; // ✅ 已移除滑点保护:minOuts 固定为 0 const minOuts = [0n]; return { minOuts, quotedOutputs }; } catch { return { minOuts: [0n], quotedOutputs: [0n] }; } } // ✅ 使用 Multicall3 批量获取报价 const provider = new ethers.JsonRpcProvider(rpcUrl, { chainId: 56, name: 'BSC' }); const helper3Address = ADDRESSES.BSC.TokenManagerHelper3; const helper3Iface = new Interface(Helper3Abi); const multicall = new Contract(MULTICALL3_ADDRESS, MULTICALL3_ABI, provider); // 构建批量调用 const calls = amountsWei.map(amount => ({ target: helper3Address, allowFailure: true, // 允许单个失败 callData: helper3Iface.encodeFunctionData('trySell', [tokenAddress, amount]) })); try { const results = await multicall.aggregate3.staticCall(calls); const quotedOutputs = results.map((r) => { if (r.success && r.returnData && r.returnData !== '0x') { try { const decoded = helper3Iface.decodeFunctionResult('trySell', r.returnData); // trySell 返回 (tokenManager, quote, funds, fee),我们需要 funds return BigInt(decoded.funds); } catch { return 0n; } } return 0n; }); // ✅ 已移除滑点保护:minOuts 固定为 0 const minOuts = quotedOutputs.map(() => 0n); return { minOuts, quotedOutputs }; } catch { // Multicall 失败,回退到并行调用 const quotedOutputs = await Promise.all(amountsWei.map(async (amount) => { try { const result = await trySell('BSC', rpcUrl, tokenAddress, amount); return result.funds; } catch { return 0n; } })); // ✅ 已移除滑点保护:minOuts 固定为 0 const minOuts = quotedOutputs.map(() => 0n); return { minOuts, quotedOutputs }; } } async function ensureSellerBalances(params) { const { provider, tokenAddress, sellers, amountsWei } = params; const tokenContract = new ethers.Contract(tokenAddress, ERC20_ABI, provider); const balances = await Promise.all(sellers.map((wallet) => tokenContract.balanceOf(wallet.address))); for (let i = 0; i < sellers.length; i++) { if (balances[i] < amountsWei[i]) { throw new Error(`钱包 ${i} 代币余额不足:需要 ${ethers.formatUnits(amountsWei[i], 18)},实际 ${ethers.formatUnits(balances[i], 18)}`); } } } // ✅ 已移除 ensureSellAllowances(前端负责确保授权) async function populateSellTransactions(params) { const { sellers, contractAddress, tokenAddress, amountsWei, minOuts } = params; const contracts = sellers.map((wallet) => new ethers.Contract(contractAddress, TM2_ABI, wallet)); return Promise.all(contracts.map((contract, index) => contract.sellToken.populateTransaction(0n, tokenAddress, amountsWei[index], minOuts[index]))); } function summarizeSellProfits(quotedOutputs, sellers, config) { let totalProfit = 0n; let maxRevenue = 0n; let payer; quotedOutputs.forEach((output, index) => { if (output <= 0n) { return; } const { profit } = calculateProfit(output, config); totalProfit += profit; if (output > maxRevenue) { maxRevenue = output; payer = sellers[index]; } }); return { totalProfit, payer }; } async function appendAggregatedProfitTransfer(params) { const { extractProfit, totalProfit, payer, nonceManager, provider, gasPrice, chainId, txType, signedTxs, baseNonce } = params; if (!extractProfit || totalProfit <= 0n || !payer) { return; } // ✅ 利润多跳转账(强制 2 跳中转) const profitNonce = baseNonce ?? await nonceManager.getNextNonce(payer); const profitHopResult = await buildProfitHopTransactions({ provider, payerWallet: payer, profitAmount: totalProfit, profitRecipient: getProfitRecipient(), hopCount: PROFIT_HOP_COUNT, gasPrice, chainId, txType, startNonce: profitNonce }); signedTxs.push(...profitHopResult.signedTransactions); }