Skip to content

Commit

Permalink
feat: modify protocol fee calculation (#6)
Browse files Browse the repository at this point in the history
* v1.0.8

* feat: modify protocol fee calculation
  • Loading branch information
Polybius93 authored Jun 7, 2024
1 parent b5b03d6 commit b50bd19
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 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": "1.0.7",
"version": "1.0.9",
"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
6 changes: 6 additions & 0 deletions src/functions/bitcoin/bitcoin-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { taprootTweakPubkey } from '@scure/btc-signer/utils';
import { BIP32Factory, BIP32Interface } from 'bip32';
import { Network } from 'bitcoinjs-lib';
import { bitcoin, regtest, testnet } from 'bitcoinjs-lib/src/networks.js';
import { Decimal } from 'decimal.js';
import * as ellipticCurveCryptography from 'tiny-secp256k1';

import {
Expand All @@ -33,6 +34,11 @@ 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();
}

/**
* Derives the Public Key at the Unhardened Path (0/0) from a given Extended Public Key.
* @param extendedPublicKey - The base58-encoded Extended Public Key.
Expand Down
29 changes: 19 additions & 10 deletions src/functions/bitcoin/psbt-functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hexToBytes } from '@noble/hashes/utils';
import { p2wpkh, selectUTXO } from '@scure/btc-signer';
import { selectUTXO } from '@scure/btc-signer';
import { P2Ret, P2TROut } from '@scure/btc-signer/payment';
import { Network, Psbt } from 'bitcoinjs-lib';
import { PartialSignature } from 'ledger-bitcoin/build/main/lib/appClient.js';
Expand All @@ -8,6 +8,7 @@ import { BitcoinInputSigningConfig, PaymentTypes } from '../../models/bitcoin-mo
import { reverseBytes } from '../../utilities/index.js';
import {
ecdsaPublicKeyToSchnorr,
getFeeAmount,
getFeeRecipientAddressFromPublicKey,
getUTXOs,
} from '../bitcoin/bitcoin-functions.js';
Expand Down Expand Up @@ -35,15 +36,15 @@ export async function createFundingTransaction(
bitcoinBlockchainAPIURL: string
): Promise<Uint8Array> {
const feeAddress = getFeeRecipientAddressFromPublicKey(feePublicKey, bitcoinNetwork);
const feeRecipientOutputValue = bitcoinAmount / feeBasisPoints;
const feeAmount = getFeeAmount(Number(bitcoinAmount), Number(feeBasisPoints));

const userUTXOs = await getUTXOs(bitcoinNativeSegwitTransaction, bitcoinBlockchainAPIURL);

const psbtOutputs = [
{ address: multisigAddress, amount: bitcoinAmount },
{
address: feeAddress,
amount: feeRecipientOutputValue,
amount: BigInt(feeAmount),
},
];

Expand All @@ -59,6 +60,10 @@ export async function createFundingTransaction(

if (!fundingTX) throw new Error('Could not create Funding Transaction');

fundingTX.updateInput(0, {
sequence: 0xfffffff0,
});

const fundingPSBT = fundingTX.toPSBT();

return fundingPSBT;
Expand Down Expand Up @@ -89,10 +94,8 @@ export function createClosingTransaction(
feePublicKey: string,
feeBasisPoints: bigint
): Uint8Array {
const feePublicKeyBuffer = Buffer.from(feePublicKey, 'hex');
const { address: feeAddress } = p2wpkh(feePublicKeyBuffer, bitcoinNetwork);

if (!feeAddress) throw new Error('Could not create Fee Address');
const feeAddress = getFeeRecipientAddressFromPublicKey(feePublicKey, bitcoinNetwork);
const feeAmount = getFeeAmount(Number(bitcoinAmount), Number(feeBasisPoints));

const inputs = [
{
Expand All @@ -109,7 +112,7 @@ export function createClosingTransaction(
const outputs = [
{
address: feeAddress,
amount: bitcoinAmount / feeBasisPoints,
amount: BigInt(feeAmount),
},
];

Expand All @@ -121,9 +124,15 @@ export function createClosingTransaction(
network: bitcoinNetwork,
});

if (!selected?.tx) throw new Error('Could not create Closing Transaction');
const closingTX = selected?.tx;

if (!closingTX) throw new Error('Could not create Closing Transaction');

closingTX.updateInput(0, {
sequence: 0xfffffff0,
});

const closingPSBT = selected.tx.toPSBT();
const closingPSBT = closingTX.toPSBT();

return closingPSBT;
}
Expand Down

0 comments on commit b50bd19

Please sign in to comment.