Skip to content

Instantly share code, notes, and snippets.

@josh-richardson
Created November 16, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josh-richardson/01f115f4084c861d7d78a92217bfe2b9 to your computer and use it in GitHub Desktop.
Save josh-richardson/01f115f4084c861d7d78a92217bfe2b9 to your computer and use it in GitHub Desktop.
Horrible hack to intercept Web3js requests in node. Probably extremely fragile.
function Web3RequestLogger(httpProvider) {
let handler = {
get(target, propKey, receiver) {
const origMethod = Reflect.get(target, propKey, receiver);
if (propKey === "send") {
return function (...args) {
console.log(`Sent JSONRPC Request: ${JSON.stringify(args[0])}`);
let responseCallback = function(err, result) {
console.log(`Received JSONRPC Response: ${JSON.stringify(result)}`)
args[1](err, result)
};
return origMethod.apply(this, [args[0], responseCallback]);
};
}
return origMethod;
}
};
return new Proxy(httpProvider, handler);
}
@josh-richardson
Copy link
Author

Similar functionality can be achieved using web3-provider-engine, however I think it only supports HTTP rather than websockets.

  const engine = new RpcEngine();
    engine.push(function (req, res, next, end) {
        next(function(cb) {
            console.log(req);
            console.log(res);
            cb();
        });
    });
    engine.push(createInfuraMiddleware({ network: 'mainnet', projectId: '<id>' }));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment