Skip to content

Commit 957b22b

Browse files
committed
ref: split appraise into component parts
1 parent ddc0bb4 commit 957b22b

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

dashtx.js

+54-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* @prop {Uint32} MAX_INPUT_SIZE - 149 each (with padding)
1010
* @prop {Uint32} OUTPUT_SIZE - 34 each
1111
* @prop {TxAppraise} appraise
12+
* @prop {TxAppraiseCounts} _appraiseCounts
13+
* @prop {TxAppraiseMemos} _appraiseMemos
1214
* @prop {TxToDash} toDash
1315
* @prop {TxToSats} toSats
1416
* @prop {TxCreate} create
@@ -133,34 +135,56 @@ var DashTx = ("object" === typeof module && exports) || {};
133135
25; // lockscript
134136

135137
Tx.appraise = function (txInfo) {
138+
let extraSize = Tx._appraiseMemos(txInfo.outputs);
139+
let fees = Tx._appraiseCounts(
140+
txInfo.inputs.length,
141+
txInfo.outputs.length,
142+
extraSize,
143+
);
144+
145+
return fees;
146+
};
147+
148+
Tx._appraiseCounts = function (numInputs, numOutputs, extraSize) {
136149
let min = Tx._HEADER_ONLY_SIZE;
137150

138-
min += Tx.utils.toVarIntSize(txInfo.inputs.length);
139-
min += Tx.MIN_INPUT_SIZE * txInfo.inputs.length;
151+
min += Tx.utils.toVarIntSize(numInputs);
152+
min += Tx.MIN_INPUT_SIZE * numInputs;
153+
154+
min += Tx.utils.toVarIntSize(numOutputs);
155+
if (extraSize) {
156+
min += extraSize; // memos, etc
157+
}
158+
159+
let maxPadding = Tx.MAX_INPUT_PAD * numInputs;
160+
let max = min + maxPadding;
161+
162+
let spread = max - min;
163+
let halfSpread = Math.ceil(spread / 2);
164+
let mid = min + halfSpread;
165+
166+
let fees = { min, mid, max };
167+
return fees;
168+
};
169+
170+
Tx._appraiseMemos = function (outputs) {
171+
let size = 0;
140172

141-
min += Tx.utils.toVarIntSize(txInfo.outputs.length);
142-
for (let output of txInfo.outputs) {
173+
for (let output of outputs) {
143174
if (output.memo) {
144175
let memoSize = output.memo.length / 2;
145176
if (memoSize > MAX_U8) {
146-
min += 2;
177+
size += 2;
147178
} else if (memoSize >= OP_PUSHDATA1_INT) {
148-
min += 1;
179+
size += 1;
149180
}
150-
min += OP_RETURN_HEADER_SIZE + memoSize;
181+
size += OP_RETURN_HEADER_SIZE + memoSize;
151182
continue;
152183
}
153-
min += Tx.OUTPUT_SIZE;
184+
size += Tx.OUTPUT_SIZE;
154185
}
155186

156-
let maxPadding = Tx.MAX_INPUT_PAD * txInfo.inputs.length;
157-
let max = min + maxPadding;
158-
159-
let spread = max - min;
160-
let halfSpread = Math.ceil(spread / 2);
161-
let mid = min + halfSpread;
162-
163-
return { min: min, mid: mid, max: max };
187+
return size;
164188
};
165189

166190
Tx.toDash = function (satoshis) {
@@ -1392,6 +1416,20 @@ if ("object" === typeof module) {
13921416
* @returns {TxFees}
13931417
*/
13941418

1419+
/**
1420+
* @callback TxAppraiseCounts
1421+
* @param {Uint32} numInputs
1422+
* @param {Uint32} numOutputs
1423+
* @param {Uint32} [extraSize] - for memos
1424+
* @returns {TxFees}
1425+
*/
1426+
1427+
/**
1428+
* @callback TxAppraiseMemos
1429+
* @param {Array<TxOutput>} outputs
1430+
* @returns {Uint32}
1431+
*/
1432+
13951433
/**
13961434
* @callback TxCreateHashable
13971435
* @param {TxInfo} txInfo

0 commit comments

Comments
 (0)