Skip to content

Commit

Permalink
Check if addresses are sanctioned when adding funds
Browse files Browse the repository at this point in the history
  • Loading branch information
luads committed Nov 13, 2024
1 parent ca12bd3 commit a8dd4df
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ import { Review } from './views/Review';
import { fetchBalances } from './functions/fetchBalances';
import { useTokens } from './hooks/useTokens';
import { useProvidersContext } from '../../context/providers-context/ProvidersContext';
import { ServiceUnavailableToRegionErrorView } from '../../views/error/ServiceUnavailableToRegionErrorView';
import { ServiceType } from '../../views/error/serviceTypes';
import { orchestrationEvents } from '../../lib/orchestrationEvents';
import { getRemoteImage } from '../../lib/utils';
import { isValidAddress } from '../../lib/validations/widgetValidators';
import { amountInputValidation } from '../../lib/validations/amountInputValidations';
import { useError } from './hooks/useError';
import { AddTokensErrorTypes } from './types';
import { ServiceUnavailableErrorView } from '../../views/error/ServiceUnavailableErrorView';

export type AddTokensWidgetInputs = Omit<AddTokensWidgetParams, 'toProvider'> & {
config: StrongCheckoutWidgetsConfig;
Expand Down Expand Up @@ -265,9 +264,15 @@ export default function AddTokensWidget({
)}
{viewState.view.type
=== SharedViews.SERVICE_UNAVAILABLE_ERROR_VIEW && (
<ServiceUnavailableToRegionErrorView
service={ServiceType.GENERIC}
<ServiceUnavailableErrorView
onCloseClick={() => sendAddTokensCloseEvent(eventTarget)}
onBackButtonClick={() => {
viewDispatch({
payload: {
type: ViewActions.GO_BACK,
},
});
}}
/>
)}
</Stack>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useEffect, useState } from 'react';
import { AssessmentResult, fetchRiskAssessment } from '@imtbl/checkout-sdk';
import { useProvidersContext } from '../../../context/providers-context/ProvidersContext';

export const useRiskAssessment = () => {
const [riskAssessment, setRiskAssessment] = useState<AssessmentResult | undefined>();

const {
providersState: {
checkout, fromAddress, toAddress,
},
} = useProvidersContext();

useEffect(() => {
if (!checkout) {
return;
}

(async () => {
const addresses: string[] = [];

if (fromAddress) {
addresses.push(fromAddress);
}

if (toAddress && toAddress !== fromAddress) {
addresses.push(toAddress);
}

if (addresses.length === 0) {
return;
}

const assessment = await fetchRiskAssessment(addresses, checkout.config);
setRiskAssessment(assessment);
})();
}, [checkout, fromAddress, toAddress]);

return { riskAssessment };
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type Checkout,
EIP6963ProviderInfo,
IMTBLWidgetEvents,
isAddressSanctioned,
TokenFilterTypes,
type TokenInfo,
WalletProviderRdns,
Expand All @@ -31,6 +32,7 @@ import { useTranslation } from 'react-i18next';
import { SimpleLayout } from '../../../components/SimpleLayout/SimpleLayout';
import { EventTargetContext } from '../../../context/event-target-context/EventTargetContext';
import {
SharedViews,
ViewActions,
ViewContext,
} from '../../../context/view-context/ViewContext';
Expand Down Expand Up @@ -66,6 +68,7 @@ import { SquidFooter } from '../components/SquidFooter';
import { getFormattedNumberWithDecimalPlaces } from '../functions/getFormattedNumber';
import { TokenDrawerMenu } from '../components/TokenDrawerMenu';
import { PULSE_SHADOW } from '../utils/animation';
import { useRiskAssessment } from '../hooks/useRiskAssessment';

interface AddTokensProps {
checkout: Checkout;
Expand Down Expand Up @@ -133,6 +136,8 @@ export function AddTokens({
const [fetchingRoutes, setFetchingRoutes] = useState(false);
const [insufficientBalance, setInsufficientBalance] = useState(false);

const { riskAssessment } = useRiskAssessment();

const selectedAmountUsd = useMemo(
() => convertToUsd(tokens, inputValue, selectedToken),
[tokens, inputValue, selectedToken],
Expand Down Expand Up @@ -316,6 +321,20 @@ export function AddTokens({
}, [checkout]);

const sendRequestOnRampEvent = () => {
if (!riskAssessment || isAddressSanctioned(riskAssessment)) {
viewDispatch({
payload: {
type: ViewActions.UPDATE_VIEW,
view: {
type: SharedViews.SERVICE_UNAVAILABLE_ERROR_VIEW,
error: new Error('Sanctioned address'),
},
},
});

return;
}

track({
userJourney: UserJourney.ADD_TOKENS,
screen: 'InputScreen',
Expand Down Expand Up @@ -349,10 +368,10 @@ export function AddTokens({
};

useEffect(() => {
if (toProvider && payWithCardClicked) {
if (toProvider && riskAssessment && payWithCardClicked) {
sendRequestOnRampEvent();
}
}, [toProvider]);
}, [toProvider, riskAssessment, payWithCardClicked]);

const handleRouteClick = (route: RouteData) => {
setShowOptionsDrawer(false);
Expand All @@ -371,6 +390,20 @@ export function AddTokens({
const handleReviewClick = () => {
if (!selectedRouteData || !selectedToken?.address) return;

if (!riskAssessment || isAddressSanctioned(riskAssessment)) {
viewDispatch({
payload: {
type: ViewActions.UPDATE_VIEW,
view: {
type: SharedViews.SERVICE_UNAVAILABLE_ERROR_VIEW,
error: new Error('Sanctioned address'),
},
},
});

return;
}

track({
userJourney: UserJourney.ADD_TOKENS,
screen: 'InputScreen',
Expand Down Expand Up @@ -424,7 +457,7 @@ export function AddTokens({
const loading = (routeInputsReady || fetchingRoutes)
&& !(selectedRouteData || insufficientBalance);

const readyToReview = routeInputsReady && !!toAddress && !!selectedRouteData && !loading;
const readyToReview = routeInputsReady && !!toAddress && !!selectedRouteData && !loading && !!riskAssessment;

const handleWalletConnected = (
providerType: 'from' | 'to',
Expand Down

0 comments on commit a8dd4df

Please sign in to comment.