Skip to content

Commit

Permalink
handleSendTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonmanRolls committed Dec 7, 2024
1 parent 1535ceb commit 356293c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next'
import styled, { css } from 'styled-components'
import { match, P } from 'ts-pattern'
import { BaseError } from 'viem'
import { useAccount, useClient, useConnectorClient, useSendTransaction } from 'wagmi'
import { useClient, useConnectorClient, useSendTransaction } from 'wagmi'

import {
Button,
Expand Down Expand Up @@ -43,6 +43,7 @@ import { makeEtherscanLink } from '@app/utils/utils'
import { DisplayItems } from '../DisplayItems'
import {
createTransactionRequestQueryFn,
createTransactionRequestUnsafe,
getTransactionErrorQueryFn,
getUniqueTransaction,
transactionSuccessHandler,
Expand Down Expand Up @@ -307,6 +308,31 @@ const getPreTransactionError = ({
})
}

export const handleSendTransaction = async (
request: Awaited<ReturnType<typeof createTransactionRequestUnsafe>>,
actionName: string,
trackEvent: ReturnType<typeof useEventTracker>['trackEvent'],
sendTransaction: ReturnType<typeof useSendTransaction>['sendTransaction'],
) => {
if (!request) {
throw Error('No request object')
}
if (request?.chainId) {
await switchChain(wagmiConfig, { chainId: request?.chainId })
}
sendTransaction(request)

const eventName = match(actionName)
.with('commitName', () => 'commit_wallet_opened' as const)
.with('registerName', () => 'register_wallet_opened' as const)
.with('approveDnsRegistrar', () => 'dns_approve_registrar_wallet_opened' as const)
.with('importDnsName', () => 'dns_import_wallet_opened' as const)
.with('claimDnsName', () => 'dns_claim_wallet_opened' as const)
.otherwise(() => undefined)

if (eventName) trackEvent({ eventName })
}

export const TransactionStageModal = ({
actionName,
currentStep,
Expand All @@ -325,7 +351,6 @@ export const TransactionStageModal = ({
const { data: isSafeApp, isLoading: safeAppStatusLoading } = useIsSafeApp()
const { data: connectorClient } = useConnectorClient<ConfigWithEns>()
const client = useClient()
const { data: account } = useAccount()
const addRecentTransaction = useAddRecentTransaction()

const stage = transaction.stage || 'confirm'
Expand Down Expand Up @@ -506,7 +531,7 @@ export const TransactionStageModal = ({
if (stage === 'failed') {
return (
<Button
onClick={() => sendTransaction(request!)}
onClick={() => handleSendTransaction(request, actionName, trackEvent, sendTransaction)}
disabled={!canEnableTransactionRequest || requestLoading || !request}
colorStyle="redSecondary"
data-testid="transaction-modal-failed-button"
Expand Down Expand Up @@ -550,21 +575,7 @@ export const TransactionStageModal = ({
!!requestError ||
isTransactionRequestCachedData
}
onClick={async () => {
if (account?.chain?.id !== request?.chainId) {
await switchChain(wagmiConfig, { chainId: request?.chainId })
}
sendTransaction(request!)

const eventName = match(actionName)
.with('commitName', () => 'commit_wallet_opened' as const)
.with('registerName', () => 'register_wallet_opened' as const)
.with('approveDnsRegistrar', () => 'dns_approve_registrar_wallet_opened' as const)
.with('importDnsName', () => 'dns_import_wallet_opened' as const)
.with('claimDnsName', () => 'dns_claim_wallet_opened' as const)
.otherwise(() => undefined)
if (eventName) trackEvent({ eventName })
}}
onClick={() => handleSendTransaction(request, actionName, trackEvent, sendTransaction)}
data-testid="transaction-modal-confirm-button"
>
{t('transaction.dialog.confirm.openWallet')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const calculateGasLimit = async ({
}
}

type CreateTransactionRequestQueryKey = CreateQueryKey<
export type CreateTransactionRequestQueryKey = CreateQueryKey<
UniqueTransaction,
'createTransactionRequest',
'standard'
Expand All @@ -175,7 +175,7 @@ type CreateTransactionRequestUnsafeParameters = {
chainId: SupportedChain['id']
}

const createTransactionRequestUnsafe = async ({
export const createTransactionRequestUnsafe = async ({
client,
connectorClient,
isSafeApp,
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ export function useQueryOptions<
const { address } = useAccount()
const config = useConfig()

// console.log('chainId: ', chainId)

if (queryDependencyType === 'independent')
return {
queryKey: createQueryKey({ params, scopeKey, functionName, queryDependencyType }),
Expand Down
28 changes: 10 additions & 18 deletions src/utils/query/wagmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ import { holesky, localhost, mainnet, sepolia } from 'wagmi/chains'

import { ccipRequest } from '@ensdomains/ensjs/utils'

import {
holeskyWithEns,
localhostWithEns,
mainnetWithEns,
sepoliaWithEns,
} from '@app/constants/chains'

import { getChainFromSubdomain, getChainFromUrl } from '../utils'
import { localhostWithEns } from '@app/constants/chains'

import { getChainFromUrl } from '../utils'
import { rainbowKitConnectors } from './wallets'

const isLocalProvider = !!process.env.NEXT_PUBLIC_PROVIDER
Expand Down Expand Up @@ -78,12 +73,11 @@ const localStorageWithInvertMiddleware = (): Storage | undefined => {
}
}

const chains = [
...(isLocalProvider ? ([localhostWithEns] as const) : ([] as const)),
mainnetWithEns,
sepoliaWithEns,
holeskyWithEns,
] as const
const getChain = () => {
if (isLocalProvider) return localhostWithEns
const chain = getChainFromUrl()
return chain
}

const transports = {
...(isLocalProvider
Expand All @@ -105,10 +99,8 @@ const wagmiConfig_ = createConfig({
ssr: true,
multiInjectedProviderDiscovery: true,
storage: createStorage({ storage: localStorageWithInvertMiddleware(), key: prefix }),
chains,
client: () => {
const chain = getChainFromUrl()

chains: [getChain()],
client: ({ chain }) => {
const chainId = chain.id

return createClient<Transport, typeof chain>({
Expand Down

0 comments on commit 356293c

Please sign in to comment.