Skip to content

Commit

Permalink
US-2029 RIF Wallet - Socket error - [TypeError: Cannot set property '…
Browse files Browse the repository at this point in the history
…cache' of undefined] (#810)

* chore: upgrade @rsksmart/rif-wallet-services

* refactor: update usage of rifSockets in the app

* lint: fix unused errors
  • Loading branch information
TravellerOnTheRun authored Nov 20, 2023
1 parent b6b862e commit d92688e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@rsksmart/rif-wallet-bitcoin": "^1.2.0",
"@rsksmart/rif-wallet-core": "^1.0.2",
"@rsksmart/rif-wallet-eip681": "1.0.1",
"@rsksmart/rif-wallet-services": "^1.0.8",
"@rsksmart/rif-wallet-services": "^1.1.0",
"@rsksmart/rif-wallet-token": "^1.0.1",
"@rsksmart/rlogin-dpath": "^1.0.1",
"@rsksmart/rns-resolver.js": "^1.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/redux/slices/settingsSlice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const createWallet = createAsyncThunk<

// connect to sockets
rifSockets({
wallet: kms.rifWallet,
address: kms.rifWallet.smartWalletAddress,
fetcher: fetcherInstance,
dispatch: thunkAPI.dispatch,
setGlobalError: thunkAPI.rejectWithValue,
Expand Down Expand Up @@ -259,7 +259,7 @@ export const unlockApp = createAsyncThunk<

// connect to sockets
rifSockets({
wallet: rifWallet,
address: rifWallet.smartWalletAddress,
fetcher: fetcherInstance,
dispatch: thunkAPI.dispatch,
setGlobalError: thunkAPI.rejectWithValue,
Expand Down
5 changes: 2 additions & 3 deletions src/screens/activity/ActivityScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EnhancedResult } from '@rsksmart/rif-wallet-abi-enhancer'
import { RIFWallet } from '@rsksmart/rif-wallet-core'
import { IApiTransaction } from '@rsksmart/rif-wallet-services'
import { ethers } from 'ethers'
import { useContext, useEffect } from 'react'
Expand All @@ -21,6 +20,7 @@ import {
} from 'store/slices/transactionsSlice/selectors'
import { useAppDispatch, useAppSelector } from 'store/storeUtils'
import { WalletContext } from 'shared/wallet'
import { ChainTypesByIdType } from 'src/shared/constants/chainConstants'

import { ActivityBasicRow } from './ActivityRow'

Expand Down Expand Up @@ -116,10 +116,9 @@ const styles = StyleSheet.create({

export const enhanceTransactionInput = async (
transaction: IApiTransaction,
wallet: RIFWallet,
chainId: ChainTypesByIdType,
): Promise<EnhancedResult | null> => {
try {
const chainId = await wallet.getChainId()
const enhancedTx = await abiEnhancer.enhance(chainId, {
from: transaction.from.toLowerCase(),
to: transaction.to.toLowerCase(),
Expand Down
19 changes: 7 additions & 12 deletions src/subscriptions/onSocketChangeEmitted.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IApiTransaction } from '@rsksmart/rif-wallet-services'
import { EnhancedResult } from '@rsksmart/rif-wallet-abi-enhancer'
import { EnhancedResult, IAbiEnhancer } from '@rsksmart/rif-wallet-abi-enhancer'

import { resetSocketState } from 'store/shared/actions/resetSocketState'
import { addOrUpdateBalances } from 'store/slices/balancesSlice'
Expand All @@ -16,19 +16,21 @@ import { AppDispatch } from 'store/index'
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import { getCurrentChainId } from 'storage/ChainStorage'

import { AbiWallet, Action } from './types'
import { Action } from './types'

interface OnNewTransactionEventEmittedArgs extends AbiWallet {
interface OnNewTransactionEventEmittedArgs {
dispatch: AppDispatch
payload: IApiTransaction
usdPrices: UsdPricesState
chainId: ChainTypesByIdType
abiEnhancer: IAbiEnhancer
}

interface OnSocketChangeEmittedArgs extends AbiWallet {
interface OnSocketChangeEmittedArgs {
dispatch: AppDispatch
usdPrices: UsdPricesState
chainId: ChainTypesByIdType
abiEnhancer: IAbiEnhancer
}

const onNewTransactionEventEmitted = async ({
Expand Down Expand Up @@ -67,13 +69,7 @@ const onNewTransactionEventEmitted = async ({
}

export const onSocketChangeEmitted =
({
dispatch,
abiEnhancer,
wallet,
usdPrices,
chainId,
}: OnSocketChangeEmittedArgs) =>
({ dispatch, abiEnhancer, usdPrices, chainId }: OnSocketChangeEmittedArgs) =>
(action: Action) => {
// Temporal patch to avoid dispatching events if current chainId does not match
// @TODO find root cause of why the rifSockets is emitting an outdated event
Expand All @@ -92,7 +88,6 @@ export const onSocketChangeEmitted =
case 'newTransaction':
onNewTransactionEventEmitted({
abiEnhancer,
wallet,
dispatch,
payload,
usdPrices,
Expand Down
25 changes: 11 additions & 14 deletions src/subscriptions/rifSockets.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import EventEmitter from 'eventemitter3'
import { RIFWallet } from '@rsksmart/rif-wallet-core'
import {
ITokenWithBalance,
RifWalletServicesFetcher,
RifWalletServicesSocket,
} from '@rsksmart/rif-wallet-services'
import { Options, setInternetCredentials } from 'react-native-keychain'
import DeviceInfo from 'react-native-device-info'

import { resetSocketState } from 'store/shared/actions/resetSocketState'
Expand Down Expand Up @@ -35,12 +33,12 @@ export enum SocketsEvents {
}

interface RifSockets {
wallet: RIFWallet
address: string
chainId: ChainTypesByIdType
setGlobalError: (err: string) => void
dispatch: AppDispatch
usdPrices: UsdPricesState
fetcher: RifWalletServicesFetcher
chainId: ChainTypesByIdType
balances: Record<string, TokenBalanceObject>
}

Expand All @@ -52,25 +50,21 @@ const onSocketInit = (
}

export const rifSockets = ({
wallet,
address,
chainId,
fetcher,
dispatch,
setGlobalError,
usdPrices,
chainId,
balances,
}: RifSockets) => {
const onChange = onSocketChangeEmitted({
dispatch,
abiEnhancer,
wallet,
usdPrices,
chainId,
})
const rifWalletServicesSocket = new RifWalletServicesSocket<
Options,
ReturnType<typeof setInternetCredentials>
>(
const rifWalletServicesSocket = new RifWalletServicesSocket(
getWalletSetting(SETTINGS.RIF_WALLET_SERVICE_URL, chainTypesById[chainId]),
abiEnhancer,
{
Expand All @@ -79,10 +73,11 @@ export const rifSockets = ({
SETTINGS.RIF_WALLET_KEY,
chainTypesById[chainId],
),
// @TODO: make sure that the chainId type is more specific in lib
onEnhanceTransaction: enhanceTransactionInput,
onFilterOutRepeatedTransactions: filterEnhancedTransactions,
onBeforeInit: (encryptionKey, currentInstance) => {
currentInstance.cache = new MMKVStorage('txs', encryptionKey)
onBeforeInit: currentInstance => {
currentInstance.cache = new MMKVStorage('txs')
},
},
{
Expand Down Expand Up @@ -116,7 +111,9 @@ export const rifSockets = ({
)
rifWalletServicesSocket.on('change', onChange)
rifWalletServicesSocket
.connect(wallet, fetcher, { 'User-Agent': DeviceInfo.getUserAgentSync() })
.connect(address, chainId, fetcher, {
'User-Agent': DeviceInfo.getUserAgentSync(),
})
.catch(err => {
if (err instanceof Error) {
setGlobalError(err.message)
Expand Down
6 changes: 0 additions & 6 deletions src/subscriptions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ITokenWithBalance,
} from '@rsksmart/rif-wallet-services'
import { IAbiEnhancer, EnhancedResult } from '@rsksmart/rif-wallet-abi-enhancer'
import { RIFWallet } from '@rsksmart/rif-wallet-core'

export interface IPrice {
price: number
Expand Down Expand Up @@ -85,8 +84,3 @@ export interface TransactionsServerResponseWithActivityTransactions
extends TransactionsServerResponse {
activityTransactions: IActivityTransaction[]
}

export interface AbiWallet {
abiEnhancer: IAbiEnhancer
wallet: RIFWallet
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2131,10 +2131,10 @@
dependencies:
"@rsksmart/rsk-utils" "^1.1.0"

"@rsksmart/rif-wallet-services@^1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rsksmart/rif-wallet-services/-/rif-wallet-services-1.0.8.tgz#d4b9099c0c931c295dbac357d1a35f383700028f"
integrity sha512-sKXNT3PU6eDYKSBZxV1vLOA/N/lOYavGjw4Q12dWCjyT3xv7uQtAKIB+njYTyK0HxaKfKcR0+Agq/4KIdiAZ/w==
"@rsksmart/rif-wallet-services@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@rsksmart/rif-wallet-services/-/rif-wallet-services-1.1.0.tgz#5d981fcfc1d4015d41e1b11c293363bdd117246a"
integrity sha512-DHiW0j+MNrxzBKCgW6mSheiD7H38K4OIv2a0WSopXd/B4vswiOHY3zyZghmajNKqqzT87yD9AE0/+y8fHpu0RA==
dependencies:
"@ethersproject/contracts" "^5.7.0"
"@rsksmart/rif-wallet-abi-enhancer" "*"
Expand Down

0 comments on commit d92688e

Please sign in to comment.