From 2cb19bd306d30c3d82809bc974b6363b730a65dd Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Thu, 26 Oct 2023 13:38:44 +0200 Subject: [PATCH] Refactor bitcoinNetwork variable source Instead of analysing environmental variable the value is currently available to use in corelated data structures. Addded comment elaborating on mock Bitcoin network transaction fee. Refactored tBTC utility functions to pass bitcoin network as a parameter. --- .../TbtcMintingConfirmationModal/index.tsx | 8 +------ .../Modal/tBTC/InitiateUnminting.tsx | 5 ++++- src/tbtc/mock-bitcoin-client.ts | 21 ++++++++----------- src/threshold-ts/tbtc/index.ts | 14 ++++--------- src/utils/tBTC.ts | 15 ++++++------- 5 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/components/Modal/TbtcMintingConfirmationModal/index.tsx b/src/components/Modal/TbtcMintingConfirmationModal/index.tsx index 979df0e02..d87e899ff 100644 --- a/src/components/Modal/TbtcMintingConfirmationModal/index.tsx +++ b/src/components/Modal/TbtcMintingConfirmationModal/index.tsx @@ -27,9 +27,6 @@ import { BigNumber } from "ethers" import { getChainIdentifier } from "../../../threshold-ts/utils" import { InlineTokenBalance } from "../../TokenBalance" import { BridgeContractLink } from "../../tBTC" -import { supportedChainId } from "../../../utils/getEnvVariable" -import { BitcoinNetwork } from "@keep-network/tbtc-v2.ts" -import { ChainID } from "../../../enums" export interface TbtcMintingConfirmationModalProps extends BaseModalProps { utxo: UnspentTransactionOutput @@ -60,10 +57,7 @@ const TbtcMintingConfirmationModal: FC = ({ const { sendTransaction: revealDeposit } = useRevealDepositTransaction( onSuccessfulDepositReveal ) - const bitcoinNetwork = - supportedChainId === ChainID.Ethereum.toString() - ? BitcoinNetwork.Mainnet - : BitcoinNetwork.Testnet + const bitcoinNetwork = threshold.tbtc.bitcoinNetwork const initiateMintTransaction = async () => { const depositScriptParameters: DepositScriptParameters = { depositor: getChainIdentifier(ethAddress), diff --git a/src/components/Modal/tBTC/InitiateUnminting.tsx b/src/components/Modal/tBTC/InitiateUnminting.tsx index adc1a1b28..8f316c00f 100644 --- a/src/components/Modal/tBTC/InitiateUnminting.tsx +++ b/src/components/Modal/tBTC/InitiateUnminting.tsx @@ -13,6 +13,7 @@ import { import { useWeb3React } from "@web3-react/core" import { FC } from "react" import { useNavigate } from "react-router-dom" +import { useThreshold } from "../../../contexts/ThresholdContext" import { useRedemptionEstimatedFees, useRequestRedemption, @@ -53,6 +54,7 @@ const InitiateUnmintingBase: FC = ({ const { account } = useWeb3React() const { estimatedBTCAmount, thresholdNetworkFee } = useRedemptionEstimatedFees(unmintAmount) + const threshold = useThreshold() const onSuccess: OnSuccessCallback = (receipt) => { navigate( @@ -60,7 +62,8 @@ const InitiateUnmintingBase: FC = ({ receipt.transactionHash, account!, wallet.walletPublicKey, - btcAddress + btcAddress, + threshold.tbtc.bitcoinNetwork ) ) closeModal() diff --git a/src/tbtc/mock-bitcoin-client.ts b/src/tbtc/mock-bitcoin-client.ts index 4f7d38c42..47e54f453 100644 --- a/src/tbtc/mock-bitcoin-client.ts +++ b/src/tbtc/mock-bitcoin-client.ts @@ -17,8 +17,6 @@ import { BigNumber } from "ethers" import { getChainIdentifier } from "../threshold-ts/utils" import { delay } from "../utils/helpers" import { BitcoinNetwork } from "../threshold-ts/types" -import { supportedChainId } from "../utils/getEnvVariable" -import { ChainID } from "../enums" const testnetTransactionHash = TransactionHash.from( "2f952bdc206bf51bb745b967cb7166149becada878d3191ffe341155ebcd4883" @@ -40,10 +38,11 @@ export const testnetUTXO: UnspentTransactionOutput & RawTransaction = { ...testnetTransaction, } const testnetPrivateKey = "cRJvyxtoggjAm9A94cB86hZ7Y62z2ei5VNJHLksFi2xdnz1GJ6xt" -const bitcoinNetwork = - supportedChainId === ChainID.Ethereum.toString() - ? BitcoinNetwork.Mainnet - : BitcoinNetwork.Testnet + +/** + * The average transaction fee for the Bitcoin network, ~ 0.00047 BTC. + * This value is used to calculate the fee for transactions on the Bitcoin network. + */ const bitcoinNetworkTransactionFee = BigNumber.from("47000") export class MockBitcoinClient implements Client { @@ -128,15 +127,13 @@ export class MockBitcoinClient implements Client { refundLocktime, blindingFactor, } = tbtc + const network = await this.getNetwork() const depositScriptParameters: DepositScriptParameters = { depositor: getChainIdentifier(ethAddress), blindingFactor: blindingFactor, walletPublicKeyHash: walletPublicKeyHash, - refundPublicKeyHash: decodeBitcoinAddress( - btcRecoveryAddress, - bitcoinNetwork - ), + refundPublicKeyHash: decodeBitcoinAddress(btcRecoveryAddress, network), refundLocktime: refundLocktime, } @@ -177,7 +174,7 @@ export class MockBitcoinClient implements Client { depositUtxo, rawTransaction: transaction, } = await assembleDepositTransaction( - bitcoinNetwork, + network, deposit, testnetPrivateKey, true, @@ -203,7 +200,7 @@ export class MockBitcoinClient implements Client { depositUtxo: depositUtxo2, rawTransaction: transaction2, } = await assembleDepositTransaction( - bitcoinNetwork, + network, deposit2, testnetPrivateKey, true, diff --git a/src/threshold-ts/tbtc/index.ts b/src/threshold-ts/tbtc/index.ts index 4845dacb1..160dbcd90 100644 --- a/src/threshold-ts/tbtc/index.ts +++ b/src/threshold-ts/tbtc/index.ts @@ -54,8 +54,7 @@ import { findWalletForRedemption, } from "@keep-network/tbtc-v2.ts/dist/src/redemption" import { TBTCToken as ChainTBTCToken } from "@keep-network/tbtc-v2.ts/dist/src/chain" -import { supportedChainId } from "../../utils/getEnvVariable" -import { ChainID } from "../../enums" +import { getThresholdLib } from "../../utils/getThresholdLib" export enum BridgeActivityStatus { PENDING = "PENDING", @@ -405,11 +404,6 @@ export interface ITBTC { }> } -const bitcoinNetwork = - supportedChainId === ChainID.Ethereum.toString() - ? BitcoinNetwork.Mainnet - : BitcoinNetwork.Testnet - export class TBTC implements ITBTC { private _bridge: EthereumBridge private _tbtcVault: Contract @@ -529,7 +523,7 @@ export class TBTC implements ITBTC { const refundPublicKeyHash = decodeBitcoinAddress( btcRecoveryAddress, - bitcoinNetwork + this.bitcoinNetwork ) const refundLocktime = calculateDepositRefundLocktime( @@ -960,7 +954,7 @@ export class TBTC implements ITBTC { const tx = await requestRedemption( walletPublicKey, _mainUtxo, - createOutputScriptFromAddress(btcAddress, bitcoinNetwork).toString(), + createOutputScriptFromAddress(btcAddress, this.bitcoinNetwork).toString(), BigNumber.from(amount), this._token ) @@ -981,7 +975,7 @@ export class TBTC implements ITBTC { const redeemerOutputScript = createOutputScriptFromAddress( btcAddress, - bitcoinNetwork + this.bitcoinNetwork ).toString() return await findWalletForRedemption( diff --git a/src/utils/tBTC.ts b/src/utils/tBTC.ts index 5ecab2016..aff8f5bdc 100644 --- a/src/utils/tBTC.ts +++ b/src/utils/tBTC.ts @@ -5,8 +5,7 @@ import { createOutputScriptFromAddress, prependScriptPubKeyByLength, } from "../threshold-ts/utils" -import { supportedChainId } from "./getEnvVariable" -import { ChainID } from "../enums" +import { getThresholdLib } from "./getThresholdLib" const MINTING_MAINNET_BTC_RECOVERY_ADDRESS_PREFIXES = ["1", "bc1"] as const const MINTING_TESTNET_BTC_RECOVERY_ADDRESS_PREFIXES = ["m", "n", "tb1"] as const @@ -14,10 +13,7 @@ const MINTING_TESTNET_BTC_RECOVERY_ADDRESS_PREFIXES = ["m", "n", "tb1"] as const const UNMINTING_MAINNET_BTC_ADDRESS_PREFIXES = ["1", "bc1", "3"] as const const UNMINTING_TESTNET_BTC_ADDRESS_PREFIXES = ["m", "n", "tb1", "2"] as const -const bitcoinNetwork = - supportedChainId === ChainID.Ethereum.toString() - ? BitcoinNetwork.Mainnet - : BitcoinNetwork.Testnet +const bitcoinNetwork = getThresholdLib().tbtc.bitcoinNetwork type SupportedBitcoinNetworks = Exclude @@ -110,7 +106,7 @@ export class RedemptionDetailsLinkBuilder { return this } - withBitcoinAddress = (btcAddress: string) => { + withBitcoinAddress = (btcAddress: string, bitcoinNetwork: BitcoinNetwork) => { const redeemerOutputScript = createOutputScriptFromAddress( btcAddress, bitcoinNetwork @@ -172,11 +168,12 @@ export const buildRedemptionDetailsLink = ( txHash: string, redeemer: string, walletPublicKey: string, - btcAddress: string + btcAddress: string, + bitcoinNetwork: BitcoinNetwork ): string => { return RedemptionDetailsLinkBuilder.createFromTxHash(txHash) .withRedeemer(redeemer) .withWalletPublicKey(walletPublicKey) - .withBitcoinAddress(btcAddress) + .withBitcoinAddress(btcAddress, bitcoinNetwork) .build() }