Skip to content

Commit

Permalink
feat: add stakeSdkProvider and context with stake-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
siibars committed Oct 17, 2024
1 parent 0482c6e commit d85c30d
Show file tree
Hide file tree
Showing 11 changed files with 4,657 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import styleSheet from './StakeInputView.styles';
import useStakingInputHandlers from '../../hooks/useStakingInput';
import useBalance from '../../hooks/useBalance';
import InputDisplay from '../../components/InputDisplay';
import { useStakeContext } from '../../hooks/useStakeContext';

const StakeInputView = () => {
const title = strings('stake.stake_eth');
Expand All @@ -43,6 +44,9 @@ const StakeInputView = () => {
estimatedAnnualRewards,
} = useStakingInputHandlers(balanceWei);


const { sdkService } = useStakeContext();

const navigateToLearnMoreModal = () => {
navigation.navigate('StakeModals', {
screen: Routes.STAKING.MODALS.LEARN_MORE,
Expand All @@ -57,7 +61,7 @@ const StakeInputView = () => {
amountFiat: fiatAmount,
},
});
}, [amountWei, fiatAmount, navigation]);
}, [amountWei, fiatAmount, navigation, sdkService]);

Check failure on line 64 in app/components/UI/Stake/Views/StakeInputView/StakeInputView.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint)

React Hook useCallback has an unnecessary dependency: 'sdkService'. Either exclude it or remove the dependency array

const balanceText = strings('stake.balance');

Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/Stake/hooks/useBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const useBalance = () => {
[balanceWei, conversionRate],
);

return { balance, balanceFiat, balanceWei, balanceFiatNumber };
return { balance, balanceFiat, balanceWei, balanceFiatNumber, conversionRate, currentCurrency };
};

export default useBalance;
7 changes: 7 additions & 0 deletions app/components/UI/Stake/hooks/useStakeContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useContext } from "react";

Check failure on line 1 in app/components/UI/Stake/hooks/useStakeContext.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Strings must use singlequote
import { Stake, StakeContext } from "../sdk/stakeSdkProvider";

Check failure on line 2 in app/components/UI/Stake/hooks/useStakeContext.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Strings must use singlequote

export const useStakeContext = () => {
const context = useContext(StakeContext);
return context as Stake;
};
23 changes: 13 additions & 10 deletions app/components/UI/Stake/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import LearnMoreModal from '../components/LearnMoreModal';
import Routes from '../../../../constants/navigation/Routes';
import StakeConfirmationView from '../Views/StakeConfirmationView/StakeConfirmationView';
import UnstakeInputView from '../Views/UnstakeInputView/UnstakeInputView';
import { StakeSDKProvider } from '../sdk/stakeSdkProvider';
const Stack = createStackNavigator();
const ModalStack = createStackNavigator();

Expand All @@ -30,16 +31,18 @@ const StakeScreenStack = () => (

// Modal Stack for Modals
const StakeModalStack = () => (
<ModalStack.Navigator
mode={'modal'}
screenOptions={clearStackNavigatorOptions}
>
<ModalStack.Screen
name={Routes.STAKING.MODALS.LEARN_MORE}
component={LearnMoreModal}
options={{ headerShown: false }}
/>
</ModalStack.Navigator>
<StakeSDKProvider>
<ModalStack.Navigator
mode={'modal'}
screenOptions={clearStackNavigatorOptions}
>
<ModalStack.Screen
name={Routes.STAKING.MODALS.LEARN_MORE}
component={LearnMoreModal}
options={{ headerShown: false }}
/>
</ModalStack.Navigator>
</StakeSDKProvider>
);

export { StakeScreenStack, StakeModalStack };
72 changes: 72 additions & 0 deletions app/components/UI/Stake/sdk/UseSdkProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
ChainId,
PooledStakingContract,
StakingType,
} from '@metamask/stake-sdk';
import renderWithProvider from '../../../../util/test/renderWithProvider';
import { createStackNavigator } from '@react-navigation/stack';

Check failure on line 7 in app/components/UI/Stake/sdk/UseSdkProvider.test.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint)

'createStackNavigator' is defined but never used
import { backgroundState } from '../../../../util/test/initial-root-state';
import { Stake } from '../sdk/stakeSdkProvider';
import * as useStakeContextHook from '../hooks/useStakeContext';

Check failure on line 10 in app/components/UI/Stake/sdk/UseSdkProvider.test.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint)

Unexpected namespace import
import { Contract } from '@ethersproject/contracts';
import { StakeModalStack, StakeScreenStack } from '../routes';

const mockPooledStakingContractService: PooledStakingContract = {
chainId: ChainId.ETHEREUM,
connectSignerOrProvider: jest.fn(),
contract: new Contract('0x0000000000000000000000000000000000000000', []),
convertToShares: jest.fn(),
encodeClaimExitedAssetsTransactionData: jest.fn(),
encodeDepositTransactionData: jest.fn(),
encodeEnterExitQueueTransactionData: jest.fn(),
encodeMulticallTransactionData: jest.fn(),
estimateClaimExitedAssetsGas: jest.fn(),
estimateDepositGas: jest.fn(),
estimateEnterExitQueueGas: jest.fn(),
estimateMulticallGas: jest.fn(),
};

const mockSDK: Stake = {
sdkService: mockPooledStakingContractService,
sdkType: StakingType.POOLED,
setSdkType: jest.fn(),
};

jest.mock('../../Stake/constants', () => ({
isPooledStakingFeatureEnabled: jest.fn().mockReturnValue(true),
}));

describe('Stake Modals With Stake Sdk Provider', () => {
const initialState = {
engine: {
backgroundState,
},
};

it('should render correctly stake screen with stake sdk provider and resolve the stake context', () => {
const useStakeContextSpy = jest
.spyOn(useStakeContextHook, 'useStakeContext')
.mockReturnValue(mockSDK);

const { toJSON } = renderWithProvider(StakeScreenStack(), {
state: initialState,
});

expect(toJSON()).toMatchSnapshot();
expect(useStakeContextSpy).toHaveBeenCalled();
});

it('should render correctly stake modal with stake sdk provider and resolve the stake context', () => {
const useStakeContextSpy = jest
.spyOn(useStakeContextHook, 'useStakeContext')
.mockReturnValue(mockSDK);

const { toJSON } = renderWithProvider(StakeModalStack(), {
state: initialState,
});

expect(toJSON()).toMatchSnapshot();
expect(useStakeContextSpy).toHaveBeenCalledTimes(0);

});
});
Loading

0 comments on commit d85c30d

Please sign in to comment.