ethers.js/src.ts/providers/fallback-provider.ts

106 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-06-13 22:39:39 +03:00
'use strict';
2018-08-03 03:30:44 +03:00
import { BaseProvider } from './base-provider';
2018-06-13 22:39:39 +03:00
// Imported Types
import { Network } from '../utils/networks';
import * as errors from '../errors';
2018-06-13 22:39:39 +03:00
// Returns:
// - true is all networks match
// - false if any network is null
// - throws if any 2 networks do not match
function checkNetworks(networks: Array<Network>): boolean {
var result = true;
let check: Network = null;
2018-06-13 22:39:39 +03:00
networks.forEach((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 &&
check.ensAddress === network.ensAddress) { return; }
errors.throwError(
'provider mismatch',
errors.INVALID_ARGUMENT,
{ arg: 'networks', value: networks }
2018-06-13 22:39:39 +03:00
);
});
return result;
}
2018-08-03 03:30:44 +03:00
export class FallbackProvider extends BaseProvider {
private _providers: Array<BaseProvider>;
2018-06-13 22:39:39 +03:00
2018-08-03 03:30:44 +03:00
constructor(providers: Array<BaseProvider>) {
2018-06-13 22:39:39 +03:00
if (providers.length === 0) { throw new Error('no providers'); }
2018-06-18 12:42:41 +03:00
// All networks are ready, we can know the network for certain
2018-06-13 22:39:39 +03:00
let ready = checkNetworks(providers.map((p) => p.network));
if (ready) {
super(providers[0].network);
2018-06-18 12:42:41 +03:00
} else {
// The network won't be known until all child providers know
let ready = Promise.all(providers.map((p) => p.getNetwork())).then((networks) => {
2018-06-13 22:39:39 +03:00
if (!checkNetworks(networks)) {
errors.throwError('getNetwork returned null', errors.UNKNOWN_ERROR, { })
}
return networks[0];
});
2018-06-18 12:42:41 +03:00
super(ready);
}
errors.checkNew(this, FallbackProvider);
2018-06-18 12:42:41 +03:00
// Preserve a copy, so we don't get mutated
2018-06-13 22:39:39 +03:00
this._providers = providers.slice(0);
}
2018-08-03 03:30:44 +03:00
get providers(): Array<BaseProvider> {
2018-06-18 12:42:41 +03:00
// Return a copy, so we don't get mutated
2018-06-13 22:39:39 +03:00
return this._providers.slice(0);
}
perform(method: string, params: { [name: string]: any }): any {
2018-06-13 22:39:39 +03:00
// Creates a copy of the providers array
var providers = this.providers;
return new Promise((resolve, reject) => {
var firstError: Error = null;
2018-06-13 22:39:39 +03:00
function next() {
if (!providers.length) {
reject(firstError);
return;
}
var provider = providers.shift();
2018-07-03 23:44:05 +03:00
provider.perform(method, params).then((result) => {
return resolve(result);
}).catch((error) => {
2018-06-13 22:39:39 +03:00
if (!firstError) { firstError = error; }
2018-07-03 23:44:05 +03:00
setTimeout(next, 0);
2018-06-13 22:39:39 +03:00
});
}
next();
});
}
}