-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of stake form #93
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
7269487
Form initialization for stake/unstake action
kkosiorowska 2fdfae3
Add function to convert user amount to `bigint`
kkosiorowska e4ccfbe
Add a validation for stake form
kkosiorowska 4c67f66
Handling the submission of the stake form
kkosiorowska 062cadf
Merge branch 'main' into stake-form
kkosiorowska 1b1b9ab
Improvements for forms
kkosiorowska 4b845b9
Define custom data from the form level
kkosiorowska a1bdfc7
Merge branch 'main' of github.com:thesis/acre into stake-form
kkosiorowska 303f3b2
Make a `validate` function more general
kkosiorowska c697ec8
Create a utils function to find for currency by type
kkosiorowska 9984076
Make the token amount form component clearer
kkosiorowska 4399b2c
Simplify the ActionForm component
kkosiorowska 8c8d2a1
Store the amount as bigint for the token amount form
kkosiorowska acda15a
Merge branch 'main' of github.com:thesis/acre into stake-form
kkosiorowska 9eba84c
Update the submit form function for staking
kkosiorowska a0c4b71
Use a more precise name for custom form data
kkosiorowska 443c881
Refactor the display of transaction details in the form
kkosiorowska d49a39c
Use `Currency` instead `CurrencyType` for TokenAmountForm
kkosiorowska 99bcdfe
Move conversion logic to `CurrencyBalanceWithConversion` component
kkosiorowska a28a07f
Refactor for `useTransactionDetails`
kkosiorowska 8f397e2
Fix the positioning bug for `TransactionDetailsAmountItem`
kkosiorowska 720b567
Merge branch 'main' of github.com:thesis/acre into stake-form
kkosiorowska 47c0ca1
Save the token amount field name for the form as const
kkosiorowska 96e87fc
Make a formId optional for the the token amount form
kkosiorowska cf1eabb
Render the submit button inside form
kkosiorowska c6b6c7b
Use always `CurrencyType`
kkosiorowska 39cfbbc
Fix typo from `seTokenAmount` to `setTokenAmount`
kkosiorowska 597a890
Create a special hook for the token amount value from form
kkosiorowska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the name of this component because at first sight, it may indicate we can render any form but actually we are rendering stake/unstake form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's true this name isn't perfect. But I don't have a better idea. You are right, but on the other hand, we are just displaying the form there. The type of action restricts us on the types of form. If you just have any better idea let me know.