#!/usr/bin/env node /** * Create Pool from Real-Time Prices Script * Fetches real-time prices from CoinGecko and creates a new pool on the chain specified (could be mockchain, sepolia or prod) */ import { ethers } from 'ethers'; import { readFile } from 'fs/promises'; import IPartyPlannerABI from "../src/contracts/IPartyPlannerABI.ts"; // ============================================================================ // CONFIGURATION // ============================================================================ const RPC_URL = "https://eth-sepolia.g.alchemy.com/v2/sJ7rLYKJzdRqXUs9cOiLVvHN8aTI30dn" // sepolia dev wallet const PRIVATE_KEY = '89c8f2542b5ff7f3cf0b73255e0a8d79d89c2be598e7f272a275a380ff56a212'; const RECEIVER_ADDRESS = '0xd3b310bd32d782f89eea49cb79656bcaccde7213'; // Test token addresses (mapping to real coins) const TEST_TOKENS = { USDC: { address: '0xCb2F4B07eFe0F06264AD28590aC7f03D5cdb0729', // USXD (mock) = USDC (real) coingeckoId: 'usd-coin', decimals: 6 }, BTC: { address: '0x19Ba3A189dc3DEbC07ADF7757dc8170702E91696', // BUTC (mock) = BTC (real) coingeckoId: 'bitcoin', decimals: 8 }, WETH: { address: '0xd406e1a6b028D17b72f826E45bF36BB8Ad4077dB', // WTETH (mock) = WETH (real) coingeckoId: 'weth', decimals: 18 } }; // Default pool parameters const DEFAULT_POOL_PARAMS = { name: 'Balanced Portfolio Pool', symbol: 'BPP', kappa: ethers.BigNumber.from('100000000000000000'), // 0.1 * 1e18 = 1e17 swapFeesPpm: [ 50, // 0.0050% 2450, // 0.2450% 2950, // 0.2950% ], flashFeePpm: 5, // 0.0005% stable: false, initialLpAmount: ethers.BigNumber.from('1000000000000000000') // 1e18 }; // Input amount in USD const INPUT_USD_AMOUNT = 100; // ============================================================================ // LOAD ABIs AND CONFIG // ============================================================================ const chainInfoData = JSON.parse(await readFile(new URL('../src/contracts/liqp-deployments.json', import.meta.url), 'utf-8')); const PARTY_PLANNER_ADDRESS = chainInfoData['11155111'].v1.PartyPlanner; const ERC20ABI = [ { "type": "function", "name": "balanceOf", "stateMutability": "view", "inputs": [{ "name": "account", "type": "address" }], "outputs": [{ "name": "", "type": "uint256" }] }, { "type": "function", "name": "decimals", "stateMutability": "view", "inputs": [], "outputs": [{ "name": "", "type": "uint8" }] }, { "type": "function", "name": "approve", "stateMutability": "nonpayable", "inputs": [{ "name": "spender", "type": "address" }, { "name": "amount", "type": "uint256" }], "outputs": [{ "name": "", "type": "bool" }] } ]; // ============================================================================ // HELPER FUNCTIONS // ============================================================================ /** * Fetch real-time prices from CoinGecko API */ async function fetchCoinGeckoPrices() { try { const ids = Object.values(TEST_TOKENS).map(t => t.coingeckoId).join(','); const url = `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`; console.log(`[~] Fetching prices from CoinGecko...`); const response = await fetch(url); if (!response.ok) { throw new Error(`CoinGecko API request failed: ${response.statusText}`); } const data = await response.json(); const prices = { USDC: data['usd-coin']?.usd || 1, BTC: data['bitcoin']?.usd || 0, WETH: data['weth']?.usd || 0 }; if (prices.BTC === 0 || prices.WETH === 0) { throw new Error('Failed to fetch valid prices from CoinGecko'); } console.log(`[+] Prices fetched successfully:`); console.log(` USDC: $${prices.USDC}`); console.log(` BTC: $${prices.BTC.toLocaleString()}`); console.log(` WETH: $${prices.WETH.toLocaleString()}`); return prices; } catch (error) { console.error(`[!] Error fetching prices:`, error.message); throw error; } } /** * Calculate token amounts based on equal USD distribution */ function calculateTokenAmounts(prices, usdAmount) { const usdPerToken = usdAmount / 3; // Equally distribute among 3 tokens // Calculate raw amounts const usdcAmount = usdPerToken / prices.USDC; const btcAmount = usdPerToken / prices.BTC; const wethAmount = usdPerToken / prices.WETH; // Convert to BigNumber with proper decimals const usdcAmountBN = ethers.utils.parseUnits(usdcAmount.toFixed(TEST_TOKENS.USDC.decimals), TEST_TOKENS.USDC.decimals); const btcAmountBN = ethers.utils.parseUnits(btcAmount.toFixed(TEST_TOKENS.BTC.decimals), TEST_TOKENS.BTC.decimals); const wethAmountBN = ethers.utils.parseUnits(wethAmount.toFixed(TEST_TOKENS.WETH.decimals), TEST_TOKENS.WETH.decimals); console.log(`\n[~] Calculated token amounts for $${usdAmount} ($${usdPerToken.toFixed(2)} per token):`); console.log(` USDC: ${usdcAmount.toFixed(6)} USDC (${usdcAmountBN.toString()} wei)`); console.log(` BTC: ${btcAmount.toFixed(8)} BTC (${btcAmountBN.toString()} wei)`); console.log(` WETH: ${wethAmount.toFixed(8)} WETH (${wethAmountBN.toString()} wei)`); return { USDC: usdcAmountBN, BTC: btcAmountBN, WETH: wethAmountBN }; } /** * Check token balances */ async function checkBalances(provider, wallet, tokenAmounts) { console.log(`\n[~] Checking token balances for wallet: ${wallet.address}`); const balances = {}; let hasEnoughBalance = true; for (const [symbol, tokenInfo] of Object.entries(TEST_TOKENS)) { const tokenContract = new ethers.Contract(tokenInfo.address, ERC20ABI, provider); const balance = await tokenContract.balanceOf(wallet.address); const requiredAmount = tokenAmounts[symbol]; balances[symbol] = balance; const balanceFormatted = ethers.utils.formatUnits(balance, tokenInfo.decimals); const requiredFormatted = ethers.utils.formatUnits(requiredAmount, tokenInfo.decimals); const sufficient = balance.gte(requiredAmount); console.log(` ${symbol}: ${balanceFormatted} (required: ${requiredFormatted}) ${sufficient ? '✓' : '✗'}`); if (!sufficient) { hasEnoughBalance = false; } } if (!hasEnoughBalance) { console.log(`\n[!] Insufficient token balance. Please ensure your wallet has enough tokens.`); throw new Error('Insufficient token balance'); } console.log(`[+] All balances sufficient`); return balances; } /** * Approve tokens for the PartyPlanner contract */ async function approveTokens(wallet, tokenAmounts) { console.log(`\n[~] Approving tokens for PartyPlanner contract...`); for (const [symbol, tokenInfo] of Object.entries(TEST_TOKENS)) { const tokenContract = new ethers.Contract(tokenInfo.address, ERC20ABI, wallet); // Approve 1% more than needed to account for fees/slippage const requiredAmount = tokenAmounts[symbol]; const approvalAmount = requiredAmount.mul(101).div(100); // 1% buffer console.log(` [~] Approving ${symbol} (1% buffer)...`); try { const tx = await tokenContract.approve(PARTY_PLANNER_ADDRESS, approvalAmount); await tx.wait(); console.log(` [+] ${symbol} approved (tx: ${tx.hash})`); } catch (error) { console.error(` [!] Failed to approve ${symbol}:`, error.message); throw error; } } console.log(`[+] All tokens approved`); } /** * Create a new pool using cast send */ async function createPool(wallet, tokenAmounts) { console.log(`\n[~] Creating new pool...`); // Prepare parameters const tokenAddresses = [ TEST_TOKENS.USDC.address, TEST_TOKENS.BTC.address, TEST_TOKENS.WETH.address ]; const initialDeposits = [ tokenAmounts.USDC.toString(), tokenAmounts.BTC.toString(), tokenAmounts.WETH.toString() ]; // Set deadline to 1 hour from now const deadline = Math.floor(Date.now() / 1000) + 3600; console.log(`[~] Pool parameters:`); console.log(` Name: ${DEFAULT_POOL_PARAMS.name}`); console.log(` Symbol: ${DEFAULT_POOL_PARAMS.symbol}`); console.log(` Tokens: ${tokenAddresses.join(', ')}`); console.log(` Swap Fees PPM: [${DEFAULT_POOL_PARAMS.swapFeesPpm.join(', ')}]`); console.log(` Payer: ${wallet.address}`); console.log(` Receiver: ${RECEIVER_ADDRESS}`); console.log(` Deadline: ${new Date(deadline * 1000).toISOString()}`); // Build cast send command const castCommand = `cast send ${PARTY_PLANNER_ADDRESS} \ "newPool(string,string,address[],int128,uint256[],uint256,bool,address,address,uint256[],uint256,uint256)" \ "${DEFAULT_POOL_PARAMS.name}" \ "${DEFAULT_POOL_PARAMS.symbol}" \ "[${tokenAddresses.join(',')}]" \ ${DEFAULT_POOL_PARAMS.kappa.toString()} \ "[${DEFAULT_POOL_PARAMS.swapFeesPpm.join(',')}]" \ ${DEFAULT_POOL_PARAMS.flashFeePpm} \ ${DEFAULT_POOL_PARAMS.stable} \ ${wallet.address} \ ${RECEIVER_ADDRESS} \ "[${initialDeposits.join(',')}]" \ ${DEFAULT_POOL_PARAMS.initialLpAmount.toString()} \ ${deadline} \ --rpc-url ${RPC_URL} \ --private-key ${PRIVATE_KEY}`; console.log(`\n[~] Cast command:\n${castCommand}\n`); try { // Execute cast command const { execSync } = await import('child_process'); const output = execSync(castCommand, { encoding: 'utf-8' }); console.log(`[+] Pool created successfully!`); console.log(output); return output; } catch (error) { console.error(`[!] Failed to create pool:`, error.message); if (error.stderr) { console.error(` Error output: ${error.stderr.toString()}`); } throw error; } } /** * Print help message */ function printHelp() { console.log(` Usage: node create_pool_from_prices.js [OPTIONS] Options: --amount USD amount to distribute (default: ${INPUT_USD_AMOUNT}) --name Pool name (default: "${DEFAULT_POOL_PARAMS.name}") --symbol Pool symbol (default: "${DEFAULT_POOL_PARAMS.symbol}") --help, -h Show this help message Example: node create_pool_from_prices.js node create_pool_from_prices.js --amount 200 --name "My Pool" --symbol "MP" `); } // ============================================================================ // MAIN FUNCTION // ============================================================================ async function main() { console.log(`${'='.repeat(70)}`); console.log(`Create Pool from Real-Time Prices`); console.log(`${'='.repeat(70)}\n`); // Parse command line arguments const args = process.argv.slice(2); if (args.includes('--help') || args.includes('-h')) { printHelp(); process.exit(0); } let usdAmount = INPUT_USD_AMOUNT; let poolName = DEFAULT_POOL_PARAMS.name; let poolSymbol = DEFAULT_POOL_PARAMS.symbol; for (let i = 0; i < args.length; i++) { if (args[i] === '--amount' && i + 1 < args.length) { usdAmount = parseFloat(args[i + 1]); i++; } else if (args[i] === '--name' && i + 1 < args.length) { poolName = args[i + 1]; i++; } else if (args[i] === '--symbol' && i + 1 < args.length) { poolSymbol = args[i + 1]; i++; } } // Update pool params with parsed values DEFAULT_POOL_PARAMS.name = poolName; DEFAULT_POOL_PARAMS.symbol = poolSymbol; try { // Step 1: Fetch prices const prices = await fetchCoinGeckoPrices(); // Step 2: Calculate token amounts const tokenAmounts = calculateTokenAmounts(prices, usdAmount); // Step 3: Connect to wallet console.log(`\n[~] Connecting to test wallet at ${RPC_URL}...`); const provider = new ethers.providers.JsonRpcProvider(RPC_URL); const wallet = new ethers.Wallet(PRIVATE_KEY, provider); console.log(`[+] Connected. Using wallet: ${wallet.address}`); // Step 4: Check balances await checkBalances(provider, wallet, tokenAmounts); // Step 5: Approve tokens await approveTokens(wallet, tokenAmounts); // Step 6: Create pool await createPool(wallet, tokenAmounts); console.log(`\n${'='.repeat(70)}`); console.log(`Success! Pool created with real-time price-based deposits.`); console.log(`${'='.repeat(70)}\n`); } catch (error) { console.error(`\n${'='.repeat(70)}`); console.error(`[!] Error: ${error.message}`); console.error(`${'='.repeat(70)}\n`); process.exit(1); } } // Run the main function main().catch(error => { console.error('[!] Unexpected error:', error); process.exit(1); });