-
ProblemI created a custom react hook and got a problem when using "useContract" to call contract's functions deployed in hardhat localhost node. My contract//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;
contract Greeter {
string greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
} My codeimport { useContract } from 'wagmi';
import Greeter from '../abi/Greeter.json';
const useGreeter = () => {
const contract = useContract({
addressOrName: String(process.env.NEXT_PUBLIC_GREETER_ADDRESS),
contractInterface: Greeter.abi,
});
async function greet() {
try {
const result = await contract.functions.greet();
console.log(result);
} catch (error) {
console.log(error);
}
}
return { greet };
};
export default useGreeter; The contract was already deployed and metamask account was connected.
Can anyone tell me where I go wrong? or there is a problem with wagmi?. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
You must pass a |
Beta Was this translation helpful? Give feedback.
-
I'm having a similar issue. I've noticed that when I try to debug what the provider is, by calling; const provider = useProvider();
useEffect(() => {
console.log('network', provider.network)
}, [provider.network]) The provider network is "homestead" even though I connected on Rinkeby. My parent component looks like: import { Provider } from "wagmi";
import App from './App';
import { chain } from "wagmi";
import { InjectedConnector } from "wagmi/connectors/injected";
const chains = [chain.rinkeby];
// Set up connectors
const connectors = ({ chainId }: { chainId?: number }) => {
return [
new InjectedConnector({ chains }),
];
};
const AppWithWeb3 = () => {
return (
<Provider connectors={connectors}>
<App />
</Provider>
);
};
export default SpaceWithWeb3; How can I make sure the provider is connected to the proper network? |
Beta Was this translation helpful? Give feedback.
-
const [{data: signerData}] = useSigner()
const contract = useContract({
addressOrName: String(process.env.NEXT_PUBLIC_GREETER_ADDRESS),
contractInterface: Greeter.abi,
signerOrProvider: signerData,
}); I solved it by replacing |
Beta Was this translation helpful? Give feedback.
I solved it by replacing
useProvider
withuseSigner
.ref: #93 (reply in thread)