Skip to content

Commit 22371c8

Browse files
authored
Merge pull request #7463 from BitGo/WIN-7828-qty-validation-fix
fix(sdk-coin-ada): shouldn't throw insufficient qty for total token s…
2 parents 3d1c90c + 6820999 commit 22371c8

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

modules/sdk-coin-ada/src/lib/transactionBuilder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
259259

260260
const currentQty = this._mutableSenderAssetList[fingerprint].quantity;
261261
const remainingQty = BigInt(currentQty) - BigInt(quantity);
262-
this._mutableSenderAssetList[fingerprint].quantity = (remainingQty > 0n ? remainingQty : 0n).toString();
263-
264-
if (CardanoWasm.BigNum.from_str(this._mutableSenderAssetList[fingerprint].quantity).is_zero()) {
262+
if (remainingQty < 0n) {
265263
throw new BuildTransactionError('Insufficient qty: not enough token qty to cover receiver output');
266264
}
267265

266+
this._mutableSenderAssetList[fingerprint].quantity = remainingQty.toString();
267+
268268
const minAmountNeededForAssetOutput = this.addTokensToOutput(change, outputs, receiverAddress, {
269269
policy_id: policyId,
270270
asset_name: assetName,

modules/sdk-coin-ada/test/unit/tokenWithdrawal.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,39 @@ describe('ADA Token Operations', async () => {
291291

292292
await txBuilder.build().should.rejectedWith('Insufficient qty: not enough token qty to cover receiver output');
293293
});
294+
295+
it(`should build a transaction with ${assetName} when the token qty is exactly the qty in withdrawal`, async () => {
296+
const quantity = '1';
297+
const totalInput = 20000000;
298+
const totalAssetList = {
299+
[fingerprint]: {
300+
quantity: '1',
301+
policy_id: policyId,
302+
asset_name: asciiEncodedName,
303+
},
304+
};
305+
306+
const txBuilder = factory.getTransferBuilder();
307+
txBuilder.input({
308+
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
309+
transaction_index: 1,
310+
});
311+
312+
txBuilder.output({
313+
address: receiverAddress,
314+
amount: '0', // Set ADA amount to 0 for token transfer (min ADA is handled in sdk build)
315+
multiAssets: {
316+
asset_name: asciiEncodedName,
317+
policy_id: policyId,
318+
quantity,
319+
fingerprint,
320+
},
321+
});
322+
323+
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
324+
txBuilder.ttl(800000000);
325+
txBuilder.isTokenTransaction();
326+
327+
await txBuilder.build().should.not.be.rejected();
328+
});
294329
});

0 commit comments

Comments
 (0)