Skip to content

Commit

Permalink
fix: wagmi transaction auto close (#1854)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir authored Feb 5, 2024
1 parent cc592f2 commit 9c4f81c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
7 changes: 2 additions & 5 deletions packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import {
} from '@web3modal/scaffold-utils/ethers'
import type { EthereumProviderOptions } from '@walletconnect/ethereum-provider'
import type { Eip1193Provider } from 'ethers'
import { W3mFrameRpcConstants, W3mFrameProvider } from '@web3modal/wallet'
import type { W3mFrameTypes } from '@web3modal/wallet'
import { W3mFrameProvider, W3mFrameHelpers } from '@web3modal/wallet'
import type { CombinedProvider } from '@web3modal/scaffold-utils/ethers'

// -- Types ---------------------------------------------------------------------
Expand Down Expand Up @@ -748,10 +747,8 @@ export class Web3Modal extends Web3ModalScaffold {
private watchEmail() {
if (this.emailProvider) {
this.emailProvider.onRpcRequest(request => {
const req = request as W3mFrameTypes.AppEvent & { payload?: unknown }
const payload = req.payload as W3mFrameTypes.RPCRequest
// We only open the modal if it's not a safe (auto-approve)
if (!W3mFrameRpcConstants.SAFE_RPC_METHODS.includes(payload.method)) {
if (!W3mFrameHelpers.checkIfRequestIsAllowed(request)) {
super.open({ view: 'ApproveTransaction' })
}
})
Expand Down
7 changes: 5 additions & 2 deletions packages/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
getEmailCaipNetworks,
getWalletConnectCaipNetworks
} from './utils/helpers.js'
import { W3mFrameHelpers } from '@web3modal/wallet'
import type { W3mFrameProvider } from '@web3modal/wallet'
import { ConstantsUtil as CoreConstants } from '@web3modal/core'
import type { defaultWagmiConfig as coreConfig } from './utils/defaultWagmiCoreConfig.js'
Expand Down Expand Up @@ -390,8 +391,10 @@ export class Web3Modal extends Web3ModalScaffold {
const provider = (await connector.getProvider()) as W3mFrameProvider
const isLoginEmailUsed = provider.getLoginEmailUsed()
super.setLoading(isLoginEmailUsed)
provider.onRpcRequest(() => {
super.open({ view: 'ApproveTransaction' })
provider.onRpcRequest(request => {
if (!W3mFrameHelpers.checkIfRequestIsAllowed(request)) {
super.open({ view: 'ApproveTransaction' })
}
})
provider.onRpcResponse(() => {
super.close()
Expand Down
9 changes: 8 additions & 1 deletion packages/wallet/src/W3mFrameHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { W3mFrameStorage } from './W3mFrameStorage.js'
import { W3mFrameConstants } from './W3mFrameConstants.js'
import { W3mFrameConstants, W3mFrameRpcConstants } from './W3mFrameConstants.js'
import type { W3mFrameTypes } from './W3mFrameTypes.js'

const RESTRICTED_TIMEZONES = [
'ASIA/SHANGHAI',
Expand Down Expand Up @@ -53,5 +54,11 @@ export const W3mFrameHelpers = {
return 0
},

checkIfRequestIsAllowed(request: unknown) {
const method = (request as { payload: W3mFrameTypes.RPCRequest })?.payload?.method

return W3mFrameRpcConstants.SAFE_RPC_METHODS.includes(method)
},

isClient: typeof window !== 'undefined'
}
2 changes: 1 addition & 1 deletion scripts/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const __dirname = path.dirname(__filename)
/**
* Reads the coverage-summary.{XXX}.json file and returns the parsed JSON object
* @param {*} pathToReport
* @returns {Object | null} parsed JSON object
* @returns {Object} parsed JSON object
*/
function readPreviousCoverageSummary(pathToReport) {
if (!pathToReport) {
Expand Down

0 comments on commit 9c4f81c

Please sign in to comment.