Skip to content

Commit

Permalink
fix: add op_return value size when calculating fee
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Jul 1, 2024
1 parent ae7292a commit 8dafe14
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btc-staking-ts",
"version": "0.2.7",
"version": "0.2.8",
"description": "Library exposing methods for the creation and consumption of Bitcoin transactions pertaining to Babylon's Bitcoin Staking protocol. Experimental version, should not be used for production purposes or with real funds.",
"module": "dist/index.js",
"main": "dist/index.cjs",
Expand Down Expand Up @@ -60,4 +60,4 @@
"@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3",
"bitcoinjs-lib": "^6.1.5"
}
}
}
5 changes: 5 additions & 0 deletions src/constants/fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export const MAX_NON_LEGACY_OUTPUT_SIZE = 43;
export const WITHDRAW_TX_BUFFER_SIZE = 17;
// Threshold for wallet relay fee rate. Different buffer fees are used based on this threshold
export const WALLET_RELAY_FEE_RATE_THRESHOLD = 2;
// Estimated size of the OP_RETURN output value in bytes
export const OP_RETURN_OUTPUT_VALUE_SIZE = 8;
// Because our OP_RETURN data will always be less than 80 bytes, which is less than 0xfd (253),
// the value serialization size will always be 1 byte.
export const OP_RETURN_VALUE_SERIALIZE_SIZE = 1;
4 changes: 3 additions & 1 deletion src/utils/fee/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { BTC_DUST_SAT } from "../../constants/dustSat";
import {
LOW_RATE_ESTIMATION_ACCURACY_BUFFER,
MAX_NON_LEGACY_OUTPUT_SIZE,
OP_RETURN_OUTPUT_VALUE_SIZE,
OP_RETURN_VALUE_SERIALIZE_SIZE,
P2TR_INPUT_SIZE,
TX_BUFFER_SIZE_OVERHEAD,
WALLET_RELAY_FEE_RATE_THRESHOLD,
Expand Down Expand Up @@ -141,7 +143,7 @@ const getEstimatedSize = (
? address.toOutputScript(output.address, network)
: output.script;
if (isOP_RETURN(script)) {
return acc + script.length;
return acc + script.length + OP_RETURN_OUTPUT_VALUE_SIZE + OP_RETURN_VALUE_SERIALIZE_SIZE;
}
return acc + MAX_NON_LEGACY_OUTPUT_SIZE;
}, 0);
Expand Down
14 changes: 7 additions & 7 deletions tests/utils/fee/stakingtxFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ testingNetworks.forEach(({ networkName, network, dataGenerator }) => {
1,
outputs,
);
expect(result.fee).toBe(316); // This number is calculated manually
expect(result.fee).toBe(325); // This number is calculated manually
expect(result.selectedUTXOs.length).toEqual(2);

result = getStakingTxInputUTXOsAndFees(
Expand All @@ -106,7 +106,7 @@ testingNetworks.forEach(({ networkName, network, dataGenerator }) => {
2,
outputs,
);
expect(result.fee).toBe(516); // This number is calculated manually
expect(result.fee).toBe(534); // This number is calculated manually
expect(result.selectedUTXOs.length).toEqual(2);

// Once fee rate is set to 3, the fee will be calculated with addition of TX_BUFFER_SIZE_OVERHEAD * feeRate
Expand All @@ -117,7 +117,7 @@ testingNetworks.forEach(({ networkName, network, dataGenerator }) => {
3,
outputs,
);
expect(result.fee).toBe(729); // This number is calculated manually
expect(result.fee).toBe(756); // This number is calculated manually
expect(result.selectedUTXOs.length).toEqual(2);
});

Expand Down Expand Up @@ -149,7 +149,7 @@ testingNetworks.forEach(({ networkName, network, dataGenerator }) => {
1,
outputs,
);
expect(result.fee).toBe(336); // This number is calculated manually
expect(result.fee).toBe(345); // This number is calculated manually
expect(result.selectedUTXOs.length).toEqual(2);
});

Expand All @@ -163,7 +163,7 @@ testingNetworks.forEach(({ networkName, network, dataGenerator }) => {
txid: dataGenerator.generateRandomTxId(),
vout: Math.floor(Math.random() * 10),
scriptPubKey: nativeSegwit.scriptPubKey,
value: 1000,
value: 1009,
},
{
txid: dataGenerator.generateRandomTxId(),
Expand All @@ -181,7 +181,7 @@ testingNetworks.forEach(({ networkName, network, dataGenerator }) => {
1,
outputs,
);
expect(result.fee).toBe(293); // This is the fee for 2 inputs and 2 outputs without change
expect(result.fee).toBe(302); // This is the fee for 2 inputs and 2 outputs without change
expect(result.selectedUTXOs.length).toEqual(2);
});

Expand Down Expand Up @@ -213,7 +213,7 @@ testingNetworks.forEach(({ networkName, network, dataGenerator }) => {
1,
outputs,
);
expect(result.fee).toBe(225);
expect(result.fee).toBe(234);
expect(result.selectedUTXOs.length).toEqual(1);
});
});
Expand Down

0 comments on commit 8dafe14

Please sign in to comment.