mango-v4-ui/hooks/useIpAddress.ts

116 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-12-18 12:26:38 -08:00
import { CLUSTER } from '@store/mangoStore'
2023-01-20 08:13:03 -08:00
import { useQuery } from '@tanstack/react-query'
import { useEffect, useState } from 'react'
2022-12-18 12:26:38 -08:00
export const SANCTIONED_COUNTRIES = [
2022-12-18 12:26:38 -08:00
['AG', 'Antigua and Barbuda'],
['DZ', 'Algeria'],
['BD', 'Bangladesh'],
['BO', 'Bolivia'],
['BY', 'Belarus'],
['BI', 'Burundi'],
['MM', 'Burma (Myanmar)'],
['CI', "Cote D'Ivoire (Ivory Coast)"],
['CU', 'Cuba'],
['CD', 'Democratic Republic of Congo'],
['EC', 'Ecuador'],
['GB', 'United Kingdom'],
2022-12-18 12:26:38 -08:00
['IR', 'Iran'],
['IQ', 'Iraq'],
['LR', 'Liberia'],
['LY', 'Libya'],
['ML', 'Mali'],
['MA', 'Morocco'],
['NP', 'Nepal'],
['KP', 'North Korea'],
['SO', 'Somalia'],
['SD', 'Sudan'],
['SY', 'Syria'],
['VE', 'Venezuela'],
['YE', 'Yemen'],
['ZW', 'Zimbabwe'],
['US', 'United States'],
]
const SANCTIONED_COUNTRY_CODES = SANCTIONED_COUNTRIES.map(
2023-07-21 11:47:53 -07:00
(country) => country[0],
2022-12-18 12:26:38 -08:00
)
const PERP_ALLOWED: string[] = []
const SPOT_ALLOWED: string[] = []
const SWAP_ALLOWED: string[] = []
const BORROW_LEND_ALLOWED: string[] = []
const SHOW_WARNING: string[] = ['GB']
2022-12-18 12:26:38 -08:00
2023-01-20 08:13:03 -08:00
const fetchIpGeolocation = async () => {
try {
const response = await fetch(
`https://country-code.mangomarkets.workers.dev`,
)
const parsedResponse = await response.json()
const ipCountryCode = parsedResponse ? parsedResponse?.country : ''
2023-01-20 08:13:03 -08:00
return ipCountryCode
} catch (e) {
console.log('failed to fetch ip country', e)
2024-01-04 02:29:20 -08:00
return ''
}
2023-01-20 08:13:03 -08:00
}
2022-12-18 12:26:38 -08:00
export default function useIpAddress() {
const [ipAllowed, setIpAllowed] = useState(true)
const [spotAllowed, setSpotAllowed] = useState(true)
const [perpAllowed, setPerpAllowed] = useState(true)
const [swapAllowed, setSwapAllowed] = useState(true)
const [borrowLendAllowed, setBorrowLendAllowed] = useState(true)
const [showWarning, setShowWarning] = useState(false)
2022-12-18 12:26:38 -08:00
const [ipCountry, setIpCountry] = useState('')
2024-01-06 03:52:31 -08:00
const { data: ipCountryCode, isInitialLoading } = useQuery<string, Error>(
['ip-address'],
() => fetchIpGeolocation(),
{
cacheTime: 1000 * 60 * 2,
staleTime: 1000 * 60 * 2,
retry: 3,
refetchOnWindowFocus: true,
},
)
2022-12-18 12:26:38 -08:00
2022-12-19 13:58:22 -08:00
useEffect(() => {
if (ipCountryCode) {
setIpCountry(ipCountryCode)
setIpAllowed(!SANCTIONED_COUNTRY_CODES.includes(ipCountryCode))
setSpotAllowed(SPOT_ALLOWED.includes(ipCountryCode))
setPerpAllowed(PERP_ALLOWED.includes(ipCountryCode))
setSwapAllowed(SWAP_ALLOWED.includes(ipCountryCode))
setBorrowLendAllowed(BORROW_LEND_ALLOWED.includes(ipCountryCode))
setShowWarning(SHOW_WARNING.includes(ipCountryCode))
2023-01-20 08:13:03 -08:00
}
}, [ipCountryCode])
2022-12-18 12:26:38 -08:00
if (CLUSTER === 'mainnet-beta' && !process.env.NEXT_PUBLIC_DISABLE_GEOBLOCK) {
return {
ipAllowed,
spotAllowed,
perpAllowed,
swapAllowed,
borrowLendAllowed,
showWarning,
ipCountry,
2024-01-06 03:52:31 -08:00
loadingIpCountry: isInitialLoading,
}
2022-12-18 12:26:38 -08:00
} else {
return {
ipAllowed: true,
spotAllowed: true,
perpAllowed: true,
swapAllowed: true,
borrowLendAllowed: true,
showWarning: true,
ipCountry,
2024-01-06 03:52:31 -08:00
loadingIpCountry: isInitialLoading,
}
2022-12-18 12:26:38 -08:00
}
}