Add helper to initialize a new instance with provider

So far we always had to initialze a provider and signer and pass those
to the kredits constructor.
This helper makes it easier to initialize a default ethers provider and
a default signer.
This commit is contained in:
bumi 2019-05-14 11:25:21 +02:00
parent 2a675c9417
commit df7536589d

View File

@ -66,6 +66,23 @@ class Kredits {
return new Kredits(provider, signer, { ipfsConfig: ipfsConfig }).init(); return new Kredits(provider, signer, { ipfsConfig: ipfsConfig }).init();
} }
static for (connectionOptions, kreditsOptions) {
const { network, rpcUrl, wallet } = connectionOptions;
if (!rpcUrl && network === 'local') { rpcUrl = 'http://localhost:8545'; }
let ethProvider, signer;
if (rpcUrl || network === 'local') {
ethProvider = new ethers.providers.JsonRpcProvider(rpcUrl);
} else {
ethProvider = new ethers.getDefaultProvider(network);
}
if (wallet) {
signer = wallet.connect(ethProvider);
} else if (ethProvider.getSigner) {
signer = ethProvider.getSigner();
}
return new Kredits(ethProvider, signer, kreditsOptions);
}
get Kernel () { get Kernel () {
let k = this.contractFor('Kernel'); let k = this.contractFor('Kernel');
// in case we want to use a special apm (e.g. development vs. production) // in case we want to use a special apm (e.g. development vs. production)