Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useContract returns undefined #288

Closed
account4sO8sF3 opened this issue Oct 11, 2022 · 6 comments
Closed

useContract returns undefined #288

account4sO8sF3 opened this issue Oct 11, 2022 · 6 comments

Comments

@account4sO8sF3
Copy link

Initializing a contract through the useContract hook almost always results in undefined.

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { ThirdwebProvider } from "@thirdweb-dev/react";

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <ThirdwebProvider
    >
        <App />
    </ThirdwebProvider>
  </React.StrictMode>
);

reportWebVitals();

App.js

import Home from "./pages/Home";

function App() {
  return (
   <Home></Home>
  );
}

export default App;

Home.js

import React from 'react';
import { ConnectWallet, ChainId, useContract, useContractWrite } from "@thirdweb-dev/react";
import myABI from "./myABI.json";


function Home() {
    const { contract } = useContract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", myABI);

    console.log(contract);
    console.log(myABI);

    return (
        <>
            <ConnectWallet accentColor="#f213a4" colorMode="dark" />

            <h1>Thirdweb</h1>
        </>
    );
}

export default Home;

myABI.json

[{"constant":false,"inputs":[{"name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"newImplementation","type":"address"},{"name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"implementation","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newAdmin","type":"address"}],"name":"changeAdmin","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"admin","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_implementation","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"previousAdmin","type":"address"},{"indexed":false,"name":"newAdmin","type":"address"}],"name":"AdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"implementation","type":"address"}],"name":"Upgraded","type":"event"}]

I am connected using the ConnectWallet button, and the network is the same as the one the contract is on (mainnet, the contract is the address of the USDC token).

thirdweb-bug

Tried it with different ABIs and contract addresses, as well as without any ABI, just the address. Why does this happen? Does the hook work ONLY for contracts deployed using the Thirdweb Dashboard, or what's the case here?

@nachoiacovino
Copy link
Contributor

hello @account4sO8sF3 , are you on the latest version of both the TypeScript and React SDK?

also not 100% sure but it might be that we haven't made a release yet to be able to pass ABI as second parameter of useContract, in that case, you would need to install @thirdweb-dev/react@nightly and @thirdweb-dev/sdk@nightly for the time being.

Hope that helps!

@account4sO8sF3
Copy link
Author

account4sO8sF3 commented Oct 11, 2022 via email

@nachoiacovino
Copy link
Contributor

Hey @account4sO8sF3 I just tested in the nightly version of the SDK and I'm getting the contract back, one thing I did notice is that you're getting the ABI incorrectly, because USDC it's actually a proxy contract you need to get it from the implementation contract, you can see how in this video: https://www.youtube.com/watch?v=gChC3Wiz1Cw

Tell me if by changing the ABI it works correctly or you still get undefined back because I wasn't able to replicate that, otherwise I can share a repo with my working version.

@account4sO8sF3
Copy link
Author

account4sO8sF3 commented Oct 11, 2022 via email

@nachoiacovino
Copy link
Contributor

here's the repo, tell me if this helps @account4sO8sF3 https://github.com/nachoiacovino/useContract-USDC

@account4sO8sF3
Copy link
Author

Very helpful, seems like I was missing desiredChainId in index.js!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants