thirdweb-dev / js

Best in class web3 SDKs for Browser, Node and Mobile apps
https://thirdweb.com
Apache License 2.0
430 stars 328 forks source link

Error Initialise SDK via fromSigner / fromPrivateKey: "Must call super constructor" #1519

Closed regina-c closed 11 months ago

regina-c commented 1 year ago

I am getting a reference error when initialising the ThirdwebSDK - "Must call super constructor in derived class before accessing 'this' or returning from derived constructor". This only happen on my deployed site but it the same code I have using works fine on my localhost.

Not sure if anyone getting the same error and know how to troubleshoot this?

References:

Screenshot 2023-08-22 at 5 19 18 pm
onChange={async () => {
            const signer = provider?.getSigner();
            let rolesSet = false;
            if (signer && account === '0x123') {
              if (chainId === item.chainID) {
                try {
                  // const sdk = ThirdwebSDK.fromSigner(signer, '', {
                  //   clientId: 'xxx',
                  // });
                  const sdk = new ThirdwebSDK.fromPrivateKey(
                    'xxx',
                    '',
                    { clientId: 'xxx' },
                  );
                  console.log('sdk successfully launched');
                  const contract = await sdk.getContract(item.contractAddress);
                  console.log('successfully getContract');
                  if (!rolesSet) {
                    await contract.roles.setAll({
                      admin: ['0x123'],
                      minter: ['0x123']
                    });
                    rolesSet = true;
                    console.log('successfully setAll');
                  }
                  await request(api.collection, { method: 'PUT', data: { ...item, setAll: !val } });
                } catch (error) {
                  alert(error);
                }
                actionRef.current?.reload();
              } else {
                alert('Please switch to the network of the collection');
              }
            } else {
              alert('Only admin is allowed');
            }
          }}
iketw commented 1 year ago

@regina-c thanks for opening this issue.

I noticed you do new ThirdwebSDK.fromPrivateKey( in your code, but please note that fromPrivateKey is a static method so no need to add new in that call. Please, just do:

ThirdwebSDK.fromPrivateKey or ThirdwebSDK.fromSigner

regina-c commented 1 year ago

@regina-c thanks for opening this issue.

I noticed you do new ThirdwebSDK.fromPrivateKey( in your code, but please note that fromPrivateKey is a static method so no need to add new in that call. Please, just do:

ThirdwebSDK.fromPrivateKey or ThirdwebSDK.fromSigner

I had tried without new in the call and it the same error still occurred.

iketw commented 1 year ago

@regina-c I just tried running:

const sdk = ThirdwebSDK.fromPrivateKey(
        'my-private-key',
        'ethereum',
      );
const address = await sdk.wallet.getAddress();
console.log('address', address);

and it worked for me. Could you confirm the version of the sdk you're using?

stale[bot] commented 11 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.