-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/simple native send form (#12040)
* fix(suite-native): crypto amount formatter supports multi-word network symbols * feat(suite-native): TextInputField allow transform function * feat(suite-native): add feeReducer to suite-native * feat(suite-native): sendForm validation schema * feat(suite-native): sendForm component
- Loading branch information
Showing
11 changed files
with
286 additions
and
17 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
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,93 @@ | ||
import React from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { formInputsMaxLength } from '@suite-common/validators'; | ||
import { VStack, Button, Text } from '@suite-native/atoms'; | ||
import { useForm, Form, TextInputField } from '@suite-native/forms'; | ||
import { AccountKey } from '@suite-common/wallet-types'; | ||
import { | ||
AccountsRootState, | ||
FeesRootState, | ||
selectAccountByKey, | ||
selectNetworkFeeInfo, | ||
} from '@suite-common/wallet-core'; | ||
|
||
import { SendFormValues, sendFormValidationSchema } from '../sendFormSchema'; | ||
|
||
type SendFormProps = { | ||
accountKey: AccountKey; | ||
}; | ||
|
||
const amountTransformer = (value: string) => | ||
value | ||
.replace(/[^0-9\.]/g, '') // remove all non-numeric characters | ||
.replace(/(?<=\..*)\./g, '') // keep only first appearance of the '.' symbol | ||
.replace(/(?<=^0+)0/g, ''); // remove all leading zeros except the first one | ||
|
||
export const SendForm = ({ accountKey }: SendFormProps) => { | ||
const account = useSelector((state: AccountsRootState) => | ||
selectAccountByKey(state, accountKey), | ||
); | ||
|
||
const networkFeeInfo = useSelector((state: FeesRootState) => | ||
selectNetworkFeeInfo(state, account?.symbol), | ||
); | ||
|
||
const form = useForm<SendFormValues>({ | ||
validation: sendFormValidationSchema, | ||
context: { | ||
networkFeeInfo, | ||
networkSymbol: account?.symbol, | ||
availableAccountBalance: account?.availableBalance, | ||
}, | ||
defaultValues: { | ||
address: '', | ||
amount: '', | ||
}, | ||
}); | ||
|
||
const { | ||
handleSubmit, | ||
formState: { isValid }, | ||
} = form; | ||
|
||
const handleValidateForm = handleSubmit(() => { | ||
// TODO: start on-device inputs validation via TrezorConnect | ||
}); | ||
|
||
return ( | ||
<Form form={form}> | ||
<VStack spacing="medium" padding="medium"> | ||
<TextInputField | ||
multiline | ||
label="Address" | ||
name="address" | ||
maxLength={formInputsMaxLength.address} | ||
accessibilityLabel="address input" | ||
autoCapitalize="none" | ||
testID="@send/address-input" | ||
/> | ||
<TextInputField | ||
label="Amount to send" | ||
name="amount" | ||
keyboardType="numeric" | ||
accessibilityLabel="amount to send input" | ||
testID="@send/amount-input" | ||
valueTransformer={amountTransformer} | ||
/> | ||
<VStack> | ||
<Button | ||
accessibilityRole="button" | ||
accessibilityLabel="validate send form" | ||
testID="@send/form-submit-button" | ||
onPress={handleValidateForm} | ||
> | ||
Validate form | ||
</Button> | ||
</VStack> | ||
{/* TODO: remove this message in followup PR */} | ||
{isValid && <Text color="textSecondaryHighlight">Form is valid 🎉</Text>} | ||
</VStack> | ||
</Form> | ||
); | ||
}; |
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,120 @@ | ||
import { G } from '@mobily/ts-belt'; | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { NetworkSymbol } from '@suite-common/wallet-config'; | ||
import { formatNetworkAmount, isAddressValid } from '@suite-common/wallet-utils'; | ||
import { FeeInfo } from '@suite-common/wallet-types'; | ||
import { yup } from '@suite-common/validators'; | ||
|
||
export type SendFormFormContext = { | ||
networkSymbol?: NetworkSymbol; | ||
availableAccountBalance?: string; | ||
networkFeeInfo?: FeeInfo; | ||
}; | ||
|
||
const isAmountDust = ({ | ||
value, | ||
networkSymbol, | ||
isValueInSats, | ||
networkFeeInfo, | ||
}: { | ||
value: string; | ||
networkSymbol: NetworkSymbol; | ||
isValueInSats: boolean; | ||
networkFeeInfo: FeeInfo; | ||
}) => { | ||
const valueBigNumber = new BigNumber(value); | ||
const rawDust = networkFeeInfo.dustLimit?.toString(); | ||
|
||
const dustThreshold = | ||
rawDust && (isValueInSats ? rawDust : formatNetworkAmount(rawDust, networkSymbol)); | ||
|
||
if (!dustThreshold) { | ||
return false; | ||
} | ||
|
||
return valueBigNumber.lt(dustThreshold); | ||
}; | ||
|
||
const isAmountHigherThanBalance = ({ | ||
value, | ||
networkSymbol, | ||
isValueInSats, | ||
availableAccountBalance, | ||
}: { | ||
value: string; | ||
networkSymbol: NetworkSymbol; | ||
isValueInSats: boolean; | ||
availableAccountBalance: string; | ||
}) => { | ||
const formattedAvailableBalance = isValueInSats | ||
? availableAccountBalance | ||
: formatNetworkAmount(availableAccountBalance, networkSymbol); | ||
|
||
const valueBig = new BigNumber(value); | ||
|
||
return valueBig.gt(formattedAvailableBalance); | ||
}; | ||
|
||
// TODO: change error messages copy when is design ready | ||
export const sendFormValidationSchema = yup.object({ | ||
address: yup | ||
.string() | ||
.required() | ||
.test( | ||
'is-invalid-address', | ||
'Address is not valid.', | ||
(value, { options: { context } }: yup.TestContext<SendFormFormContext>) => { | ||
const networkSymbol = context?.networkSymbol; | ||
|
||
return ( | ||
G.isNotNullable(value) && | ||
G.isNotNullable(networkSymbol) && | ||
isAddressValid(value, networkSymbol) | ||
); | ||
}, | ||
), | ||
amount: yup | ||
.string() | ||
.required() | ||
.matches(/^\d+.*\d+$/, 'Invalid decimal value.') | ||
.test( | ||
'is-dust-amount', | ||
'The value is lower than dust threshold.', | ||
(value, { options: { context } }: yup.TestContext<SendFormFormContext>) => { | ||
if (!context || !value) return false; | ||
const { networkSymbol, networkFeeInfo } = context; | ||
|
||
if (!networkSymbol || !networkFeeInfo) return false; | ||
|
||
return !isAmountDust({ | ||
value, | ||
networkSymbol, | ||
isValueInSats: false, | ||
networkFeeInfo, | ||
}); | ||
}, | ||
) | ||
.test( | ||
'is-higher-than-balance', | ||
'Amount is higher than available balance.', | ||
(value, { options: { context } }: yup.TestContext<SendFormFormContext>) => { | ||
if (!context || !value) return false; | ||
const { networkSymbol, networkFeeInfo, availableAccountBalance } = context; | ||
|
||
if (!networkSymbol || !networkFeeInfo || !availableAccountBalance) return false; | ||
|
||
return !isAmountHigherThanBalance({ | ||
value, | ||
networkSymbol, | ||
isValueInSats: false, | ||
availableAccountBalance, | ||
}); | ||
}, | ||
), | ||
// TODO: other validations have to be added in the following PRs | ||
// 1) validation of token amount | ||
// 2) check if the amount is not higher than XRP reserve | ||
}); | ||
|
||
export type SendFormValues = yup.InferType<typeof sendFormValidationSchema>; |
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.