-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added query for bitcoin contracts
- Loading branch information
1 parent
4b97c5c
commit 408ce03
Showing
6 changed files
with
50 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 10 additions & 29 deletions
39
src/app/components/bitcoin-contract-entry-point/bitcoin-contract-entry-point.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,32 @@ | ||
import { useState } from 'react'; | ||
import { useAsync } from 'react-async-hook'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { Money, createMoneyFromDecimal } from '@shared/models/money.model'; | ||
import { RouteUrls } from '@shared/route-urls'; | ||
|
||
import { useBitcoinContracts } from '@app/common/hooks/use-bitcoin-contracts'; | ||
import { i18nFormatCurrency } from '@app/common/money/format-money'; | ||
import { useCalculateBitcoinFiatValue } from '@app/query/common/market-data/market-data.hooks'; | ||
import { useBitcoinContractsBalanceQuery } from '@app/query/stacks/balance/bitcoin-contracts-balance.query'; | ||
import { BitcoinContractIcon } from '@app/ui/components/icons/bitcoin-contract-icon'; | ||
|
||
import { useCryptoCurrencyMarketData } from '@app/query/common/market-data/market-data.hooks'; | ||
import { createMoney } from '@shared/models/money.model'; | ||
import { BitcoinContractEntryPointLayout } from './bitcoin-contract-entry-point-layout'; | ||
|
||
interface BitcoinContractEntryPointProps { | ||
btcAddress: string; | ||
} | ||
export function BitcoinContractEntryPoint({ btcAddress }: BitcoinContractEntryPointProps) { | ||
export function BitcoinContractEntryPoint() { | ||
const navigate = useNavigate(); | ||
const { sumBitcoinContractCollateralAmounts } = useBitcoinContracts(); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const calculateFiatValue = useCalculateBitcoinFiatValue(); | ||
const [bitcoinContractSum, setBitcoinContractSum] = useState<Money>( | ||
createMoneyFromDecimal(0, 'BTC') | ||
); | ||
|
||
useAsync(async () => { | ||
setIsLoading(true); | ||
const currentBitcoinContractSum = await sumBitcoinContractCollateralAmounts(); | ||
if (!currentBitcoinContractSum) return; | ||
setBitcoinContractSum(currentBitcoinContractSum); | ||
setIsLoading(false); | ||
}, [btcAddress]); | ||
|
||
const { data: bitcoinContactsBalance, isLoading, refetch } = useBitcoinContractsBalanceQuery(); | ||
|
||
|
||
function onClick() { | ||
navigate(RouteUrls.BitcoinContractList); | ||
} | ||
|
||
return ( | ||
!bitcoinContractSum.amount.isZero() && ( | ||
<BitcoinContractEntryPointLayout | ||
isLoading={isLoading} | ||
balance={bitcoinContractSum} | ||
caption={bitcoinContractSum.symbol} | ||
balance={bitcoinContactsBalance ? bitcoinContactsBalance : createMoney(0, 'BTC')} | ||
caption={bitcoinContactsBalance ? bitcoinContactsBalance.symbol : 'BTC'} | ||
icon={<BitcoinContractIcon />} | ||
usdBalance={i18nFormatCurrency(calculateFiatValue(bitcoinContractSum))} | ||
// usdBalance={btcUsdBalance} | ||
onClick={onClick} | ||
/> | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/app/query/stacks/balance/bitcoin-contracts-balance.query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { UseQueryResult, useQuery } from '@tanstack/react-query'; | ||
|
||
import { Money } from '@shared/models/money.model'; | ||
|
||
import { useBitcoinContracts } from '@app/common/hooks/use-bitcoin-contracts'; | ||
import { useCurrentNetwork } from '@app/store/networks/networks.selectors'; | ||
|
||
const staleTime = 1 * 60 * 1000; | ||
|
||
const balanceQueryOptions = { | ||
staleTime, | ||
keepPreviousData: false, | ||
refetchOnMount: true, | ||
} as const; | ||
|
||
export function useBitcoinContractsBalanceQuery(): UseQueryResult<Money> { | ||
const { sumBitcoinContractCollateralAmounts } = useBitcoinContracts(); | ||
const currentNetwork = useCurrentNetwork(); | ||
|
||
return useQuery({ | ||
queryKey: ['sum-bitcoin-contract-collateral-amounts', currentNetwork.id], | ||
queryFn: sumBitcoinContractCollateralAmounts, | ||
enabled: !!currentNetwork, | ||
...balanceQueryOptions, | ||
}); | ||
} |