File size: 6,991 Bytes
4a8b431
 
 
 
 
8d9331b
 
4a8b431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d9331b
4a8b431
 
 
8d9331b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8b431
 
8d9331b
4a8b431
 
 
8d9331b
4a8b431
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

// Supported exchanges
const EXCHANGES = {
    CEX: ['Binance', 'Coinbase', 'Kraken', 'KuCoin', 'OKX', 'Bybit', 'Bitget', 'Huobi', 'Gate.io'],
    DEX: ['Uniswap', 'PancakeSwap', 'SushiSwap', 'Curve', 'Balancer', 'Jupiter', 'QuickSwap', 'Trader Joe']
};

// Fetch top 200 coins from CoinGecko
async function fetchTopCoins() {
    try {
        const response = await fetch('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=200&page=1&sparkline=false');
        const data = await response.json();
        return data.map(coin => coin.symbol.toUpperCase());
    } catch (error) {
        console.error('Error fetching top coins:', error);
        return ['BTC', 'ETH', 'BNB', 'SOL', 'ADA', 'XRP', 'DOT', 'DOGE', 'SHIB', 'MATIC'];
    }
}

// Fetch prices from Binance API
async function fetchBinancePrices(symbols) {
    try {
        const response = await fetch('https://api.binance.com/api/v3/ticker/price');
        const data = await response.json();
        return data.filter(item => symbols.includes(item.symbol.replace('USDT', '')));
    } catch (error) {
        console.error('Error fetching Binance prices:', error);
        return [];
    }
}

// Simulate DEX prices with slight variation from CEX prices
function simulateDexPrices(cexPrices) {
    return cexPrices.map(item => {
        const variation = (Math.random() * 0.05) - 0.025; // -2.5% to +2.5%
        const price = parseFloat(item.price) * (1 + variation);
        return {
            symbol: item.symbol.replace('USDT', ''),
            price: price.toFixed(8)
        };
    });
}

// Find arbitrage opportunities
async function findArbitrageOpportunities() {
    const symbols = await fetchTopCoins();
    const binancePrices = await fetchBinancePrices(symbols);
    const dexPrices = simulateDexPrices(binancePrices);
    
    const opportunities = [];
    
    // Compare Binance (CEX) with simulated DEX prices
    binancePrices.forEach(cexItem => {
        const symbol = cexItem.symbol.replace('USDT', '');
        const dexItem = dexPrices.find(item => item.symbol === symbol);
        
        if (dexItem) {
            const cexPrice = parseFloat(cexItem.price);
            const dexPrice = parseFloat(dexItem.price);
            const spread = ((dexPrice - cexPrice) / cexPrice) * 100;
            
            if (Math.abs(spread) > 0.5) { // Only show opportunities with >0.5% spread
                opportunities.push({
                    symbol,
                    buyFrom: spread > 0 ? 'Binance' : EXCHANGES.DEX[Math.floor(Math.random() * EXCHANGES.DEX.length)],
                    sellTo: spread > 0 ? EXCHANGES.DEX[Math.floor(Math.random() * EXCHANGES.DEX.length)] : 'Binance',
                    buyPrice: spread > 0 ? cexPrice : dexPrice,
                    sellPrice: spread > 0 ? dexPrice : cexPrice,
                    profitPercent: Math.abs(spread),
                    liquidity: Math.floor(Math.random() * 10000000) + 100000,
                    confidence: Math.floor(Math.random() * 30) + 70,
                    status: spread > 10 ? 'EXTREME_ARBITRAGE' : 'GOOD_ARBITRAGE'
                });
            }
        }
    });
    
    return {
        active: opportunities.filter(opp => opp.profitPercent <= 10),
        extreme: opportunities.filter(opp => opp.profitPercent > 10),
        expired: [],
        inactive: [],
        coins: symbols.map(symbol => {
            const cexItem = binancePrices.find(item => item.symbol === `${symbol}USDT`);
            const dexItem = dexPrices.find(item => item.symbol === symbol);
            return {
                symbol,
                cexPrice: cexItem ? parseFloat(cexItem.price) : 0,
                dexPrice: dexItem ? parseFloat(dexItem.price) : 0,
                spread: cexItem && dexItem ? 
                    ((parseFloat(dexItem.price) - parseFloat(cexItem.price)) / parseFloat(cexItem.price)) * 100 : 0
            };
        })
    };
}

// Render arbitrage opportunities
async function renderArbitrage() {
    const data = await findArbitrageOpportunities();
const activeContainer = document.getElementById('active-arbitrage');
    const extremeContainer = document.getElementById('extreme-arbitrage');
    const expiredContainer = document.getElementById('expired-arbitrage');
    const inactiveContainer = document.getElementById('inactive-arbitrage');
    const coinList = document.getElementById('coin-list');
    
    // Clear existing content
    activeContainer.innerHTML = '';
    extremeContainer.innerHTML = '';
    expiredContainer.innerHTML = '';
    inactiveContainer.innerHTML = '';
    coinList.innerHTML = '';
    
    // Render active arbitrage
    mockData.active.forEach(opp => {
        const card = document.createElement('arbitrage-card');
        card.setAttribute('data', JSON.stringify(opp));
        activeContainer.appendChild(card);
    });
    
    // Render extreme arbitrage
    mockData.extreme.forEach(opp => {
        const card = document.createElement('arbitrage-card');
        card.setAttribute('data', JSON.stringify(opp));
        extremeContainer.appendChild(card);
    });
    
    // Render expired arbitrage
    mockData.expired.forEach(opp => {
        const card = document.createElement('arbitrage-card');
        card.setAttribute('data', JSON.stringify(opp));
        expiredContainer.appendChild(card);
    });
    
    // Render inactive arbitrage
    mockData.inactive.forEach(opp => {
        const card = document.createElement('arbitrage-card');
        card.setAttribute('data', JSON.stringify(opp));
        inactiveContainer.appendChild(card);
    });
    
    // Render coin list
    mockData.coins.forEach(coin => {
        const row = document.createElement('tr');
        row.className = 'border-b border-gray-700 hover:bg-gray-700/50';
        row.innerHTML = `
            <td class="py-3 font-medium">${coin.symbol}</td>
            <td class="py-3">$${coin.cexPrice.toLocaleString()}</td>
            <td class="py-3">$${coin.dexPrice.toLocaleString()}</td>
            <td class="py-3 ${coin.spread > 0 ? 'text-green-400' : 'text-red-400'}">
                ${coin.spread > 0 ? '+' : ''}${coin.spread.toFixed(2)}%
            </td>
            <td class="py-3">
                <button class="text-blue-400 hover:text-blue-300 flex items-center gap-1">
                    <i data-feather="arrow-up-right" class="w-4 h-4"></i> Details
                </button>
            </td>
        `;
        coinList.appendChild(row);
    });
    
    // Refresh icons
    feather.replace();
}
// Initialize the app
document.addEventListener('DOMContentLoaded', async () => {
    await renderArbitrage();
    
    // Refresh data every 30 seconds
    setInterval(async () => {
        await renderArbitrage();
    }, 30000);
});

// Add refresh button functionality
document.addEventListener('click', async (e) => {
    if (e.target.closest('.refresh-btn')) {
        await renderArbitrage();
    }
});