|
| 1 | +import * as t from 'io-ts'; |
| 2 | +import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http'; |
| 3 | +import { BitgoExpressError } from '../../schemas/error'; |
| 4 | +import { SendManyResponse } from './sendmany'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Request path parameters for sweeping a wallet |
| 8 | + */ |
| 9 | +export const WalletSweepParams = { |
| 10 | + /** The coin type */ |
| 11 | + coin: t.string, |
| 12 | + /** The wallet ID */ |
| 13 | + id: t.string, |
| 14 | +} as const; |
| 15 | + |
| 16 | +/** |
| 17 | + * Request body for sweeping all funds from a wallet |
| 18 | + * |
| 19 | + * The sweep operation sends all available funds from the wallet to a specified address. |
| 20 | + * For UTXO coins, it uses the native /sweepWallet endpoint. |
| 21 | + * For account-based coins, it calculates the maximum spendable amount and uses sendMany. |
| 22 | + */ |
| 23 | +export const WalletSweepBody = { |
| 24 | + /** The destination address to send all funds to - REQUIRED */ |
| 25 | + address: t.string, |
| 26 | + |
| 27 | + /** The wallet passphrase to decrypt the user key */ |
| 28 | + walletPassphrase: optional(t.string), |
| 29 | + |
| 30 | + /** The extended private key (alternative to walletPassphrase) */ |
| 31 | + xprv: optional(t.string), |
| 32 | + |
| 33 | + /** One-time password for 2FA */ |
| 34 | + otp: optional(t.string), |
| 35 | + |
| 36 | + /** The desired fee rate for the transaction in satoshis/kB (UTXO coins) */ |
| 37 | + feeRate: optional(t.number), |
| 38 | + |
| 39 | + /** Upper limit for fee rate in satoshis/kB (UTXO coins) */ |
| 40 | + maxFeeRate: optional(t.number), |
| 41 | + |
| 42 | + /** Estimate fees to aim for confirmation within this number of blocks (UTXO coins) */ |
| 43 | + feeTxConfirmTarget: optional(t.number), |
| 44 | + |
| 45 | + /** Allows sweeping 200 unspents when wallet has more than that (UTXO coins) */ |
| 46 | + allowPartialSweep: optional(t.boolean), |
| 47 | + |
| 48 | + /** Transaction format: 'legacy', 'psbt', or 'psbt-lite' (UTXO coins) */ |
| 49 | + txFormat: optional(t.union([t.literal('legacy'), t.literal('psbt'), t.literal('psbt-lite')])), |
| 50 | +} as const; |
| 51 | + |
| 52 | +/** |
| 53 | + * Sweep all funds from a wallet to a specified address |
| 54 | + * |
| 55 | + * This endpoint sweeps (sends) all available funds from a wallet to a single destination address. |
| 56 | + * |
| 57 | + * **Behavior by coin type:** |
| 58 | + * - **UTXO coins (BTC, LTC, etc.)**: Uses the native /sweepWallet endpoint that: |
| 59 | + * - Collects all unspents in the wallet |
| 60 | + * - Builds a transaction sending everything (minus fees) to the destination |
| 61 | + * - Signs and broadcasts the transaction |
| 62 | + * - Validates that all funds go to the specified destination address |
| 63 | + * |
| 64 | + * - **Account-based coins (ETH, etc.)**: |
| 65 | + * - Checks for unconfirmed funds (fails if any exist) |
| 66 | + * - Queries the maximumSpendable amount |
| 67 | + * - Creates a sendMany transaction with that amount to the destination |
| 68 | + * |
| 69 | + * **Implementation Note:** |
| 70 | + * Both execution paths (UTXO and account-based) ultimately call the same underlying |
| 71 | + * transaction sending mechanisms as sendMany, resulting in identical response structures. |
| 72 | + * |
| 73 | + * **Authentication:** |
| 74 | + * - Requires either `walletPassphrase` (to decrypt the encrypted user key) or `xprv` (raw private key) |
| 75 | + * - Optional `otp` for 2FA |
| 76 | + * |
| 77 | + * **Fee control (UTXO coins):** |
| 78 | + * - `feeRate`: Desired fee rate in satoshis/kB |
| 79 | + * - `maxFeeRate`: Upper limit for fee rate |
| 80 | + * - `feeTxConfirmTarget`: Target number of blocks for confirmation |
| 81 | + * |
| 82 | + * **Special options:** |
| 83 | + * - `allowPartialSweep`: For UTXO wallets with >200 unspents, allows sweeping just 200 |
| 84 | + * - `txFormat`: Choose between 'legacy', 'psbt', or 'psbt-lite' format |
| 85 | + * |
| 86 | + * @tag express |
| 87 | + * @operationId express.v2.wallet.sweep |
| 88 | + */ |
| 89 | +export const PostWalletSweep = httpRoute({ |
| 90 | + path: '/api/v2/{coin}/wallet/{id}/sweep', |
| 91 | + method: 'POST', |
| 92 | + request: httpRequest({ |
| 93 | + params: WalletSweepParams, |
| 94 | + body: WalletSweepBody, |
| 95 | + }), |
| 96 | + response: { |
| 97 | + /** Successfully swept funds - same structure as sendMany */ |
| 98 | + 200: SendManyResponse, |
| 99 | + /** Invalid request or sweep operation fails */ |
| 100 | + 400: BitgoExpressError, |
| 101 | + }, |
| 102 | +}); |
0 commit comments