Spaces:
Running
Running
| document.addEventListener('DOMContentLoaded', function() { | |
| // DOM Elements | |
| const transferForm = document.getElementById('transferForm'); | |
| const refreshBalanceBtn = document.getElementById('refreshBalance'); | |
| const checkStatusBtn = document.getElementById('checkStatus'); | |
| const responseLog = document.getElementById('responseLog'); | |
| const balanceAmount = document.getElementById('balanceAmount'); | |
| const taskIdElement = document.getElementById('taskId'); | |
| const transactionStatus = document.getElementById('transactionStatus'); | |
| const transferAmount = document.getElementById('transferAmount'); | |
| // State | |
| let currentTaskId = null; | |
| let currentBalance = 0; | |
| // Log messages to the response log | |
| function logMessage(message) { | |
| const timestamp = new Date().toLocaleTimeString(); | |
| responseLog.textContent += `\n[${timestamp}] ${message}`; | |
| responseLog.scrollTop = responseLog.scrollHeight; | |
| } | |
| // Get form values | |
| function getFormValues() { | |
| return { | |
| apiKey: document.getElementById('apiKey').value, | |
| username: document.getElementById('username').value, | |
| password: document.getElementById('password').value, | |
| destination: document.getElementById('destination').value | |
| }; | |
| } | |
| // Update transaction status display | |
| function updateStatusDisplay(status, taskId = null, amount = null) { | |
| // Update status text and class | |
| transactionStatus.textContent = status; | |
| transactionStatus.className = 'px-3 py-1 rounded-full '; | |
| switch(status.toUpperCase()) { | |
| case 'SUCCESS': | |
| transactionStatus.classList.add('status-success'); | |
| break; | |
| case 'FAILURE': | |
| transactionStatus.classList.add('status-failure'); | |
| break; | |
| case 'PROCESSING': | |
| case 'PENDING': | |
| transactionStatus.classList.add('status-processing'); | |
| break; | |
| default: | |
| transactionStatus.classList.add('status-pending'); | |
| } | |
| // Update task ID if provided | |
| if (taskId) { | |
| taskIdElement.textContent = taskId; | |
| } | |
| // Update amount if provided | |
| if (amount) { | |
| transferAmount.textContent = `${amount.toFixed(8)} BTC`; | |
| } | |
| } | |
| // Get account balance | |
| async function getBalance() { | |
| const { username, password } = getFormValues(); | |
| if (!username || !password) { | |
| logMessage('Error: Username and password required to get balance'); | |
| return; | |
| } | |
| logMessage('Fetching account balance...'); | |
| try { | |
| const response = await fetch('https://sohei.io/api/v1/BTC/balance', { | |
| method: 'GET', | |
| headers: { | |
| 'Authorization': 'Basic ' + btoa(`${username}:${password}`) | |
| } | |
| }); | |
| if (response.ok) { | |
| const data = await response.json(); | |
| currentBalance = parseFloat(data.balance) || 0; | |
| balanceAmount.textContent = `${currentBalance.toFixed(8)} BTC`; | |
| logMessage(`Balance retrieved: ${currentBalance.toFixed(8)} BTC`); | |
| } else { | |
| const errorText = await response.text(); | |
| logMessage(`Balance error: ${response.status} - ${errorText}`); | |
| } | |
| } catch (error) { | |
| logMessage(`Network error: ${error.message}`); | |
| } | |
| } | |
| // Create payout transaction | |
| async function createPayout() { | |
| const { username, password, destination } = getFormValues(); | |
| if (!username || !password || !destination) { | |
| logMessage('Error: All fields required to create payout'); | |
| return; | |
| } | |
| // Calculate transfer amount (reserve fee) | |
| const feeReserve = 0.00005; | |
| const amount = currentBalance - feeReserve; | |
| if (amount <= 0) { | |
| logMessage('Error: Insufficient balance after fee deduction'); | |
| return; | |
| } | |
| logMessage(`Creating payout for ${amount.toFixed(8)} BTC...`); | |
| const payload = { | |
| amount: amount.toFixed(8), | |
| destination: destination, | |
| fee: "15" | |
| }; | |
| try { | |
| const response = await fetch('https://sohei.io/api/v1/BTC/payout', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': 'Basic ' + btoa(`${username}:${password}`) | |
| }, | |
| body: JSON.stringify(payload) | |
| }); | |
| if (response.ok) { | |
| const data = await response.json(); | |
| currentTaskId = data.task_id; | |
| logMessage(`Payout created successfully. Task ID: ${currentTaskId}`); | |
| updateStatusDisplay('Processing', currentTaskId, amount); | |
| // Auto-check status after creation | |
| setTimeout(checkTransactionStatus, 3000); | |
| } else { | |
| const errorText = await response.text(); | |
| logMessage(`Payout error: ${response.status} - ${errorText}`); | |
| updateStatusDisplay('Failure'); | |
| } | |
| } catch (error) { | |
| logMessage(`Network error: ${error.message}`); | |
| updateStatusDisplay('Failure'); | |
| } | |
| } | |
| // Check transaction status | |
| async function checkTransactionStatus() { | |
| if (!currentTaskId) { | |
| logMessage('Error: No task ID available'); | |
| return; | |
| } | |
| const { username, password } = getFormValues(); | |
| if (!username || !password) { | |
| logMessage('Error: Username and password required to check status'); | |
| return; | |
| } | |
| logMessage(`Checking status for task: ${currentTaskId}`); | |
| try { | |
| const response = await fetch(`https://sohei.io/api/v1/BTC/task/${currentTaskId}`, { | |
| method: 'GET', | |
| headers: { | |
| 'Authorization': 'Basic ' + btoa(`${username}:${password}`) | |
| } | |
| }); | |
| if (response.ok) { | |
| const data = await response.json(); | |
| const status = data.status || 'UNKNOWN'; | |
| logMessage(`Transaction status: ${status}`); | |
| updateStatusDisplay(status); | |
| // If still processing, check again in 5 seconds | |
| if (status === 'PENDING' || status === 'PROCESSING') { | |
| setTimeout(checkTransactionStatus, 5000); | |
| } | |
| } else { | |
| const errorText = await response.text(); | |
| logMessage(`Status check error: ${response.status} - ${errorText}`); | |
| } | |
| } catch (error) { | |
| logMessage(`Network error: ${error.message}`); | |
| } | |
| } | |
| // Event Listeners | |
| transferForm.addEventListener('submit', async function(e) { | |
| e.preventDefault(); | |
| // Check confirmation | |
| const confirmed = document.getElementById('confirmTransfer').checked; | |
| if (!confirmed) { | |
| logMessage('Error: Please confirm the transfer'); | |
| return; | |
| } | |
| // Disable button during transfer | |
| const transferBtn = document.getElementById('transferBtn'); | |
| transferBtn.disabled = true; | |
| transferBtn.textContent = 'Processing...'; | |
| try { | |
| // First get balance | |
| await getBalance(); | |
| // Then create payout | |
| await createPayout(); | |
| } finally { | |
| transferBtn.disabled = false; | |
| transferBtn.textContent = 'Transfer All BTC'; | |
| } | |
| }); | |
| refreshBalanceBtn.addEventListener('click', getBalance); | |
| checkStatusBtn.addEventListener('click', checkTransactionStatus); | |
| // Initialize | |
| logMessage('Bitcoin Transfer Pro initialized. Enter your credentials to begin.'); | |
| }); |