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:
parent
3662f1ae24
commit
51e5da414f
@ -17,11 +17,7 @@ class Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
on(type, callback) {
|
on(type, callback) {
|
||||||
let eventMethod = `on${type.toLowerCase()}`;
|
return this.contract.on(type, callback);
|
||||||
// Don't use this.contract.events here. Seems to be a bug in ethers.js
|
|
||||||
this.contract[eventMethod] = callback;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
module.exports = Base;
|
module.exports = Base;
|
||||||
|
@ -12,7 +12,9 @@ class Kernel extends Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
appNamehash(appName) {
|
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,9 +36,7 @@ class Kredits {
|
|||||||
|
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.signer = signer;
|
this.signer = signer;
|
||||||
// by default we only need the DAO/Kernel address.
|
this.addresses = addresses || {};
|
||||||
// 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.abis = abis || ABIS;
|
this.abis = abis || ABIS;
|
||||||
this.ipfs = new IPFS(ipfsConfig);
|
this.ipfs = new IPFS(ipfsConfig);
|
||||||
this.contracts = {};
|
this.contracts = {};
|
||||||
@ -46,6 +44,8 @@ class Kredits {
|
|||||||
|
|
||||||
init(names) {
|
init(names) {
|
||||||
let contractsToLoad = names || APP_CONTRACTS;
|
let contractsToLoad = names || APP_CONTRACTS;
|
||||||
|
return this.provider.getNetwork().then(network => {
|
||||||
|
this.addresses['Kernel'] = this.addresses['Kernel'] || DaoAddresses[network.chainId.toString()];
|
||||||
let addressPromises = contractsToLoad.map((contractName) => {
|
let addressPromises = contractsToLoad.map((contractName) => {
|
||||||
return this.Kernel.getApp(contractName).then((address) => {
|
return this.Kernel.getApp(contractName).then((address) => {
|
||||||
this.addresses[contractName] = address;
|
this.addresses[contractName] = address;
|
||||||
@ -57,6 +57,7 @@ class Kredits {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
return RSVP.all(addressPromises).then(() => { return this });
|
return RSVP.all(addressPromises).then(() => { return this });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static setup(provider, signer, ipfsConfig = null) {
|
static setup(provider, signer, ipfsConfig = null) {
|
||||||
@ -82,7 +83,7 @@ class Kredits {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get Operator() {
|
get Operator() {
|
||||||
return this.Proposal();
|
return this.Proposal;
|
||||||
}
|
}
|
||||||
|
|
||||||
get Token() {
|
get Token() {
|
||||||
|
824
package-lock.json
generated
824
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -42,7 +42,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"eth-ens-namehash": "^2.0.8",
|
"eth-ens-namehash": "^2.0.8",
|
||||||
"ethers": "3.0.15",
|
"ethers": "^4.0.27",
|
||||||
"ipfs-api": "^19.0.0",
|
"ipfs-api": "^19.0.0",
|
||||||
"rsvp": "^4.8.2"
|
"rsvp": "^4.8.2"
|
||||||
},
|
},
|
||||||
|
@ -4,22 +4,22 @@ const Kredits = require('../../lib/kredits');
|
|||||||
|
|
||||||
module.exports = async function(web3) {
|
module.exports = async function(web3) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
getNetworkId(web3).then(networkId => {
|
const provider = new ethers.providers.Web3Provider(web3.currentProvider);
|
||||||
const provider = new ethers.providers.Web3Provider(
|
|
||||||
web3.currentProvider, { chainId: parseInt(networkId) }
|
|
||||||
);
|
|
||||||
let signer = provider.getSigner();
|
let signer = provider.getSigner();
|
||||||
// checking if siner supports signing transactions
|
// checking if siner supports signing transactions
|
||||||
signer.getAddress().then(_ => {
|
signer.getAddress().then(_ => {
|
||||||
new Kredits(provider, signer).init().then(kredits => {
|
new Kredits(provider, signer).init().then(kredits => {
|
||||||
resolve(kredits);
|
resolve(kredits);
|
||||||
})
|
}).catch(e => {
|
||||||
|
reject(e);
|
||||||
|
});
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
console.log(`Signer account not available; readonly connection (${e.message}`);
|
console.log(`Signer account not available; readonly connection (${e.message}`);
|
||||||
new Kredits(provider, null).init().then(kredits => {
|
new Kredits(provider, null).init().then(kredits => {
|
||||||
resolve(kredits);
|
resolve(kredits);
|
||||||
})
|
}).catch(e => {
|
||||||
})
|
reject(e);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user