|
| 1 | +import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 2 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 3 | +import { MPTAmount, Payment } from 'xrpl'; |
| 4 | +import { XrpTransactionType } from './iface'; |
| 5 | +import { Transaction } from './transaction'; |
| 6 | +import { TransactionBuilder } from './transactionBuilder'; |
| 7 | +import utils from './utils'; |
| 8 | + |
| 9 | +export class MptTransferBuilder extends TransactionBuilder { |
| 10 | + private _mptIssuanceId?: string; |
| 11 | + private _value?: string; |
| 12 | + private _destination?: string; |
| 13 | + private _destinationTag?: number; |
| 14 | + |
| 15 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 16 | + super(_coinConfig); |
| 17 | + } |
| 18 | + |
| 19 | + protected get transactionType(): TransactionType { |
| 20 | + return TransactionType.SendMPT; |
| 21 | + } |
| 22 | + |
| 23 | + protected get xrpTransactionType(): XrpTransactionType.Payment { |
| 24 | + // MPT transfers use a standard Payment transaction with an MPT Amount object |
| 25 | + return XrpTransactionType.Payment; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Set the recipient address (with optional destination tag). |
| 30 | + * @param {string} address - the recipient XRP address, optionally with destination tag |
| 31 | + */ |
| 32 | + to(address: string): this { |
| 33 | + const { address: xrpAddress, destinationTag } = utils.getAddressDetails(address); |
| 34 | + this._destination = xrpAddress; |
| 35 | + this._destinationTag = destinationTag; |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Set the MPT issuance ID and raw integer amount to transfer. |
| 41 | + * The value is a raw integer string — AssetScale is display-only and never applied here. |
| 42 | + * @param {string} issuanceId - 48-character hex MPTokenIssuanceID |
| 43 | + * @param {string} value - raw integer string (e.g. "1000" = 1000 base units) |
| 44 | + */ |
| 45 | + mptAmount(issuanceId: string, value: string): this { |
| 46 | + if (!/^[0-9a-fA-F]{48}$/.test(issuanceId)) { |
| 47 | + throw new BuildTransactionError('MPTokenIssuanceID must be a 48-character hex string'); |
| 48 | + } |
| 49 | + if (!/^\d+$/.test(value)) { |
| 50 | + throw new BuildTransactionError('MPT value must be a non-negative integer string'); |
| 51 | + } |
| 52 | + this._mptIssuanceId = issuanceId; |
| 53 | + this._value = value; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + initBuilder(tx: Transaction): void { |
| 58 | + super.initBuilder(tx); |
| 59 | + const { destination, destinationTag, mptAmount } = tx.toJson(); |
| 60 | + if (destination) { |
| 61 | + const normalizedAddress = utils.normalizeAddress({ address: destination, destinationTag }); |
| 62 | + this.to(normalizedAddress); |
| 63 | + } |
| 64 | + if (mptAmount) { |
| 65 | + this.mptAmount(mptAmount.mpt_issuance_id, mptAmount.value); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** @inheritdoc */ |
| 70 | + protected async buildImplementation(): Promise<Transaction> { |
| 71 | + if (!this._sender) { |
| 72 | + throw new BuildTransactionError('Sender must be set before building the transaction'); |
| 73 | + } |
| 74 | + if (!this._destination || !this._mptIssuanceId || !this._value) { |
| 75 | + throw new BuildTransactionError( |
| 76 | + 'Missing mandatory MPT payment parameters: destination, mptIssuanceId, and value are all required' |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + const mptAmountObj: MPTAmount = { |
| 81 | + mpt_issuance_id: this._mptIssuanceId, |
| 82 | + value: this._value, |
| 83 | + }; |
| 84 | + |
| 85 | + const paymentFields: Payment = { |
| 86 | + TransactionType: this.xrpTransactionType, |
| 87 | + Account: this._sender, |
| 88 | + Destination: this._destination, |
| 89 | + Amount: mptAmountObj, |
| 90 | + }; |
| 91 | + |
| 92 | + if (typeof this._destinationTag === 'number') { |
| 93 | + paymentFields.DestinationTag = this._destinationTag; |
| 94 | + } |
| 95 | + |
| 96 | + this._specificFields = paymentFields; |
| 97 | + |
| 98 | + return await super.buildImplementation(); |
| 99 | + } |
| 100 | +} |
0 commit comments