Skip to content

Commit

Permalink
fix: Use unchecked signer for dispatch calls
Browse files Browse the repository at this point in the history
  • Loading branch information
usame-algan committed May 28, 2024
1 parent d5e2c17 commit 960bc34
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 29 deletions.
1 change: 1 addition & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
2 changes: 1 addition & 1 deletion src/components/common/BuyCryptoButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const useOnrampAppUrl = (): string | undefined => {

const useBuyCryptoHref = (): LinkProps['href'] | undefined => {
const query = useSearchParams()
const safe = query.get('safe')
const safe = query?.get('safe')
const appUrl = useOnrampAppUrl()

return useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/SafeTokenWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const SafeTokenWidget = () => {

const url = {
pathname: AppRoutes.apps.open,
query: { safe: query.get('safe'), appUrl: GOVERNANCE_APP_URL },
query: { safe: query?.get('safe'), appUrl: GOVERNANCE_APP_URL },
}

const canRedeemSep5 = canRedeemSep5Airdrop(allocationData)
Expand Down
2 changes: 1 addition & 1 deletion src/components/tx-flow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const TxModalProvider = ({ children }: { children: ReactNode }): ReactEle
const safeId = useChainId() + useSafeAddress()
const prevSafeId = useRef<string>(safeId ?? '')
const pathname = usePathname()
const prevPathname = useRef<string>(pathname)
const prevPathname = useRef<string | null>(pathname)

const handleModalClose = useCallback(() => {
if (shouldWarn.current && !confirmClose()) {
Expand Down
11 changes: 2 additions & 9 deletions src/features/recovery/services/recovery-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import type { OnboardAPI } from '@web3-onboard/core'
import type { TransactionAddedEvent } from '@gnosis.pm/zodiac/dist/cjs/types/Delay'
import type { TransactionResponse } from 'ethers'

import { createWeb3 } from '@/hooks/wallets/web3'
import { didReprice, didRevert } from '@/utils/ethers-utils'
import { recoveryDispatch, RecoveryEvent, RecoveryTxType } from './recoveryEvents'
import { asError } from '@/services/exceptions/utils'
import { assertWalletChain } from '../../../services/tx/tx-sender/sdk'
import { assertWalletChain, getUncheckedSigner } from '../../../services/tx/tx-sender/sdk'
import { isSmartContractWallet } from '@/utils/wallets'
import { UncheckedJsonRpcSigner } from '@/utils/providers/UncheckedJsonRpcSigner'

async function getDelayModifierContract({
onboard,
Expand All @@ -25,14 +23,9 @@ async function getDelayModifierContract({
// Switch signer to chain of Safe
const wallet = await assertWalletChain(onboard, chainId)

const provider = createWeb3(wallet.provider)
const isSmartContract = await isSmartContractWallet(wallet.chainId, wallet.address)

const originalSigner = await provider.getSigner()
// Use unchecked signer for smart contract wallets as transactions do not necessarily immediately execute
const signer = isSmartContract
? new UncheckedJsonRpcSigner(provider, await originalSigner.getAddress())
: originalSigner
const signer = await getUncheckedSigner(wallet.provider)
const delayModifier = getModuleInstance(KnownContracts.DELAY, delayModifierAddress, signer).connect(signer)

return {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useIsSidebarRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const TOGGLE_SIDEBAR_ROUTES = [AppRoutes.apps.open]
*/
export function useIsSidebarRoute(pathname?: string): [boolean, boolean] {
const clientPathname = usePathname()
const route = pathname || clientPathname
const route = pathname || clientPathname || ''
const noSidebar = NO_SIDEBAR_ROUTES.includes(route)
const toggledSidebar = TOGGLE_SIDEBAR_ROUTES.includes(route)
const router = useRouter()
Expand Down
16 changes: 7 additions & 9 deletions src/services/tx/tx-sender/dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { UncheckedJsonRpcSigner } from '@/utils/providers/UncheckedJsonRpcSigner'
import { relayTransaction, type SafeInfo, type TransactionDetails } from '@safe-global/safe-gateway-typescript-sdk'
import type { SafeTransaction, TransactionOptions, TransactionResult } from '@safe-global/safe-core-sdk-types'
import { didRevert } from '@/utils/ethers-utils'
Expand All @@ -17,8 +16,9 @@ import {
getUncheckedSafeSDK,
assertWalletChain,
tryOffChainTxSigning,
getUncheckedSigner,
} from './sdk'
import { createWeb3, getUserNonce, getWeb3ReadOnly } from '@/hooks/wallets/web3'
import { getUserNonce, getWeb3ReadOnly } from '@/hooks/wallets/web3'
import { type OnboardAPI } from '@web3-onboard/core'
import { asError } from '@/services/exceptions/utils'
import chains from '@/config/chains'
Expand Down Expand Up @@ -187,12 +187,11 @@ export const dispatchCustomTxSpeedUp = async (
const eventParams = { txId }
const wallet = await assertWalletChain(onboard, chainId)
const signerNonce = txOptions.nonce
const web3Provider = createWeb3(wallet.provider)
const signer = await web3Provider.getSigner()

// Execute the tx
let result: TransactionResponse | undefined
try {
const signer = await getUncheckedSigner(wallet.provider)
result = await signer.sendTransaction({ to, data, ...txOptions })
txDispatch(TxEvent.EXECUTING, eventParams)
} catch (error) {
Expand Down Expand Up @@ -290,10 +289,9 @@ export const dispatchBatchExecution = async (
if (signerNonce === undefined || signerNonce === null) {
signerNonce = await getUserNonce(signerAddress)
}
const provider = createWeb3(wallet.provider)
const uncheckedJsonRpcSigner = new UncheckedJsonRpcSigner(provider, (await provider.getSigner()).address)
const signer = await getUncheckedSigner(wallet.provider)

result = await multiSendContract.contract.connect(uncheckedJsonRpcSigner).multiSend(multiSendTxData, overrides)
result = await multiSendContract.contract.connect(signer).multiSend(multiSendTxData, overrides)

txIds.forEach((txId) => {
txDispatch(TxEvent.EXECUTING, { txId, groupKey })
Expand Down Expand Up @@ -341,8 +339,8 @@ export const dispatchSpendingLimitTxExecution = async (
let result: ContractTransactionResponse | undefined
try {
const wallet = await assertWalletChain(onboard, chainId)
const provider = createWeb3(wallet.provider)
const contract = getSpendingLimitContract(chainId, await provider.getSigner())
const signer = await getUncheckedSigner(wallet.provider)
const contract = getSpendingLimitContract(chainId, signer)

result = await contract.executeAllowanceTransfer(
txParams.safeAddress,
Expand Down
7 changes: 6 additions & 1 deletion src/services/tx/tx-sender/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getSafeSDK } from '@/hooks/coreSDK/safeCoreSDK'
import type Safe from '@safe-global/protocol-kit'
import { EthersAdapter, SigningMethod } from '@safe-global/protocol-kit'
import type { JsonRpcSigner } from 'ethers'
import type { Eip1193Provider, JsonRpcSigner } from 'ethers'
import { ethers } from 'ethers'
import { isWalletRejection, isHardwareWallet, isWalletConnect } from '@/utils/wallets'
import { OperationType, type SafeTransaction } from '@safe-global/safe-core-sdk-types'
Expand Down Expand Up @@ -120,6 +120,11 @@ export const getAssertedChainSigner = async (
return provider.getSigner()
}

export const getUncheckedSigner = async (provider: Eip1193Provider) => {
const browserProvider = createWeb3(provider)
return new UncheckedJsonRpcSigner(browserProvider, (await browserProvider.getSigner()).address)
}

/**
* https://docs.ethers.io/v5/api/providers/jsonrpc-provider/#UncheckedJsonRpcSigner
* This resolves the promise sooner when executing a tx and mocks
Expand Down
36 changes: 30 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "es2020",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -16,11 +20,31 @@
"incremental": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"@/public/*": ["./public/*"]
"@/*": [
"./src/*"
],
"@/public/*": [
"./public/*"
]
},
"plugins": [{ "name": "typescript-plugin-css-modules" }]
"plugins": [
{
"name": "typescript-plugin-css-modules"
},
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", "src/definitions.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "src/types/contracts"]
"include": [
"next-env.d.ts",
"src/definitions.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules",
"src/types/contracts"
]
}

0 comments on commit 960bc34

Please sign in to comment.