|
| 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 | + |
| 5 | +/** |
| 6 | + * Path parameters for the delegate resources endpoint |
| 7 | + */ |
| 8 | +export const DelegateResourcesParams = { |
| 9 | + /** Coin identifier (e.g., 'trx', 'ttrx') */ |
| 10 | + coin: t.string, |
| 11 | + /** Wallet ID */ |
| 12 | + id: t.string, |
| 13 | +} as const; |
| 14 | + |
| 15 | +/** |
| 16 | + * A single resource delegation entry |
| 17 | + */ |
| 18 | +export const DelegationEntryCodec = t.type({ |
| 19 | + /** On-chain address that will receive the delegated resources */ |
| 20 | + receiverAddress: t.string, |
| 21 | + /** Amount of TRX (in SUN) to stake for the delegation */ |
| 22 | + amount: t.string, |
| 23 | + /** Resource type to delegate (e.g. 'ENERGY', 'BANDWIDTH') */ |
| 24 | + resource: t.string, |
| 25 | +}); |
| 26 | + |
| 27 | +/** |
| 28 | + * Request body for delegating resources to multiple receiver addresses. |
| 29 | + * Each delegation entry triggers a separate on-chain staking transaction |
| 30 | + * from the wallet's root address to the receiver address. |
| 31 | + * |
| 32 | + * Signing behaviour by wallet type: |
| 33 | + * - Hot (non-TSS) → signed locally with walletPassphrase and submitted |
| 34 | + * - Custodial non-TSS → sent for BitGo approval via initiateTransaction |
| 35 | + * - TSS (any) → build response contains txRequestId; signed by TSS service |
| 36 | + */ |
| 37 | +export const DelegateResourcesRequestBody = { |
| 38 | + /** Delegation entries — one on-chain transaction is built per entry */ |
| 39 | + delegations: t.array(DelegationEntryCodec), |
| 40 | + |
| 41 | + /** Wallet passphrase to decrypt the user key (hot wallets) */ |
| 42 | + walletPassphrase: optional(t.string), |
| 43 | + /** Extended private key (alternative to walletPassphrase) */ |
| 44 | + xprv: optional(t.string), |
| 45 | + /** One-time password for 2FA */ |
| 46 | + otp: optional(t.string), |
| 47 | + |
| 48 | + /** API version for TSS transaction request response ('lite' or 'full') */ |
| 49 | + apiVersion: optional(t.union([t.literal('lite'), t.literal('full')])), |
| 50 | +} as const; |
| 51 | + |
| 52 | +export const DelegationFailureEntry = t.type({ |
| 53 | + /** Human-readable error message */ |
| 54 | + message: t.string, |
| 55 | + /** Receiver address that failed, if available */ |
| 56 | + receiverAddress: optional(t.string), |
| 57 | +}); |
| 58 | + |
| 59 | +/** |
| 60 | + * Response for the delegate resources operation. |
| 61 | + * Returns arrays of successful and failed delegation transactions. |
| 62 | + */ |
| 63 | +export const DelegateResourcesResponse = t.type({ |
| 64 | + /** Successfully sent delegation transactions */ |
| 65 | + success: t.array(t.unknown), |
| 66 | + /** Errors from failed delegation transactions */ |
| 67 | + failure: t.array(DelegationFailureEntry), |
| 68 | +}); |
| 69 | + |
| 70 | +/** |
| 71 | + * Response for partial success or failure cases (202/400). |
| 72 | + * Includes both the transaction results and error metadata. |
| 73 | + */ |
| 74 | +export const DelegateResourcesErrorResponse = t.intersection([DelegateResourcesResponse, BitgoExpressError]); |
| 75 | + |
| 76 | +/** |
| 77 | + * Bulk Resource Delegation |
| 78 | + * |
| 79 | + * Delegates resources (ENERGY or BANDWIDTH) from a wallet's root address to one or more |
| 80 | + * receiver addresses. Each delegation entry produces a separate on-chain staking transaction. |
| 81 | + * This is the resource-delegation analogue of the consolidateAccount endpoint. |
| 82 | + * |
| 83 | + * Supported coins: TRON (trx, ttrx) and any future coins that support resource delegation. |
| 84 | + * |
| 85 | + * The API may return partial success (status 202) if some delegations succeed but others fail. |
| 86 | + * |
| 87 | + * @operationId express.v2.wallet.delegateresources |
| 88 | + * @tag express |
| 89 | + */ |
| 90 | +export const PostDelegateResources = httpRoute({ |
| 91 | + path: '/api/v2/{coin}/wallet/{id}/delegateResources', |
| 92 | + method: 'POST', |
| 93 | + request: httpRequest({ |
| 94 | + params: DelegateResourcesParams, |
| 95 | + body: DelegateResourcesRequestBody, |
| 96 | + }), |
| 97 | + response: { |
| 98 | + /** All delegations succeeded */ |
| 99 | + 200: DelegateResourcesResponse, |
| 100 | + /** Partial success — some delegations succeeded, others failed */ |
| 101 | + 202: DelegateResourcesErrorResponse, |
| 102 | + /** All delegations failed */ |
| 103 | + 400: DelegateResourcesErrorResponse, |
| 104 | + }, |
| 105 | +}); |
0 commit comments