-
Notifications
You must be signed in to change notification settings - Fork 17
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: Improve protection logic #1732
Changes from 13 commits
310f918
27db0b5
f6efe45
7d6954d
5fd1de0
bdfe34f
5384bae
0d64d58
e63c670
4ff5f99
589a27b
acfefa0
d355b4c
eaa1d88
7eb1290
d5b8126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
const address = req.nextUrl.searchParams.get('address') | ||
const path = address | ||
? `/api/v2/protections?${new URLSearchParams({ address }).toString()}` | ||
: '/api/v2/protections' | ||
// FIXME: Update hostname | ||
const res = await fetch(`https://api-pr-29-80wy.onrender.com${path}`, { | ||
headers: { | ||
...req.headers, | ||
'ic-ip-address': | ||
req.headers.get('cf-connecting-ip') ?? | ||
req.headers.get('x-forwarded-for') ?? | ||
undefined, | ||
'ic-ip-country': | ||
req.headers.get('cf-ipcountry') ?? | ||
req.headers.get('x-vercel-ip-country') ?? | ||
undefined, | ||
}, | ||
}) | ||
const { isRestrictedCountry, isUsingVpn } = await res.json() | ||
|
||
return NextResponse.json({ | ||
isRestrictedCountry, | ||
isUsingVpn, | ||
}) | ||
} catch (e) { | ||
console.error('Caught protections error', e) | ||
return NextResponse.json({ | ||
isRestrictedCountry: false, | ||
isUsingVpn: false, | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import { TradeButton } from '@/components/trade-button' | |
import { Token } from '@/constants/tokens' | ||
import { useApproval } from '@/lib/hooks/use-approval' | ||
import { useNetwork } from '@/lib/hooks/use-network' | ||
import { useProtection } from '@/lib/providers/protection' | ||
import { useProtectionContext } from '@/lib/providers/protection' | ||
import { useSignTerms } from '@/lib/providers/sign-terms-provider' | ||
import { useSlippage } from '@/lib/providers/slippage' | ||
import { getNativeToken, isTokenPairTradable } from '@/lib/utils/tokens' | ||
|
@@ -52,7 +52,7 @@ export function SmartTradeButton(props: SmartTradeButtonProps) { | |
|
||
const { chainId } = useNetwork() | ||
const { open } = useWeb3Modal() | ||
const requiresProtection = useProtection() | ||
const { isRestrictedCountry, isUsingVpn } = useProtectionContext() | ||
const { signTermsOfService } = useSignTerms() | ||
const { slippage } = useSlippage() | ||
|
||
|
@@ -65,12 +65,18 @@ export function SmartTradeButton(props: SmartTradeButtonProps) { | |
const isTradablePair = useMemo( | ||
() => | ||
isTokenPairTradable( | ||
requiresProtection, | ||
isRestrictedCountry || isUsingVpn, | ||
inputToken.symbol, | ||
outputToken.symbol, | ||
chainId ?? 1, | ||
), | ||
[chainId, requiresProtection, inputToken, outputToken], | ||
[ | ||
isRestrictedCountry, | ||
isUsingVpn, | ||
inputToken.symbol, | ||
outputToken.symbol, | ||
chainId, | ||
], | ||
) | ||
|
||
const shouldApprove = useMemo(() => { | ||
|
@@ -97,10 +103,22 @@ export function SmartTradeButton(props: SmartTradeButtonProps) { | |
const [warnings, setWarnings] = useState<WarningType[]>([]) | ||
|
||
useEffect(() => { | ||
if (!isTradablePair && !hiddenWarnings?.includes(WarningType.restricted)) { | ||
if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im thinking there is no effect here, so instead of an effect and a state you could just use useMemo, and return the result There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tokdaniel good call, updated to |
||
!isTradablePair && | ||
isRestrictedCountry && | ||
!hiddenWarnings?.includes(WarningType.restricted) | ||
) { | ||
setWarnings([WarningType.restricted]) | ||
return | ||
} | ||
if ( | ||
!isTradablePair && | ||
isUsingVpn && | ||
!hiddenWarnings?.includes(WarningType.vpn) | ||
) { | ||
setWarnings([WarningType.vpn]) | ||
return | ||
} | ||
if ( | ||
buttonState === TradeButtonState.signTerms && | ||
!hiddenWarnings?.includes(WarningType.signTerms) | ||
|
@@ -117,7 +135,14 @@ export function SmartTradeButton(props: SmartTradeButtonProps) { | |
return | ||
} | ||
setWarnings([]) | ||
}, [buttonState, hiddenWarnings, isTradablePair, slippage]) | ||
}, [ | ||
buttonState, | ||
hiddenWarnings, | ||
isRestrictedCountry, | ||
isTradablePair, | ||
isUsingVpn, | ||
slippage, | ||
]) | ||
|
||
const onClick = useCallback(async () => { | ||
if (buttonState === TradeButtonState.connectWallet) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallbacks are there so the code works on preview deployments (i.e. not passing through the cloudflare proxy)