From b1a0831e7892a74fa56763babcb7039d4bae16e9 Mon Sep 17 00:00:00 2001 From: shreerang6921 <68148922+shreerang6921@users.noreply.github.com> Date: Tue, 23 Apr 2024 10:22:30 +0530 Subject: [PATCH] Take gas limit and fees values from user while approving transaction (#109) * Take gas limit and fees from user * Update text input ui * Use gasPrice from networks data * Use default gas limit from env * Use default gas price if not found in registry * Remove appended denom in gas price * Use gas limit from env * Show error dialog when transaction fails * Calculate gas limit and gas price if not received from dapp * Update example env * Improve syntax --------- Co-authored-by: IshaVenikar --- .env.example | 2 + react-native-config.d.ts | 2 + src/components/TxErrorDialog.tsx | 28 +++ src/screens/AddNetwork.tsx | 36 ++++ src/screens/ApproveTransaction.tsx | 197 ++++++++++++++---- src/screens/EditNetwork.tsx | 29 ++- src/screens/SignRequest.tsx | 4 +- src/styles/stylesheet.js | 1 + src/types.ts | 1 + src/utils/accounts.ts | 5 +- src/utils/constants.ts | 1 + .../wallet-connect/WalletConnectRequests.ts | 58 +++--- 12 files changed, 290 insertions(+), 74 deletions(-) create mode 100644 src/components/TxErrorDialog.tsx diff --git a/.env.example b/.env.example index c039d12..9687d13 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,3 @@ WALLET_CONNECT_PROJECT_ID= +DEFAULT_GAS_LIMIT= +DEFAULT_GAS_PRICE= diff --git a/react-native-config.d.ts b/react-native-config.d.ts index db50fa4..8f78aab 100644 --- a/react-native-config.d.ts +++ b/react-native-config.d.ts @@ -2,6 +2,8 @@ declare module 'react-native-config' { export interface NativeConfig { WALLET_CONNECT_PROJECT_ID: string; + DEFAULT_GAS_LIMIT: string; + DEFAULT_GAS_PRICE: string; } export const Config: NativeConfig; diff --git a/src/components/TxErrorDialog.tsx b/src/components/TxErrorDialog.tsx new file mode 100644 index 0000000..e110760 --- /dev/null +++ b/src/components/TxErrorDialog.tsx @@ -0,0 +1,28 @@ +import React from 'react'; +import { Button, Dialog, Portal, Text } from 'react-native-paper'; + +const TxErrorDialog = ({ + error, + visible, + hideDialog, +}: { + error: string; + visible: boolean; + hideDialog: () => void; +}) => { + return ( + + + Error sending transaction + + {error} + + + + + + + ); +}; + +export default TxErrorDialog; diff --git a/src/screens/AddNetwork.tsx b/src/screens/AddNetwork.tsx index 0abf8d7..a2b5947 100644 --- a/src/screens/AddNetwork.tsx +++ b/src/screens/AddNetwork.tsx @@ -53,6 +53,10 @@ const cosmosNetworkDataSchema = z.object({ coinType: z.string().nonempty({ message: EMPTY_FIELD_ERROR }).regex(/^\d+$/), nativeDenom: z.string().nonempty({ message: EMPTY_FIELD_ERROR }), addressPrefix: z.string().nonempty({ message: EMPTY_FIELD_ERROR }), + gasPrice: z + .string() + .nonempty({ message: EMPTY_FIELD_ERROR }) + .regex(/^\d+(\.\d+)?$/), }); const AddNetwork = () => { @@ -113,6 +117,13 @@ const AddNetwork = () => { setValue('addressPrefix', cosmosChainDetails.bech32_prefix); setValue('coinType', String(cosmosChainDetails.slip44 ?? '118')); setValue('nativeDenom', cosmosChainDetails.fees?.fee_tokens[0].denom || ''); + setValue( + 'gasPrice', + String( + cosmosChainDetails.fees?.fee_tokens[0].average_gas_price || + String(process.env.DEFAULT_GAS_PRICE), + ), + ); }, CHAINID_DEBOUNCE_DELAY); const submit = useCallback( @@ -359,6 +370,31 @@ const AddNetwork = () => { )} /> + ( + <> + + + { + ( + errors as FieldErrors< + z.infer + > + ).gasPrice?.message + } + + + )} + /> )}