-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of stake form (#93)
This PR adds a stake form. The data after approving the form will be saved in the context to use the given amount in a later step. The form for ustake and stake are very similar, so let's create a form base component. It will allow us to use it for unstake flow later. Changes: - Added styles for the `Tabs` component - Created the form base component - Handling of the stake form - Basic validation for form input - Added necessary methods that convert numbers from floating point number to bigint
- Loading branch information
Showing
37 changed files
with
747 additions
and
153 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,41 @@ | ||
import React from "react" | ||
import { | ||
ModalBody, | ||
Tabs, | ||
TabList, | ||
Tab, | ||
TabPanels, | ||
TabPanel, | ||
} from "@chakra-ui/react" | ||
import StakeForm from "../Staking/StakeForm" | ||
import { useModalFlowContext } from "../../../hooks" | ||
|
||
const TABS = ["stake", "unstake"] as const | ||
|
||
type Action = (typeof TABS)[number] | ||
|
||
function ActionForm({ action }: { action: Action }) { | ||
const { goNext } = useModalFlowContext() | ||
|
||
return ( | ||
<ModalBody> | ||
<Tabs w="100%" variant="underline" defaultIndex={TABS.indexOf(action)}> | ||
<TabList> | ||
{TABS.map((tab) => ( | ||
<Tab key={tab} w="50%"> | ||
{tab} | ||
</Tab> | ||
))} | ||
</TabList> | ||
<TabPanels> | ||
<TabPanel> | ||
<StakeForm goNext={goNext} /> | ||
</TabPanel> | ||
<TabPanel>{/* TODO: Add form for unstake */}</TabPanel> | ||
</TabPanels> | ||
</Tabs> | ||
</ModalBody> | ||
) | ||
} | ||
|
||
export default ActionForm |
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,48 @@ | ||
import React from "react" | ||
import { List } from "@chakra-ui/react" | ||
import { useTransactionDetails } from "../../../../hooks" | ||
import TransactionDetailsAmountItem from "../../../shared/TransactionDetails/AmountItem" | ||
import { CurrencyType } from "../../../../types" | ||
import { useTokenAmountFormValue } from "../../../shared/TokenAmountForm/TokenAmountFormBase" | ||
|
||
function Details({ currency }: { currency: CurrencyType }) { | ||
const value = useTokenAmountFormValue() | ||
const details = useTransactionDetails(value ?? 0n) | ||
|
||
return ( | ||
<List spacing={3} mt={10}> | ||
<TransactionDetailsAmountItem | ||
label="Amount to be staked" | ||
from={{ | ||
currency, | ||
amount: details?.btcAmount, | ||
}} | ||
to={{ | ||
currency: "usd", | ||
}} | ||
/> | ||
<TransactionDetailsAmountItem | ||
label="Protocol fee (0.01%)" | ||
from={{ | ||
currency, | ||
amount: details?.protocolFee, | ||
}} | ||
to={{ | ||
currency: "usd", | ||
}} | ||
/> | ||
<TransactionDetailsAmountItem | ||
label="Approximately staked tokens" | ||
from={{ | ||
currency, | ||
amount: details?.estimatedAmount, | ||
}} | ||
to={{ | ||
currency: "usd", | ||
}} | ||
/> | ||
</List> | ||
) | ||
} | ||
|
||
export default Details |
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,40 @@ | ||
import React, { useCallback } from "react" | ||
import { Button } from "@chakra-ui/react" | ||
import { BITCOIN_MIN_AMOUNT } from "../../../../constants" | ||
import { ModalStep } from "../../../../contexts" | ||
import { useWalletContext, useTransactionContext } from "../../../../hooks" | ||
import TokenAmountForm from "../../../shared/TokenAmountForm" | ||
import { TokenAmountFormValues } from "../../../shared/TokenAmountForm/TokenAmountFormBase" | ||
import Details from "./Details" | ||
|
||
function StakeForm({ goNext }: ModalStep) { | ||
const { btcAccount } = useWalletContext() | ||
const { setTokenAmount } = useTransactionContext() | ||
|
||
const handleSubmitForm = useCallback( | ||
(values: TokenAmountFormValues) => { | ||
if (!values.amount) return | ||
|
||
setTokenAmount({ amount: values.amount, currency: "bitcoin" }) | ||
goNext() | ||
}, | ||
[goNext, setTokenAmount], | ||
) | ||
|
||
return ( | ||
<TokenAmountForm | ||
tokenBalanceInputPlaceholder="BTC" | ||
currency="bitcoin" | ||
tokenBalance={btcAccount?.balance.toString() ?? "0"} | ||
minTokenAmount={BITCOIN_MIN_AMOUNT} | ||
onSubmitForm={handleSubmitForm} | ||
> | ||
<Details currency="bitcoin" /> | ||
<Button type="submit" size="lg" width="100%" mt={4}> | ||
Stake | ||
</Button> | ||
</TokenAmountForm> | ||
) | ||
} | ||
|
||
export default StakeForm |
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
17 changes: 15 additions & 2 deletions
17
dapp/src/components/shared/CurrencyBalanceWithConversion/index.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 |
---|---|---|
@@ -1,17 +1,30 @@ | ||
import React from "react" | ||
import React, { useMemo } from "react" | ||
import { CurrencyBalance, CurrencyBalanceProps } from "../CurrencyBalance" | ||
|
||
const MOCK_CONVERSION_AMOUNT = 100 | ||
|
||
export function CurrencyBalanceWithConversion({ | ||
from, | ||
to, | ||
}: { | ||
from: CurrencyBalanceProps | ||
to: CurrencyBalanceProps | ||
}) { | ||
// TODO: Make the correct conversion | ||
const conversionAmount = useMemo(() => { | ||
if (!from.amount || BigInt(from.amount) < 0n) return undefined | ||
|
||
return MOCK_CONVERSION_AMOUNT | ||
}, [from.amount]) | ||
|
||
return ( | ||
<> | ||
<CurrencyBalance {...from} /> | ||
<CurrencyBalance {...to} /> | ||
<CurrencyBalance | ||
amount={conversionAmount} | ||
shouldBeFormatted={false} | ||
{...to} | ||
/> | ||
</> | ||
) | ||
} |
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,4 @@ | ||
import { chakra } from "@chakra-ui/react" | ||
import { Form as FormikForm } from "formik" | ||
|
||
export const Form = chakra(FormikForm) |
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,24 @@ | ||
import React from "react" | ||
import { useField } from "formik" | ||
import TokenBalanceInput, { TokenBalanceInputProps } from "../TokenBalanceInput" | ||
|
||
export type FormTokenBalanceInputProps = { | ||
name: string | ||
} & Omit<TokenBalanceInputProps, "setAmount"> | ||
export function FormTokenBalanceInput({ | ||
name, | ||
...restProps | ||
}: FormTokenBalanceInputProps) { | ||
const [field, meta, helpers] = useField(name) | ||
|
||
return ( | ||
<TokenBalanceInput | ||
{...restProps} | ||
{...field} | ||
amount={meta.value} | ||
setAmount={helpers.setValue} | ||
hasError={Boolean(meta.touched && meta.error)} | ||
errorMsgText={meta.error} | ||
/> | ||
) | ||
} |
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,2 @@ | ||
export * from "./Form" | ||
export * from "./FormTokenBalanceInput" |
Oops, something went wrong.