Skip to content

Instantly share code, notes, and snippets.

@czarly
Created August 7, 2020 11:13
Show Gist options
  • Save czarly/5e6da924a8f236ff23d447ef102f9767 to your computer and use it in GitHub Desktop.
Save czarly/5e6da924a8f236ff23d447ef102f9767 to your computer and use it in GitHub Desktop.
var http = require('http'),
request = require('request');
const geth_node_url = 'https://mainnet.infura.io/v3/<project_id>'
const anyblock_api_key = '<api_key>';
var trace_api = [
'trace_call',
'trace_callMany',
'trace_rawTransaction',
'trace_replayBlockTransactions',
'trace_replayTransaction',
'trace_block',
'trace_filter',
'trace_get',
'trace_transaction'
];
var server = http.createServer(function(req, res) {
let data = []
req.on('data', chunk => {
data.push(chunk)
});
req.on('end', () => {
let body = JSON.parse(data)
var headers = {
"content-type": "application/json",
}
var url = '';
if (trace_api.includes(body.method)){
// send it to the better node
headers['Authorization'] = 'Bearer ' + anyblock_api_key;
url = 'https://api.anyblock.tools/ethereum/ethereum/mainnet/rpc/';
} else {
// send it to the normal node
url = geth_node_url;
}
request.post({
headers : headers,
url: url,
body: JSON.stringify(body)}, function (err, response, body) {
if (!err) {
res.writeHead(response.statusCode, {'Content-Type': 'application/json'});
res.end(body);
} else {
console.log(err, body);
}
});
});
});
console.log("listening on port 5050")
server.listen(5050);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment