Merge pull request #8 from blockworks-foundation/max/ip-block

enable ip block
This commit is contained in:
Maximilian Schneider 2021-08-09 22:28:28 +02:00 committed by GitHub
commit 836e7d5e44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 19 deletions

View File

@ -19,6 +19,7 @@ import usePool from '../hooks/usePool'
import styled from '@emotion/styled'
import 'twin.macro'
import { notify } from '../utils/notifications'
import useIpAddress from '../hooks/useIpAddress'
const SmallButton = styled.button``
@ -29,6 +30,7 @@ const ContributionModal = () => {
const largestAccounts = useLargestAccounts()
//const vaults = useVaults()
const { endIdo, endDeposits } = usePool()
const { ipAllowed } = useIpAddress()
const usdcBalance = largestAccounts.usdc?.balance || 0
const redeemableBalance = largestAccounts.redeemable?.balance || 0
@ -333,22 +335,29 @@ const ContributionModal = () => {
</div>
)}
</div>
<Button
onClick={() => handleSetContribution()}
className="w-full py-2.5"
disabled={disableSubmit}
>
<div className={`flex items-center justify-center`}>
{dontAddMore
? "Sorry you can't add anymore 🥲"
: !hasUSDC && connected
? 'Your USDC balance is 0'
: difference >= 0
? `Deposit $${usdFormat.format(difference)}`
: `Withdraw $${usdFormat.format(-difference)}`}
</div>
</Button>
{ipAllowed || !connected ? (
<Button
onClick={() => handleSetContribution()}
className="w-full py-2.5"
disabled={disableSubmit}
>
<div className={`flex items-center justify-center`}>
{dontAddMore
? "Sorry you can't add anymore 🥲"
: !hasUSDC && connected
? 'Your USDC balance is 0'
: difference >= 0
? `Deposit $${usdFormat.format(difference)}`
: `Withdraw $${usdFormat.format(-difference)}`}
</div>
</Button>
) : (
<Button className="w-full py-2.5" disabled>
<div className={`flex items-center justify-center`}>
Country Not Allowed 🇺🇸😭
</div>
</Button>
)}
</div>
</div>
<div className="flex items-center justify-center">

View File

@ -44,9 +44,11 @@ const RedeemModal = () => {
setSubmitting(true)
}
// useEffect(() => {
// actions.fetchMints()
// }, [])
useEffect(() => {
if (redeemableMint) {
actions.fetchMints()
}
}, [])
useEffect(() => {
setLoading(true)

53
hooks/useIpAddress.tsx Normal file
View File

@ -0,0 +1,53 @@
import { useEffect, useState } from 'react'
const SANCTIONED_COUNTRIES = [
{ country: 'Antigua and Barbuda', code: 'AG' },
{ country: 'Algeria', code: 'DZ' },
{ country: 'Bangladesh', code: 'BD' },
{ country: 'Bolivia', code: 'BO' },
{ country: 'Belarus', code: 'BY' },
{ country: 'Burundi', code: 'BI' },
{ country: 'Burma (Myanmar)', code: 'MM' },
{ country: "Cote D'Ivoire (Ivory Coast)", code: 'CI' },
{ country: 'Cuba', code: 'CU' },
{ country: 'Democratic Republic of Congo', code: 'CD' },
{ country: 'Ecuador', code: 'EC' },
{ country: 'Iran', code: 'IR' },
{ country: 'Iraq', code: 'IQ' },
{ country: 'Liberia', code: 'LR' },
{ country: 'Libya', code: 'LY' },
{ country: 'Mali', code: 'ML' },
{ country: 'Morocco', code: 'MA' },
{ country: 'Nepal', code: 'NP' },
{ country: 'North Korea', code: 'KP' },
{ country: 'Somalia', code: 'SO' },
{ country: 'Sudan', code: 'SD' },
{ country: 'Syria', code: 'SY' },
{ country: 'Venezuela', code: 'VE' },
{ country: 'Yemen', code: 'YE' },
{ country: 'Zimbabwe', code: 'ZW' },
{ country: 'United States', code: 'US' },
]
const SANCTIONED_COUNTRY_CODES = SANCTIONED_COUNTRIES.map(({ code }) => code)
export default function useIpAddress() {
const [ipAllowed, setIpAllowed] = useState(true)
useEffect(() => {
const checkIpLocation = async () => {
const response = await fetch(`https://www.cloudflare.com/cdn-cgi/trace`)
const parsedResponse = await response.text()
const ipLocation = parsedResponse.match(/loc=(.+)/)
const ipCountryCode = ipLocation ? ipLocation[1] : ''
if (ipCountryCode) {
setIpAllowed(!SANCTIONED_COUNTRY_CODES.includes(ipCountryCode))
}
}
checkIpLocation()
}, [])
return { ipAllowed }
}