Skip to content

Commit

Permalink
chore: remove unnecessary lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Mar 29, 2024
1 parent 0d66de6 commit 8823ee6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 91 deletions.
83 changes: 16 additions & 67 deletions src/app/hooks/use-bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<void>;
broadcastTransaction: (transaction: btc.Transaction) => Promise<string>;
}

export function useBitcoin(): UseBitcoinReturnType {
const { bitcoinNetwork, bitcoinNetworkName, bitcoinBlockchainAPIURL, mempoolSpaceAPIFeeURL } =
useEndpoints();
const { bitcoinNetwork, bitcoinNetworkName, bitcoinBlockchainAPIURL } = useEndpoints();
const { getAttestorGroupPublicKey, sendClosingTransactionToAttestors } = useAttestors();

/**
Expand Down Expand Up @@ -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<number> {
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.
Expand Down Expand Up @@ -242,23 +215,21 @@ 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.
*/ function createFundingTransaction(
multisigAddress: string,
userChangeAddress: string,
utxos: any[],
feeRate: bigint,
bitcoinAmount: number,
bitcoinNetwork: BitcoinNetwork
): Uint8Array {
const outputs = [{ address: multisigAddress, amount: BigInt(bitcoinAmount) }];

const selected = btc.selectUTXO(utxos, outputs, 'default', {
changeAddress: userChangeAddress,
feePerByte: feeRate,
feePerByte: 2n,
bip69: false,
createTx: true,
network: bitcoinNetwork,
Expand Down Expand Up @@ -289,41 +260,25 @@ export function useBitcoin(): UseBitcoinReturnType {
fundingTransactionID: string,
multisigTransaction: any,
userNativeSegwitAddress: string,
attestorGroupPublicKey: string,
bitcoinAmount: number,
bitcoinNetwork: BitcoinNetwork
): Promise<Uint8Array> {
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;
}
Expand Down Expand Up @@ -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
);
Expand All @@ -425,7 +377,6 @@ export function useBitcoin(): UseBitcoinReturnType {
fundingTransaction: transaction,
multisigTransaction,
userNativeSegwitAddress,
attestorGroupPublicKey,
};
}

Expand All @@ -444,14 +395,12 @@ export function useBitcoin(): UseBitcoinReturnType {
multisigTransaction: btc.P2TROut,
uuid: string,
userNativeSegwitAddress: string,
attestorGroupPublicKey: string,
bitcoinAmount: number
): Promise<void> {
const closingTransaction = await createClosingTransaction(
fundingTransactionID,
multisigTransaction,
userNativeSegwitAddress,
attestorGroupPublicKey,
bitcoinAmount,
bitcoinNetwork
);
Expand Down
14 changes: 0 additions & 14 deletions src/app/hooks/use-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface NetworkEndpoints {
ethereumAttestorChainID: string;
bitcoinExplorerAPIURL: string;
bitcoinBlockchainAPIURL: string;
mempoolSpaceAPIFeeURL: string;
bitcoinNetwork: BitcoinNetwork;
bitcoinNetworkName: string;
}
Expand All @@ -24,7 +23,6 @@ export function useEndpoints(): NetworkEndpoints {
const [ethereumAttestorChainID, setEthereumAttestorChainID] = useState<string>('');
const [bitcoinExplorerAPIURL, setBitcoinExplorerAPIURL] = useState<string>('');
const [bitcoinBlockchainAPIURL, setBitcoinBlockchainAPIURL] = useState<string>('');
const [mempoolSpaceAPIFeeURL, setMempoolSpaceAPIFeeURL] = useState<string>('');

const [bitcoinNetwork, setBitcoinNetwork] = useState<BitcoinNetwork>(regtest);
const [bitcoinNetworkName, setBitcoinNetworkName] = useState<string>('');
Expand All @@ -38,7 +36,6 @@ export function useEndpoints(): NetworkEndpoints {
ethereumAttestorChainID,
bitcoinExplorerAPIURL,
bitcoinBlockchainAPIURL,
mempoolSpaceAPIFeeURL,
bitcoinNetwork,
bitcoinNetworkName,
} = getEndpoints();
Expand All @@ -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) {
Expand All @@ -86,7 +76,6 @@ export function useEndpoints(): NetworkEndpoints {
ethereumAttestorChainID: 'evm-sepolia',
bitcoinExplorerAPIURL,
bitcoinBlockchainAPIURL,
mempoolSpaceAPIFeeURL,
bitcoinNetwork,
bitcoinNetworkName,
};
Expand All @@ -97,7 +86,6 @@ export function useEndpoints(): NetworkEndpoints {
ethereumAttestorChainID: 'evm-goerli',
bitcoinExplorerAPIURL,
bitcoinBlockchainAPIURL,
mempoolSpaceAPIFeeURL,
bitcoinNetwork,
bitcoinNetworkName,
};
Expand All @@ -108,7 +96,6 @@ export function useEndpoints(): NetworkEndpoints {
ethereumAttestorChainID: 'evm-x1-test',
bitcoinExplorerAPIURL,
bitcoinBlockchainAPIURL,
mempoolSpaceAPIFeeURL,
bitcoinNetwork,
bitcoinNetworkName,
};
Expand All @@ -122,7 +109,6 @@ export function useEndpoints(): NetworkEndpoints {
ethereumAttestorChainID,
bitcoinExplorerAPIURL,
bitcoinBlockchainAPIURL,
mempoolSpaceAPIFeeURL,
bitcoinNetwork,
bitcoinNetworkName,
};
Expand Down
12 changes: 2 additions & 10 deletions src/app/hooks/use-psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export function usePSBT(): UsePSBTReturnType {
const [userNativeSegwitAddress, setUserNativeSegwitAddress] = useState<string | undefined>();
const [fundingTransaction, setFundingTransaction] = useState<btc.Transaction | undefined>();
const [multisigTransaction, setMultisigTransaction] = useState<btc.P2TROut | undefined>();
const [attestorGroupPublicKey, setAttestorGroupPublicKey] = useState<string | undefined>();

async function handleSignFundingTransaction(
bitcoinAmount: number,
Expand All @@ -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) {
Expand All @@ -61,7 +55,6 @@ export function usePSBT(): UsePSBTReturnType {
!fundingTransaction ||
!multisigTransaction ||
!userNativeSegwitAddress ||
!attestorGroupPublicKey ||
!bitcoinAmount ||
!vaultUUID ||
!network
Expand All @@ -76,7 +69,6 @@ export function usePSBT(): UsePSBTReturnType {
multisigTransaction,
vaultUUID,
userNativeSegwitAddress,
attestorGroupPublicKey,
bitcoinAmount
);

Expand Down

0 comments on commit 8823ee6

Please sign in to comment.