Skip to content

Commit

Permalink
fix: create signer just-in-time for transaction (#4587)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeolianeth authored Jan 19, 2025
1 parent 6a21f12 commit edcfde3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
7 changes: 2 additions & 5 deletions src/hooks/useLoadContractFromAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Contract, ContractInterface } from 'ethers'
import { isAddress } from 'ethers/lib/utils'
import { useEffect, useState } from 'react'
import { isZeroAddress } from 'utils/address'
import { useWallet } from './Wallet'

const isInputAddressValid = (
address: string | undefined,
Expand All @@ -23,15 +22,13 @@ export const useLoadContractFromAddress = <ABI extends ContractInterface>({
}) => {
const [contract, setContract] = useState<Contract>()

const { signer } = useWallet()

useEffect(() => {
if (!abi || !isInputAddressValid(address)) {
return setContract(undefined)
}

setContract(new Contract(address, abi, signer ?? readProvider))
}, [address, abi, signer])
setContract(new Contract(address, abi, readProvider))
}, [address, abi])

return contract
}
14 changes: 12 additions & 2 deletions src/hooks/useTransactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ export type TransactorInstance<T = undefined> = (
export function useTransactor(): Transactor | undefined {
const { addTransaction } = useContext(TxHistoryContext)

const { chain, signer, userAddress } = useWallet()
const { chain, userAddress } = useWallet()
const { chainUnsupported, isConnected, changeNetworks, connect } = useWallet()
const { signer } = useWallet()

return useCallback(
async (
contract: Contract,
_contract: Contract,
functionName: string,
args: unknown[],
options?: TransactionOptions,
Expand All @@ -111,6 +112,15 @@ export function useTransactor(): Transactor | undefined {
return false
}

/**
* Create a new contract instance with the signer.
*/
const contract = new Contract(
_contract.address,
_contract.interface,
signer,
)

logTx({ functionName, contract, args, options })

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ export function useV2V3ContractLoader({ cv }: { cv: CV2V3 | undefined }) {
try {
const network = readNetwork.name

// Contracts can be used read-only without a signer, but require a signer to create transactions.
const signerOrProvider = signer ?? readProvider

const contractLoaders = await Promise.all(
Object.values(V2V3ContractName).map(contractName =>
loadV2V3Contract(contractName, network, signerOrProvider, cv),
loadV2V3Contract(contractName, network, readProvider, cv),
),
)

Expand Down

0 comments on commit edcfde3

Please sign in to comment.