Skip to content

Commit

Permalink
chore: rename estimated fee
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Apr 24, 2024
1 parent e5a6206 commit 2015df6
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ describe(determineUtxosForSpend.name, () => {
expect(estimation.txVBytes).toBeGreaterThan(208);
expect(estimation.txVBytes).toBeLessThan(209);
});

test('that Native Segwit, 10 input 2 outputs weighs 200vBytes', () => {
const estimation = determineUtxosForSpend({
utxos: [
{ value: 20_000 },
{ value: 20_000 },
{ value: 10_000 },
{ value: 10_000 },
{ value: 10_000 },
{ value: 10_000 },
{ value: 10_000 },
{ value: 10_000 },
{ value: 10_000 },
{ value: 10_000 },
] as any[],
amount: 100_000,
recipient: 'tb1qt28eagxcl9gvhq2rpj5slg7dwgxae2dn2hk93m',
feeRate: 20,
});
expect(estimation.txVBytes).toBeGreaterThan(750);
expect(estimation.txVBytes).toBeLessThan(751);
});
});

describe('sorting algorithm', () => {
Expand Down Expand Up @@ -94,13 +116,13 @@ describe(determineUtxosForSpend.name, () => {
test('that given a set of utxos, legacy is more expensive', () => {
const legacy = generate10kSpendWithDummyUtxoSet('15PyZveQd28E2SHZu2ugkWZBp6iER41vXj');
const segwit = generate10kSpendWithDummyUtxoSet('33SVjoCHJovrXxjDKLFSXo1h3t5KgkPzfH');
expect(legacy.estimatedFee).toBeGreaterThan(segwit.estimatedFee);
expect(legacy.fee).toBeGreaterThan(segwit.fee);
});

test('that given a set of utxos, wrapped segwit is more expensive than native', () => {
const segwit = generate10kSpendWithDummyUtxoSet('33SVjoCHJovrXxjDKLFSXo1h3t5KgkPzfH');
const native = generate10kSpendWithDummyUtxoSet('tb1qt28eagxcl9gvhq2rpj5slg7dwgxae2dn2hk93m');
expect(segwit.estimatedFee).toBeGreaterThan(native.estimatedFee);
expect(segwit.fee).toBeGreaterThan(native.fee);
});

test('that given a set of utxos, taproot is more expensive than native segwit', () => {
Expand All @@ -111,7 +133,7 @@ describe(determineUtxosForSpend.name, () => {
const taproot = generate10kSpendWithDummyUtxoSet(
'tb1parwmj7533de3k2fw2kntyqacspvhm67qnjcmpqnnpfvzu05l69nsczdywd'
);
expect(taproot.estimatedFee).toBeGreaterThan(native.estimatedFee);
expect(taproot.fee).toBeGreaterThan(native.fee);
});

test('against a random set of generated utxos', () => {
Expand All @@ -129,7 +151,7 @@ describe(determineUtxosForSpend.name, () => {

expect(result.outputs[1].value.toString()).toEqual(
sumNumbers(result.inputs.map(i => i.value))
.minus(result.estimatedFee)
.minus(result.fee)
.minus(amount.toString())
.toString()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ export function determineUtxosForSpendAll({
// Fee has already been deducted from the amount with send all
const outputs = [{ value: BigInt(amount), address: recipient }];

const estimatedFee = Math.ceil(sizeInfo.txVBytes * feeRate);
const fee = Math.ceil(sizeInfo.txVBytes * feeRate);

return {
inputs: filteredUtxos,
outputs,
size: sizeInfo.txVBytes,
estimatedFee,
fee,
};
}

Expand Down Expand Up @@ -101,24 +101,24 @@ export function determineUtxosForSpend({
neededUtxos.push(nextUtxo);
}

const estimatedFee = Math.ceil(
const fee = Math.ceil(
new BigNumber(estimateTransactionSize().txVBytes).multipliedBy(feeRate).toNumber()
);

const outputs = [
// outputs[0] = the desired amount going to recipient
{ value: BigInt(amount), address: recipient },
// outputs[1] = the remainder to be returned to a change address
{ value: BigInt(getUtxoTotal(neededUtxos).toString()) - BigInt(amount) - BigInt(estimatedFee) },
{ value: BigInt(getUtxoTotal(neededUtxos).toString()) - BigInt(amount) - BigInt(fee) },
];

return {
filteredUtxos,
inputs: neededUtxos,
outputs,
size: estimateTransactionSize().txVBytes,
fee,
...estimateTransactionSize(),
estimatedFee,
};
}

Expand Down Expand Up @@ -158,13 +158,13 @@ export function determineUtxosForSpendAllMultipleRecipients({
address,
}));

const estimatedFee = Math.ceil(sizeInfo.txVBytes * feeRate);
const fee = Math.ceil(sizeInfo.txVBytes * feeRate);

return {
inputs: filteredUtxos,
outputs,
size: sizeInfo.txVBytes,
estimatedFee,
fee,
};
}

Expand Down Expand Up @@ -204,7 +204,7 @@ export function determineUtxosForSpendMultipleRecipients({

if (!sizeInfo) throw new InsufficientFundsError();

const estimatedFee = Math.ceil(sizeInfo.txVBytes * feeRate);
const fee = Math.ceil(sizeInfo.txVBytes * feeRate);

const outputs: {
value: bigint;
Expand All @@ -216,14 +216,14 @@ export function determineUtxosForSpendMultipleRecipients({
address,
})),
// outputs[recipients.length] = the remainder to be returned to a change address
{ value: sum - BigInt(amount) - BigInt(estimatedFee) },
{ value: sum - BigInt(amount) - BigInt(fee) },
];

return {
filteredUtxos,
inputs: neededUtxos,
outputs,
size: sizeInfo.txVBytes,
estimatedFee,
fee,
};
}
12 changes: 4 additions & 8 deletions src/app/common/transactions/bitcoin/use-generate-bitcoin-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export function useGenerateUnsignedNativeSegwitSingleRecipientTx() {
utxos,
};

const {
inputs,
outputs,
estimatedFee: fee,
} = isSendingMax
const { inputs, outputs, fee } = isSendingMax
? determineUtxosForSpendAll(determineUtxosArgs)
: determineUtxosForSpend(determineUtxosArgs);

Expand Down Expand Up @@ -131,11 +127,11 @@ export function useGenerateUnsignedNativeSegwitMultipleRecipientsTx() {
utxos,
};

const { inputs, outputs, estimatedFee } = isSendingMax
const { inputs, outputs, fee } = isSendingMax
? determineUtxosForSpendAllMultipleRecipients(determineUtxosArgs)
: determineUtxosForSpendMultipleRecipients(determineUtxosArgs);

logger.info('Coin selection', { inputs, outputs, estimatedFee });
logger.info('Coin selection', { inputs, outputs, fee });

if (!inputs.length) throw new Error('No inputs to sign');
if (!outputs.length) throw new Error('No outputs to sign');
Expand Down Expand Up @@ -170,7 +166,7 @@ export function useGenerateUnsignedNativeSegwitMultipleRecipientsTx() {
tx.addOutputAddress(output.address, BigInt(output.value), networkMode);
});

return { hex: tx.hex, fee: estimatedFee, psbt: tx.toPSBT(), inputs };
return { hex: tx.hex, fee: fee, psbt: tx.toPSBT(), inputs };
} catch (e) {
// eslint-disable-next-line no-console
console.log('Error signing bitcoin transaction', e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ export function useBitcoinCustomFee({ amount, isSendingMax, recipient }: UseBitc
utxos,
feeRate,
};
const { estimatedFee } = isSendingMax
const { fee } = isSendingMax
? determineUtxosForSpendAll(determineUtxosArgs)
: determineUtxosForSpend(determineUtxosArgs);

return {
estimatedFee,
fee,
fiatFeeValue: `~ ${i18nFormatCurrency(
baseCurrencyAmountInQuote(createMoney(Math.ceil(estimatedFee), 'BTC'), btcMarketData)
baseCurrencyAmountInQuote(createMoney(Math.ceil(fee), 'BTC'), btcMarketData)
)}`,
};
},
Expand Down Expand Up @@ -81,14 +81,14 @@ export function useBitcoinCustomFeeMultipleRecipients({
utxos,
feeRate,
};
const { estimatedFee } = isSendingMax
const { fee } = isSendingMax
? determineUtxosForSpendAllMultipleRecipients(determineUtxosArgs)
: determineUtxosForSpendMultipleRecipients(determineUtxosArgs);

return {
estimatedFee,
fee,
fiatFeeValue: `~ ${i18nFormatCurrency(
baseCurrencyAmountInQuote(createMoney(Math.ceil(estimatedFee), 'BTC'), btcMarketData)
baseCurrencyAmountInQuote(createMoney(Math.ceil(fee), 'BTC'), btcMarketData)
)}`,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ function getFeeForList(
isSendingMax?: boolean
) {
try {
const { estimatedFee } = isSendingMax
const { fee } = isSendingMax
? determineUtxosForSpendAllMultipleRecipients(determineUtxosForFeeArgs)
: determineUtxosForSpendMultipleRecipients(determineUtxosForFeeArgs);
return estimatedFee;
return fee;
} catch (error) {
return null;
}
Expand Down
66 changes: 33 additions & 33 deletions src/app/components/bitcoin-fees-list/use-bitcoin-fees-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ function getFeeForList(
isSendingMax?: boolean
) {
try {
const { estimatedFee } = isSendingMax
const { fee } = isSendingMax
? determineUtxosForSpendAll(determineUtxosForFeeArgs)
: determineUtxosForSpend(determineUtxosForFeeArgs);
return estimatedFee;
return fee;
} catch (error) {
return null;
}
Expand Down Expand Up @@ -64,37 +64,37 @@ export function useBitcoinFeesList({
utxos,
};

// const determineUtxosForHighFeeArgs = {
// ...determineUtxosDefaultArgs,
// feeRate: feeRates.fastestFee.toNumber(),
// };
const determineUtxosForHighFeeArgs = {
...determineUtxosDefaultArgs,
feeRate: feeRates.fastestFee.toNumber(),
};

const determineUtxosForStandardFeeArgs = {
...determineUtxosDefaultArgs,
feeRate: feeRates.halfHourFee.toNumber(),
};

// const determineUtxosForLowFeeArgs = {
// ...determineUtxosDefaultArgs,
// feeRate: feeRates.hourFee.toNumber(),
// };
const determineUtxosForLowFeeArgs = {
...determineUtxosDefaultArgs,
feeRate: feeRates.hourFee.toNumber(),
};

const feesArr = [];

// const highFeeValue = getFeeForList(determineUtxosForHighFeeArgs, isSendingMax);
const highFeeValue = getFeeForList(determineUtxosForHighFeeArgs, isSendingMax);
const standardFeeValue = getFeeForList(determineUtxosForStandardFeeArgs, isSendingMax);
// const lowFeeValue = getFeeForList(determineUtxosForLowFeeArgs, isSendingMax);

// if (highFeeValue) {
// feesArr.push({
// label: BtcFeeType.High,
// value: highFeeValue,
// btcValue: formatMoneyPadded(createMoney(highFeeValue, 'BTC')),
// time: btcTxTimeMap.fastestFee,
// fiatValue: getFiatFeeValue(highFeeValue),
// feeRate: feeRates.fastestFee.toNumber(),
// });
// }
const lowFeeValue = getFeeForList(determineUtxosForLowFeeArgs, isSendingMax);

if (highFeeValue) {
feesArr.push({
label: BtcFeeType.High,
value: highFeeValue,
btcValue: formatMoneyPadded(createMoney(highFeeValue, 'BTC')),
time: btcTxTimeMap.fastestFee,
fiatValue: getFiatFeeValue(highFeeValue),
feeRate: feeRates.fastestFee.toNumber(),
});
}

if (standardFeeValue) {
feesArr.push({
Expand All @@ -107,16 +107,16 @@ export function useBitcoinFeesList({
});
}

// if (lowFeeValue) {
// feesArr.push({
// label: BtcFeeType.Low,
// value: lowFeeValue,
// btcValue: formatMoneyPadded(createMoney(lowFeeValue, 'BTC')),
// time: btcTxTimeMap.hourFee,
// fiatValue: getFiatFeeValue(lowFeeValue),
// feeRate: feeRates.hourFee.toNumber(),
// });
// }
if (lowFeeValue) {
feesArr.push({
label: BtcFeeType.Low,
value: lowFeeValue,
btcValue: formatMoneyPadded(createMoney(lowFeeValue, 'BTC')),
time: btcTxTimeMap.hourFee,
fiatValue: getFiatFeeValue(lowFeeValue),
feeRate: feeRates.hourFee.toNumber(),
});
}

return feesArr;
}, [feeRates, utxos, isSendingMax, balance.amount, amount.amount, recipient, btcMarketData]);
Expand Down
4 changes: 1 addition & 3 deletions src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ export function useRpcSignPsbt() {
txValue: formatMoney(transferTotalAsMoney),
};

navigate(RouteUrls.RpcSignPsbtSummary, {
state: psbtTxSummaryState,
});
navigate(RouteUrls.RpcSignPsbtSummary, { state: psbtTxSummaryState });
},
onError(e) {
navigate(RouteUrls.RequestError, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function useGenerateUnsignedOrdinalTx(inscriptionInput: UtxoWithDerivatio
utxos: nativeSegwitUtxos,
};

const { inputs, outputs, estimatedFee: fee } = determineUtxosForSpend(determineUtxosArgs);
const { inputs, outputs, fee } = determineUtxosForSpend(determineUtxosArgs);

try {
const tx = new btc.Transaction();
Expand Down
Loading

0 comments on commit 2015df6

Please sign in to comment.