Skip to content

Commit 4bad0f8

Browse files
OttoAllmendingerllm-git
andcommitted
fix(abstract-utxo): add fee rate test for backup key recovery
Add test case to verify the actual fee rate used in backup key recovery transactions. This ensures that the fee calculation logic is working as expected. Note that basically all tests fail because fee calculation is utterly broken. Issue: BTC-2891 Co-authored-by: llm-git <[email protected]>
1 parent 8f61ac4 commit 4bad0f8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

modules/abstract-utxo/test/unit/recovery/backupKeyRecovery.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import nock = require('nock');
77
import { BIP32Interface } from '@bitgo/utxo-lib';
88
import * as utxolib from '@bitgo/utxo-lib';
99
import { Config, krsProviders, Triple } from '@bitgo/sdk-core';
10+
import { Dimensions } from '@bitgo/unspents';
1011

1112
import {
1213
AbstractUtxoCoin,
1314
backupKeyRecovery,
1415
BackupKeyRecoveryTransansaction,
1516
CoingeckoApi,
17+
DEFAULT_RECOVERY_FEERATE_SAT_VBYTE_V2,
1618
FormattedOfflineVaultTxInfo,
1719
} from '../../../src';
1820
import {
@@ -190,6 +192,21 @@ function run(
190192
);
191193
});
192194

195+
it('has expected fee rate', function () {
196+
if (!(recoveryTx instanceof utxolib.bitgo.UtxoPsbt)) {
197+
this.skip();
198+
}
199+
const expectedFeeRate = params.feeRate ?? DEFAULT_RECOVERY_FEERATE_SAT_VBYTE_V2;
200+
const inputSum = utxolib.bitgo.unspentSum(recoverUnspents, 'bigint');
201+
const outputSum = recoveryTx.txOutputs.reduce((sum, o) => sum + o.value, BigInt(0));
202+
const fee = inputSum - outputSum;
203+
const vsize = Dimensions.fromPsbt(recoveryTx).getVSize();
204+
const feeRateSatB = Number(fee) / vsize;
205+
const diff = Math.abs(feeRateSatB - expectedFeeRate) / expectedFeeRate;
206+
// within 10%
207+
assert.strictEqual(diff < 0.1, true, `expected fee rate ${expectedFeeRate} but got ${feeRateSatB}`);
208+
});
209+
193210
it('matches fixture', async function () {
194211
const fixtureCoin = getNormalTestnetCoin(coin);
195212
const fixtureRecovery = { ...recovery };

0 commit comments

Comments
 (0)