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 21, 2024
1 parent d53f721 commit cdbf28a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module '@onekeyfe/kaspacore-lib' {
declare module '@onekeyfe/kaspa-core-lib' {

function initRuntime(): Promise;
function setDebugLevel(level:number):void;
Expand Down
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
4 changes: 2 additions & 2 deletions 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",
"name": "@onekeyfe/kaspa-core-lib",
"version": "1.0.0-alpha.2",
"main": "index.js",
"authors": [
{
Expand Down

0 comments on commit cdbf28a

Please sign in to comment.