-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add Node.js httpAgent for FetchRequest options to enable proxy support #4337
Conversation
Hi I have an error "ethers": "6.7.1" |
@Mnemon1k Hello, please try the following instead const { ethers, JsonRpcProvider, FetchRequest } = require('ethers');
const { HttpsProxyAgent } = require('https-proxy-agent');
const fetch = require('cross-fetch');
const agent = new HttpsProxyAgent(PROXY_URL_HERE);
const getUrl = async (req, _signal) => {
// Inherited from https://github.com/ethers-io/ethers.js/blob/main/src.ts/utils/geturl-browser.ts
let signal;
if (_signal) {
const controller = new AbortController();
signal = controller.signal;
_signal.addListener(() => { controller.abort(); });
}
const init = {
method: req.method,
headers: req.headers,
body: req.body || undefined,
signal
};
// This is what we want
init.agent = agent;
// Inherited from https://github.com/ethers-io/ethers.js/blob/main/src.ts/utils/geturl-browser.ts
const resp = await fetch(req.url, init);
const headers = {};
resp.headers.forEach((value, key) => {
headers[key.toLowerCase()] = value;
});
const respBody = await resp.arrayBuffer();
const body = (respBody == null) ? null: new Uint8Array(respBody);
return {
statusCode: resp.status,
statusMessage: resp.statusText,
headers,
body
};
};
/**
* Define new FetchRequest used for a provider
*/
// Assigning custom getUrl function will apply to all ethers v6 providers
FetchRequest.registerGetUrl(getUrl);
const provider = new JsonRpcProvider(ETH_RPC);
const sleep = (sec) => new Promise(r => setTimeout(r, sec * 1000));
// Sleep until proxy server is booted up
sleep(2).then(() => provider.getBlockNumber().then(console.log)); It is kind of dirty since this is what the current ethers.js requires to do, See the open issue #4353 for future improvements happening on. |
the code snippet below, why it is able to work without a http proxy server running?
|
@victoryeo Right now you should modify global getUrl function in order to work with proxies The snippet you posted doesn't work right now without this PR |
Closing the PR since an alternative approach is now available with the updated version https://github.com/ethers-io/ethers.js/releases/tag/v6.8.0 For anyone interested please practice the latest example code https://github.com/kaliubuntu0206/ethers-proxy-example/blob/main/index.js |
The second link does no longer exist. |
supersedes #2829
This is the PR to add an option for FetchRequest so that mimicking registerGetUrl function to only use proxies are no longer required ( See #4336 for the example code of it )
It will reduce needs for TypeScript projects to import and create bloat of types to fulfil the mimicked registerGetUrl function.
In short, only the following code will be required to use proxy connections
Without this PR, you may try this