Revert "mtx: allow multiple indicies for subtractIndex."
This reverts commit d489238711.
This commit is contained in:
parent
d489238711
commit
31651ce76e
@ -1223,27 +1223,41 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempt to subtract a fee from all outputs evenly.
|
* Attempt to subtract a fee from a single output.
|
||||||
|
* @param {Number} index
|
||||||
* @param {Amount} fee
|
* @param {Amount} fee
|
||||||
* @param {Set|null} set
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MTX.prototype.subtractFee = function subtractFee(fee, set) {
|
MTX.prototype.subtractIndex = function subtractIndex(index, fee) {
|
||||||
|
assert(typeof index === 'number');
|
||||||
|
assert(typeof fee === 'number');
|
||||||
|
|
||||||
|
const output = this.outputs[index];
|
||||||
|
|
||||||
|
if (!output)
|
||||||
|
throw new Error('Subtraction index does not exist.');
|
||||||
|
|
||||||
|
if (output.value < fee + output.getDustThreshold())
|
||||||
|
throw new Error('Could not subtract fee.');
|
||||||
|
|
||||||
|
output.value -= fee;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to subtract a fee from all outputs evenly.
|
||||||
|
* @param {Amount} fee
|
||||||
|
*/
|
||||||
|
|
||||||
|
MTX.prototype.subtractFee = function subtractFee(fee) {
|
||||||
assert(typeof fee === 'number');
|
assert(typeof fee === 'number');
|
||||||
|
|
||||||
let outputs = 0;
|
let outputs = 0;
|
||||||
|
|
||||||
for (let i = 0; i < this.outputs.length; i++) {
|
for (const output of this.outputs) {
|
||||||
const output = this.outputs[i];
|
|
||||||
|
|
||||||
if (set && !set.has(i))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Ignore nulldatas and
|
// Ignore nulldatas and
|
||||||
// other OP_RETURN scripts.
|
// other OP_RETURN scripts.
|
||||||
if (output.script.isUnspendable())
|
if (output.script.isUnspendable())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
outputs += 1;
|
outputs += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1254,12 +1268,7 @@ MTX.prototype.subtractFee = function subtractFee(fee, set) {
|
|||||||
const share = (fee - left) / outputs;
|
const share = (fee - left) / outputs;
|
||||||
|
|
||||||
// First pass, remove even shares.
|
// First pass, remove even shares.
|
||||||
for (let i = 0; i < this.outputs.length; i++) {
|
for (const output of this.outputs) {
|
||||||
const output = this.outputs[i];
|
|
||||||
|
|
||||||
if (set && !set.has(i))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (output.script.isUnspendable())
|
if (output.script.isUnspendable())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -1271,12 +1280,7 @@ MTX.prototype.subtractFee = function subtractFee(fee, set) {
|
|||||||
|
|
||||||
// Second pass, remove the remainder
|
// Second pass, remove the remainder
|
||||||
// for the one unlucky output.
|
// for the one unlucky output.
|
||||||
for (let i = 0; i < this.outputs.length; i++) {
|
for (const output of this.outputs) {
|
||||||
const output = this.outputs[i];
|
|
||||||
|
|
||||||
if (set && !set.has(i))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (output.script.isUnspendable())
|
if (output.script.isUnspendable())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -1309,8 +1313,13 @@ MTX.prototype.fund = async function fund(coins, options) {
|
|||||||
this.addCoin(coin);
|
this.addCoin(coin);
|
||||||
|
|
||||||
// Attempt to subtract fee.
|
// Attempt to subtract fee.
|
||||||
if (select.subtractFee)
|
if (select.subtractFee) {
|
||||||
this.subtractFee(select.fee, select.subtractIndex);
|
const index = select.subtractIndex;
|
||||||
|
if (index !== -1)
|
||||||
|
this.subtractIndex(index, select.fee);
|
||||||
|
else
|
||||||
|
this.subtractFee(select.fee);
|
||||||
|
}
|
||||||
|
|
||||||
// Add a change output.
|
// Add a change output.
|
||||||
const output = new Output();
|
const output = new Output();
|
||||||
@ -1545,7 +1554,7 @@ function CoinSelector(tx, options) {
|
|||||||
|
|
||||||
this.selection = 'value';
|
this.selection = 'value';
|
||||||
this.subtractFee = false;
|
this.subtractFee = false;
|
||||||
this.subtractIndex = null;
|
this.subtractIndex = -1;
|
||||||
this.height = -1;
|
this.height = -1;
|
||||||
this.depth = -1;
|
this.depth = -1;
|
||||||
this.hardFee = -1;
|
this.hardFee = -1;
|
||||||
@ -1601,32 +1610,22 @@ CoinSelector.prototype.fromOptions = function fromOptions(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options.subtractFee != null) {
|
if (options.subtractFee != null) {
|
||||||
assert(typeof options.subtractFee === 'boolean');
|
if (typeof options.subtractFee === 'number') {
|
||||||
this.subtractFee = options.subtractFee;
|
assert(util.isInt(options.subtractFee));
|
||||||
|
assert(options.subtractFee >= -1);
|
||||||
|
this.subtractIndex = options.subtractFee;
|
||||||
|
this.subtractFee = this.subtractIndex !== -1;
|
||||||
|
} else {
|
||||||
|
assert(typeof options.subtractFee === 'boolean');
|
||||||
|
this.subtractFee = options.subtractFee;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.subtractIndex != null) {
|
if (options.subtractIndex != null) {
|
||||||
let indicies = null;
|
assert(util.isInt(options.subtractIndex));
|
||||||
|
assert(options.subtractIndex >= -1);
|
||||||
if (typeof options.subtractIndex === 'number') {
|
this.subtractIndex = options.subtractIndex;
|
||||||
indicies = [options.subtractIndex];
|
this.subtractFee = this.subtractIndex !== -1;
|
||||||
} else {
|
|
||||||
assert(Array.isArray(options.subtractIndex));
|
|
||||||
indicies = options.subtractIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (indicies.length > 0) {
|
|
||||||
const set = new Set();
|
|
||||||
|
|
||||||
for (const index of indicies) {
|
|
||||||
assert(util.isU32(index));
|
|
||||||
assert(index < this.tx.outputs.length);
|
|
||||||
set.add(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.subtractIndex = set;
|
|
||||||
this.subtractFee = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.height != null) {
|
if (options.height != null) {
|
||||||
|
|||||||
@ -391,7 +391,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
|
|||||||
selection: valid.str('selection'),
|
selection: valid.str('selection'),
|
||||||
smart: valid.bool('smart'),
|
smart: valid.bool('smart'),
|
||||||
subtractFee: valid.bool('subtractFee'),
|
subtractFee: valid.bool('subtractFee'),
|
||||||
subtractIndex: valid.get('subtractIndex'),
|
subtractIndex: valid.i32('subtractIndex'),
|
||||||
depth: valid.u32(['confirmations', 'depth']),
|
depth: valid.u32(['confirmations', 'depth']),
|
||||||
outputs: []
|
outputs: []
|
||||||
};
|
};
|
||||||
@ -431,7 +431,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
|
|||||||
selection: valid.str('selection'),
|
selection: valid.str('selection'),
|
||||||
smart: valid.bool('smart'),
|
smart: valid.bool('smart'),
|
||||||
subtractFee: valid.bool('subtractFee'),
|
subtractFee: valid.bool('subtractFee'),
|
||||||
subtractIndex: valid.get('subtractIndex'),
|
subtractIndex: valid.i32('subtractIndex'),
|
||||||
depth: valid.u32(['confirmations', 'depth']),
|
depth: valid.u32(['confirmations', 'depth']),
|
||||||
outputs: []
|
outputs: []
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user