From df7536589df4a711db7b2eefe6b6b65dbe89ef6c Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Tue, 14 May 2019 11:25:21 +0200 Subject: [PATCH 1/4] 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) -- 2.25.1 From 1a227ba67cd826ee449f4099ee6a7a376f42e2b0 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Tue, 14 May 2019 11:31:33 +0200 Subject: [PATCH 2/4] Linting --- lib/kredits.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kredits.js b/lib/kredits.js index b774e16..a78dd6d 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -67,7 +67,7 @@ class Kredits { } static for (connectionOptions, kreditsOptions) { - const { network, rpcUrl, wallet } = connectionOptions; + let { network, rpcUrl, wallet } = connectionOptions; if (!rpcUrl && network === 'local') { rpcUrl = 'http://localhost:8545'; } let ethProvider, signer; if (rpcUrl || network === 'local') { -- 2.25.1 From 4c64aa7c2ae1a9c15a1c9459ff27d82869bb49b3 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Tue, 14 May 2019 12:03:29 +0200 Subject: [PATCH 3/4] linting --- lib/kredits.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kredits.js b/lib/kredits.js index a78dd6d..003c88e 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -79,7 +79,7 @@ class Kredits { signer = wallet.connect(ethProvider); } else if (ethProvider.getSigner) { signer = ethProvider.getSigner(); - } + } return new Kredits(ethProvider, signer, kreditsOptions); } -- 2.25.1 From e20bda73fb333d0b36b8685e20f9f0aea661bca4 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Fri, 17 May 2019 09:17:10 +0200 Subject: [PATCH 4/4] Remove not needed network check We default to localhost anyway --- lib/kredits.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kredits.js b/lib/kredits.js index 003c88e..fdcb63b 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -70,7 +70,7 @@ class Kredits { let { network, rpcUrl, wallet } = connectionOptions; if (!rpcUrl && network === 'local') { rpcUrl = 'http://localhost:8545'; } let ethProvider, signer; - if (rpcUrl || network === 'local') { + if (rpcUrl) { ethProvider = new ethers.providers.JsonRpcProvider(rpcUrl); } else { ethProvider = new ethers.getDefaultProvider(network); -- 2.25.1