Skip to content

Commit

Permalink
Move logic for fetching activities to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed May 17, 2024
1 parent b3e00ed commit acd81b7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useDepositBTCTransaction,
useDepositTelemetry,
useExecuteFunction,
useFetchDeposits,
useStakeFlowContext,
useToast,
useWalletContext,
Expand All @@ -30,11 +31,12 @@ export default function DepositBTCModal() {
const depositTelemetry = useDepositTelemetry()
const { closeToast, openToast } = useToast()
const dispatch = useAppDispatch()
const fetchDeposits = useFetchDeposits()

const onStakeBTCSuccess = useCallback(
() => dispatch(setStatus(PROCESS_STATUSES.SUCCEEDED)),
[dispatch],
)
const onStakeBTCSuccess = useCallback(() => {
fetchDeposits()
dispatch(setStatus(PROCESS_STATUSES.SUCCEEDED))
}, [dispatch, fetchDeposits])

const onStakeBTCError = useCallback(() => {
dispatch(setStatus(PROCESS_STATUSES.FAILED))
Expand Down
1 change: 0 additions & 1 deletion dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from "./store"
export * from "./toasts"
export * from "./sdk"
export * from "./subgraph"
export * from "./useDetectThemeMode"
export * from "./useRequestBitcoinAccount"
export * from "./useRequestEthereumAccount"
Expand Down
1 change: 1 addition & 0 deletions dapp/src/hooks/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./useInitializeAcreSdk"
export * from "./useFetchMinDepositAmount"
export * from "./useInitDataFromSdk"
export * from "./useFetchBTCBalance"
export * from "./useFetchDeposits"
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { useCallback, useEffect } from "react"
import { logPromiseFailure, subgraphAPI } from "#/utils"
import { useCallback } from "react"
import { setActivities, setTransactions } from "#/store/wallet"
import { useInterval } from "@chakra-ui/react"
import { ONE_MINUTE_IN_SECONDS, ONE_SEC_IN_MILLISECONDS } from "#/constants"
import { logPromiseFailure, subgraphAPI } from "#/utils"
import { useAppDispatch } from "../store/useAppDispatch"
import { useWalletContext } from "../useWalletContext"

const INTERVAL_TIME = ONE_SEC_IN_MILLISECONDS * ONE_MINUTE_IN_SECONDS * 30
// TODO: Use the correct function from SDK
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const calculateEthAddress = (btcAddress: string) =>
"0x45e2e415989477ea5450e440405f6ac858019e6b"

export function useFetchActivities() {
export function useFetchDeposits() {
const dispatch = useAppDispatch()
const { btcAccount } = useWalletContext()

const fetchActivities = useCallback(async () => {
const fetchDeposits = useCallback(async () => {
if (!btcAccount) return

// TODO: Use function from the SDK.
const ethAddress = calculateEthAddress(btcAccount.address)
const result = await subgraphAPI.fetchActivityData(ethAddress)
const activities = result.filter(({ status }) => status !== "completed")
Expand All @@ -27,16 +25,5 @@ export function useFetchActivities() {
dispatch(setActivities(activities))
}, [btcAccount, dispatch])

const handleFetchActivities = useCallback(
() => logPromiseFailure(fetchActivities()),
[fetchActivities],
)

useEffect(() => {
if (!btcAccount) return

handleFetchActivities()
}, [btcAccount, dispatch, handleFetchActivities])

useInterval(handleFetchActivities, INTERVAL_TIME)
return useCallback(() => logPromiseFailure(fetchDeposits()), [fetchDeposits])
}
17 changes: 17 additions & 0 deletions dapp/src/hooks/sdk/useInitDataFromSdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { useEffect } from "react"
import { useInterval } from "@chakra-ui/react"
import { ONE_MINUTE_IN_SECONDS, ONE_SEC_IN_MILLISECONDS } from "#/constants"
import { useFetchBTCBalance } from "./useFetchBTCBalance"
import { useFetchMinDepositAmount } from "./useFetchMinDepositAmount"
import { useFetchDeposits } from "./useFetchDeposits"
import { useWalletContext } from "../useWalletContext"

const INTERVAL_TIME = ONE_SEC_IN_MILLISECONDS * ONE_MINUTE_IN_SECONDS * 30

export function useInitDataFromSdk() {
const { btcAccount } = useWalletContext()
const fetchDeposits = useFetchDeposits()

useEffect(() => {
if (btcAccount) {
fetchDeposits()
}
}, [btcAccount, fetchDeposits])

useFetchBTCBalance()
useFetchMinDepositAmount()
useInterval(fetchDeposits, INTERVAL_TIME)
}
2 changes: 0 additions & 2 deletions dapp/src/hooks/subgraph/index.ts

This file was deleted.

5 changes: 0 additions & 5 deletions dapp/src/hooks/subgraph/useInitDataFromSubgraph.ts

This file was deleted.

2 changes: 0 additions & 2 deletions dapp/src/hooks/useInitApp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useInitDataFromSdk, useInitializeAcreSdk } from "./sdk"
import { useSentry } from "./sentry"
import { useFetchBTCPriceUSD } from "./useFetchBTCPriceUSD"
import { useInitDataFromSubgraph } from "./subgraph"

export function useInitApp() {
// TODO: Let's uncomment when dark mode is ready
Expand All @@ -13,5 +12,4 @@ export function useInitApp() {
// useInitGlobalToasts()

useInitDataFromSdk()
useInitDataFromSubgraph()
}

0 comments on commit acd81b7

Please sign in to comment.