File size: 8,262 Bytes
9ff6a28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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.');
});