diff --git a/src/screens/ApproveTransfer.tsx b/src/screens/ApproveTransfer.tsx index 27a912e..a7ef4f9 100644 --- a/src/screens/ApproveTransfer.tsx +++ b/src/screens/ApproveTransfer.tsx @@ -46,29 +46,22 @@ export const MEMO = 'Sending signed tx from Laconic Wallet'; // Reference: https://ethereum.org/en/developers/docs/gas/#what-is-gas-limit const ETH_MINIMUM_GAS = 21000; -type ApproveTransferProps = NativeStackScreenProps & { - route: { - params: { - transaction: any; - requestEvent?: { - params: { - chainId: string; - request: { - method: string; - }; - }; - }; - requestSessionData?: any; - chainId?: string; - }; - path?: string; - }; -}; +type ApproveTransferProps = NativeStackScreenProps const ApproveTransfer = ({ route }: ApproveTransferProps) => { const { networksData } = useNetworks(); const { web3wallet } = useWalletConnect(); + // Extract data from route params or path + const requestSession = route.params?.requestSessionData; + const requestName = requestSession?.peer?.metadata?.name; + const requestIcon = requestSession?.peer?.metadata?.icons?.[0]; + const requestURL = requestSession?.peer?.metadata?.url; + const transaction = route.params?.transaction; + const requestEvent = route.params?.requestEvent; + const chainId = requestEvent?.params?.chainId || route.params?.chainId; + const requestMethod = requestEvent?.params?.request?.method; + const [account, setAccount] = useState(); const [isLoading, setIsLoading] = useState(true); const [balance, setBalance] = useState(''); @@ -85,80 +78,6 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => { const [ethMaxPriorityFee, setEthMaxPriorityFee] = useState(); - const navigation = - useNavigation>(); - - // Extract data from route params or path - const requestSession = route.params?.requestSessionData; - const requestName = requestSession?.peer?.metadata?.name; - const requestIcon = requestSession?.peer?.metadata?.icons?.[0]; - const requestURL = requestSession?.peer?.metadata?.url; - const transaction = route.params?.transaction; - const requestEvent = route.params?.requestEvent; - const chainId = requestEvent?.params?.chainId || route.params?.chainId; - const requestMethod = requestEvent?.params?.request?.method; - - const sanitizePath = useCallback((path: string) => { - const regex = /^\/transfer\/(eip155|cosmos)\/(.+)\/(.+)\/(.+)\/(.+)$/; - const match = path.match(regex); - if (match) { - const [, pathNamespace, pathChainId, pathAddress, pathTo, pathAmount] = match; - return { - namespace: pathNamespace, - chainId: pathChainId, - address: pathAddress, - to: pathTo, - amount: pathAmount, - }; - } else { - navigation.navigate('InvalidPath'); - } - return null; - }, [navigation]); - - const retrieveData = useCallback(async (requestNamespace: string, requestChainId: string, requestAddress: string) => { - const requestAccount = await retrieveSingleAccount( - requestNamespace, - requestChainId, - requestAddress, - ); - if (!requestAccount) { - navigation.navigate('InvalidPath'); - return; - } - - setAccount(requestAccount); - }, [navigation]); - - useEffect(() => { - if (route.path) { - const sanitizedRoute = sanitizePath(route.path); - if (sanitizedRoute) { - retrieveData( - sanitizedRoute.namespace, - sanitizedRoute.chainId, - sanitizedRoute.address, - ); - return; - } - } - - if (requestEvent) { - const requestedNetwork = networksData.find( - networkData => { - return `${networkData.namespace}:${networkData.chainId}` === chainId; - } - ); - if (requestedNetwork && transaction?.from) { - retrieveData( - requestedNetwork.namespace, - requestedNetwork.chainId, - transaction.from, - ); - } - } - }, [retrieveData, sanitizePath, route, networksData, requestEvent, chainId, transaction]); - const isSufficientFunds = useMemo(() => { if (!transaction.value) { return; @@ -218,7 +137,7 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => { ).privKey; const sender = await DirectSecp256k1Wallet.fromKey( - Uint8Array.from(Buffer.from(cosmosPrivKey.split('0x')[1], 'hex')), + Buffer.from(cosmosPrivKey.split('0x')[1], 'hex'), requestedNetwork?.addressPrefix, ); @@ -264,6 +183,44 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => { } }, [requestedNetwork, namespace]); + const navigation = + useNavigation>(); + + const sanitizePath = useCallback((path: string) => { + const regex = /^\/transfer\/(eip155|cosmos)\/(.+)\/(.+)\/(.+)\/(.+)$/; + const match = path.match(regex); + if (match) { + const [, pathNamespace, pathChainId, pathAddress, pathTo, pathAmount] = match; + return { + namespace: pathNamespace, + chainId: pathChainId, + address: pathAddress, + to: pathTo, + amount: pathAmount, + }; + } else { + navigation.navigate('InvalidPath'); + } + return null; + }, [navigation]); + + const retrieveData = useCallback( + async (requestAddress: string) => { + const requestAccount = await retrieveSingleAccount( + requestedNetwork!.namespace, + requestedNetwork!.chainId, + requestAddress, + ); + if (!requestAccount) { + navigation.navigate('InvalidPath'); + return; + } + + setAccount(requestAccount); + }, + [navigation, requestedNetwork], + ); + useEffect(() => { // Set loading to false when gas values for requested chain are fetched // If requested chain is EVM compatible, the cosmos gas values will be undefined and vice-versa, hence the condition checks only one of them at the same time @@ -306,6 +263,10 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => { ethMaxFee, ]); + useEffect(() => { + retrieveData(transaction.from!); + }, [retrieveData, transaction]); + const isEIP1559 = useMemo(() => { if (cosmosGasLimit) { return; diff --git a/src/types.ts b/src/types.ts index ae93310..8af325f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,6 +21,7 @@ export type StackParamsList = { requestSessionData?: SessionTypes.Struct; }; ApproveTransfer: { + chainId?: string; transaction: PopulatedTransaction; requestEvent: Web3WalletTypes.SessionRequest; requestSessionData: SessionTypes.Struct;