Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: modify protocol fee calculation by dropping decimal points #40

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
});
});
});
Loading