diff --git a/app/components/UI/Stake/hooks/usePoolStakedDeposit/index.ts b/app/components/UI/Stake/hooks/usePoolStakedDeposit/index.ts index fc45f3c0477..bd696294e1e 100644 --- a/app/components/UI/Stake/hooks/usePoolStakedDeposit/index.ts +++ b/app/components/UI/Stake/hooks/usePoolStakedDeposit/index.ts @@ -1,4 +1,3 @@ -import { captureException } from '@sentry/react-native'; import { ChainId, PooledStakingContract } from '@metamask/stake-sdk'; import { TransactionParams, @@ -8,6 +7,7 @@ import { ORIGIN_METAMASK, toHex } from '@metamask/controller-utils'; import { addTransaction } from '../../../../../util/transaction-controller'; import { formatEther } from 'ethers/lib/utils'; import { useStakeContext } from '../useStakeContext'; +import trackErrorAsAnalytics from '../../../../../util/metrics/TrackError/trackErrorAsAnalytics'; const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; @@ -68,21 +68,17 @@ const attemptDepositTransaction = }); } catch (e) { const errorMessage = (e as Error).message; - captureException( - new Error( - `Failed to submit Pooled Staking transaction to transaction controller with message: ${errorMessage}`, - ), - ); + trackErrorAsAnalytics('Pooled Staking Transaction Failed', errorMessage); } }; const usePoolStakedDeposit = () => { const stakeContext = useStakeContext(); + const stakingContract = stakeContext.stakingContract as PooledStakingContract; + return { - attemptDepositTransaction: attemptDepositTransaction( - stakeContext.sdkService, - ), + attemptDepositTransaction: attemptDepositTransaction(stakingContract), }; }; diff --git a/app/components/UI/Stake/hooks/usePoolStakedDeposit/usePoolStakedDeposit.test.tsx b/app/components/UI/Stake/hooks/usePoolStakedDeposit/usePoolStakedDeposit.test.tsx index 60dba319798..27195b5842f 100644 --- a/app/components/UI/Stake/hooks/usePoolStakedDeposit/usePoolStakedDeposit.test.tsx +++ b/app/components/UI/Stake/hooks/usePoolStakedDeposit/usePoolStakedDeposit.test.tsx @@ -90,7 +90,7 @@ const mockPooledStakingContractService: PooledStakingContract = { }; const mockSdkContext: Stake = { - sdkService: mockPooledStakingContractService, + stakingContract: mockPooledStakingContractService, sdkType: StakingType.POOLED, setSdkType: jest.fn(), }; diff --git a/app/components/UI/Stake/sdk/stakeSdkProvider.tsx b/app/components/UI/Stake/sdk/stakeSdkProvider.tsx index 6736b6dd7ef..88c12506681 100644 --- a/app/components/UI/Stake/sdk/stakeSdkProvider.tsx +++ b/app/components/UI/Stake/sdk/stakeSdkProvider.tsx @@ -2,48 +2,80 @@ import { StakingType, StakeSdk, PooledStakingContract, + type StakingApiService, } from '@metamask/stake-sdk'; -import React, { useState, createContext, useMemo } from 'react'; +import Logger from '../../../../util/Logger'; +import React, { + useState, + useEffect, + createContext, + useMemo, + PropsWithChildren, +} from 'react'; import { getProviderByChainId } from '../../../../util/notifications'; import { useSelector } from 'react-redux'; import { selectChainId } from '../../../../selectors/networkController'; -import { hexToDecimal } from '../../../../util/conversions'; + +export const SDK = StakeSdk.create({ stakingType: StakingType.POOLED }); export interface Stake { - sdkService: PooledStakingContract; // to do : facade it for other services implementation + sdkError?: Error; + stakingContract?: PooledStakingContract; + stakingApiService?: StakingApiService; sdkType?: StakingType; setSdkType: (stakeType: StakingType) => void; } export const StakeContext = createContext(undefined); -export const StakeSDKProvider: React.FC = ({ children }) => { +export interface StakeProviderProps { + stakingType?: StakingType; +} +export const StakeSDKProvider: React.FC< + PropsWithChildren +> = ({ children }) => { + const [stakingContract, setStakingContract] = + useState(); + const [stakingApiService, setStakingApiService] = + useState(); + + const [sdkError, setSdkError] = useState(); const [sdkType, setSdkType] = useState(StakingType.POOLED); const chainId = useSelector(selectChainId); - const sdkService = useMemo(() => { - const provider = getProviderByChainId(chainId); - - const sdk = StakeSdk.create({ - chainId: parseInt(hexToDecimal(chainId).toString()), - stakingType: sdkType, - }); - - sdk.pooledStakingContract.connectSignerOrProvider(provider); - - return sdk.pooledStakingContract; + useEffect(() => { + (async () => { + try { + setStakingApiService(SDK.stakingApiService); + if (sdkType === StakingType?.POOLED) { + const provider = getProviderByChainId(chainId); + SDK.pooledStakingContract.connectSignerOrProvider(provider); + setStakingContract(SDK.pooledStakingContract); + } else { + const notImplementedError = new Error( + `StakeSDKProvider SDK.StakingType ${sdkType} not implemented yet`, + ); + Logger.error(notImplementedError); + setSdkError(notImplementedError); + } + } catch (error) { + Logger.error(error as Error, `StakeSDKProvider SDK.service failed`); + setSdkError(error as Error); + } + })(); }, [chainId, sdkType]); const stakeContextValue = useMemo( (): Stake => ({ - sdkService, + sdkError, + stakingContract, sdkType, setSdkType, + stakingApiService, }), - [sdkService, sdkType, setSdkType], + [sdkError, stakingContract, sdkType, stakingApiService], ); - return ( {children}