-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Peter/earn strategies feat deposit withdraw form (#1229)
* chore: update monetary to latest 0.7.3 * wip * feat(earn-strategies): add deposit and withdrawal form components * refactor: add padding under tabs in earn strategy forms * chore(earn-strategies): change file structure
- Loading branch information
1 parent
891de67
commit 918e944
Showing
19 changed files
with
390 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { EarnStrategyFormType } from '@/pages/EarnStrategies/types/form'; | ||
|
||
import yup, { MaxAmountValidationParams, MinAmountValidationParams } from '../yup.custom'; | ||
|
||
type EarnStrategyValidationParams = MaxAmountValidationParams & MinAmountValidationParams; | ||
|
||
const earnStrategySchema = ( | ||
earnStrategyFormType: EarnStrategyFormType, | ||
params: EarnStrategyValidationParams | ||
): yup.ObjectSchema<any> => { | ||
return yup.object().shape({ | ||
[earnStrategyFormType]: yup | ||
.string() | ||
.requiredAmount(earnStrategyFormType) | ||
.maxAmount(params) | ||
.minAmount(params, earnStrategyFormType) | ||
}); | ||
}; | ||
|
||
export { earnStrategySchema }; | ||
export type { EarnStrategyValidationParams }; |
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,13 @@ | ||
import styled from 'styled-components'; | ||
|
||
import { theme } from '@/component-library'; | ||
const StyledEarnStrategiesLayout = styled.div` | ||
display: grid; | ||
gap: ${theme.spacing.spacing6}; | ||
@media (min-width: 80em) { | ||
grid-template-columns: 1fr 1fr; | ||
} | ||
padding: ${theme.spacing.spacing6}; | ||
`; | ||
|
||
export { StyledEarnStrategiesLayout }; |
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
66 changes: 66 additions & 0 deletions
66
...trategies/components/EarnStrategyForm/EarnStrategyDepositForm/EarnStrategyDepositForm.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,66 @@ | ||
import { newMonetaryAmount } from '@interlay/interbtc-api'; | ||
import { mergeProps } from '@react-aria/utils'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { convertMonetaryAmountToValueInUSD, newSafeMonetaryAmount } from '@/common/utils/utils'; | ||
import { TokenInput } from '@/component-library'; | ||
import { AuthCTA } from '@/components'; | ||
import { TRANSACTION_FEE_AMOUNT, WRAPPED_TOKEN, WRAPPED_TOKEN_SYMBOL } from '@/config/relay-chains'; | ||
import { earnStrategySchema, isFormDisabled, useForm } from '@/lib/form'; | ||
import { useGetBalances } from '@/utils/hooks/api/tokens/use-get-balances'; | ||
import { useGetPrices } from '@/utils/hooks/api/use-get-prices'; | ||
import { useTransaction } from '@/utils/hooks/transaction'; | ||
|
||
import { EarnStrategyDepositFormData } from '../../../types/form'; | ||
import { EarnStrategyFormBaseProps } from '../EarnStrategyForm'; | ||
import { StyledEarnStrategyFormContent } from '../EarnStrategyForm.style'; | ||
import { EarnStrategyFormFees } from '../EarnStrategyFormFees'; | ||
|
||
const EarnStrategyDepositForm = ({ riskVariant, hasActiveStrategy }: EarnStrategyFormBaseProps): JSX.Element => { | ||
const { getAvailableBalance } = useGetBalances(); | ||
const prices = useGetPrices(); | ||
const { t } = useTranslation(); | ||
// TODO: add transaction | ||
const transaction = useTransaction(); | ||
|
||
const handleSubmit = (data: EarnStrategyDepositFormData) => { | ||
// TODO: Execute transaction with params | ||
// transaction.execute(); | ||
console.log(`transaction should be executed with parameters: ${data}, ${riskVariant}`); | ||
}; | ||
|
||
const minAmount = newMonetaryAmount(1, WRAPPED_TOKEN); | ||
const maxDepositAmount = getAvailableBalance(WRAPPED_TOKEN_SYMBOL) || newMonetaryAmount(0, WRAPPED_TOKEN); | ||
|
||
const form = useForm<EarnStrategyDepositFormData>({ | ||
initialValues: { deposit: '' }, | ||
validationSchema: earnStrategySchema('deposit', { maxAmount: maxDepositAmount, minAmount }), | ||
onSubmit: handleSubmit | ||
}); | ||
|
||
const inputMonetaryAmount = newSafeMonetaryAmount(form.values['deposit'] || 0, WRAPPED_TOKEN, true); | ||
const inputUSDValue = convertMonetaryAmountToValueInUSD(inputMonetaryAmount, prices?.[WRAPPED_TOKEN_SYMBOL].usd); | ||
const isSubmitButtonDisabled = isFormDisabled(form); | ||
|
||
return ( | ||
<form onSubmit={form.handleSubmit}> | ||
<StyledEarnStrategyFormContent> | ||
<TokenInput | ||
placeholder='0.00' | ||
ticker={WRAPPED_TOKEN_SYMBOL} | ||
aria-label={t('forms.field_amount', { field: t('deposit') })} | ||
balance={maxDepositAmount?.toString()} | ||
humanBalance={maxDepositAmount?.toString()} | ||
valueUSD={inputUSDValue ?? undefined} | ||
{...mergeProps(form.getFieldProps('deposit'))} | ||
/> | ||
<EarnStrategyFormFees amount={TRANSACTION_FEE_AMOUNT} /> | ||
<AuthCTA type='submit' size='large' disabled={isSubmitButtonDisabled} loading={transaction.isLoading}> | ||
{hasActiveStrategy ? t('earn_strategy.update_position') : t('deposit')} | ||
</AuthCTA> | ||
</StyledEarnStrategyFormContent> | ||
</form> | ||
); | ||
}; | ||
|
||
export { EarnStrategyDepositForm }; |
1 change: 1 addition & 0 deletions
1
src/pages/EarnStrategies/components/EarnStrategyForm/EarnStrategyDepositForm/index.ts
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 @@ | ||
export { EarnStrategyDepositForm } from './EarnStrategyDepositForm'; |
34 changes: 34 additions & 0 deletions
34
src/pages/EarnStrategies/components/EarnStrategyForm/EarnStrategyForm.style.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,34 @@ | ||
import styled from 'styled-components'; | ||
|
||
import { Dl, Flex, theme } from '@/component-library'; | ||
|
||
const StyledEarnStrategyForm = styled(Flex)` | ||
margin-top: ${theme.spacing.spacing8}; | ||
background: ${theme.colors.bgPrimary}; | ||
padding: ${theme.spacing.spacing6}; | ||
border-radius: ${theme.rounded.md}; | ||
`; | ||
|
||
const StyledDl = styled(Dl)` | ||
background-color: ${theme.card.bg.secondary}; | ||
padding: ${theme.spacing.spacing4}; | ||
font-size: ${theme.text.xs}; | ||
border-radius: ${theme.rounded.rg}; | ||
`; | ||
|
||
const StyledEarnStrategyFormContent = styled(Flex)` | ||
margin-top: ${theme.spacing.spacing8}; | ||
flex-direction: column; | ||
gap: ${theme.spacing.spacing8}; | ||
`; | ||
|
||
const StyledSwitchLabel = styled('label')` | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
align-items: center; | ||
width: 100%; | ||
font-weight: ${theme.fontWeight.bold}; | ||
`; | ||
|
||
export { StyledDl, StyledEarnStrategyForm, StyledEarnStrategyFormContent, StyledSwitchLabel }; |
61 changes: 61 additions & 0 deletions
61
src/pages/EarnStrategies/components/EarnStrategyForm/EarnStrategyForm.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,61 @@ | ||
import { newMonetaryAmount } from '@interlay/interbtc-api'; | ||
|
||
import { Tabs, TabsItem } from '@/component-library'; | ||
import { WRAPPED_TOKEN } from '@/config/relay-chains'; | ||
|
||
import { EarnStrategyFormType, EarnStrategyRiskVariant } from '../../types/form'; | ||
import { EarnStrategyDepositForm } from './EarnStrategyDepositForm'; | ||
import { StyledEarnStrategyForm } from './EarnStrategyForm.style'; | ||
import { EarnStrategyWithdrawalForm } from './EarnStrategyWithdrawalForm'; | ||
|
||
interface EarnStrategyFormProps { | ||
riskVariant: EarnStrategyRiskVariant; | ||
} | ||
|
||
interface EarnStrategyFormBaseProps extends EarnStrategyFormProps { | ||
hasActiveStrategy: boolean | undefined; | ||
} | ||
|
||
type TabData = { type: EarnStrategyFormType; title: string }; | ||
|
||
const tabs: Array<TabData> = [ | ||
{ | ||
type: 'deposit', | ||
title: 'Deposit' | ||
}, | ||
{ | ||
type: 'withdraw', | ||
title: 'Withdraw' | ||
} | ||
]; | ||
|
||
const EarnStrategyForm = ({ riskVariant }: EarnStrategyFormProps): JSX.Element => { | ||
// TODO: replace with actually withdrawable amount once we know how to get that information, | ||
// for now it's statically set for display purposes | ||
const maxWithdrawableAmount = newMonetaryAmount(1.337, WRAPPED_TOKEN, true); | ||
const hasActiveStrategy = maxWithdrawableAmount && !maxWithdrawableAmount.isZero(); | ||
|
||
return ( | ||
<StyledEarnStrategyForm> | ||
<Tabs fullWidth size='large'> | ||
{tabs.map(({ type, title }) => ( | ||
<TabsItem key={type} title={title}> | ||
{type === 'deposit' ? ( | ||
<EarnStrategyDepositForm key={type} riskVariant={riskVariant} hasActiveStrategy={hasActiveStrategy} /> | ||
) : ( | ||
<EarnStrategyWithdrawalForm | ||
key={type} | ||
riskVariant={riskVariant} | ||
hasActiveStrategy={hasActiveStrategy} | ||
maxWithdrawableAmount={maxWithdrawableAmount} | ||
/> | ||
)} | ||
</TabsItem> | ||
))} | ||
</Tabs> | ||
</StyledEarnStrategyForm> | ||
); | ||
}; | ||
|
||
export { EarnStrategyForm }; | ||
export type { EarnStrategyFormBaseProps, EarnStrategyFormProps }; |
35 changes: 35 additions & 0 deletions
35
src/pages/EarnStrategies/components/EarnStrategyForm/EarnStrategyFormFees.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,35 @@ | ||
import { GovernanceCurrency } from '@interlay/interbtc-api'; | ||
import { MonetaryAmount } from '@interlay/monetary-js'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { displayMonetaryAmountInUSDFormat } from '@/common/utils/utils'; | ||
import { Dd, DlGroup, Dt } from '@/component-library'; | ||
import { getTokenPrice } from '@/utils/helpers/prices'; | ||
import { useGetPrices } from '@/utils/hooks/api/use-get-prices'; | ||
|
||
import { StyledDl } from './EarnStrategyForm.style'; | ||
|
||
interface EarnStrategyFormFeesProps { | ||
amount: MonetaryAmount<GovernanceCurrency>; | ||
} | ||
|
||
const EarnStrategyFormFees = ({ amount }: EarnStrategyFormFeesProps): JSX.Element => { | ||
const prices = useGetPrices(); | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<StyledDl direction='column' gap='spacing2'> | ||
<DlGroup justifyContent='space-between'> | ||
<Dt size='xs' color='primary'> | ||
{t('fees')} | ||
</Dt> | ||
<Dd size='xs'> | ||
{amount.toHuman()} {amount.currency.ticker} ( | ||
{displayMonetaryAmountInUSDFormat(amount, getTokenPrice(prices, amount.currency.ticker)?.usd)}) | ||
</Dd> | ||
</DlGroup> | ||
</StyledDl> | ||
); | ||
}; | ||
|
||
export { EarnStrategyFormFees }; |
Oops, something went wrong.
918e944
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.
Successfully deployed to the following URLs:
interbtc-ui-interlay-testnet – ./
interbtc-ui-interlay-testnet.vercel.app
interbtc-ui-interlay-testnet-git-master-interlay.vercel.app
interbtc-ui-interlay-testnet-interlay.vercel.app
testnet.interlay.io
918e944
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.
Successfully deployed to the following URLs:
interbtc-ui-kintsugi-testnet – ./
interbtc-ui-kintsugi-testnet-interlay.vercel.app
interbtc-ui.vercel.app
interbtc-ui-kintsugi-testnet-git-master-interlay.vercel.app
kintnet.interlay.io