Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Check if addresses are sanctioned when adding funds #2385

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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