UNPKG

revshare-sdk

Version:

JavaScript SDK for RevShare Public API - Create bonding curve tokens with distribution features

218 lines (186 loc) • 7.83 kB
/** * Bonding Curve Example - RevShare SDK * * This example demonstrates how to: * 1. Get bonding curve progress for a token * 2. Build buy/sell transactions for bonding curve tokens * 3. Execute bonding curve swaps * * Prerequisites: * - Node.js with ES modules support * - Solana web3.js installed * - A Solana keypair for signing transactions */ import { RevShareSDK } from '../src/revshare-sdk.js'; import { Keypair } from '@solana/web3.js'; // Initialize the SDK const revshare = new RevShareSDK(); // Example token address (replace with your actual token) const TOKEN_ADDRESS = 'BSAwhpQuJDV6tL27VYsYoCUMcRKeTigTbDX8CyDMwUP'; // Optional: Custom RPC URL (falls back to default if invalid) const CUSTOM_RPC_URL = 'https://api.mainnet-beta.solana.com'; // Create a keypair for signing transactions (replace with your actual keypair) // WARNING: Never hardcode private keys in production code! const keypair = Keypair.generate(); // Replace with your actual keypair async function bondingCurveExample() { try { console.log('šŸš€ Starting Bonding Curve Example...\n'); // Example 1: Get bonding curve progress console.log('šŸ“Š Example 1: Getting bonding curve progress...'); const progress = await revshare.getBondingCurveProgress({ tokenAddress: TOKEN_ADDRESS, rpcUrl: CUSTOM_RPC_URL }); console.log('āœ… Progress retrieved:'); console.log(` Pool ID: ${progress.poolId}`); console.log(` Migration Threshold: ${progress.migrationThreshold.sol} SOL`); console.log(` Base Reserve: ${progress.progress.baseReserve}`); console.log(` Quote Reserve: ${progress.progress.quoteReserve}`); console.log(` Current Price: ${progress.progress.sqrtPrice}`); console.log(''); // Example 2: Build a buy transaction (without executing) console.log('šŸ›’ Example 2: Building buy transaction...'); const buyTransaction = await revshare.buildBondingCurveBuyTransaction({ tokenAddress: TOKEN_ADDRESS, amount: 0.001, // 0.001 SOL buyerWallet: keypair.publicKey.toString(), slippageBps: 500, // 5% slippage rpcUrl: CUSTOM_RPC_URL }); console.log('āœ… Buy transaction built:'); console.log(` Pool ID: ${buyTransaction.poolId}`); console.log(` Expected tokens: ${buyTransaction.expectedTokens}`); console.log(` Transaction size: ${buyTransaction.transactionBase64.length} bytes`); console.log(''); // Example 3: Build a sell transaction (without executing) console.log('šŸ’ø Example 3: Building sell transaction...'); const sellTransaction = await revshare.buildBondingCurveSellTransaction({ tokenAddress: TOKEN_ADDRESS, amount: '1000000', // 1,000,000 tokens sellerWallet: keypair.publicKey.toString(), slippageBps: 500, // 5% slippage rpcUrl: CUSTOM_RPC_URL }); console.log('āœ… Sell transaction built:'); console.log(` Pool ID: ${sellTransaction.poolId}`); console.log(` Expected SOL: ${sellTransaction.expectedSOL}`); console.log(` Transaction size: ${sellTransaction.transactionBase64.length} bytes`); console.log(''); // Example 4: Execute a buy swap (commented out for safety) console.log('āš ļø Example 4: Execute buy swap (commented out for safety)'); console.log(' Uncomment the code below to execute actual transactions:'); console.log(''); /* console.log('šŸ”„ Executing buy swap...'); const buyResult = await revshare.executeBondingCurveSwap({ tokenAddress: TOKEN_ADDRESS, action: 'buy', amount: 0.001, // 0.001 SOL keypair: keypair, slippageBps: 500, rpcUrl: CUSTOM_RPC_URL }); console.log('āœ… Buy swap executed:'); console.log(` Transaction signature: ${buyResult.transactionSignature}`); console.log(` Expected output: ${buyResult.expectedOutput} tokens`); console.log(''); */ // Example 5: Execute a sell swap (commented out for safety) console.log('āš ļø Example 5: Execute sell swap (commented out for safety)'); console.log(' Uncomment the code below to execute actual transactions:'); console.log(''); /* console.log('šŸ”„ Executing sell swap...'); const sellResult = await revshare.executeBondingCurveSwap({ tokenAddress: TOKEN_ADDRESS, action: 'sell', amount: '1000000', // 1,000,000 tokens keypair: keypair, slippageBps: 500, rpcUrl: CUSTOM_RPC_URL }); console.log('āœ… Sell swap executed:'); console.log(` Transaction signature: ${sellResult.transactionSignature}`); console.log(` Expected output: ${sellResult.expectedOutput} SOL`); console.log(''); */ console.log('šŸŽ‰ Bonding curve example completed successfully!'); console.log(''); console.log('šŸ“ Summary:'); console.log(' - Progress retrieval: āœ…'); console.log(' - Buy transaction building: āœ…'); console.log(' - Sell transaction building: āœ…'); console.log(' - Actual swaps: āš ļø (commented out for safety)'); console.log(''); console.log('šŸ’” To execute actual swaps:'); console.log(' 1. Replace the generated keypair with your actual keypair'); console.log(' 2. Ensure you have sufficient SOL for fees'); console.log(' 3. Uncomment the swap execution code'); console.log(' 4. Run the example again'); } catch (error) { console.error('āŒ Bonding curve example failed:', error.message); if (error.details) { console.error(' Details:', error.details); } } } // Example 6: Advanced usage with error handling async function advancedBondingCurveExample() { try { console.log('šŸ”§ Advanced Bonding Curve Example...\n'); // Get progress with different RPC URLs console.log('šŸ“Š Testing with different RPC URLs...'); const rpcUrls = [ 'https://api.mainnet-beta.solana.com', 'https://solana-api.projectserum.com', 'https://rpc.ankr.com/solana' ]; for (const rpcUrl of rpcUrls) { try { console.log(` Testing RPC: ${rpcUrl}`); const progress = await revshare.getBondingCurveProgress({ tokenAddress: TOKEN_ADDRESS, rpcUrl: rpcUrl }); console.log(` āœ… Success with ${rpcUrl}`); break; // Use the first working RPC } catch (error) { console.log(` āŒ Failed with ${rpcUrl}: ${error.message}`); } } // Test with different slippage values console.log('\nšŸ“Š Testing with different slippage values...'); const slippageValues = [100, 500, 1000]; // 1%, 5%, 10% for (const slippageBps of slippageValues) { try { console.log(` Testing slippage: ${slippageBps} bps (${slippageBps/100}%)`); const transaction = await revshare.buildBondingCurveBuyTransaction({ tokenAddress: TOKEN_ADDRESS, amount: 0.001, buyerWallet: keypair.publicKey.toString(), slippageBps: slippageBps, rpcUrl: CUSTOM_RPC_URL }); console.log(` āœ… Success with ${slippageBps} bps slippage`); break; // Use the first working slippage } catch (error) { console.log(` āŒ Failed with ${slippageBps} bps: ${error.message}`); } } console.log('\nāœ… Advanced example completed!'); } catch (error) { console.error('āŒ Advanced example failed:', error.message); } } // Run the examples async function main() { console.log('šŸŽÆ RevShare SDK - Bonding Curve Examples\n'); await bondingCurveExample(); console.log(''); await advancedBondingCurveExample(); } // Execute if this file is run directly if (import.meta.url === `file://${process.argv[1]}`) { main().catch(console.error); } export { bondingCurveExample, advancedBondingCurveExample };