Update ethers.js to latest version v4

The main change is how ethers loads the networkId which is now async.
Thus the init process had to change a bit
This commit is contained in:
2019-03-29 18:13:51 +01:00
parent 3662f1ae24
commit 51e5da414f
6 changed files with 635 additions and 258 deletions

View File

@@ -17,11 +17,7 @@ class Base {
}
on(type, callback) {
let eventMethod = `on${type.toLowerCase()}`;
// Don't use this.contract.events here. Seems to be a bug in ethers.js
this.contract[eventMethod] = callback;
return this;
return this.contract.on(type, callback);
}
}
module.exports = Base;

View File

@@ -12,7 +12,9 @@ class Kernel extends Base {
}
appNamehash(appName) {
return AppIds[this.contract.provider.chainId.toString()][appName];
// actually provider.network is an asynchronous property.
// but when we call this function kredits is already initialized and the network is already loaded
return AppIds[this.contract.provider.network.chainId.toString()][appName];
}
}

View File

@@ -36,9 +36,7 @@ class Kredits {
this.provider = provider;
this.signer = signer;
// by default we only need the DAO/Kernel address.
// the rest is loaded from there in the init() function
this.addresses = addresses || { Kernel: DaoAddresses[this.provider.chainId.toString()] }; // chainID must be a string
this.addresses = addresses || {};
this.abis = abis || ABIS;
this.ipfs = new IPFS(ipfsConfig);
this.contracts = {};
@@ -46,17 +44,20 @@ class Kredits {
init(names) {
let contractsToLoad = names || APP_CONTRACTS;
let addressPromises = contractsToLoad.map((contractName) => {
return this.Kernel.getApp(contractName).then((address) => {
this.addresses[contractName] = address;
}).catch((error) => {
console.log(error);
throw new Error(`Failed to get address for ${contractName} from DAO at ${this.Kernel.contract.address}
- ${error.message}`
);
return this.provider.getNetwork().then(network => {
this.addresses['Kernel'] = this.addresses['Kernel'] || DaoAddresses[network.chainId.toString()];
let addressPromises = contractsToLoad.map((contractName) => {
return this.Kernel.getApp(contractName).then((address) => {
this.addresses[contractName] = address;
}).catch((error) => {
console.log(error);
throw new Error(`Failed to get address for ${contractName} from DAO at ${this.Kernel.contract.address}
- ${error.message}`
);
});
});
return RSVP.all(addressPromises).then(() => { return this });
});
return RSVP.all(addressPromises).then(() => { return this });
}
static setup(provider, signer, ipfsConfig = null) {
@@ -82,7 +83,7 @@ class Kredits {
}
get Operator() {
return this.Proposal();
return this.Proposal;
}
get Token() {