ethers.js/providers/fallback-provider.js

112 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

2018-06-13 15:39:39 -04:00
'use strict';
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
2018-08-02 20:43:09 -04:00
var base_provider_1 = require("./base-provider");
2018-09-24 16:07:14 -04:00
var errors = __importStar(require("../errors"));
2018-06-13 15:39:39 -04:00
// Returns:
// - true is all networks match
// - false if any network is null
// - throws if any 2 networks do not match
function checkNetworks(networks) {
var result = true;
var check = null;
networks.forEach(function (network) {
// Null
if (network == null) {
result = false;
return;
}
// Have nothing to compre to yet
if (check == null) {
check = network;
return;
}
// Matches!
if (check.name === network.name &&
check.chainId === network.chainId &&
2018-11-20 15:45:47 -05:00
((check.ensAddress === network.ensAddress) ||
(check.ensAddress == null && network.ensAddress == null))) {
2018-06-13 15:39:39 -04:00
return;
}
errors.throwError('provider mismatch', errors.INVALID_ARGUMENT, { arg: 'networks', value: networks });
2018-06-13 15:39:39 -04:00
});
return result;
}
var FallbackProvider = /** @class */ (function (_super) {
__extends(FallbackProvider, _super);
function FallbackProvider(providers) {
var _this = this;
if (providers.length === 0) {
throw new Error('no providers');
}
2018-06-18 05:42:41 -04:00
// All networks are ready, we can know the network for certain
2018-06-13 15:39:39 -04:00
var ready = checkNetworks(providers.map(function (p) { return p.network; }));
if (ready) {
_this = _super.call(this, providers[0].network) || this;
}
else {
2018-06-18 05:42:41 -04:00
// The network won't be known until all child providers know
var ready_1 = Promise.all(providers.map(function (p) { return p.getNetwork(); })).then(function (networks) {
2018-06-13 15:39:39 -04:00
if (!checkNetworks(networks)) {
errors.throwError('getNetwork returned null', errors.UNKNOWN_ERROR, {});
}
return networks[0];
});
2018-06-18 05:42:41 -04:00
_this = _super.call(this, ready_1) || this;
2018-06-13 15:39:39 -04:00
}
2018-06-18 05:42:41 -04:00
errors.checkNew(_this, FallbackProvider);
// Preserve a copy, so we don't get mutated
2018-06-13 15:39:39 -04:00
_this._providers = providers.slice(0);
return _this;
}
Object.defineProperty(FallbackProvider.prototype, "providers", {
get: function () {
2018-06-18 05:42:41 -04:00
// Return a copy, so we don't get mutated
2018-06-13 15:39:39 -04:00
return this._providers.slice(0);
},
enumerable: true,
configurable: true
});
FallbackProvider.prototype.perform = function (method, params) {
// Creates a copy of the providers array
var providers = this.providers;
return new Promise(function (resolve, reject) {
var firstError = null;
function next() {
if (!providers.length) {
reject(firstError);
return;
}
var provider = providers.shift();
provider.perform(method, params).then(function (result) {
2018-07-03 16:44:05 -04:00
return resolve(result);
}).catch(function (error) {
2018-06-13 15:39:39 -04:00
if (!firstError) {
firstError = error;
}
2018-07-03 16:44:05 -04:00
setTimeout(next, 0);
2018-06-13 15:39:39 -04:00
});
}
next();
});
};
return FallbackProvider;
2018-08-02 20:43:09 -04:00
}(base_provider_1.BaseProvider));
2018-06-13 15:39:39 -04:00
exports.FallbackProvider = FallbackProvider;