From aef56c5c14941f768678f8d2c3e9f0a92206d89c Mon Sep 17 00:00:00 2001 From: Gary Nadir Date: Sat, 16 Jan 2021 14:22:02 -0400 Subject: [PATCH] fix(generateBurnOpReturn): Added handler when burning amount is higher than token amount --- src/slp/tokentype1.js | 7 ++++++- test/unit/slp-tokentype1.js | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/slp/tokentype1.js b/src/slp/tokentype1.js index c71d0f2..32283d3 100644 --- a/src/slp/tokentype1.js +++ b/src/slp/tokentype1.js @@ -220,7 +220,12 @@ class TokenType1 { // Calculate the total amount of tokens owned by the wallet. let totalTokens = 0 for (let i = 0; i < tokenUtxos.length; i++) { - totalTokens += tokenUtxos[i].tokenQty + totalTokens += parseFloat(tokenUtxos[i].tokenQty) + } + + // Make sure burn quantity isn't bigger than the total amount in tokens + if (burnQty > totalTokens) { + burnQty = totalTokens } const remainder = totalTokens - burnQty diff --git a/test/unit/slp-tokentype1.js b/test/unit/slp-tokentype1.js index d3bda31..2dc3e74 100644 --- a/test/unit/slp-tokentype1.js +++ b/test/unit/slp-tokentype1.js @@ -1217,11 +1217,11 @@ describe('#SLP TokenType1', () => { tokenDocumentUrl: '', tokenDocumentHash: '', decimals: 8, - tokenQty: 7 + tokenQty: '7' } ] - const result = bchjs.SLP.TokenType1.generateBurnOpReturn(tokenUtxos, 1) + const result = bchjs.SLP.TokenType1.generateBurnOpReturn(tokenUtxos, 8) // console.log(`result: ${JSON.stringify(result, null, 2)}`) // console.log(`result: `, result)