From df7536589df4a711db7b2eefe6b6b65dbe89ef6c Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Tue, 14 May 2019 11:25:21 +0200 Subject: [PATCH] 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. --- lib/kredits.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/kredits.js b/lib/kredits.js index d477274..b774e16 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -66,6 +66,23 @@ class Kredits { 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 () { let k = this.contractFor('Kernel'); // in case we want to use a special apm (e.g. development vs. production)