diff --git a/src/app/hooks/use-bitcoin.ts b/src/app/hooks/use-bitcoin.ts index 93520332..b8fd7c96 100644 --- a/src/app/hooks/use-bitcoin.ts +++ b/src/app/hooks/use-bitcoin.ts @@ -42,14 +42,6 @@ interface UTXO { value: number; } -interface FeeRates { - fastestFee: number; - halfHourFee: number; - hourFee: number; - economyFee: number; - minimumFee: number; -} - interface BitcoinNativeSegwitAddress { address: string; derivationPath: string; @@ -87,22 +79,19 @@ interface UseBitcoinReturnType { fundingTransaction: btc.Transaction; multisigTransaction: btc.P2TROut; userNativeSegwitAddress: string; - attestorGroupPublicKey: string; }>; signAndSendClosingPSBT: ( fundingTransactionID: string, multisigTransaction: btc.P2TROut, uuid: string, userNativeSegwitAddress: string, - attestorGroupPublicKey: string, bitcoinAmount: number ) => Promise; broadcastTransaction: (transaction: btc.Transaction) => Promise; } export function useBitcoin(): UseBitcoinReturnType { - const { bitcoinNetwork, bitcoinNetworkName, bitcoinBlockchainAPIURL, mempoolSpaceAPIFeeURL } = - useEndpoints(); + const { bitcoinNetwork, bitcoinNetworkName, bitcoinBlockchainAPIURL } = useEndpoints(); const { getAttestorGroupPublicKey, sendClosingTransactionToAttestors } = useAttestors(); /** @@ -146,22 +135,6 @@ export function useBitcoin(): UseBitcoinReturnType { } } - /** - * Fetches the fee rate from the mempool.space API. - * - * @returns A promise that resolves to the hour fee rate. - */ - async function getFeeRate(): Promise { - try { - const response = await fetch(mempoolSpaceAPIFeeURL); - const feeRates: FeeRates = await response.json(); - - return feeRates.hourFee; - } catch (error) { - throw new BitcoinError(`Error getting Fee Rate: ${error}`); - } - } - /** * Fetches the UTXOs for the user's native segwit address. @@ -242,7 +215,6 @@ export function useBitcoin(): UseBitcoinReturnType { * * @param multisigAddress - The multisig address created from the multisig transaction between the user and the attestor group. * @param utxos - The user's UTXOs. - * @param feeRate - The fee rate. * @param bitcoinAmount - The amount of bitcoin to be used in the transaction. * @param bitcoinNetwork - The bitcoin network. * @returns A promise that resolves to the funding PSBT. @@ -250,7 +222,6 @@ export function useBitcoin(): UseBitcoinReturnType { multisigAddress: string, userChangeAddress: string, utxos: any[], - feeRate: bigint, bitcoinAmount: number, bitcoinNetwork: BitcoinNetwork ): Uint8Array { @@ -258,7 +229,7 @@ export function useBitcoin(): UseBitcoinReturnType { const selected = btc.selectUTXO(utxos, outputs, 'default', { changeAddress: userChangeAddress, - feePerByte: feeRate, + feePerByte: 2n, bip69: false, createTx: true, network: bitcoinNetwork, @@ -289,41 +260,25 @@ export function useBitcoin(): UseBitcoinReturnType { fundingTransactionID: string, multisigTransaction: any, userNativeSegwitAddress: string, - attestorGroupPublicKey: string, bitcoinAmount: number, bitcoinNetwork: BitcoinNetwork ): Promise { - const redemptionFeeAddress = import.meta.env.VITE_REDEMPTION_FEE_ADDRESS as string; - - const inputs = [ - { - txid: hexToBytes(fundingTransactionID), - index: 0, - witnessUtxo: { amount: BigInt(bitcoinAmount), script: multisigTransaction.script }, - ...multisigTransaction, - }, - ]; - - const outputs = [ - { - address: redemptionFeeAddress, - amount: BigInt(bitcoinAmount) / 100n, - }, - ]; - - const feeRate = BigInt(await getFeeRate()); - - const selected = btc.selectUTXO(inputs, outputs, 'default', { - changeAddress: userNativeSegwitAddress, - feePerByte: feeRate, - bip69: false, - createTx: true, - network: bitcoinNetwork, - }); + const closingTransaction = new btc.Transaction({ PSBTVersion: 0 }); - if (!selected?.tx) throw new BitcoinError('Could not create Closing Transaction'); + const fundingInput = { + txid: hexToBytes(fundingTransactionID), + index: 0, + witnessUtxo: { amount: BigInt(bitcoinAmount), script: multisigTransaction.script }, + ...multisigTransaction, + }; - const closingPSBT = selected.tx.toPSBT(); + closingTransaction.addInput(fundingInput); + closingTransaction.addOutputAddress( + userNativeSegwitAddress, + BigInt(bitcoinAmount - 10000), + bitcoinNetwork + ); + const closingPSBT = closingTransaction.toPSBT(); return closingPSBT; } @@ -405,13 +360,10 @@ export function useBitcoin(): UseBitcoinReturnType { const multisigAddress = multisigTransaction.address; if (!multisigAddress) throw new BitcoinError('Could not create multisig address'); - const feeRate = BigInt(await getFeeRate()); - const fundingTransaction = createFundingTransaction( multisigAddress, userNativeSegwitAddress, userUTXOs, - feeRate, bitcoinAmount, bitcoinNetwork ); @@ -425,7 +377,6 @@ export function useBitcoin(): UseBitcoinReturnType { fundingTransaction: transaction, multisigTransaction, userNativeSegwitAddress, - attestorGroupPublicKey, }; } @@ -444,14 +395,12 @@ export function useBitcoin(): UseBitcoinReturnType { multisigTransaction: btc.P2TROut, uuid: string, userNativeSegwitAddress: string, - attestorGroupPublicKey: string, bitcoinAmount: number ): Promise { const closingTransaction = await createClosingTransaction( fundingTransactionID, multisigTransaction, userNativeSegwitAddress, - attestorGroupPublicKey, bitcoinAmount, bitcoinNetwork ); diff --git a/src/app/hooks/use-endpoints.ts b/src/app/hooks/use-endpoints.ts index 3051c2a9..4eb73742 100644 --- a/src/app/hooks/use-endpoints.ts +++ b/src/app/hooks/use-endpoints.ts @@ -11,7 +11,6 @@ interface NetworkEndpoints { ethereumAttestorChainID: string; bitcoinExplorerAPIURL: string; bitcoinBlockchainAPIURL: string; - mempoolSpaceAPIFeeURL: string; bitcoinNetwork: BitcoinNetwork; bitcoinNetworkName: string; } @@ -24,7 +23,6 @@ export function useEndpoints(): NetworkEndpoints { const [ethereumAttestorChainID, setEthereumAttestorChainID] = useState(''); const [bitcoinExplorerAPIURL, setBitcoinExplorerAPIURL] = useState(''); const [bitcoinBlockchainAPIURL, setBitcoinBlockchainAPIURL] = useState(''); - const [mempoolSpaceAPIFeeURL, setMempoolSpaceAPIFeeURL] = useState(''); const [bitcoinNetwork, setBitcoinNetwork] = useState(regtest); const [bitcoinNetworkName, setBitcoinNetworkName] = useState(''); @@ -38,7 +36,6 @@ export function useEndpoints(): NetworkEndpoints { ethereumAttestorChainID, bitcoinExplorerAPIURL, bitcoinBlockchainAPIURL, - mempoolSpaceAPIFeeURL, bitcoinNetwork, bitcoinNetworkName, } = getEndpoints(); @@ -48,34 +45,27 @@ export function useEndpoints(): NetworkEndpoints { setEthereumAttestorChainID(ethereumAttestorChainID); setBitcoinExplorerAPIURL(bitcoinExplorerAPIURL); setBitcoinBlockchainAPIURL(bitcoinBlockchainAPIURL); - setMempoolSpaceAPIFeeURL(mempoolSpaceAPIFeeURL); setBitcoinNetwork(bitcoinNetwork); setBitcoinNetworkName(bitcoinNetworkName); // eslint-disable-next-line react-hooks/exhaustive-deps }, [network]); function getEndpoints(): NetworkEndpoints { - const attestorAPIURLs: string[] = import.meta.env.ATTESTOR_API_URLS.split(','); - const bitcoinNetworkName = import.meta.env.VITE_BITCOIN_NETWORK; const bitcoinBlockchainAPIURL = import.meta.env.VITE_BITCOIN_BLOCKCHAIN_API_URL; const bitcoinExplorerAPIURL = import.meta.env.VITE_BITCOIN_EXPLORER_API_URL; let bitcoinNetwork: BitcoinNetwork; - let mempoolSpaceAPIFeeURL: string; switch (bitcoinNetworkName) { case 'mainnet': bitcoinNetwork = bitcoin; - mempoolSpaceAPIFeeURL = 'https://mempool.space/api/v1/fees/recommended'; break; case 'testnet': bitcoinNetwork = testnet; - mempoolSpaceAPIFeeURL = 'https://mempool.space/testnet/api/v1/fees/recommended'; break; default: bitcoinNetwork = regtest; - mempoolSpaceAPIFeeURL = 'https://mempool.space/testnet/api/v1/fees/recommended'; } switch (network?.id) { @@ -86,7 +76,6 @@ export function useEndpoints(): NetworkEndpoints { ethereumAttestorChainID: 'evm-sepolia', bitcoinExplorerAPIURL, bitcoinBlockchainAPIURL, - mempoolSpaceAPIFeeURL, bitcoinNetwork, bitcoinNetworkName, }; @@ -97,7 +86,6 @@ export function useEndpoints(): NetworkEndpoints { ethereumAttestorChainID: 'evm-goerli', bitcoinExplorerAPIURL, bitcoinBlockchainAPIURL, - mempoolSpaceAPIFeeURL, bitcoinNetwork, bitcoinNetworkName, }; @@ -108,7 +96,6 @@ export function useEndpoints(): NetworkEndpoints { ethereumAttestorChainID: 'evm-x1-test', bitcoinExplorerAPIURL, bitcoinBlockchainAPIURL, - mempoolSpaceAPIFeeURL, bitcoinNetwork, bitcoinNetworkName, }; @@ -122,7 +109,6 @@ export function useEndpoints(): NetworkEndpoints { ethereumAttestorChainID, bitcoinExplorerAPIURL, bitcoinBlockchainAPIURL, - mempoolSpaceAPIFeeURL, bitcoinNetwork, bitcoinNetworkName, }; diff --git a/src/app/hooks/use-psbt.ts b/src/app/hooks/use-psbt.ts index 53d14b7c..238a5f2d 100644 --- a/src/app/hooks/use-psbt.ts +++ b/src/app/hooks/use-psbt.ts @@ -28,7 +28,6 @@ export function usePSBT(): UsePSBTReturnType { const [userNativeSegwitAddress, setUserNativeSegwitAddress] = useState(); const [fundingTransaction, setFundingTransaction] = useState(); const [multisigTransaction, setMultisigTransaction] = useState(); - const [attestorGroupPublicKey, setAttestorGroupPublicKey] = useState(); async function handleSignFundingTransaction( bitcoinAmount: number, @@ -37,17 +36,12 @@ export function usePSBT(): UsePSBTReturnType { const shiftedBTCDepositAmount = customShiftValue(bitcoinAmount, 8, false); try { - const { - fundingTransaction, - multisigTransaction, - userNativeSegwitAddress, - attestorGroupPublicKey, - } = await signAndBroadcastFundingPSBT(shiftedBTCDepositAmount, vaultUUID); + const { fundingTransaction, multisigTransaction, userNativeSegwitAddress } = + await signAndBroadcastFundingPSBT(shiftedBTCDepositAmount, vaultUUID); setVaultUUID(vaultUUID); setBitcoinAmount(shiftedBTCDepositAmount); setUserNativeSegwitAddress(userNativeSegwitAddress); - setAttestorGroupPublicKey(attestorGroupPublicKey); setFundingTransaction(fundingTransaction); setMultisigTransaction(multisigTransaction); } catch (error) { @@ -61,7 +55,6 @@ export function usePSBT(): UsePSBTReturnType { !fundingTransaction || !multisigTransaction || !userNativeSegwitAddress || - !attestorGroupPublicKey || !bitcoinAmount || !vaultUUID || !network @@ -76,7 +69,6 @@ export function usePSBT(): UsePSBTReturnType { multisigTransaction, vaultUUID, userNativeSegwitAddress, - attestorGroupPublicKey, bitcoinAmount );