Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: split appraise into component parts #39

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 54 additions & 16 deletions dashtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @prop {Uint32} MAX_INPUT_SIZE - 149 each (with padding)
* @prop {Uint32} OUTPUT_SIZE - 34 each
* @prop {TxAppraise} appraise
* @prop {TxAppraiseCounts} _appraiseCounts
* @prop {TxAppraiseMemos} _appraiseMemos
* @prop {TxToDash} toDash
* @prop {TxToSats} toSats
* @prop {TxCreate} create
Expand Down Expand Up @@ -133,34 +135,56 @@ var DashTx = ("object" === typeof module && exports) || {};
25; // lockscript

Tx.appraise = function (txInfo) {
let extraSize = Tx._appraiseMemos(txInfo.outputs);
let fees = Tx._appraiseCounts(
txInfo.inputs.length,
txInfo.outputs.length,
extraSize,
);

return fees;
};

Tx._appraiseCounts = function (numInputs, numOutputs, extraSize) {
let min = Tx._HEADER_ONLY_SIZE;

min += Tx.utils.toVarIntSize(txInfo.inputs.length);
min += Tx.MIN_INPUT_SIZE * txInfo.inputs.length;
min += Tx.utils.toVarIntSize(numInputs);
min += Tx.MIN_INPUT_SIZE * numInputs;

min += Tx.utils.toVarIntSize(numOutputs);
if (extraSize) {
min += extraSize; // memos, etc
}

let maxPadding = Tx.MAX_INPUT_PAD * numInputs;
let max = min + maxPadding;

let spread = max - min;
let halfSpread = Math.ceil(spread / 2);
let mid = min + halfSpread;

let fees = { min, mid, max };
return fees;
};

Tx._appraiseMemos = function (outputs) {
let size = 0;

min += Tx.utils.toVarIntSize(txInfo.outputs.length);
for (let output of txInfo.outputs) {
for (let output of outputs) {
if (output.memo) {
let memoSize = output.memo.length / 2;
if (memoSize > MAX_U8) {
min += 2;
size += 2;
} else if (memoSize >= OP_PUSHDATA1_INT) {
min += 1;
size += 1;
}
min += OP_RETURN_HEADER_SIZE + memoSize;
size += OP_RETURN_HEADER_SIZE + memoSize;
continue;
}
min += Tx.OUTPUT_SIZE;
size += Tx.OUTPUT_SIZE;
}

let maxPadding = Tx.MAX_INPUT_PAD * txInfo.inputs.length;
let max = min + maxPadding;

let spread = max - min;
let halfSpread = Math.ceil(spread / 2);
let mid = min + halfSpread;

return { min: min, mid: mid, max: max };
return size;
};

Tx.toDash = function (satoshis) {
Expand Down Expand Up @@ -1392,6 +1416,20 @@ if ("object" === typeof module) {
* @returns {TxFees}
*/

/**
* @callback TxAppraiseCounts
* @param {Uint32} numInputs
* @param {Uint32} numOutputs
* @param {Uint32} [extraSize] - for memos
* @returns {TxFees}
*/

/**
* @callback TxAppraiseMemos
* @param {Array<TxOutput>} outputs
* @returns {Uint32}
*/

/**
* @callback TxCreateHashable
* @param {TxInfo} txInfo
Expand Down
Loading