From 78b47753b3d809849167242a2cc9528edaa7a7fc Mon Sep 17 00:00:00 2001 From: bumi Date: Mon, 23 Apr 2018 11:01:08 +0200 Subject: [PATCH 1/2] Constructor confugration options This changes the function signature of the constructor but allows us to pass any options. Mainly this is now used for the ipfs configs. --- lib/kredits.js | 14 +++++--------- lib/utils/ipfs.js | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/kredits.js b/lib/kredits.js index 5ec030a..b0aa7f7 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -22,16 +22,16 @@ function capitalize(word) { class Kredits { - constructor(provider, signer, addresses) { + constructor(provider, signer, options = {}) { this.provider = provider; this.signer = signer; // by default we only need the registry address. // the rest is loaded from there in the init() function - this.addresses = addresses || {Registry: RegistryAddress[this.provider.chainId.toString()]}; // chaiID must be a string - this.abis = ABIS; + this.addresses = options['addresses'] || { Registry: RegistryAddress[this.provider.chainId.toString()] }; // chaiID must be a string + this.abis = options['abis'] || ABIS; + this.ipfs = new IPFS(options['ipfsConfig']); this.contracts = {}; - this.ipfs = new IPFS(); } init(names) { @@ -50,11 +50,7 @@ class Kredits { static setup(provider, signer, ipfsConfig = null) { console.log('Kredits.setup() is deprecated use new Kredits().init() instead'); - let ipfs = new IPFS(ipfsConfig); - return new Kredits(provider, signer).init().then((kredits) => { - kredits.ipfs = ipfs; - return kredits; - }); + return new Kredits(provider, signer, { ipfsConfig: ipfsConfig }).init(); } get Registry() { diff --git a/lib/utils/ipfs.js b/lib/utils/ipfs.js index 3c53ce0..4a6606d 100644 --- a/lib/utils/ipfs.js +++ b/lib/utils/ipfs.js @@ -5,7 +5,7 @@ class IPFS { constructor(config) { if (!config) { - config = {host: 'localhost', port: '5001', protocol: 'http'}; + config = { host: 'localhost', port: '5001', protocol: 'http' }; } this._ipfsAPI = ipfsAPI(config); this._config = config; From 3895553e88c60a9a272c9476138cd417496a6b33 Mon Sep 17 00:00:00 2001 From: bumi Date: Thu, 26 Apr 2018 10:23:27 +0200 Subject: [PATCH 2/2] Use destructuring for options This makes it more readable what options are used --- lib/kredits.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/kredits.js b/lib/kredits.js index b0aa7f7..541d362 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -23,14 +23,15 @@ function capitalize(word) { class Kredits { constructor(provider, signer, options = {}) { + let { addresses, abis, ipfsConfig } = options; + this.provider = provider; this.signer = signer; - // by default we only need the registry address. // the rest is loaded from there in the init() function - this.addresses = options['addresses'] || { Registry: RegistryAddress[this.provider.chainId.toString()] }; // chaiID must be a string - this.abis = options['abis'] || ABIS; - this.ipfs = new IPFS(options['ipfsConfig']); + this.addresses = addresses || { Registry: RegistryAddress[this.provider.chainId.toString()] }; // chaiID must be a string + this.abis = abis || ABIS; + this.ipfs = new IPFS(ipfsConfig); this.contracts = {}; }