Skip to content

Commit

Permalink
feat: revert stakeSdkProvider changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt561 committed Oct 21, 2024
1 parent 895c588 commit 51faab0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
14 changes: 5 additions & 9 deletions app/components/UI/Stake/hooks/usePoolStakedDeposit/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { captureException } from '@sentry/react-native';
import { ChainId, PooledStakingContract } from '@metamask/stake-sdk';
import {
TransactionParams,
Expand All @@ -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';

Expand Down Expand Up @@ -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),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const mockPooledStakingContractService: PooledStakingContract = {
};

const mockSdkContext: Stake = {
sdkService: mockPooledStakingContractService,
stakingContract: mockPooledStakingContractService,
sdkType: StakingType.POOLED,
setSdkType: jest.fn(),
};
Expand Down
68 changes: 50 additions & 18 deletions app/components/UI/Stake/sdk/stakeSdkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stake | undefined>(undefined);

export const StakeSDKProvider: React.FC = ({ children }) => {
export interface StakeProviderProps {
stakingType?: StakingType;
}
export const StakeSDKProvider: React.FC<
PropsWithChildren<StakeProviderProps>
> = ({ children }) => {
const [stakingContract, setStakingContract] =
useState<PooledStakingContract>();
const [stakingApiService, setStakingApiService] =
useState<StakingApiService>();

const [sdkError, setSdkError] = useState<Error>();
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 (
<StakeContext.Provider value={stakeContextValue}>
{children}
Expand Down

0 comments on commit 51faab0

Please sign in to comment.