-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: filter out uneconomical utxos, closes #4505
- Loading branch information
1 parent
be0d9a7
commit a6e116c
Showing
7 changed files
with
291 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/app/common/transactions/bitcoin/fees/bitcoin-fees.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import BigNumber from 'bignumber.js'; | ||
import { sha256 } from 'bitcoinjs-lib/src/crypto'; | ||
|
||
import { UtxoResponseItem } from '@app/query/bitcoin/bitcoin-client'; | ||
|
||
import { filterUneconomicalUtxos } from '../utils'; | ||
import { calculateMaxBitcoinSpend } from './calculate-max-bitcoin-spend'; | ||
|
||
function generateTxId(value: number): UtxoResponseItem { | ||
const buffer = Buffer.from(Math.random().toString()); | ||
return { | ||
txid: sha256(sha256(buffer)).toString(), | ||
vout: 0, | ||
status: { | ||
confirmed: true, | ||
block_height: 2568495, | ||
block_hash: '000000000000008622fafce4a5388861b252d534f819d0f7cb5d4f2c5f9c1638', | ||
block_time: 1703787327, | ||
}, | ||
value, | ||
}; | ||
} | ||
|
||
function generateTransactions(values: number[]) { | ||
return values.map(val => generateTxId(val)); | ||
} | ||
|
||
function generateAverageFee(value: number) { | ||
return { | ||
hourFee: BigNumber(value / 2), | ||
halfHourFee: BigNumber(value), | ||
fastestFee: BigNumber(value * 2), | ||
}; | ||
} | ||
|
||
describe(calculateMaxBitcoinSpend.name, () => { | ||
const utxos = generateTransactions([600, 600, 1200, 1200, 10000, 10000, 25000, 40000, 50000000]); | ||
|
||
test('with 1 sat/vb fee', () => { | ||
const fee = 1; | ||
const maxBitcoinSpend = calculateMaxBitcoinSpend({ | ||
address: '', | ||
utxos, | ||
fetchedFeeRates: generateAverageFee(fee), | ||
}); | ||
expect(maxBitcoinSpend.amount.amount.toNumber()).toEqual(50087948); | ||
}); | ||
|
||
test('with 5 sat/vb fee', () => { | ||
const fee = 5; | ||
const maxBitcoinSpend = calculateMaxBitcoinSpend({ | ||
address: '', | ||
utxos, | ||
fetchedFeeRates: generateAverageFee(fee), | ||
}); | ||
expect(maxBitcoinSpend.amount.amount.toNumber()).toEqual(50085342); | ||
}); | ||
|
||
test('with 30 sat/vb fee', () => { | ||
const fee = 30; | ||
const maxBitcoinSpend = calculateMaxBitcoinSpend({ | ||
address: '', | ||
utxos, | ||
fetchedFeeRates: generateAverageFee(fee), | ||
}); | ||
expect(maxBitcoinSpend.amount.amount.toNumber()).toEqual(50073585); | ||
}); | ||
|
||
test('with 100 sat/vb fee', () => { | ||
const fee = 100; | ||
const maxBitcoinSpend = calculateMaxBitcoinSpend({ | ||
address: '', | ||
utxos, | ||
fetchedFeeRates: generateAverageFee(fee), | ||
}); | ||
expect(maxBitcoinSpend.amount.amount.toNumber()).toEqual(50046950); | ||
}); | ||
|
||
test('with 400 sat/vb fee', () => { | ||
const fee = 400; | ||
const maxBitcoinSpend = calculateMaxBitcoinSpend({ | ||
address: '', | ||
utxos, | ||
fetchedFeeRates: generateAverageFee(fee), | ||
}); | ||
expect(maxBitcoinSpend.amount.amount.toNumber()).toEqual(49969100); | ||
}); | ||
}); | ||
|
||
describe(filterUneconomicalUtxos.name, () => { | ||
const utxos = generateTransactions([600, 600, 1200, 1200, 10000, 10000, 25000, 40000, 50000000]); | ||
|
||
test('with 1 sat/vb fee', () => { | ||
const fee = 1; | ||
const filteredUtxos = filterUneconomicalUtxos({ | ||
address: '', | ||
utxos, | ||
feeRate: fee, | ||
}); | ||
|
||
expect(filteredUtxos.length).toEqual(9); | ||
}); | ||
|
||
test('with 10 sat/vb fee', () => { | ||
const fee = 10; | ||
const filteredUtxos = filterUneconomicalUtxos({ | ||
address: '', | ||
utxos, | ||
feeRate: fee, | ||
}); | ||
expect(filteredUtxos.length).toEqual(7); | ||
}); | ||
|
||
test('with 30 sat/vb fee', () => { | ||
const fee = 30; | ||
const filteredUtxos = filterUneconomicalUtxos({ | ||
address: '', | ||
utxos, | ||
feeRate: fee, | ||
}); | ||
expect(filteredUtxos.length).toEqual(5); | ||
}); | ||
|
||
test('with 200 sat/vb fee', () => { | ||
const fee = 200; | ||
const filteredUtxos = filterUneconomicalUtxos({ | ||
address: '', | ||
utxos, | ||
feeRate: fee, | ||
}); | ||
expect(filteredUtxos.length).toEqual(3); | ||
}); | ||
|
||
test('with 400 sat/vb fee', () => { | ||
const fee = 400; | ||
const filteredUtxos = filterUneconomicalUtxos({ | ||
address: '', | ||
utxos, | ||
feeRate: fee, | ||
}); | ||
expect(filteredUtxos.length).toEqual(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/app/common/transactions/bitcoin/fees/calculate-max-bitcoin-spend.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { AverageBitcoinFeeRates } from '@shared/models/fees/bitcoin-fees.model'; | ||
import { createMoney } from '@shared/models/money.model'; | ||
|
||
import { satToBtc } from '@app/common/money/unit-conversion'; | ||
import { UtxoResponseItem } from '@app/query/bitcoin/bitcoin-client'; | ||
|
||
import { filterUneconomicalUtxos, getSpendableAmount } from '../utils'; | ||
|
||
interface CalculateMaxBitcoinSpend { | ||
address: string; | ||
utxos: UtxoResponseItem[]; | ||
fetchedFeeRates?: AverageBitcoinFeeRates; | ||
feeRate?: number; | ||
} | ||
|
||
export function calculateMaxBitcoinSpend({ | ||
address, | ||
utxos, | ||
feeRate, | ||
fetchedFeeRates, | ||
}: CalculateMaxBitcoinSpend) { | ||
if (!utxos.length || !fetchedFeeRates) | ||
return { | ||
spendAllFee: 0, | ||
amount: createMoney(0, 'BTC'), | ||
spendableBitcoin: new BigNumber(0), | ||
}; | ||
|
||
const currentFeeRate = feeRate ?? fetchedFeeRates.halfHourFee.toNumber(); | ||
|
||
const filteredUtxos = filterUneconomicalUtxos({ | ||
utxos, | ||
feeRate: currentFeeRate, | ||
address, | ||
}); | ||
|
||
const { spendableAmount, fee } = getSpendableAmount({ | ||
utxos: filteredUtxos, | ||
feeRate: currentFeeRate, | ||
address, | ||
}); | ||
|
||
return { | ||
spendAllFee: fee, | ||
amount: createMoney(spendableAmount, 'BTC'), | ||
spendableBitcoin: satToBtc(spendableAmount), | ||
}; | ||
} |
Oops, something went wrong.