From f3080cb27f568b8373b3030750dcf3e67ad206fe Mon Sep 17 00:00:00 2001 From: tjs Date: Mon, 19 Dec 2022 16:58:22 -0500 Subject: [PATCH] add geo check to trade forms --- components/swap/SwapForm.tsx | 34 ++++++++++++---- components/trade/AdvancedTradeForm.tsx | 56 +++++++++++++++++--------- hooks/useIpAddress.ts | 31 +++++++------- public/locales/en/common.json | 2 + public/locales/es/common.json | 2 + public/locales/ru/common.json | 2 + public/locales/zh/common.json | 2 + public/locales/zh_tw/common.json | 2 + 8 files changed, 87 insertions(+), 44 deletions(-) diff --git a/components/swap/SwapForm.tsx b/components/swap/SwapForm.tsx index 4e190ab0..6c54bb71 100644 --- a/components/swap/SwapForm.tsx +++ b/components/swap/SwapForm.tsx @@ -45,6 +45,7 @@ import TokenVaultWarnings from '@components/shared/TokenVaultWarnings' import MaxSwapAmount from './MaxSwapAmount' import PercentageSelectButtons from './PercentageSelectButtons' import Tooltip from '@components/shared/Tooltip' +import useIpAddress from 'hooks/useIpAddress' const MAX_DIGITS = 11 export const withValueLimit = (values: NumberFormatValues): boolean => { @@ -64,6 +65,7 @@ const SwapForm = () => { const [showConfirm, setShowConfirm] = useState(false) const { group } = useMangoGroup() const [swapFormSizeUi] = useLocalStorageState(SIZE_INPUT_UI_KEY, 'Slider') + const { ipAllowed, ipCountry } = useIpAddress() const { margin: useMargin, @@ -418,14 +420,30 @@ const SwapForm = () => { useMargin={useMargin} /> )} - + {ipAllowed ? ( + + ) : ( +
+
+ +
+
+ )} {group && inputBank ? (
diff --git a/components/trade/AdvancedTradeForm.tsx b/components/trade/AdvancedTradeForm.tsx index e9d103a4..327f2761 100644 --- a/components/trade/AdvancedTradeForm.tsx +++ b/components/trade/AdvancedTradeForm.tsx @@ -39,6 +39,7 @@ import useSelectedMarket from 'hooks/useSelectedMarket' import Slippage from './Slippage' import { formatFixedDecimals, getDecimalCount } from 'utils/numbers' import LogoWithFallback from '@components/shared/LogoWithFallback' +import useIpAddress from 'hooks/useIpAddress' const TABS: [string, number][] = [ ['Limit', 0], @@ -55,6 +56,7 @@ const AdvancedTradeForm = () => { const [useMargin, setUseMargin] = useState(true) const [placingOrder, setPlacingOrder] = useState(false) const [tradeFormSizeUi] = useLocalStorageState(SIZE_INPUT_UI_KEY, 'Slider') + const { ipAllowed, ipCountry } = useIpAddress() const baseSymbol = useMemo(() => { return selectedMarket?.name.split(/-|\//)[0] @@ -557,27 +559,41 @@ const AdvancedTradeForm = () => { ) : null}
- + ) : ( +
+
+
- )} - +
+ )}
{tradeForm.price && tradeForm.baseSize ? ( diff --git a/hooks/useIpAddress.ts b/hooks/useIpAddress.ts index 50fbfd68..85fb757b 100644 --- a/hooks/useIpAddress.ts +++ b/hooks/useIpAddress.ts @@ -1,5 +1,5 @@ import { CLUSTER } from '@store/mangoStore' -import { useEffect, useState } from 'react' +import { useCallback, useEffect, useState } from 'react' const SANCTIONED_COUNTRIES = [ ['AG', 'Antigua and Barbuda'], @@ -13,7 +13,6 @@ const SANCTIONED_COUNTRIES = [ ['CU', 'Cuba'], ['CD', 'Democratic Republic of Congo'], ['EC', 'Ecuador'], - ['GB', 'United Kingdom'], ['IR', 'Iran'], ['IQ', 'Iraq'], ['LR', 'Liberia'], @@ -42,25 +41,25 @@ export default function useIpAddress() { const [spotAllowed, setSpotAllowed] = useState(false) const [ipCountry, setIpCountry] = useState('') - useEffect(() => { - const checkIpLocation = async () => { - const response = await fetch( - `https://country-code.mangomarkets.workers.dev` - ) - const parsedResponse = await response.json() - const ipCountryCode = parsedResponse ? parsedResponse?.country : '' + const checkIpLocation = useCallback(async () => { + const response = await fetch( + `https://country-code.mangomarkets.workers.dev` + ) + const parsedResponse = await response.json() + const ipCountryCode = parsedResponse ? parsedResponse?.country : '' - setIpCountry(ipCountryCode) + setIpCountry(ipCountryCode) - if (ipCountryCode) { - setIpAllowed(!SANCTIONED_COUNTRY_CODES.includes(ipCountryCode)) - setSpotAllowed(SPOT_ALLOWED.includes(ipCountryCode)) - } + if (ipCountryCode) { + setIpAllowed(!SANCTIONED_COUNTRY_CODES.includes(ipCountryCode)) + setSpotAllowed(SPOT_ALLOWED.includes(ipCountryCode)) } - - checkIpLocation() }, []) + useEffect(() => { + checkIpLocation() + }, [checkIpLocation]) + if (CLUSTER === 'mainnet-beta') { return { ipAllowed, spotAllowed, ipCountry } } else { diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 183724c4..4c3c47d3 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -37,6 +37,8 @@ "connect-helper": "Connect to get started", "copy-address": "Copy Address", "copy-address-success": "Copied address: {{pk}}", + "country-not-allowed": "Country {{country}} Not Allowed", + "country-not-allowed-tooltip": "You are using an open-source frontend facilitated by the Mango DAO. As such, it restricts access to certain regions out of an abundance of caution, due to regulatory uncertainty.", "create-account": "Create Account", "creating-account": "Creating Account...", "cumulative-interest-value": "Cumulative Interest Earned", diff --git a/public/locales/es/common.json b/public/locales/es/common.json index 183724c4..4c3c47d3 100644 --- a/public/locales/es/common.json +++ b/public/locales/es/common.json @@ -37,6 +37,8 @@ "connect-helper": "Connect to get started", "copy-address": "Copy Address", "copy-address-success": "Copied address: {{pk}}", + "country-not-allowed": "Country {{country}} Not Allowed", + "country-not-allowed-tooltip": "You are using an open-source frontend facilitated by the Mango DAO. As such, it restricts access to certain regions out of an abundance of caution, due to regulatory uncertainty.", "create-account": "Create Account", "creating-account": "Creating Account...", "cumulative-interest-value": "Cumulative Interest Earned", diff --git a/public/locales/ru/common.json b/public/locales/ru/common.json index 183724c4..4c3c47d3 100644 --- a/public/locales/ru/common.json +++ b/public/locales/ru/common.json @@ -37,6 +37,8 @@ "connect-helper": "Connect to get started", "copy-address": "Copy Address", "copy-address-success": "Copied address: {{pk}}", + "country-not-allowed": "Country {{country}} Not Allowed", + "country-not-allowed-tooltip": "You are using an open-source frontend facilitated by the Mango DAO. As such, it restricts access to certain regions out of an abundance of caution, due to regulatory uncertainty.", "create-account": "Create Account", "creating-account": "Creating Account...", "cumulative-interest-value": "Cumulative Interest Earned", diff --git a/public/locales/zh/common.json b/public/locales/zh/common.json index 183724c4..4c3c47d3 100644 --- a/public/locales/zh/common.json +++ b/public/locales/zh/common.json @@ -37,6 +37,8 @@ "connect-helper": "Connect to get started", "copy-address": "Copy Address", "copy-address-success": "Copied address: {{pk}}", + "country-not-allowed": "Country {{country}} Not Allowed", + "country-not-allowed-tooltip": "You are using an open-source frontend facilitated by the Mango DAO. As such, it restricts access to certain regions out of an abundance of caution, due to regulatory uncertainty.", "create-account": "Create Account", "creating-account": "Creating Account...", "cumulative-interest-value": "Cumulative Interest Earned", diff --git a/public/locales/zh_tw/common.json b/public/locales/zh_tw/common.json index 183724c4..4c3c47d3 100644 --- a/public/locales/zh_tw/common.json +++ b/public/locales/zh_tw/common.json @@ -37,6 +37,8 @@ "connect-helper": "Connect to get started", "copy-address": "Copy Address", "copy-address-success": "Copied address: {{pk}}", + "country-not-allowed": "Country {{country}} Not Allowed", + "country-not-allowed-tooltip": "You are using an open-source frontend facilitated by the Mango DAO. As such, it restricts access to certain regions out of an abundance of caution, due to regulatory uncertainty.", "create-account": "Create Account", "creating-account": "Creating Account...", "cumulative-interest-value": "Cumulative Interest Earned",