-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
176 additions
and
114 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
trade.renegade.fi/app/(desktop)/[base]/[quote]/deposit-button.tsx
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,114 @@ | ||
import { CreateStepper } from "@/components/steppers/create-stepper/create-stepper" | ||
import { DepositStepper } from "@/components/steppers/deposit-stepper/deposit-stepper" | ||
import { useDeposit } from "@/contexts/Deposit/deposit-context" | ||
import { useRenegade } from "@/contexts/Renegade/renegade-context" | ||
import { env } from "@/env.mjs" | ||
import { useErc20Allowance, useErc20Approve, usePrepareErc20Approve } from "@/generated" | ||
import { useButton } from "@/hooks/use-button" | ||
import { useIsLocked } from "@/hooks/use-is-locked" | ||
import { | ||
ArrowForwardIcon | ||
} from "@chakra-ui/icons" | ||
import { | ||
Button, | ||
useDisclosure | ||
} from "@chakra-ui/react" | ||
import { Token } from "@renegade-fi/renegade-js" | ||
import { useAccount } from "wagmi" | ||
|
||
const MAX_INT = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935") | ||
|
||
export default function DepositButton() { | ||
const { baseTicker, baseTokenAmount } = | ||
useDeposit() | ||
const isLocked = useIsLocked() | ||
const { accountId } = useRenegade() | ||
const { | ||
isOpen: stepperIsOpen, | ||
onOpen: onOpenStepper, | ||
onClose: onCloseStepper, | ||
} = useDisclosure() | ||
const { | ||
isOpen: signInIsOpen, | ||
onOpen: onOpenSignIn, | ||
onClose: onCloseSignIn, | ||
} = useDisclosure() | ||
const { buttonOnClick, buttonText, cursor, shouldUse } = useButton({ | ||
connectText: "Connect Wallet to Deposit", | ||
onOpenSignIn, | ||
signInText: "Sign in to Deposit", | ||
}) | ||
const isDisabled = accountId && (isLocked || !baseTokenAmount) | ||
|
||
const { address } = useAccount() | ||
const { data: allowance } = useErc20Allowance({ | ||
address: Token.findAddressByTicker(baseTicker) as `0x${string}`, | ||
args: [address ? address : "0x", env.NEXT_PUBLIC_DARKPOOL_CONTRACT as `0x${string}`] | ||
, watch: true | ||
}) | ||
const needsApproval = allowance === BigInt(0) | ||
|
||
const { config } = usePrepareErc20Approve({ | ||
address: Token.findAddressByTicker(baseTicker) as `0x${string}`, | ||
args: [env.NEXT_PUBLIC_DARKPOOL_CONTRACT as `0x${string}`, MAX_INT] | ||
}) | ||
const { write: approve, isLoading } = useErc20Approve(config) | ||
|
||
const handleApprove = async () => { | ||
if (!accountId || !approve) return | ||
approve() | ||
} | ||
|
||
const handleClick = () => { | ||
if (shouldUse) { | ||
buttonOnClick() | ||
} else if (needsApproval) { | ||
handleApprove() | ||
} else { | ||
onOpenStepper() | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<Button | ||
padding="20px" | ||
color="white.80" | ||
fontSize="1.2em" | ||
fontWeight="200" | ||
opacity={baseTokenAmount ? 1 : 0} | ||
borderWidth="thin" | ||
borderColor="white.40" | ||
borderRadius="100px" | ||
_hover={ | ||
isDisabled | ||
? { backgroundColor: "transparent" } | ||
: { | ||
borderColor: "white.60", | ||
color: "white", | ||
} | ||
} | ||
transform={baseTokenAmount ? "translateY(10px)" : "translateY(-10px)"} | ||
visibility={baseTokenAmount ? "visible" : "hidden"} | ||
cursor={cursor} | ||
transition="0.15s" | ||
backgroundColor="transparent" | ||
isDisabled={isDisabled} | ||
// isLoading={isLocked} | ||
// loadingText="Please wait for task completion" | ||
isLoading={isLoading} | ||
loadingText="Approving" | ||
onClick={handleClick} | ||
rightIcon={<ArrowForwardIcon />} | ||
> | ||
{shouldUse | ||
? buttonText | ||
: needsApproval | ||
? `Approve ${baseTicker}` | ||
: `Deposit ${baseTokenAmount || ""} ${baseTicker}`} | ||
</Button> | ||
{stepperIsOpen && <DepositStepper onClose={onCloseStepper} />} | ||
{signInIsOpen && <CreateStepper onClose={onCloseSignIn} />} | ||
</> | ||
) | ||
} |
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
Oops, something went wrong.