diff --git a/modules/sdk-coin-canton/src/lib/allocationAllocateBuilder.ts b/modules/sdk-coin-canton/src/lib/allocationAllocateBuilder.ts index 1797db039a..c80bd73cce 100644 --- a/modules/sdk-coin-canton/src/lib/allocationAllocateBuilder.ts +++ b/modules/sdk-coin-canton/src/lib/allocationAllocateBuilder.ts @@ -10,7 +10,7 @@ export class AllocationAllocateBuilder extends TransactionBuilder { private _amount: number; private _token: string; private _operatorId: string; - private _contractId: string; + private _contractId: string | null | undefined; private _tradeId: string; private _transferLegId: string; private _allocateBefore: string; @@ -116,11 +116,11 @@ export class AllocationAllocateBuilder extends TransactionBuilder { * @returns The current builder instance for chaining. * @throws Error if id is empty. */ - contractId(id: string): this { - if (!id || !id.trim()) { + contractId(id: string | null): this { + if (id !== null && (!id || !id.trim())) { throw new Error('contractId must be a non-empty string'); } - this._contractId = id.trim(); + this._contractId = id === null ? null : id.trim(); return this; } @@ -231,7 +231,7 @@ export class AllocationAllocateBuilder extends TransactionBuilder { amount: this._amount, token: this._token, operatorId: this._operatorId, - contractId: this._contractId, + ...(this._contractId !== null && this._contractId !== undefined && { contractId: this._contractId }), tradeId: this._tradeId, transferLegId: this._transferLegId, allocateBefore: this._allocateBefore, @@ -256,7 +256,7 @@ export class AllocationAllocateBuilder extends TransactionBuilder { if (!this._amount) throw new Error('amount is missing'); if (!this._token) throw new Error('token is missing'); if (!this._operatorId) throw new Error('operatorId is missing'); - if (!this._contractId) throw new Error('contractId is missing'); + if (this._contractId === undefined) throw new Error('contractId is missing'); if (!this._tradeId) throw new Error('tradeId is missing'); if (!this._transferLegId) throw new Error('transferLegId is missing'); if (!this._allocateBefore) throw new Error('allocateBefore is missing'); diff --git a/modules/sdk-coin-canton/src/lib/iface.ts b/modules/sdk-coin-canton/src/lib/iface.ts index f1c9421ea1..ee1871bfbb 100644 --- a/modules/sdk-coin-canton/src/lib/iface.ts +++ b/modules/sdk-coin-canton/src/lib/iface.ts @@ -194,7 +194,7 @@ export interface CantonAllocationAllocateRequest { amount: number; token: string; operatorId: string; - contractId: string; + contractId?: string; tradeId: string; transferLegId: string; allocateBefore: string; diff --git a/modules/sdk-coin-canton/test/unit/builder/allocationAllocate/allocationAllocateBuilder.ts b/modules/sdk-coin-canton/test/unit/builder/allocationAllocate/allocationAllocateBuilder.ts index beca0c6039..a4ea6820b6 100644 --- a/modules/sdk-coin-canton/test/unit/builder/allocationAllocate/allocationAllocateBuilder.ts +++ b/modules/sdk-coin-canton/test/unit/builder/allocationAllocate/allocationAllocateBuilder.ts @@ -295,6 +295,26 @@ describe('AllocationAllocate Builder', () => { assert.throws(() => txBuilder.contractId(''), /contractId must be a non-empty string/); }); + it('should accept null contractId and omit it from the request object', function () { + const txBuilder = new AllocationAllocateBuilder(coins.get('tcanton')); + const tx = new Transaction(coins.get('tcanton')); + txBuilder.initBuilder(tx); + txBuilder + .commandId(commandId) + .amount(amount) + .token(token) + .operatorId(operatorId) + .contractId(null) + .tradeId(tradeId) + .transferLegId(transferLegId) + .allocateBefore(allocateBefore) + .settleBefore(settleBefore) + .receiverPartyId(receiverPartyId) + .senderPartyId(senderPartyId); + const requestObj = txBuilder.toRequestObject(); + assert.equal(requestObj.contractId, undefined, 'contractId should be absent when set to null'); + }); + it('should throw if tradeId is an empty string', function () { const txBuilder = new AllocationAllocateBuilder(coins.get('tcanton')); assert.throws(() => txBuilder.tradeId(''), /tradeId must be a non-empty string/);