|
| 1 | +// WizardSwap — create a swap order and poll until finished |
| 2 | +// No API key needed |
| 3 | +// |
| 4 | +// ⚠ This example actually creates a real swap. |
| 5 | +// You must send the exact deposit amount within 15 minutes. |
| 6 | +// |
| 7 | +// Usage: |
| 8 | +// npx tsx examples/ts/wizardswap-create-swap.ts <BTC_ADDRESS> [XMR_AMOUNT] |
| 9 | +// |
| 10 | +// Example: |
| 11 | +// npx tsx examples/ts/wizardswap-create-swap.ts bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh 0.5 |
| 12 | + |
| 13 | +import ccxt from '../../ts/ccxt.js'; |
| 14 | + |
| 15 | +async function main () { |
| 16 | + const btcAddress = process.argv[2]; |
| 17 | + const xmrAmount = parseFloat (process.argv[3] || '0.1'); |
| 18 | + |
| 19 | + if (!btcAddress) { |
| 20 | + console.error ('Usage: npx tsx examples/ts/wizardswap-create-swap.ts <BTC_ADDRESS> [XMR_AMOUNT]'); |
| 21 | + process.exit (1); |
| 22 | + } |
| 23 | + |
| 24 | + const exchange = new ccxt.wizardswap (); |
| 25 | + await exchange.loadMarkets (); |
| 26 | + |
| 27 | + // ── 1. Estimate ────────────────────────────────────────────────── |
| 28 | + console.log (`Estimating ${xmrAmount} XMR → BTC …`); |
| 29 | + const ticker = await exchange.fetchTicker ('XMR/BTC', { 'amount_from': String (xmrAmount) }); |
| 30 | + console.log (` Estimated receive: ~${ticker['last']} BTC (2.2 % fee included)\n`); |
| 31 | + |
| 32 | + // ── 2. Create exchange ─────────────────────────────────────────── |
| 33 | + console.log ('Creating swap order…'); |
| 34 | + const order = await exchange.createOrder ('XMR/BTC', 'market', 'sell', xmrAmount, undefined, { |
| 35 | + 'address_to': btcAddress, |
| 36 | + }); |
| 37 | + |
| 38 | + console.log (` Order ID: ${order['id']}`); |
| 39 | + console.log (` Deposit address: ${order['info']['address_from']}`); |
| 40 | + if (order['info']['extra_id_from']) { |
| 41 | + console.log (` Extra ID / Memo: ${order['info']['extra_id_from']}`); |
| 42 | + } |
| 43 | + console.log (` Send exactly: ${xmrAmount} XMR`); |
| 44 | + console.log (` Expected receive: ~${order['price']} BTC → ${btcAddress}`); |
| 45 | + console.log (` ⏱ You have 15 minutes to send the deposit!\n`); |
| 46 | + |
| 47 | + // ── 3. Poll for status ─────────────────────────────────────────── |
| 48 | + console.log ('Polling for status (Ctrl+C to stop)…'); |
| 49 | + const terminal = new Set ([ 'closed', 'canceled' ]); |
| 50 | + const pollInterval = 20000; // 20 seconds |
| 51 | + |
| 52 | + while (true) { |
| 53 | + const status = await exchange.fetchOrder (order['id']); |
| 54 | + const rawStatus = status['info']['status']; |
| 55 | + console.log (` [status] ${rawStatus} (unified: ${status['status']})`); |
| 56 | + |
| 57 | + if (terminal.has (status['status'])) { |
| 58 | + if (rawStatus === 'finished') { |
| 59 | + console.log (`\n✅ Swap finished!`); |
| 60 | + console.log (` BTC received: ${status['info']['amount_to']}`); |
| 61 | + console.log (` Payout tx: ${status['info']['tx_to']}`); |
| 62 | + } else { |
| 63 | + console.log (`\n❌ Swap ended with status: ${rawStatus}`); |
| 64 | + } |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + await new Promise ((resolve) => setTimeout (resolve, pollInterval)); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +main ().catch (console.error); |
0 commit comments