Merge pull request #35 from 67P/refactor/kredits-init
Refactor kredits address initialization
This commit is contained in:
commit
6f02a4d4b2
@ -1,5 +1,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
Contributors: require('./contributor'),
|
Contributors: require('./contributor'),
|
||||||
Operator: require('./operator'),
|
Operator: require('./operator'),
|
||||||
Token: require('./token')
|
Token: require('./token'),
|
||||||
}
|
Registry: require('./registry')
|
||||||
|
};
|
||||||
|
6
lib/contracts/registry.js
Normal file
6
lib/contracts/registry.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
const Base = require('./base');
|
||||||
|
|
||||||
|
class Registry extends Base {
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Registry;
|
@ -1,17 +1,15 @@
|
|||||||
const ethers = require('ethers');
|
const ethers = require('ethers');
|
||||||
const RSVP = require('rsvp');
|
const RSVP = require('rsvp');
|
||||||
|
|
||||||
const abis = {
|
const ABIS = {
|
||||||
Contributors: require('./abis/Contributors.json'),
|
Contributors: require('./abis/Contributors.json'),
|
||||||
Operator: require('./abis/Operator.json'),
|
Operator: require('./abis/Operator.json'),
|
||||||
Registry: require('./abis/Registry.json'),
|
Registry: require('./abis/Registry.json'),
|
||||||
Token: require('./abis/Token.json')
|
Token: require('./abis/Token.json')
|
||||||
};
|
};
|
||||||
const addresses = {
|
const RegistryAddress = require('./addresses/Registry.json');
|
||||||
Registry: require('./addresses/Registry.json')
|
|
||||||
};
|
|
||||||
|
|
||||||
const contracts = require('./contracts');
|
const Contracts = require('./contracts');
|
||||||
const IPFS = require('./utils/ipfs')
|
const IPFS = require('./utils/ipfs')
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
@ -21,65 +19,50 @@ function capitalize(word) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Kredits {
|
class Kredits {
|
||||||
static get contractNames() {
|
|
||||||
return Object.keys(abis);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(provider, signer, addresses) {
|
constructor(provider, signer, addresses) {
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.signer = signer;
|
this.signer = signer;
|
||||||
|
|
||||||
// Initialize our registry contract
|
// by default we only need the registry address.
|
||||||
this.addresses = addresses;
|
// 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.contracts = {};
|
||||||
this.ipfs = new IPFS();
|
this.ipfs = new IPFS();
|
||||||
}
|
}
|
||||||
|
|
||||||
static setup(provider, signer, ipfsConfig = null) {
|
init(names) {
|
||||||
let ipfsAPI = new IPFS(ipfsConfig);
|
let contractsToLoad = names || Object.keys(ABIS);
|
||||||
|
let addressPromises = contractsToLoad.map((contractName) => {
|
||||||
return ipfsAPI._ipfsAPI.id().catch((error) => {
|
return this.Registry.functions.getProxyFor(contractName).then((address) => {
|
||||||
throw new Error(`IPFS node not available; config: ${JSON.stringify(ipfsConfig)} - ${error.message}`);
|
this.addresses[contractName] = address;
|
||||||
}).then(() => {
|
}).catch((error) => {
|
||||||
|
throw new Error(`Failed to get address for ${contractName} from registry at ${this.Registry.contract.address}
|
||||||
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}`
|
- correct registry? does it have version entry? - ${error.message}`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
return mem;
|
});
|
||||||
}, {});
|
return RSVP.all(addressPromises).then(() => { return this });
|
||||||
|
}
|
||||||
|
|
||||||
return RSVP.hash(addresses)
|
static setup(provider, signer, ipfsConfig = null) {
|
||||||
.then((addresses) => {
|
console.log('Kredits.setup() is deprecated use new Kredits().init() instead');
|
||||||
let kredits = new Kredits(provider, signer, addresses);
|
let ipfs = new IPFS(ipfsConfig);
|
||||||
kredits.ipfs = ipfsAPI;
|
|
||||||
|
return ipfs._ipfsAPI.id().catch((error) => {
|
||||||
|
throw new Error(`IPFS node not available; config: ${JSON.stringify(ipfsConfig)} - ${error.message}`);
|
||||||
|
}).then(() => {
|
||||||
|
|
||||||
|
return new Kredits(provider, signer).init().then((kredits) => {
|
||||||
|
kredits.ipfs = ipfs;
|
||||||
return kredits;
|
return kredits;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static initRegistryContract(provider) {
|
get Registry() {
|
||||||
let address = addresses['Registry'][provider.chainId];
|
return this.contractFor('registry');
|
||||||
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 Contributor() {
|
get Contributor() {
|
||||||
@ -101,13 +84,14 @@ class Kredits {
|
|||||||
return this.contracts[name];
|
return this.contracts[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
let contractName = capitalize(name);
|
const contractName = capitalize(name);
|
||||||
let address = this.addresses[contractName];
|
const address = this.addresses[contractName];
|
||||||
if (!address || !abis[contractName]) {
|
const abi = this.abis[contractName];
|
||||||
|
if (!address || !abi) {
|
||||||
throw new Error(`Address or ABI not found for ${contractName}`);
|
throw new Error(`Address or ABI not found for ${contractName}`);
|
||||||
}
|
}
|
||||||
let contract = new ethers.Contract(address, abis[contractName], this.signer);
|
let contract = new ethers.Contract(address, abi, this.signer);
|
||||||
this.contracts[name] = new contracts[contractName](contract);
|
this.contracts[name] = new Contracts[contractName](contract);
|
||||||
this.contracts[name].ipfs = this.ipfs;
|
this.contracts[name].ipfs = this.ipfs;
|
||||||
|
|
||||||
return this.contracts[name];
|
return this.contracts[name];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user