UNPKG

revshare-sdk

Version:

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

138 lines (120 loc) 6.17 kB
const { RevShareSDK } = require('../dist/sdk.cjs.js'); const { Keypair } = require('@solana/web3.js'); /** * Example demonstrating bonding curve swap functionality * * This example shows how to: * 1. Build buy transactions for bonding curve tokens (pre-bonding phase) * 2. Build sell transactions for bonding curve tokens (pre-bonding phase) * 3. Execute bonding curve swaps with automatic signing and sending * * Note: These methods are specifically for tokens in the bonding curve phase * that have not yet bonded. For regular token swaps after bonding, use different methods. */ async function swapExample() { // Initialize the SDK const revshare = new RevShareSDK(); // Example token address (replace with actual token) const tokenAddress = 'AJuWVNdaFyThTnrhTXUUTufyGG35nSbuqYLAVTV7uoZz'; // Example wallet (replace with actual keypair) const keypair = Keypair.generate(); // In production, use actual keypair const walletAddress = keypair.publicKey.toString(); console.log('🚀 RevShare SDK - Bonding Curve Swap Example'); console.log('============================================'); console.log(`Bonding Token Address: ${tokenAddress}`); console.log(`Wallet Address: ${walletAddress}`); console.log('Note: This example is for tokens in bonding curve phase (pre-bonding)'); console.log(''); try { // Example 1: Build a buy transaction (without executing) console.log('📋 Example 1: Building Bonding Curve Buy Transaction'); console.log('---------------------------------------------------'); const buyTransaction = await revshare.buildBondingCurveBuyTransaction({ tokenAddress, amount: 0.001, // 0.001 SOL buyerWallet: walletAddress, slippageBps: 500, // 5% slippage rpcUrl: 'https://api.mainnet-beta.solana.com' // Optional custom RPC URL }); console.log('✅ Bonding curve buy transaction built!'); console.log(`Expected tokens: ${buyTransaction.expectedTokens}`); console.log(`Pool ID: ${buyTransaction.poolId}`); console.log(`Transaction length: ${buyTransaction.transactionBase64.length} characters`); console.log(''); // Example 2: Build a sell transaction (without executing) console.log('📋 Example 2: Building Bonding Curve Sell Transaction'); console.log('----------------------------------------------------'); const sellTransaction = await revshare.buildBondingCurveSellTransaction({ tokenAddress, amount: '1000000', // 1,000,000 tokens sellerWallet: walletAddress, slippageBps: 500, // 5% slippage rpcUrl: 'https://api.mainnet-beta.solana.com' // Optional custom RPC URL }); console.log('✅ Bonding curve sell transaction built!'); console.log(`Expected SOL: ${sellTransaction.expectedSOL}`); console.log(`Pool ID: ${sellTransaction.poolId}`); console.log(`Transaction length: ${sellTransaction.transactionBase64.length} characters`); console.log(''); // Example 3: Execute a buy swap (commented out for safety) console.log('📋 Example 3: Execute Bonding Curve Buy Swap (Commented for Safety)'); console.log('---------------------------------------------------------------'); console.log('This would execute the actual bonding curve buy transaction:'); console.log('const buyResult = await revshare.executeBondingCurveSwap({'); console.log(' tokenAddress,'); console.log(' action: "buy",'); console.log(' amount: 0.001,'); console.log(' keypair,'); console.log(' slippageBps: 500,'); console.log(' rpcUrl: "https://api.mainnet-beta.solana.com" // Optional custom RPC URL'); console.log('});'); console.log(''); // Example 4: Execute a sell swap (commented out for safety) console.log('📋 Example 4: Execute Bonding Curve Sell Swap (Commented for Safety)'); console.log('----------------------------------------------------------------'); console.log('This would execute the actual bonding curve sell transaction:'); console.log('const sellResult = await revshare.executeBondingCurveSwap({'); console.log(' action: "sell",'); console.log(' amount: "1000000",'); console.log(' keypair,'); console.log(' slippageBps: 500,'); console.log(' rpcUrl: "https://api.mainnet-beta.solana.com" // Optional custom RPC URL'); console.log('});'); console.log(''); // Example 5: Manual transaction processing console.log('📋 Example 5: Manual Transaction Processing'); console.log('-------------------------------------------'); console.log('You can also manually process the transaction:'); console.log(''); console.log('// Deserialize the transaction'); console.log('const { Transaction } = require("@solana/web3.js");'); console.log('const transaction = Transaction.from(Buffer.from(buyTransaction.transactionBase64, "base64"));'); console.log(''); console.log('// Sign with your keypair'); console.log('transaction.sign(keypair);'); console.log(''); console.log('// Send to network'); console.log('const connection = new Connection("https://api.mainnet-beta.solana.com");'); console.log('const signature = await connection.sendTransaction(transaction, [keypair]);'); console.log('await connection.confirmTransaction(signature);'); console.log(''); console.log('✅ All examples completed successfully!'); console.log(''); console.log('💡 Tips:'); console.log('- Replace the token address with an actual bonding curve token (pre-bonding phase)'); console.log('- Use a real keypair with sufficient SOL balance'); console.log('- Test with small amounts first'); console.log('- Check slippage settings based on your needs'); console.log('- These methods only work for tokens in bonding curve phase'); } catch (error) { console.error('❌ Error in swap example:', error.message); if (error.details) { console.error('Details:', error.details); } } } // Run the example if this file is executed directly if (require.main === module) { swapExample().catch(console.error); } module.exports = { swapExample };