From 3da3e22214cd9b1ad603229281b8472fee8db17e Mon Sep 17 00:00:00 2001 From: bumi Date: Fri, 20 Apr 2018 03:08:18 +0200 Subject: [PATCH 1/2] Refactor kredits address initialization This moves the Kredits initialization to the instance which allows us to be more flexible with handling contract addresses. Example: var k = new Kredits(provider, signer, {Registry: '0xabc'}); k.init().then((kredits) { ...}); var k = new Kredits(provider, signer, {Contributors: '0xabc'}) k.Contributor.add(...); --- lib/contracts/index.js | 5 ++- lib/contracts/registry.js | 6 +++ lib/kredits.js | 89 +++++++++++++++++---------------------- 3 files changed, 47 insertions(+), 53 deletions(-) create mode 100644 lib/contracts/registry.js diff --git a/lib/contracts/index.js b/lib/contracts/index.js index 9fd06ae..a4a1792 100644 --- a/lib/contracts/index.js +++ b/lib/contracts/index.js @@ -1,5 +1,6 @@ module.exports = { Contributors: require('./contributor'), Operator: require('./operator'), - Token: require('./token') -} + Token: require('./token'), + Registry: require('./registry') +}; diff --git a/lib/contracts/registry.js b/lib/contracts/registry.js new file mode 100644 index 0000000..a3af768 --- /dev/null +++ b/lib/contracts/registry.js @@ -0,0 +1,6 @@ +const Base = require('./base'); + +class Registry extends Base { +} + +module.exports = Registry; diff --git a/lib/kredits.js b/lib/kredits.js index 8182137..8cb0075 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -1,17 +1,15 @@ const ethers = require('ethers'); const RSVP = require('rsvp'); -const abis = { +const ABIS = { Contributors: require('./abis/Contributors.json'), Operator: require('./abis/Operator.json'), Registry: require('./abis/Registry.json'), Token: require('./abis/Token.json') }; -const addresses = { - Registry: require('./addresses/Registry.json') -}; +const RegistryAddress = require('./addresses/Registry.json'); -const contracts = require('./contracts'); +const Contracts = require('./contracts'); const IPFS = require('./utils/ipfs') // Helpers @@ -22,64 +20,52 @@ function capitalize(word) { class Kredits { static get contractNames() { - return Object.keys(abis); + return Object.keys(ABIS); } constructor(provider, signer, addresses) { this.provider = provider; this.signer = signer; - // Initialize our registry contract - this.addresses = addresses; + // 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.contracts = {}; this.ipfs = new IPFS(); } - static setup(provider, signer, ipfsConfig = null) { - let ipfsAPI = new IPFS(ipfsConfig); + init(names) { + let contractsToLoad = names || Kredits.contractNames; + let addressPromises = contractsToLoad.map((contractName) => { + return this.Registry.functions.getProxyFor(contractName).then((address) => { + this.addresses[contractName] = address; + }).catch((error) => { + throw new Error(`Failed to get address for ${contractName} from registry at ${this.Registry.contract.address} + - correct registry? does it have version entry? - ${error.message}` + ); + }); + }); + return RSVP.all(addressPromises).then(() => { return this }); + } - return ipfsAPI._ipfsAPI.id().catch((error) => { + static setup(provider, signer, ipfsConfig = null) { + console.log('Kredits.setup() is deprecated use new Kredits().init() instead'); + let ipfs = new IPFS(ipfsConfig); + + return ipfs._ipfsAPI.id().catch((error) => { throw new Error(`IPFS node not available; config: ${JSON.stringify(ipfsConfig)} - ${error.message}`); }).then(() => { - let registryContract = this.initRegistryContract(provider); - - let addresses = Kredits.contractNames.reduce((mem, name) => { - let contractName = capitalize(name); - mem[contractName] = registryContract.functions.getProxyFor(contractName).catch((error) => { - throw new Error(`Failed to get address for ${contractName} from registry at ${registryContract.address} - - correct registry? does it have version entry? - ${error.message}` - ); - }); - return mem; - }, {}); - - return RSVP.hash(addresses) - .then((addresses) => { - let kredits = new Kredits(provider, signer, addresses); - kredits.ipfs = ipfsAPI; - return kredits; - }); + return new Kredits(provider, signer).init().then((kredits) => { + kredits.ipfs = ipfs; + return kredits; + }); }); } - static initRegistryContract(provider) { - let address = addresses['Registry'][provider.chainId]; - if (!address) { - throw new Error(`Registry address not found; invalid network? - requested network: ${provider.chainId} - supported networks: ${Object.keys(addresses['Registry'])} - `); - } - provider.getCode(address).then((code) => { - // not sure if we always get the same return value of the code is not available - // that's why checking if it is < 5 long - if (code === '0x00' || code.length < 5) { - throw new Error(`Registry not found at ${address} on network ${provider.chainId}`); - } - }); - let abi = abis['Registry']; - return new ethers.Contract(address, abi, provider); + get Registry() { + return this.contractFor('registry'); } get Contributor() { @@ -101,13 +87,14 @@ class Kredits { return this.contracts[name]; } - let contractName = capitalize(name); - let address = this.addresses[contractName]; - if (!address || !abis[contractName]) { + const contractName = capitalize(name); + const address = this.addresses[contractName]; + const abi = this.abis[contractName]; + if (!address || !abi) { throw new Error(`Address or ABI not found for ${contractName}`); } - let contract = new ethers.Contract(address, abis[contractName], this.signer); - this.contracts[name] = new contracts[contractName](contract); + let contract = new ethers.Contract(address, abi, this.signer); + this.contracts[name] = new Contracts[contractName](contract); this.contracts[name].ipfs = this.ipfs; return this.contracts[name]; From 92f3963c5aff73af70a4d14866e7b6216a8ed8ef Mon Sep 17 00:00:00 2001 From: bumi Date: Fri, 20 Apr 2018 13:01:44 +0200 Subject: [PATCH 2/2] Remove not needed static contractNames function --- lib/kredits.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/kredits.js b/lib/kredits.js index 8cb0075..f4a065d 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -19,9 +19,6 @@ function capitalize(word) { } class Kredits { - static get contractNames() { - return Object.keys(ABIS); - } constructor(provider, signer, addresses) { this.provider = provider; @@ -36,7 +33,7 @@ class Kredits { } init(names) { - let contractsToLoad = names || Kredits.contractNames; + let contractsToLoad = names || Object.keys(ABIS); let addressPromises = contractsToLoad.map((contractName) => { return this.Registry.functions.getProxyFor(contractName).then((address) => { this.addresses[contractName] = address;