194 lines
5.5 KiB
TypeScript
194 lines
5.5 KiB
TypeScript
// tests/tradeExecutor.test.ts
|
|
// Test trade execution logic and safeguards
|
|
|
|
// Simple test runner for Bun compatibility
|
|
function describe(name: string, fn: () => void) {
|
|
console.log(`\nš ${name}`);
|
|
fn();
|
|
}
|
|
|
|
function it(name: string, fn: () => void) {
|
|
try {
|
|
fn();
|
|
console.log(` ā
${name}`);
|
|
} catch (error) {
|
|
console.log(` ā ${name}: ${(error as Error).message}`);
|
|
}
|
|
}
|
|
|
|
function expect(actual: any) {
|
|
return {
|
|
toBe: (expected: any) => {
|
|
if (actual !== expected) {
|
|
throw new Error(`Expected ${expected}, got ${actual}`);
|
|
}
|
|
},
|
|
toBeCloseTo: (expected: number, precision = 2) => {
|
|
const diff = Math.abs(actual - expected);
|
|
const tolerance = Math.pow(10, -precision);
|
|
if (diff > tolerance) {
|
|
throw new Error(`Expected ${actual} to be close to ${expected}`);
|
|
}
|
|
},
|
|
toBeDefined: () => {
|
|
if (actual === undefined || actual === null) {
|
|
throw new Error(`Expected value to be defined, got ${actual}`);
|
|
}
|
|
},
|
|
toBeGreaterThan: (expected: number) => {
|
|
if (actual <= expected) {
|
|
throw new Error(`Expected ${actual} to be greater than ${expected}`);
|
|
}
|
|
},
|
|
toBeLessThanOrEqual: (expected: number) => {
|
|
if (actual > expected) {
|
|
throw new Error(`Expected ${actual} to be less than or equal to ${expected}`);
|
|
}
|
|
},
|
|
toMatch: (regex: RegExp) => {
|
|
if (!regex.test(actual)) {
|
|
throw new Error(`Expected ${actual} to match ${regex}`);
|
|
}
|
|
},
|
|
toThrow: () => {
|
|
if (typeof actual !== 'function') {
|
|
throw new Error('Expected a function');
|
|
}
|
|
try {
|
|
actual();
|
|
throw new Error('Expected function to throw');
|
|
} catch (error) {
|
|
// Expected behavior
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
function beforeAll(fn: () => Promise<void>) {
|
|
// Simple setup function
|
|
fn();
|
|
}
|
|
|
|
import { ethers } from 'ethers';
|
|
|
|
describe('Trade Executor', () => {
|
|
describe('Validation', () => {
|
|
it('should validate BNB amounts', () => {
|
|
const maxAmount = 0.1;
|
|
|
|
expect(0.05).toBeLessThanOrEqual(maxAmount);
|
|
expect(0.15).toBeGreaterThan(maxAmount);
|
|
});
|
|
|
|
it('should validate token addresses', () => {
|
|
const validAddress = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb';
|
|
const invalidAddress = '0xinvalid';
|
|
|
|
expect(ethers.isAddress(validAddress)).toBe(true);
|
|
expect(ethers.isAddress(invalidAddress)).toBe(false);
|
|
});
|
|
|
|
it('should calculate slippage correctly', () => {
|
|
const expectedOutput = BigInt(1000);
|
|
const slippagePercent = 2;
|
|
|
|
const slippageFactor = BigInt(Math.floor((100 - slippagePercent) * 100));
|
|
const minOutput = (expectedOutput * slippageFactor) / BigInt(10000);
|
|
|
|
expect(minOutput).toBe(BigInt(980)); // 2% slippage = 98% of expected
|
|
});
|
|
});
|
|
|
|
describe('Gas Estimation', () => {
|
|
it('should apply gas buffer correctly', () => {
|
|
const estimatedGas = BigInt(300000);
|
|
const buffer = 120n; // 20% buffer
|
|
|
|
const gasWithBuffer = (estimatedGas * buffer) / 100n;
|
|
|
|
expect(gasWithBuffer).toBe(BigInt(360000));
|
|
});
|
|
});
|
|
|
|
describe('Trade Params', () => {
|
|
it('should construct valid trade parameters', () => {
|
|
const tradeParams = {
|
|
tokenAddress: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
|
|
action: 'buy' as const,
|
|
amountBNB: 0.05,
|
|
slippagePercent: 2,
|
|
};
|
|
|
|
expect(tradeParams.action).toMatch(/^(buy|sell)$/);
|
|
expect(tradeParams.amountBNB).toBeGreaterThan(0);
|
|
expect(tradeParams.slippagePercent).toBeLessThanOrEqual(10);
|
|
});
|
|
});
|
|
|
|
describe('Safeguards', () => {
|
|
it('should enforce max trade amount', () => {
|
|
const maxTradeAmount = 0.1;
|
|
const attemptedAmount = 0.15;
|
|
|
|
const isSafe = attemptedAmount <= maxTradeAmount;
|
|
expect(isSafe).toBe(false);
|
|
});
|
|
|
|
it('should require minimum liquidity', () => {
|
|
const minLiquidity = 10000;
|
|
|
|
const pool1 = { liquidity: 25000 };
|
|
const pool2 = { liquidity: 5000 };
|
|
|
|
expect(pool1.liquidity >= minLiquidity).toBe(true);
|
|
expect(pool2.liquidity >= minLiquidity).toBe(false);
|
|
});
|
|
|
|
it('should check wallet balance before trade', () => {
|
|
const walletBalance = 0.08;
|
|
const tradeAmount = 0.05;
|
|
const gasReserve = 0.01; // Reserve for gas
|
|
|
|
const hasEnoughBalance = walletBalance >= (tradeAmount + gasReserve);
|
|
expect(hasEnoughBalance).toBe(true);
|
|
|
|
const tooLargeAmount = 0.1;
|
|
const notEnoughBalance = walletBalance < (tooLargeAmount + gasReserve);
|
|
expect(notEnoughBalance).toBe(true);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('PancakeSwap Integration', () => {
|
|
it('should construct correct swap path for buy', () => {
|
|
const WBNB = '0x4200000000000000000000000000000000000006'; // opBNB WBNB
|
|
const TOKEN = '0x123...';
|
|
|
|
const buyPath = [WBNB, TOKEN];
|
|
|
|
expect(buyPath[0]).toBe(WBNB);
|
|
expect(buyPath[1]).toBe(TOKEN);
|
|
expect(buyPath.length).toBe(2);
|
|
});
|
|
|
|
it('should construct correct swap path for sell', () => {
|
|
const WBNB = '0x4200000000000000000000000000000000000006';
|
|
const TOKEN = '0x123...';
|
|
|
|
const sellPath = [TOKEN, WBNB];
|
|
|
|
expect(sellPath[0]).toBe(TOKEN);
|
|
expect(sellPath[1]).toBe(WBNB);
|
|
expect(sellPath.length).toBe(2);
|
|
});
|
|
|
|
it('should calculate deadline correctly', () => {
|
|
const nowSeconds = Math.floor(Date.now() / 1000);
|
|
const minutes = 20;
|
|
const deadline = nowSeconds + (60 * minutes);
|
|
|
|
expect(deadline).toBeGreaterThan(nowSeconds);
|
|
expect(deadline - nowSeconds).toBe(minutes * 60);
|
|
});
|
|
});
|