-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widgets): use default input/output (#3161)
* feat: use default input/output on chain switch * feat(widgets): ErrorGenerator -> PropValidator * default prop validation * useDefaults hook * pr feedback * fix cosmos * drop token map changes * add default inputs to cosmos fixture * set up different validation layers for widget and swap * split widget/swap prop types * cleanup * pr feedback * clear defaults when they're no longer valid on the current chain * remove state checks on validators * stop using address in cosmos fixture * pr feedback * useMemo on useSwapDefaults args * tell the user what they gave to error'd props Co-authored-by: Zach Pomerantz <[email protected]>
- Loading branch information
1 parent
779625a
commit ce96873
Showing
9 changed files
with
242 additions
and
122 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
import { WidgetProps } from 'lib/components/Widget' | ||
import { IntegrationError } from 'lib/errors' | ||
import { PropsWithChildren, useEffect } from 'react' | ||
|
||
export default function WidgetsPropsValidator(props: PropsWithChildren<WidgetProps>) { | ||
const { jsonRpcEndpoint, provider } = props | ||
|
||
useEffect(() => { | ||
if (!provider && !jsonRpcEndpoint) { | ||
throw new IntegrationError('This widget requires a provider or jsonRpcEndpoint.') | ||
} | ||
}, [provider, jsonRpcEndpoint]) | ||
|
||
const { width } = props | ||
useEffect(() => { | ||
if (width && width < 300) { | ||
throw new IntegrationError(`Set widget width to at least 300px. (You set it to ${width}.)`) | ||
} | ||
}, [width]) | ||
|
||
return <>{props.children}</> | ||
} |
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,76 @@ | ||
import { BigNumber } from '@ethersproject/bignumber' | ||
import { DefaultAddress, SwapProps } from 'lib/components/Swap' | ||
import { IntegrationError } from 'lib/errors' | ||
import { PropsWithChildren, useEffect } from 'react' | ||
|
||
import { isAddress } from '../../../utils' | ||
|
||
function isAddressOrAddressMap(addressOrMap: DefaultAddress): boolean { | ||
if (typeof addressOrMap === 'object') { | ||
return Object.values(addressOrMap).every((address) => isAddress(address)) | ||
} | ||
if (typeof addressOrMap === 'string') { | ||
return typeof isAddress(addressOrMap) === 'string' | ||
} | ||
return false | ||
} | ||
|
||
type ValidatorProps = PropsWithChildren<SwapProps> | ||
|
||
export default function SwapPropValidator(props: ValidatorProps) { | ||
const { convenienceFee, convenienceFeeRecipient } = props | ||
useEffect(() => { | ||
if (convenienceFee) { | ||
if (convenienceFee > 100 || convenienceFee < 0) { | ||
throw new IntegrationError(`convenienceFee must be between 0 and 100. (You set it to ${convenienceFee})`) | ||
} | ||
if (!convenienceFeeRecipient) { | ||
throw new IntegrationError('convenienceFeeRecipient is required when convenienceFee is set.') | ||
} | ||
|
||
if (typeof convenienceFeeRecipient === 'string') { | ||
if (!isAddress(convenienceFeeRecipient)) { | ||
throw new IntegrationError( | ||
`convenienceFeeRecipient must be a valid address. (You set it to ${convenienceFeeRecipient}.)` | ||
) | ||
} | ||
} else if (typeof convenienceFeeRecipient === 'object') { | ||
Object.values(convenienceFeeRecipient).forEach((recipient) => { | ||
if (!isAddress(recipient)) { | ||
const values = Object.values(convenienceFeeRecipient).join(', ') | ||
throw new IntegrationError( | ||
`All values in convenienceFeeRecipient object must be valid addresses. (You used ${values}.)` | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
}, [convenienceFee, convenienceFeeRecipient]) | ||
|
||
const { defaultInputAddress, defaultInputAmount, defaultOutputAddress, defaultOutputAmount } = props | ||
useEffect(() => { | ||
if (defaultOutputAmount && defaultInputAmount) { | ||
throw new IntegrationError('defaultInputAmount and defaultOutputAmount may not both be defined.') | ||
} | ||
if (defaultInputAmount && BigNumber.from(defaultInputAmount).lt(0)) { | ||
throw new IntegrationError(`defaultInputAmount must be a positive number. (You set it to ${defaultInputAmount})`) | ||
} | ||
if (defaultOutputAmount && BigNumber.from(defaultOutputAmount).lt(0)) { | ||
throw new IntegrationError( | ||
`defaultOutputAmount must be a positive number. (You set it to ${defaultOutputAmount})` | ||
) | ||
} | ||
if (defaultInputAddress && !isAddressOrAddressMap(defaultInputAddress) && defaultInputAddress !== 'NATIVE') { | ||
throw new IntegrationError( | ||
`defaultInputAddress(es) must be a valid address or "NATIVE". (You set it to ${defaultInputAddress}` | ||
) | ||
} | ||
if (defaultOutputAddress && !isAddressOrAddressMap(defaultOutputAddress) && defaultOutputAddress !== 'NATIVE') { | ||
throw new IntegrationError( | ||
`defaultOutputAddress(es) must be a valid address or "NATIVE". (You set it to ${defaultOutputAddress}` | ||
) | ||
} | ||
}, [defaultInputAddress, defaultInputAmount, defaultOutputAddress, defaultOutputAmount]) | ||
|
||
return <>{props.children}</> | ||
} |
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
Oops, something went wrong.