type: 'direct',
rateId: rateId || ''
});
els.createInfo.textContent = 'Creating, please wait...';
const res = await q('proxy.php?action=create', { method:'POST', body });
els.createInfo.textContent = '';
txId = res.id;
const parts = [];
parts.push(`
Exchange created
`);
parts.push(`ID: ${res.id}
`);
parts.push(`Send ${fmt(res.fromAmount)} ${res.fromCurrency.toUpperCase()} to payin address:
`);
parts.push(`${res.payinAddress}
`);
parts.push(`Payout address: ${res.payoutAddress}
`);
els.txBox.innerHTML = parts.join('');
els.txBox.style.display = 'block';
els.statusControls.style.display = 'flex';
}
async function fetchStatus(){
if (!txId) return;
const data = await q('proxy.php?action=status&id=' + encodeURIComponent(txId));
const lines = [];
lines.push(`Status
`);
lines.push(`${new Date().toLocaleTimeString()} • ID ${data.id}
`);
lines.push(`Status: ${data.status}
`);
if (data.amountFrom != null) lines.push(`Amount from: ${fmt(data.amountFrom)} ${data.fromCurrency.toUpperCase()}
`);
if (data.amountTo != null) lines.push(`Amount to: ${fmt(data.amountTo)} ${data.toCurrency.toUpperCase()}
`);
if (data.payinAddress) lines.push(`Payin: ${data.payinAddress}
`);
if (data.payoutAddress) lines.push(`Payout: ${data.payoutAddress}
`);
if (data.payinHash) lines.push(`Payin hash: ${data.payinHash}
`);
if (data.payoutHash) lines.push(`Payout hash: ${data.payoutHash}
`);
if (data.validUntil) lines.push(`Valid until: ${new Date(data.validUntil).toLocaleString()}
`);
els.statusBox.innerHTML = lines.join('');
els.statusBox.style.display = 'block';
}
function startPolling(){
if (pollTimer) return;
fetchStatus();
pollTimer = setInterval(fetchStatus, 5000);
els.startPollBtn.disabled = true;
els.stopPollBtn.disabled = false;
}
function stopPolling(){
if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
els.startPollBtn.disabled = false;
els.stopPollBtn.disabled = true;
}
// Event wiring
els.swapBtn.addEventListener('click', swap);
els.fromAsset.addEventListener('change', updateNetworks);
els.toAsset.addEventListener('change', updateNetworks);
els.estimateBtn.addEventListener('click', () => { estimate().catch(err => alert(err.message)); });
els.continueBtn.addEventListener('click', () => { createExchange().catch(err => alert(err.message)); });
els.startPollBtn.addEventListener('click', startPolling);
els.stopPollBtn.addEventListener('click', stopPolling);
// Init
fetchCurrencies().catch(err => alert(err.message));
})();