Skip to content

Commit

Permalink
feat(safe-apps): add support for SafeNet RPC by using SAFENET_API_URL…
Browse files Browse the repository at this point in the history
… and checking if the chain is supported
  • Loading branch information
mmv08 committed Oct 9, 2024
1 parent c5ea76e commit bf95cd5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/components/safe-apps/AppFrame/useAppCommunicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import { SAFE_APPS_EVENTS, trackSafeAppEvent } from '@/services/analytics'
import { useAppSelector } from '@/store'
import { selectRpc } from '@/store/settingsSlice'
import { createSafeAppsWeb3Provider } from '@/hooks/wallets/web3'
import { useGetSafeNetConfigQuery } from '@/store/safenet'
import { QueryStatus } from '@reduxjs/toolkit/query'
import { SafenetChainType, isSupportedChain } from '@/utils/safenet'
import { SAFENET_API_URL } from '@/config/constants'

export enum CommunicatorMessages {
REJECT_TRANSACTION_MESSAGE = 'Transaction was rejected',
Expand Down Expand Up @@ -73,14 +77,25 @@ const useAppCommunicator = (
): AppCommunicator | undefined => {
const [communicator, setCommunicator] = useState<AppCommunicator | undefined>(undefined)
const customRpc = useAppSelector(selectRpc)
const { data: safeNetConfig, status: safeNetConfigStatus } = useGetSafeNetConfigQuery()
const shouldUseSafeNetRpc =
safeNetConfigStatus === QueryStatus.fulfilled &&
chain &&
safeNetConfig &&
isSupportedChain(Number(chain.chainId), safeNetConfig, SafenetChainType.DESTINATION)

const safeAppWeb3Provider = useMemo(() => {
if (!chain) {
return
}

if (shouldUseSafeNetRpc) {
console.log('Using SafeNet RPC', SAFENET_API_URL + `/jsonrpc/${chain.chainId}`)
return createSafeAppsWeb3Provider(chain, SAFENET_API_URL + `/jsonrpc/${chain.chainId}/`)
}

return createSafeAppsWeb3Provider(chain, customRpc?.[chain.chainId])
}, [chain, customRpc])
}, [chain, customRpc, shouldUseSafeNetRpc])

useEffect(() => {
let communicatorInstance: AppCommunicator
Expand Down Expand Up @@ -205,7 +220,7 @@ const useAppCommunicator = (
communicator?.on(Methods.requestAddressBook, (msg) => {
return handlers.onRequestAddressBook(msg.origin)
})
}, [safeAppWeb3Provider, handlers, chain, communicator])
}, [safeAppWeb3Provider, handlers, chain, communicator, shouldUseSafeNetRpc])

return communicator
}
Expand Down

0 comments on commit bf95cd5

Please sign in to comment.