Skip to content

Instantly share code, notes, and snippets.

@jonajosejg
Created February 12, 2023 23:48
Show Gist options
  • Save jonajosejg/e6ef3cd7b6ae109ad0096584737b72bb to your computer and use it in GitHub Desktop.
Save jonajosejg/e6ef3cd7b6ae109ad0096584737b72bb to your computer and use it in GitHub Desktop.
'use strict';
const assert = require('bsert');
const EventEmitter = require('events');
const WEB3 = require('web3-eth');
const {endpoint} = require('./constants');
/**
* API
* @alias module:API
*/
class API extends EventEmitter {
/**
* Create a API.
* @constructor
* @param {Object} options
*/
constructor(options) {
super();
this.options = new APIOptions(options);
this.eth = this.options.eth;
}
}
class APIOptions {
/**
* APIOptions
* @alias module:API.APIOptions
* @constructor
* @param {Object} options
*/
constructor(options) {
this.options = options;
this.eth = new WEB3(new WEB3.providers.HttpProvider(endpoint));
if (options)
this.fromOptions(options);
}
/**
* Inject properties from object.
* @private
* @param {Object} options
* @returns {APIOptions}
*/
fromOptions(options) {
assert(options);
this.eth = options.eth;
if (options.eth != null) {
assert(typeof options.eth === 'object');
this.eth = options.eth;
}
return this;
}
/**
* Instantiate http options from object.
* @param {Object} options
* @returns {APIOptions}
*/
static fromOptions(options) {
return new this().fromOptions(options);
}
}
/**
* Helpers
*/
function enforce(value, msg) {
if (!value) {
const err = new Error(msg);
err.statusCode = 400;
throw err;
}
}
/*
* Expose
*/
module.exports = API;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment