Skip to content

Commit

Permalink
fix: pending transaction listener infinite requests (#3443)
Browse files Browse the repository at this point in the history
Co-authored-by: tomiir <rocchitomas@gmail.com>
  • Loading branch information
magiziz and tomiir authored Dec 11, 2024
1 parent a2cf0d9 commit 53ecc19
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 107 deletions.
22 changes: 22 additions & 0 deletions .changeset/tame-walls-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Fixed an issue where pending transaction listener was causing infinite request.
24 changes: 0 additions & 24 deletions packages/adapters/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
type Connector,
type ConnectorType,
type Provider,
AlertController,
CoreHelperUtil
} from '@reown/appkit-core'
import { ConstantsUtil, PresetsUtil } from '@reown/appkit-utils'
Expand All @@ -18,7 +17,6 @@ import { CoinbaseWalletSDK, type ProviderInterface } from '@coinbase/wallet-sdk'
import type { W3mFrameProvider } from '@reown/appkit-wallet'
import { EthersMethods } from './utils/EthersMethods.js'
import { ProviderUtil } from '@reown/appkit/store'
import { BrowserProvider } from 'ethers'

export interface EIP6963ProviderDetail {
info: Connector['info']
Expand Down Expand Up @@ -481,26 +479,6 @@ export class EthersAdapter extends AdapterBlueprint {
return { profileName: undefined, profileImage: undefined }
}

private listenPendingTransactions(provider: Provider) {
const browserProvider = new BrowserProvider(provider)

try {
browserProvider.on('pending', () => {
this.emit('pendingTransactions')
})
} catch (error) {
// eslint-disable-next-line no-console
AlertController.open(
{
shortMessage: 'Error listening to pending transactions',
longMessage:
'The BrowserProvider in the EthersAdapter failed to listen to pending transactions.'
},
'error'
)
}
}

private providerHandlers: {
disconnect: () => void
accountsChanged: (accounts: string[]) => void
Expand Down Expand Up @@ -528,8 +506,6 @@ export class EthersAdapter extends AdapterBlueprint {
this.emit('switchNetwork', { chainId: chainIdNumber })
}

this.listenPendingTransactions(provider)

provider.on('disconnect', disconnectHandler)
provider.on('accountsChanged', accountsChangedHandler)
provider.on('chainChanged', chainChangedHandler)
Expand Down
32 changes: 1 addition & 31 deletions packages/adapters/ethers/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CaipNetworksUtil } from '@reown/appkit-utils'
import type { Provider } from '@reown/appkit-core'
import type { W3mFrameProvider } from '@reown/appkit-wallet'
import UniversalProvider from '@walletconnect/universal-provider'
import { JsonRpcProvider, InfuraProvider, BrowserProvider } from 'ethers'
import { JsonRpcProvider, InfuraProvider } from 'ethers'
import { mainnet } from '@reown/appkit/networks'
import { EthersMethods } from '../utils/EthersMethods'
import { ProviderUtil } from '@reown/appkit/store'
Expand Down Expand Up @@ -453,34 +453,4 @@ describe('EthersAdapter', () => {
expect(result).toBe('0x123')
})
})

describe('EthersAdapter - ListenPendingTransactions', () => {
it('should listen for pending transactions and emit event', () => {
const adapter = new EthersAdapter()
const mockProvider = {
request: vi.fn(),
on: vi.fn(),
removeListener: vi.fn(),
send: vi.fn(),
sendAsync: vi.fn()
} as unknown as Provider

const emitSpy = vi.spyOn(adapter, 'emit' as any)

vi.mocked(BrowserProvider).mockImplementation(
() =>
({
on: vi.fn((event, callback) => {
if (event === 'pending') {
callback()
}
})
}) as any
)
;(adapter as any).listenPendingTransactions(mockProvider)

expect(BrowserProvider).toHaveBeenCalledWith(mockProvider)
expect(emitSpy).toHaveBeenCalledWith('pendingTransactions')
})
})
})
22 changes: 0 additions & 22 deletions packages/adapters/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AdapterBlueprint } from '@reown/appkit/adapters'
import type { CaipNetwork } from '@reown/appkit-common'
import { ConstantsUtil as CommonConstantsUtil } from '@reown/appkit-common'
import {
AlertController,
CoreHelperUtil,
type CombinedProvider,
type Connector,
Expand Down Expand Up @@ -484,25 +483,6 @@ export class Ethers5Adapter extends AdapterBlueprint {
return { profileName: undefined, profileImage: undefined }
}

private listenPendingTransactions(provider: Provider) {
const web3Provider = new ethers.providers.Web3Provider(provider)

try {
web3Provider.on('pending', () => {
this.emit('pendingTransactions')
})
} catch (error) {
AlertController.open(
{
shortMessage: 'Error listening to pending transactions',
longMessage:
'The Web3Provider in the Ethers5Adapter failed to listen to pending transactions.'
},
'error'
)
}
}

private providerHandlers: {
disconnect: () => void
accountsChanged: (accounts: string[]) => void
Expand Down Expand Up @@ -530,8 +510,6 @@ export class Ethers5Adapter extends AdapterBlueprint {
this.emit('switchNetwork', { chainId: chainIdNumber })
}

this.listenPendingTransactions(provider)

provider.on('disconnect', disconnectHandler)
provider.on('accountsChanged', accountsChangedHandler)
provider.on('chainChanged', chainChangedHandler)
Expand Down
30 changes: 0 additions & 30 deletions packages/adapters/ethers5/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,34 +407,4 @@ describe('Ethers5Adapter', () => {
expect(result).toBe('0x123')
})
})

describe('Ethers5Adapter - ListenPendingTransactions', () => {
it('should listen for pending transactions and emit event', () => {
const adapter = new Ethers5Adapter()
const mockProvider = {
request: vi.fn(),
on: vi.fn(),
removeListener: vi.fn(),
send: vi.fn(),
sendAsync: vi.fn()
} as unknown as Provider

const emitSpy = vi.spyOn(adapter, 'emit' as any)

vi.mocked(providers.Web3Provider).mockImplementation(
() =>
({
on: vi.fn((event, callback) => {
if (event === 'pending') {
callback()
}
})
}) as any
)
;(adapter as any).listenPendingTransactions(mockProvider)

expect(providers.Web3Provider).toHaveBeenCalledWith(mockProvider)
expect(emitSpy).toHaveBeenCalledWith('pendingTransactions')
})
})
})
1 change: 1 addition & 0 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export class WagmiAdapter extends AdapterBlueprint {

private setupWatchers() {
watchPendingTransactions(this.wagmiConfig, {
pollingInterval: 15_000,
/* Magic RPC does not support the pending transactions. We handle transaction for the AuthConnector cases in AppKit client to handle all clients at once. Adding the onError handler to avoid the error to throw. */
// eslint-disable-next-line @typescript-eslint/no-empty-function
onError: () => {},
Expand Down

0 comments on commit 53ecc19

Please sign in to comment.