From fa83a8928204b6e84b6270475f15b34f0be235ee Mon Sep 17 00:00:00 2001 From: ByteZhang Date: Tue, 20 Aug 2024 16:38:44 +0800 Subject: [PATCH] fix: fee estimate --- lib/transaction/transaction.js | 44 ++++++++++++++++------------------ package.json | 2 +- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 9e60a9e..cbf676a 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -141,12 +141,12 @@ var ioProperty = { configurable: false, enumerable: true, get: function() { - return this._getInputAmount(); + return this._getInputAmount().toString(); } }; Object.defineProperty(Transaction.prototype, 'inputAmount', ioProperty); ioProperty.get = function() { - return this._getOutputAmount(); + return this._getOutputAmount().toString(); }; Object.defineProperty(Transaction.prototype, 'outputAmount', ioProperty); @@ -226,7 +226,7 @@ Transaction.prototype.getSerializationError = function(opts) { var unspent = this._getUnspentValue(); var unspentError; - if (unspent < 0) { + if (unspent.lt(0)) { if (!opts.disableMoreOutputThanInput) { unspentError = new errors.Transaction.InvalidOutputAmountSum(); } @@ -931,36 +931,34 @@ Transaction.prototype._addOutput = function(output) { /** * Calculates or gets the total output amount in satoshis * - * @return {Number} the transaction total output amount + * @return {BN} the transaction total output amount */ Transaction.prototype._getOutputAmount = function() { if (_.isUndefined(this._outputAmount)) { - var self = this; - this._outputAmount = 0; - _.each(this.outputs, function(output) { - self._outputAmount += output.satoshis; - }); + this._outputAmount = this.outputs.reduce((sum, output) => { + return sum.add(new BN(output.satoshis)); + }, new BN(0)); } return this._outputAmount; -}; + }; /** * Calculates or gets the total input amount in satoshis * - * @return {Number} the transaction total input amount + * @return {BN} the transaction total input amount */ Transaction.prototype._getInputAmount = function() { if (_.isUndefined(this._inputAmount)) { - this._inputAmount = _.sumBy(this.inputs, function(input) { - if (_.isUndefined(input.output)) { - throw new errors.Transaction.Input.MissingPreviousOutput(); - } - return input.output.satoshis; - }); + this._inputAmount = this.inputs.reduce((sum, input) => { + if (_.isUndefined(input.output)) { + throw new errors.Transaction.Input.MissingPreviousOutput(); + } + return sum.add(new BN(input.output.satoshis)); + }, new BN(0)); } return this._inputAmount; -}; + }; Transaction.prototype._updateChangeOutput = function() { if (!this._changeScript) { @@ -972,8 +970,8 @@ Transaction.prototype._updateChangeOutput = function() { } var available = this._getUnspentValue(); var fee = this.getFee(); - var changeAmount = available - fee; - if (changeAmount > 0) { + var changeAmount = available.sub(fee); + if (changeAmount.gt(0)) { this._changeIndex = this.outputs.length; this._addOutput(new Output({ script: this._changeScript, @@ -1010,7 +1008,7 @@ Transaction.prototype.getFee = function() { } // if no change output is set, fees should equal all the unspent amount if (!this._changeScript) { - return this._getUnspentValue(); + return this._getUnspentValue().toNumber(); } return this._estimateFee(); }; @@ -1028,14 +1026,14 @@ Transaction.prototype._estimateFee = function() { } var fee = Math.ceil(getFee(estimatedSize)); var feeWithChange = Math.ceil(getFee(estimatedSize) + getFee(Transaction.CHANGE_OUTPUT_MAX_SIZE)); - if (!this._changeScript || available <= feeWithChange) { + if (!this._changeScript || available.lte(feeWithChange)) { return fee; } return feeWithChange; }; Transaction.prototype._getUnspentValue = function() { - return this._getInputAmount() - this._getOutputAmount(); + return this._getInputAmount().sub(this._getOutputAmount()); }; Transaction.prototype._clearSignatures = function() { diff --git a/package.json b/package.json index cc0d83a..6468fd8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@onekeyfe/kaspacore-lib", - "version": "1.0.0-alpha.5", + "version": "1.0.0-alpha.6", "main": "index.js", "authors": [ {