Skip to content

Commit

Permalink
fix: fee estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 committed Aug 20, 2024
1 parent d53f721 commit fa83a89
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
44 changes: 21 additions & 23 deletions lib/transaction/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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();
};
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/kaspacore-lib",
"version": "1.0.0-alpha.5",
"version": "1.0.0-alpha.6",
"main": "index.js",
"authors": [
{
Expand Down

0 comments on commit fa83a89

Please sign in to comment.