-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widgets): add error reporting component, INTEGRATION ERROR type,…
… and Missing provider error (#3110) * add error reporting component, INTEGRATION ERROR type, and Missing provider error * rename reporter to generator * pr feedback * refactor provider check * add chainId, convenienceFee, and width errors * pr feedback and convenienceFeeRecipient address enforcement * fix imports for utils
- Loading branch information
1 parent
d54783a
commit 8e3b2cb
Showing
6 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { ALL_SUPPORTED_CHAIN_IDS } from 'constants/chains' | ||
import { ChainIdError, IntegrationError } from 'lib/errors' | ||
import useActiveWeb3React from 'lib/hooks/useActiveWeb3React' | ||
import { SwapWidgetProps } from 'lib/index' | ||
import { useEffect } from 'react' | ||
|
||
import { isAddress } from '../../../utils' | ||
|
||
export default function ErrorGenerator(swapWidgetProps: SwapWidgetProps) { | ||
const { jsonRpcEndpoint, provider } = swapWidgetProps | ||
useEffect(() => { | ||
if (!provider && !jsonRpcEndpoint) { | ||
throw new IntegrationError('This widget requires a provider or jsonRpcEndpoint.') | ||
} | ||
}, [provider, jsonRpcEndpoint]) | ||
|
||
const { chainId } = useActiveWeb3React() | ||
useEffect(() => { | ||
if (chainId && !ALL_SUPPORTED_CHAIN_IDS.includes(chainId)) { | ||
throw new ChainIdError('Switch to a network supported by the Uniswap Protocol.') | ||
} | ||
}, [chainId]) | ||
|
||
// size constraints | ||
const { width } = swapWidgetProps | ||
useEffect(() => { | ||
if (width && width < 300) { | ||
throw new IntegrationError('Set widget width to at least 300px.') | ||
} | ||
}, [width]) | ||
|
||
// convenience fee constraints | ||
const { convenienceFee, convenienceFeeRecipient } = swapWidgetProps | ||
useEffect(() => { | ||
if (convenienceFee) { | ||
if (convenienceFee > 100 || convenienceFee < 0) { | ||
throw new IntegrationError('Set widget convenienceFee to at least 400px.') | ||
} | ||
if (!convenienceFeeRecipient) { | ||
throw new IntegrationError('convenienceFeeRecipient is required when convenienceFee is set.') | ||
} | ||
const MustBeValidAddressError = new IntegrationError('convenienceFeeRecipient must be a valid address.') | ||
if (typeof convenienceFeeRecipient === 'string') { | ||
if (!isAddress(convenienceFeeRecipient)) { | ||
throw MustBeValidAddressError | ||
} | ||
} else if (typeof convenienceFeeRecipient === 'object') { | ||
Object.values(convenienceFeeRecipient).forEach((recipient) => { | ||
if (!isAddress(recipient)) { | ||
throw MustBeValidAddressError | ||
} | ||
}) | ||
} | ||
} | ||
}, [convenienceFee, convenienceFeeRecipient]) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export class IntegrationError extends Error { | ||
constructor(message: string) { | ||
super(message) | ||
this.name = 'Integration Error' | ||
} | ||
} | ||
|
||
export class ChainIdError extends Error { | ||
constructor(message: string) { | ||
super(message) | ||
this.name = 'Unsupported network' | ||
} | ||
} |