bxgateway
Version:
Package for connecting to Bloxroute's gateways
136 lines (135 loc) • 5.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CloudGateway = void 0;
const ws_1 = __importDefault(require("ws"));
const https_1 = require("https");
const axios_1 = __importDefault(require("axios"));
const fs_1 = __importDefault(require("fs"));
const debug_1 = require("debug");
const bxgatewayBase_1 = __importDefault(require("./bxgatewayBase"));
const debug = (0, debug_1.debug)('cloud-gateway');
class CloudGateway extends bxgatewayBase_1.default {
constructor(url, authOpts) {
super(debug);
this._http = false;
this._url = url;
if (authOpts.authorization) {
if (url.includes('http')) {
// Not a websocket gateway
this._authorizationKey = authOpts.authorization;
this._http = true;
this._httpsAgent = new https_1.Agent({
rejectUnauthorized: false
});
debug(`HTTP instance created: ${url}`);
return;
}
// Non-enterprise gateway or MEV endpoint
this._gw = new ws_1.default(url, {
headers: {
'Authorization': authOpts.authorization
},
rejectUnauthorized: false
});
debug(`WS instance created: ${url}`);
}
else {
// Enterprise gateway
this._gw = new ws_1.default(url, {
cert: fs_1.default.readFileSync(authOpts.certPath),
key: fs_1.default.readFileSync(authOpts.keyPath),
rejectUnauthorized: false
});
debug(`WS instance created: ${url}`);
}
// Pass on
this._gw.on('open', () => this.emit('open'));
this._gw.on('close', () => this.emit('close'));
this._gw.on('error', (err) => this.emit('error', err));
// Modify default messages to be more useful
this._gw.on('message', (msg) => {
debug.extend('message')(msg.toString());
const data = JSON.parse(msg);
if (data.params)
this.emit('message', data.params.result);
if (data.result)
this.emit('message', data.result);
});
}
async simulateBundle(bundle, blockNumber, options) {
if (!this._http)
throw new Error(`Wrong endpoint: ${this._url} (not HTTP)`);
bundle = bundle.map(tx => tx.startsWith('0x') ? tx.slice(2) : tx);
const req = {
method: 'blxr_simulate_bundle',
id: 1,
params: {
transaction: bundle,
block_number: '0x' + blockNumber.toString(16),
state_block_number: options?.stateBlockNumber,
timestamp: options?.timestamp
}
};
debug.extend('blxr_simulate_bundle')(JSON.stringify(req));
try {
return (await axios_1.default.post(this._url, JSON.stringify(req), {
headers: {
'Authorization': this._authorizationKey,
'Content-Type': 'application/json'
},
httpsAgent: this._httpsAgent,
})).data;
}
catch (err) {
throw {
status: err.response.status,
statusText: err.response.statusText,
url: err.config.url,
method: err.config.method,
data: err.config.data,
headers: err.config.headers,
error: err.response.data.error
};
}
}
async submitBundle(bundle, blockNumber, options) {
if (!this._http)
throw new Error(`Wrong endpoint: ${this._url} (not HTTP)`);
bundle = bundle.map(tx => tx.startsWith('0x') ? tx.slice(2) : tx);
const req = {
method: 'blxr_submit_bundle',
id: 1,
params: {
transaction: bundle,
block_number: '0x' + blockNumber.toString(16),
min_timestamp: options?.minTimestamp,
max_timestamp: options?.maxTimestamp
}
};
debug.extend('blxr_submit_bundle')(JSON.stringify(req));
try {
return (await axios_1.default.post(this._url, JSON.stringify(req), {
headers: {
'Authorization': this._authorizationKey,
'Content-Type': 'application/json'
},
httpsAgent: this._httpsAgent,
})).data;
}
catch (err) {
throw {
status: err.response.status,
statusText: err.response.statusText,
url: err.config.url,
method: err.config.method,
data: err.config.data,
headers: err.config.headers,
error: err.response.data.error
};
}
}
}
exports.CloudGateway = CloudGateway;