Skip to content

Commit

Permalink
deposit tokens modal
Browse files Browse the repository at this point in the history
  • Loading branch information
asktree committed Oct 6, 2023
1 parent 6f62f02 commit 2fa138c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
59 changes: 51 additions & 8 deletions components/DepositTokensButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import BN from 'bn.js'
import useUserGovTokenAccountQuery from '@hooks/useUserGovTokenAccount'
import { useDepositCallback } from './GovernancePower/Vanilla/useDepositCallback'
import useWalletOnePointOh from '@hooks/useWalletOnePointOh'
import Modal from './Modal'
import { useState } from 'react'
import useGoverningTokenMint from '@hooks/selectedRealm/useGoverningTokenMint'
import { useMintInfoByPubkeyQuery } from '@hooks/queries/mintInfo'
import Input from './inputs/Input'

export const DepositTokensButton = ({
role,
Expand All @@ -29,16 +34,54 @@ export const DepositTokensButton = ({
: undefined

const ButtonToUse = as === 'primary' ? Button : SecondaryButton
const [openModal, setOpenModal] = useState(false)
const [amount, setAmount] = useState('')
const mint = useGoverningTokenMint(role)
const mintInfo = useMintInfoByPubkeyQuery(mint).data?.result

const humanReadableMax =
mintInfo === undefined
? undefined
: depositAmount.shiftedBy(-mintInfo.decimals).toNumber()

const deposit = useDepositCallback(role)
return (
<ButtonToUse
{...props}
onClick={() => deposit(new BN(depositAmount.toString()))}
tooltipMessage={depositTooltipContent}
disabled={!connected || !hasTokensInWallet || props.disabled}
>
Deposit
</ButtonToUse>
<>
<ButtonToUse
{...props}
onClick={() => setOpenModal(true)}
tooltipMessage={depositTooltipContent}
disabled={!connected || !hasTokensInWallet || props.disabled}
>
Deposit
</ButtonToUse>
{openModal && (
<Modal isOpen={openModal} onClose={() => setOpenModal(false)}>
<div className="flex flex-col gap-y-4">
<h2>Deposit tokens</h2>
<Input
placeholder={humanReadableMax?.toString() + ' (max)'}
type="number"
label="Amount to deposit"
value={amount}
onChange={(e) => setAmount(e.target.value)}
max={humanReadableMax}
/>
<Button
onClick={async () => {
if (mintInfo === undefined) throw new Error()
const nativeAmount = new BN(
new BigNumber(amount).shiftedBy(mintInfo.decimals).toString()
)
await deposit(nativeAmount)
setOpenModal(false)
}}
>
Confirm
</Button>
</div>
</Modal>
)}
</>
)
}
4 changes: 4 additions & 0 deletions components/inputs/TokenAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type Props = Omit<
setError: (x: string | undefined) => void
}

/**
* @deprecated
* I feel like I must have been on drugs when I wrote this
*/
const TokenAmountInput: FC<Props> = ({
mint,
setValue,
Expand Down

0 comments on commit 2fa138c

Please sign in to comment.