web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.19k stars 4.91k forks source link

beta.49+: TypeError: web3CoreSubscriptions.NewHeadsSubscription is not a constructor #2722

Closed mqklin closed 5 years ago

mqklin commented 5 years ago

Code:

import Web3 from 'web3';
const web3 = new Web3(Web3.givenProvider);
web3.givenProvider.enable().then(() => {
  web3.eth.sendTransaction({
    to: '0x42b9dF65B219B3dD36FF330A4dD8f327A6Ada990',
    value: '1',
  });
});

"web3": "1.0.0-beta.49|50|51|52" (I use MetaMask) Error: TypeError: web3CoreSubscriptions.NewHeadsSubscription is not a constructor

nivida commented 5 years ago

Are you running it in a browser with bundling it first?

mqklin commented 5 years ago

Yep, bundling with webpack. I can create a repro repo if it's needed

mqklin commented 5 years ago

I don't know how but it is fixed now =/ Sorry for the troubling and thanks for the response

mqklin commented 5 years ago

No, it's not fixed. I'll provide the repro this week

nivida commented 5 years ago

Great thanks!:)

mqklin commented 5 years ago

Repro: https://github.com/mqklin/web3-test I found the problem, it's because of @0x/subproviders package. It is strange that I don't even import it into my project, only install with yarn, and that's enough to trigger this error. I have to use this package because I need to use HTTP provider (Alchemy in my case), but let user to sign transactions with custom provider (MetaMask, Status, Fortmatic, etc). @nivida could you please help me, how can I do this without @0x/subproviders? I tried to do this: web3.eth.transactionSigner = new web3.eth.transactionSigner.constructor({sign: window.web3.sign}); but it throws 'Invalid TransactionSigner given!'. Is transactionSigner not implemented yet?

Feel free to close the issue, because as I can understand it's related to that package, and not to web3.js.

nivida commented 5 years ago

@mqklin

Example of the usage of a custom TransactionSigner:

const options {
  transactionSigner: new CustomSigner();
};

const web3 = new Web3(provider, null, options);

The CustomSigner has to provide the following interface:

interface TransactionSigner {
    sign(txObject: Transaction): Promise<SignedTransaction>
}

interface SignedTransaction {
    messageHash: string,
    v: string,
    r: string,
    s: string,
    rawTransaction: string
}

Feel free to create a new issue here if the issue in the subproviders repository of 0x gets closed.

chipueatfast commented 5 years ago
const keyPair = {
            publicKey: process.env.GANACHE_PUB,
            privateKey: Buffer.from(process.env.GANACHE_PRI, 'hex'),
        };

this.contractInstance = new this.web3.eth.Contract(TrueGrailToken.abi, '0x833bbEd979314708B071C672E213484024D11fd8');
        console.log(this.contractInstance.methods.tokenMetadata(211014440472470).encodeABI());
        const options = {
            from: keyPair.publicKey,
            to: '0x833bbEd979314708B071C672E213484024D11fd8',
            data: this.contractInstance.methods.tokenMetadata(211014440472470).encodeABI(),
            gasPrice: 3000,
            gasLimit: 250000,
            nonce: 1000,
        };
const tx = new Tx(options);
tx.sign(keyPair.privateKey);

const rawTx = `0x${tx.serialize().toString('hex')}`;

 this.web3.eth.sendSignedTransaction(rawTx, (err, result) => {
            console.log('error: ',err);
            console.log('result: ', result);
});

I am sorry but this still does not work, this is my code run on startup in nodejs and they show the same error. Please reopen this issue, I also downgrade the version to 0.0.37 but it still doesnt work

mqklin commented 5 years ago

@chipueatfast try to create a minimal repro, maybe you have some dependencies that cause this error (like I had)

chipueatfast commented 5 years ago

@chipueatfast try to create a minimal repro, maybe you have some dependencies that cause this error (like I had)

Yes, I too have a dependency which corrupt the function. It's 'truffle-contract', a very popular one. Thank you