Skip to content

Commit

Permalink
feat: modify protocol fee calculation by dropping decimal points
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Nov 21, 2024
1 parent 6f90f08 commit a0e9815
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "dlc-btc-lib",
"version": "2.4.16",
"version": "2.4.17",
"description": "This library provides a comprehensive set of interfaces and functions for minting dlcBTC tokens on supported blockchains.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/functions/bitcoin/bitcoin-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const ECDSA_PUBLIC_KEY_LENGTH = 33;
const bip32 = BIP32Factory(ellipticCurveCryptography);

export function getFeeAmount(bitcoinAmount: number, feeBasisPoints: number): number {
const feePercentage = new Decimal(feeBasisPoints).dividedBy(100);
return new Decimal(bitcoinAmount).times(feePercentage.dividedBy(100)).toNumber();
const feePercentage = new Decimal(feeBasisPoints).dividedBy(10000);
return new Decimal(bitcoinAmount).times(feePercentage).trunc().toNumber();
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/bitcoin-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
deriveUnhardenedPublicKey,
ecdsaPublicKeyToSchnorr,
finalizeUserInputs,
getFeeAmount,
getFeeRecipientAddress,
getInputIndicesByScript,
getScriptMatchingOutputFromTransaction,
getUnspendableKeyCommittedToUUID,
} from '../../src/functions/bitcoin/bitcoin-functions';
import { shiftValue, unshiftValue } from '../../src/utilities';
import {
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
TEST_TESTNET_ATTESTOR_UNHARDENED_DERIVED_PUBLIC_KEY_1,
Expand Down Expand Up @@ -257,4 +259,27 @@ describe('Bitcoin Functions', () => {
expect(result).toBeUndefined();
});
});

describe('getFeeAmount', () => {
test('calculates correct fee for whole numbers', () => {
expect(getFeeAmount(1000000, 50)).toBe(5000);
expect(getFeeAmount(1000000, 25)).toBe(2500);
});

test('handles small fee basis points', () => {
expect(getFeeAmount(1000000, 1)).toBe(100);
expect(getFeeAmount(2000000, 1)).toBe(200);
});

test('handles typical fee calculations', () => {
expect(getFeeAmount(1500000, 15)).toBe(2250);
expect(getFeeAmount(2000000, 25)).toBe(5000);
});

test('properly drops decimals', () => {
expect(getFeeAmount(1008584578, 15)).toBe(1512876);
expect(getFeeAmount(1234567, 15)).toBe(1851); // 1234567 * 15 / 10000 = 1851.8505 -> 1851
expect(getFeeAmount(9876543, 23)).toBe(22716); // 9876543 * 23 / 10000 = 22716.0489 -> 22716
});
});
});

0 comments on commit a0e9815

Please sign in to comment.