immortal-bnb/test-trade.ts
2025-11-08 02:44:51 +03:00

131 lines
4.7 KiB
TypeScript
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bun
/**
* Test Trade Script
* Verifies PancakeSwap SDK integration works end-to-end
*
* Usage:
* 1. Set up your .env file with testnet credentials
* 2. Get testnet BNB from faucet
* 3. Run: bun test-trade.ts <token-address>
*/
import PancakeSwapV3 from './src/blockchain/pancakeSwapIntegration';
import { logger } from './src/utils/logger';
import { CONFIG } from './src/config';
async function testTrade() {
console.log('๐Ÿงช Testing PancakeSwap SDK Integration\n');
console.log('โ•'.repeat(60));
try {
// Initialize PancakeSwap SDK
const pancake = new PancakeSwapV3();
// Test 1: Check wallet balance
console.log('\n๐Ÿ“Š Test 1: Wallet Balance');
console.log('โ”€'.repeat(60));
const balance = await pancake.getBalance();
// Safe wallet address access
const walletAddress = pancake['wallet']?.address || 'No wallet configured';
console.log(`โœ“ Wallet address: ${walletAddress}`);
console.log(`โœ“ Balance: ${balance.toFixed(4)} BNB`);
console.log(`โœ“ Network: ${CONFIG.TRADING_NETWORK} (Chain ID: ${CONFIG.CHAIN_ID})`);
if (balance < 0.01) {
console.log('\nโš ๏ธ WARNING: Low balance!');
console.log('Get testnet BNB from:');
console.log(' - opBNB: https://opbnb-testnet-bridge.bnbchain.org/faucet');
console.log(' - BNB Chain: https://testnet.bnbchain.org/faucet-smart');
return;
}
// Test 2: Check if token address provided
console.log('\n๐Ÿ“Š Test 2: Token Address');
console.log('โ”€'.repeat(60));
const tokenAddress = process.argv[2];
if (!tokenAddress) {
console.log('โ„น๏ธ No token address provided');
console.log('\nTo test a real trade, run:');
console.log(' bun test-trade.ts 0xYOUR_TOKEN_ADDRESS');
console.log('\nExample testnet tokens:');
console.log(' - Check DexScreener: https://dexscreener.com/opbnb');
console.log(' - Or deploy a test token first');
console.log('\nโœ… SDK integration test PASSED (balance check works)');
return;
}
console.log(`โœ“ Token address: ${tokenAddress}`);
// Test 3: Get token info
console.log('\n๐Ÿ“Š Test 3: Token Information');
console.log('โ”€'.repeat(60));
const tokenBalance = await pancake.getTokenBalance(tokenAddress);
console.log(`โœ“ Current token balance: ${tokenBalance}`);
// Test 4: Simulate a small buy (0.001 BNB)
console.log('\n๐Ÿ“Š Test 4: Simulated Buy Trade');
console.log('โ”€'.repeat(60));
const testAmount = 0.001; // Very small test amount
console.log(`Amount: ${testAmount} BNB`);
console.log(`Slippage: 0.5%`);
console.log('\nโš ๏ธ READY TO EXECUTE REAL TRADE');
console.log('This will spend real testnet BNB!');
console.log('\nTo execute, uncomment the buyTokenWithBNB call in test-trade.ts');
console.log('Or run the full bot with: bun run dev');
// Uncomment below to execute a real test trade:
/*
console.log('\n๐Ÿ”„ Executing buy trade...');
const result = await pancake.buyTokenWithBNB(
tokenAddress,
testAmount,
50 // 0.5% slippage (50 basis points)
);
if (result.success) {
console.log('\nโœ… TRADE SUCCESSFUL!');
console.log(` TX Hash: ${result.txHash}`);
console.log(` Amount In: ${result.amountIn} BNB`);
console.log(` Amount Out: ${result.amountOut} tokens`);
console.log(` Price Impact: ${result.priceImpact}%`);
console.log(` Execution Price: ${result.executionPrice}`);
console.log(` Gas Used: ${result.gasUsed}`);
console.log('\nView transaction:');
if (CONFIG.TRADING_NETWORK === 'opbnb') {
console.log(` https://testnet.opbnbscan.com/tx/${result.txHash}`);
} else {
console.log(` https://testnet.bscscan.com/tx/${result.txHash}`);
}
} else {
console.log('\nโŒ TRADE FAILED');
console.log(` Error: ${result.error}`);
}
*/
console.log('\nโœ… All tests PASSED');
console.log('โ•'.repeat(60));
console.log('\n๐Ÿ“– Next steps:');
console.log(' 1. Uncomment the trade execution code above to test real trades');
console.log(' 2. Or run the full AI bot: bun run dev');
console.log(' 3. Check QUICKSTART_TRADING.md for detailed instructions');
} catch (error) {
console.error('\nโŒ Test FAILED');
console.error('โ•'.repeat(60));
console.error('Error:', (error as Error).message);
console.error('\nDebug info:');
console.error(' - Check your .env file is configured correctly');
console.error(' - Verify you have testnet BNB in your wallet');
console.error(' - Ensure RPC endpoint is accessible');
console.error(' - Check token address is valid on your network');
process.exit(1);
}
}
// Run the test if this file is executed directly
testTrade();