Skip to content

Commit

Permalink
Refactor bitcoinNetwork variable source
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kpyszkowski committed Oct 26, 2023
1 parent 8734e57 commit 2cb19bd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
8 changes: 1 addition & 7 deletions src/components/Modal/TbtcMintingConfirmationModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,10 +57,7 @@ const TbtcMintingConfirmationModal: FC<TbtcMintingConfirmationModalProps> = ({
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),
Expand Down
5 changes: 4 additions & 1 deletion src/components/Modal/tBTC/InitiateUnminting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -53,14 +54,16 @@ const InitiateUnmintingBase: FC<InitiateUnmintingProps> = ({
const { account } = useWeb3React()
const { estimatedBTCAmount, thresholdNetworkFee } =
useRedemptionEstimatedFees(unmintAmount)
const threshold = useThreshold()

const onSuccess: OnSuccessCallback = (receipt) => {
navigate(
buildRedemptionDetailsLink(
receipt.transactionHash,
account!,
wallet.walletPublicKey,
btcAddress
btcAddress,
threshold.tbtc.bitcoinNetwork
)
)
closeModal()
Expand Down
21 changes: 9 additions & 12 deletions src/tbtc/mock-bitcoin-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -177,7 +174,7 @@ export class MockBitcoinClient implements Client {
depositUtxo,
rawTransaction: transaction,
} = await assembleDepositTransaction(
bitcoinNetwork,
network,
deposit,
testnetPrivateKey,
true,
Expand All @@ -203,7 +200,7 @@ export class MockBitcoinClient implements Client {
depositUtxo: depositUtxo2,
rawTransaction: transaction2,
} = await assembleDepositTransaction(
bitcoinNetwork,
network,
deposit2,
testnetPrivateKey,
true,
Expand Down
14 changes: 4 additions & 10 deletions src/threshold-ts/tbtc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -529,7 +523,7 @@ export class TBTC implements ITBTC {

const refundPublicKeyHash = decodeBitcoinAddress(
btcRecoveryAddress,
bitcoinNetwork
this.bitcoinNetwork
)

const refundLocktime = calculateDepositRefundLocktime(
Expand Down Expand Up @@ -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
)
Expand All @@ -981,7 +975,7 @@ export class TBTC implements ITBTC {

const redeemerOutputScript = createOutputScriptFromAddress(
btcAddress,
bitcoinNetwork
this.bitcoinNetwork
).toString()

return await findWalletForRedemption(
Expand Down
15 changes: 6 additions & 9 deletions src/utils/tBTC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ 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

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<BitcoinNetwork, "unknown">

Expand Down Expand Up @@ -110,7 +106,7 @@ export class RedemptionDetailsLinkBuilder {
return this
}

withBitcoinAddress = (btcAddress: string) => {
withBitcoinAddress = (btcAddress: string, bitcoinNetwork: BitcoinNetwork) => {
const redeemerOutputScript = createOutputScriptFromAddress(
btcAddress,
bitcoinNetwork
Expand Down Expand Up @@ -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()
}

0 comments on commit 2cb19bd

Please sign in to comment.