ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
8k stars 1.86k forks source link

AlchemyProvider._send causing error #4173

Closed luiztools closed 1 year ago

luiztools commented 1 year ago

Ethers Version

6.6.1

Search Terms

alchemy,alchemyprovider

Describe the Problem

The function below, that works fine on InfuraProvider and BrowserProvider, doesn't work with AlchemyProvider. The complete error is below too.

I'm using Node.js v18.16.1 and I've tried ChatGPT too and it gives me almost the same code. In the Ethers docs, we don't have a complete example, but with the pieces, the resulting code is the same. I didn't found anything about it on StackOverflow too.

I'd appreciate any help.

Code Snippet

async function setEthPrice(ethPriceInPenny: number): Promise<string> {
    const provider = new ethers.AlchemyProvider(`${process.env.NETWORK}`, `${process.env.ALCHEMY_API_KEY}`);
    const wallet = new ethers.Wallet(`${process.env.PRIVATE_KEY}`, provider);
    const contract = new ethers.Contract(`${process.env.ORACLE_CONTRACT}`, oracleArtifacts.abi, wallet);

    const tx = await contract.setEthPrice(ethPriceInPenny);
    const receipt = await tx.wait();
    console.log(tx.hash);
    return tx.hash;
}

Contract ABI

[{
      "inputs": [
        {
          "internalType": "uint256",
          "name": "ethPriceInPenny",
          "type": "uint256"
        }
      ],
      "name": "setEthPrice",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
    }]

Errors

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object
    at checkListener (node:events:266:3)
    at ClientRequest.once (node:events:647:3)
    at new ClientRequest (node:_http_client:245:10)
    at Object.request (node:https:360:10)
    at Object.<anonymous> (/Users/luiztools/NodeProjects/web23/algodollar/backend/node_modules/agent-base/patch-core.js:25:22)
    at Object.request (/Users/luiztools/NodeProjects/web23/algodollar/backend/node_modules/socks-proxy-agent/node_modules/agent-base/patch-core.js:23:20)
    at getUrl (/Users/luiztools/NodeProjects/web23/algodollar/backend/node_modules/ethers/src.ts/utils/geturl.ts:31:59)
    at FetchRequest.#send (/Users/luiztools/NodeProjects/web23/algodollar/backend/node_modules/ethers/src.ts/utils/fetch.ts:513:28)
    at FetchRequest.send (/Users/luiztools/NodeProjects/web23/algodollar/backend/node_modules/ethers/src.ts/utils/fetch.ts:568:26)
    at AlchemyProvider._send (/Users/luiztools/NodeProjects/web23/algodollar/backend/node_modules/ethers/src.ts/providers/provider-jsonrpc.ts:1147:40) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Environment

Altcoin - Please specify (e.g. Polygon)

Environment (Other)

Polygon Mumbai

luiztools commented 1 year ago

Here, another (and simpler) example that fires the exact same error:

const provider = new ethers.AlchemyProvider(${process.env.NETWORK}, ${process.env.ALCHEMY_API_KEY}); const balance = await provider.getBalance(${process.env.WALLET}); console.log(balance);

luiztools commented 1 year ago

Just to mention: I tried all the Ethers versions with the support of Polygon Mumbai (since 6.2, I guess). All fired the same error. I've cleaned and installed the dependencies again for each test.

ricmoo commented 1 year ago

I am getting a SERVER_ERROR when I try, which makes sense as my API key is likely out of capacity.

You error is coming from socks-proxy-agent, so maybe you have a library that is hijacking the global http library or the fetch command (if in the Browser)?

luiztools commented 1 year ago

You are right, thanks. I removed all the other libraries and it worked like a charm. I found that one lib specifically causes the error when used together with ethers (node-binance-api). Do you have any clue if there a way that I can isolate ethers and http from another libs? Maybe a http module copy, maybe some scope isolation, I don't know...

ricmoo commented 1 year ago

With Ethers v6, you can override the web fetching library using FetchRequest.registerGetUrl and provide your own implementation. Maybe that library offers a hook into the original library they are hijacking, that you could plug in?

Do you know why they are forcing the connection over a socks5 proxy? Maybe there is a way to configure the library to skip that?