( args: string[], sphere: ReturnType<typeof createMockSphere>, )
| 78 | } |
| 79 | |
| 80 | async function runSwapCommand( |
| 81 | args: string[], |
| 82 | sphere: ReturnType<typeof createMockSphere>, |
| 83 | ): Promise<CliResult> { |
| 84 | const stdout: string[] = []; |
| 85 | const stderr: string[] = []; |
| 86 | let exitCode: number | null = null; |
| 87 | |
| 88 | const log = (msg: string) => stdout.push(msg); |
| 89 | const error = (msg: string) => stderr.push(msg); |
| 90 | const exit = (code: number) => { |
| 91 | exitCode = code; |
| 92 | throw new ExitSignal(code); |
| 93 | }; |
| 94 | |
| 95 | class ExitSignal extends Error { |
| 96 | constructor(public code: number) { |
| 97 | super(`exit(${code})`); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | const command = args[0]; |
| 102 | const swapModule = sphere.swap; |
| 103 | |
| 104 | try { |
| 105 | switch (command) { |
| 106 | case 'swap-propose': { |
| 107 | const toIdx = args.indexOf('--to'); |
| 108 | const offerIdx = args.indexOf('--offer'); |
| 109 | const wantIdx = args.indexOf('--want'); |
| 110 | const escrowIdx = args.indexOf('--escrow'); |
| 111 | const timeoutIdx = args.indexOf('--timeout'); |
| 112 | const messageIdx = args.indexOf('--message'); |
| 113 | |
| 114 | if (toIdx === -1 || !args[toIdx + 1] || |
| 115 | offerIdx === -1 || !args[offerIdx + 1] || |
| 116 | wantIdx === -1 || !args[wantIdx + 1]) { |
| 117 | error('Usage: swap-propose --to <recipient> --offer <amount> <symbol> --want <amount> <symbol> [--escrow <address>] [--timeout <seconds>] [--message <text>]'); |
| 118 | exit(1); |
| 119 | } |
| 120 | |
| 121 | // Parse --offer "<amount> <symbol>" |
| 122 | const offerParts = args[offerIdx + 1].split(/\s+/); |
| 123 | if (offerParts.length !== 2) { |
| 124 | error('--offer must be "<amount> <symbol>" (e.g., "1000000 UCT")'); |
| 125 | exit(1); |
| 126 | } |
| 127 | const [offerAmount, offerCoin] = offerParts; |
| 128 | |
| 129 | // Parse --want "<amount> <symbol>" |
| 130 | const wantParts = args[wantIdx + 1].split(/\s+/); |
| 131 | if (wantParts.length !== 2) { |
| 132 | error('--want must be "<amount> <symbol>" (e.g., "500000 USDU")'); |
| 133 | exit(1); |
| 134 | } |
| 135 | const [wantAmount, wantCoin] = wantParts; |
| 136 | |
| 137 | if (!/^[1-9][0-9]*$/.test(offerAmount)) { |
no test coverage detected