Skip to content

Commit

Permalink
feat(app): use websocket provider
Browse files Browse the repository at this point in the history
  • Loading branch information
superical committed Oct 25, 2023
1 parent 2d2f3d1 commit 1e69266
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
1 change: 0 additions & 1 deletion packages/app/src/contexts/asset-token-context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const AssetTokenContext = createContext<AssetTokenContextProps>({

export const AssetTokenProvider = ({ children }: { children: React.ReactNode }) => {
const { assetContract } = usePBMTokenContext()
// const provider = useProvider()

const [contract, setContract] = useState<IERC20Metadata>()
const [decimals, setDecimals] = useState<number>(0)
Expand Down
5 changes: 3 additions & 2 deletions packages/app/src/contexts/pbm-token-context/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client'

import { ERC20MetadataContextProps } from '@app/contexts/types'
import { useSafeProvider } from '@app/hooks/use-safe-provider'
import { getAppChainInfo } from '@app/utils/get-app-chain-info'
import {
IERC20Metadata,
Expand All @@ -12,7 +13,7 @@ import {
} from '@pbm/contracts'
import { BigNumber } from 'ethers'
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { useAccount, useNetwork, useProvider } from 'wagmi'
import { useAccount, useNetwork } from 'wagmi'

type PBMTokenContextProps = {
asset: string | undefined
Expand Down Expand Up @@ -82,7 +83,7 @@ export const PBMTokenProvider = ({ children }: { children: React.ReactNode }) =>
genesisBlock,
]
)
const provider = useProvider()
const provider = useSafeProvider()
const { address: connectedAccount } = useAccount()

useEffect(() => {
Expand Down
10 changes: 10 additions & 0 deletions packages/app/src/hooks/use-safe-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useMemo } from 'react'
import { useNetwork, useProvider, useWebSocketProvider } from 'wagmi'

export const useSafeProvider = () => {
const { chain } = useNetwork()
const wsProvider = useWebSocketProvider()
const httpProvider = useProvider()

return useMemo(() => wsProvider ?? httpProvider, [chain?.id, wsProvider, httpProvider])
}
9 changes: 5 additions & 4 deletions packages/app/src/hooks/use-token-balance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useCallback, useEffect, useState } from 'react'
import { BigNumber, ethers } from 'ethers'
import { useSafeProvider } from '@app/hooks/use-safe-provider'
import { IERC20Metadata, IERC20Metadata__factory } from '@pbm/contracts'
import { useAccount, useNetwork, useProvider } from 'wagmi'
import { BigNumber, ethers } from 'ethers'
import { useCallback, useEffect, useState } from 'react'
import { useAccount, useNetwork } from 'wagmi'

type UseTokenBalanceParams = {
token?: string
Expand All @@ -26,7 +27,7 @@ export const useTokenBalance = (props?: UseTokenBalanceParams): UseTokenBalanceR
const [symbol, setSymbol] = useState<string>()
const [loading, setLoading] = useState<boolean>(false)

const provider = useProvider()
const provider = useSafeProvider()
const { address: connectedAddress, isConnected } = useAccount()
const { chain } = useNetwork()

Expand Down
5 changes: 3 additions & 2 deletions packages/app/src/pages/payments/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PaymentsListingTable } from '@app/components/payments-listing-table'
import { usePBMTokenContext } from '@app/contexts/pbm-token-context'
import { useTransactionModal } from '@app/contexts/transaction-modal-context'
import { useFetchPaymentData } from '@app/hooks/use-fetch-payment-data'
import { useSafeProvider } from '@app/hooks/use-safe-provider'
import { formatNumberDisplay } from '@app/utils/helpers'
import { getPaymentDataSource } from '@app/utils/payments/helpers'
import { PaymentDataSource } from '@app/utils/payments/types'
Expand All @@ -14,7 +15,7 @@ import { Button, Space } from 'antd'
import { ethers, Signer } from 'ethers'
import { formatUnits } from 'ethers/lib/utils'
import React, { ElementRef, useCallback, useEffect, useRef, useState } from 'react'
import { useProvider, useSigner } from 'wagmi'
import { useSigner } from 'wagmi'

function PaymentsPage() {
useAppLayoutContext({ pageHeading: 'All Payments' })
Expand All @@ -25,7 +26,7 @@ function PaymentsPage() {
const [latestBlockLoading, setLatestBlockLoading] = useState<boolean>(false)
const [paymentDataSource, setPaymentDataSource] = useState<PaymentDataSource[]>([])

const provider = useProvider()
const provider = useSafeProvider()
const { data: signer } = useSigner()
const { decimals, contract: pbmContract, symbol } = usePBMTokenContext()
const { paymentData, loading: paymentDataLoading } = useFetchPaymentData()
Expand Down
5 changes: 3 additions & 2 deletions packages/app/src/pages/pbm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PbmUnwrapCard } from '@app/components/token-overview/pbm-unwrap-card'
import { usePBMTokenContext } from '@app/contexts/pbm-token-context'
import { useTransactionModal } from '@app/contexts/transaction-modal-context'
import { useFetchPaymentData } from '@app/hooks/use-fetch-payment-data'
import { useSafeProvider } from '@app/hooks/use-safe-provider'
import { useTokenBalance } from '@app/hooks/use-token-balance'
import { formatNumberDisplay } from '@app/utils/helpers'
import { getPaymentDataSource } from '@app/utils/payments/helpers'
Expand All @@ -15,7 +16,7 @@ import { Col, Row } from 'antd'
import { BigNumber, ethers, Signer } from 'ethers'
import { formatUnits } from 'ethers/lib/utils'
import React, { ComponentProps, useCallback, useEffect, useState } from 'react'
import { useAccount, useProvider, useSigner } from 'wagmi'
import { useAccount, useSigner } from 'wagmi'

function PbmPage() {
useAppLayoutContext({ pageHeading: 'My PBM Dashboard' })
Expand Down Expand Up @@ -51,7 +52,7 @@ function PbmPage() {
const [latestBlockLoading, setLatestBlockLoading] = useState<boolean>(false)
const [paymentDataSource, setPaymentDataSource] = useState<PaymentDataSource[]>([])

const provider = useProvider()
const provider = useSafeProvider()
const { paymentData, loading: paymentDataLoading } = useFetchPaymentData({
toAddress: connectedAddress,
})
Expand Down

0 comments on commit 1e69266

Please sign in to comment.