-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from commons-stack/initial-reserve
Do not allow users set an initial reserve higher than their balances
- Loading branch information
Showing
15 changed files
with
151 additions
and
58 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
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
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { default as useIsRegisteredDao } from "./src/hooks/useIsRegisteredDao"; | ||
export { default as DaoNameInput } from "./src/components/DaoNameInput"; | ||
export { default as useIsBalanceEnough } from "./src/hooks/useIsBalanceEnough"; | ||
export { default as DaoNameInput } from "./src/components/DaoNameInput"; | ||
export { default as BalanceInput } from "./src/components/BalanceInput"; |
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,42 @@ | ||
import { Input, InputGroup, InputRightElement } from '@chakra-ui/react'; | ||
import { useIsBalanceEnough } from '../..'; | ||
import { useEffect } from 'react'; | ||
import FetchingInputIcon from './FetchingInputIcon'; | ||
|
||
export default function BalanceInput({ | ||
value, | ||
token, | ||
decimals, | ||
setValue, | ||
...props | ||
}: { | ||
value: string; | ||
token: `0x${string}`; | ||
decimals: number; | ||
setValue: (value: {value: string, isEnough: boolean | undefined}) => void; | ||
} & Omit<React.ComponentProps<typeof Input>, 'onChange'>) { | ||
const { isEnough, error, isLoading } = useIsBalanceEnough({value, token, decimals}); | ||
|
||
function handleValueChange(value: string) { | ||
setValue({value: value, isEnough: undefined}); | ||
} | ||
|
||
useEffect(() => { | ||
setValue({ value, isEnough }); | ||
}, [isEnough, value, setValue]); | ||
|
||
return ( | ||
<InputGroup> | ||
<Input | ||
value={value} | ||
onChange={e => handleValueChange(e.target.value)} | ||
errorBorderColor='red.500' | ||
isInvalid={isEnough === false} | ||
{...props} | ||
/> | ||
<InputRightElement> | ||
<FetchingInputIcon isLoading={isLoading} inputValue={value} fetched={isEnough} error={!!error} positiveIcon={<></>} spinnerIcon={<></>} /> | ||
</InputRightElement> | ||
</InputGroup> | ||
) | ||
} |
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,20 @@ | ||
import { Spinner } from '@chakra-ui/react' | ||
import { CheckCircleIcon, WarningTwoIcon } from '@chakra-ui/icons' | ||
|
||
export default function FetchingInputIcon({ inputValue, inverted, isLoading, fetched, error, positiveIcon, negativeIcon, spinnerIcon }: { | ||
inputValue: string, | ||
inverted?: boolean, | ||
isLoading: boolean, | ||
fetched?: boolean, | ||
error: boolean, | ||
positiveIcon?: React.ReactElement, | ||
negativeIcon?: React.ReactElement, | ||
spinnerIcon?: React.ReactElement, | ||
}) { | ||
return ( | ||
!inputValue ? <></> : | ||
isLoading || fetched === undefined ? spinnerIcon ?? <Spinner size='xs' /> : | ||
!error && (!inverted && fetched || inverted && !fetched) ? positiveIcon ?? <CheckCircleIcon color="brand.500" /> : | ||
negativeIcon ?? <WarningTwoIcon color="red.500" /> | ||
) | ||
} |
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,20 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
// Modified from https://usehooks-ts.com/react-hook/use-debounce/ | ||
export default function useDebounce<T>(value: T, delay?: number): [T, boolean] { | ||
const [isDebouncing, setIsDebouncing] = useState(true); | ||
const [debouncedValue, setDebouncedValue] = useState<T>(value) | ||
|
||
useEffect(() => { | ||
const timer = setTimeout(() => { | ||
setDebouncedValue(value); | ||
setIsDebouncing(false); | ||
}, delay || 500) | ||
|
||
return () => { | ||
clearTimeout(timer) | ||
} | ||
}, [value, delay]) | ||
|
||
return [debouncedValue, isDebouncing] | ||
} |
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,27 @@ | ||
import { useAccount, useBalance } from 'wagmi'; | ||
import { parseUnits } from 'viem'; | ||
import useDebounce from './useDebounce'; | ||
|
||
/** | ||
* A custom React hook that checks if the user's token balance is enough for a given value. | ||
* @param value - The value to check if the balance is enough for. | ||
* @param token - The token address to check the balance of. | ||
* @param decimals - The number of decimals for the token. | ||
* @returns An object containing the `isEnough`, `error`, and `isLoading` properties. | ||
* - `isEnough` is a boolean indicating if the balance is enough for the given value. | ||
* - `error` is an error object if there was an error fetching the balance. | ||
* - `isLoading` is a boolean indicating if the balance is currently being fetched. | ||
*/ | ||
function useIsBalanceEnough({value, token, decimals}: {value: string, token: `0x${string}`, decimals: number}, delay?: number) { | ||
|
||
const [debouncedValue, isDebouncing] = useDebounce(value, delay); | ||
const { address } = useAccount() | ||
const { data: balance, error, isLoading: isFetching } = useBalance({address, token}); | ||
|
||
const isLoading = !address || isFetching || isDebouncing; | ||
const isEnough = !isLoading && token && balance ? balance.value >= parseUnits(debouncedValue, decimals) : undefined; | ||
|
||
return { isEnough, error, isLoading }; | ||
} | ||
|
||
export default useIsBalanceEnough; |
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