Es scheint, dass Google Chrome auf meinem Computer/Webbrowser beim Einfügen von Metamasken langsam ist. Daher muss ich so etwas schreiben, damit meine App tatsächlich die web3-Instanz erhält, anstatt zu scheitern:
setTimeout(function(){
if (typeof web3 !== 'undefined'){
console.log('getting web3');
App.web3Provider = web3.currentProvider;
}
else {
console.log('No Web3 instance given!');
// flash modal saying "please download Metamask"
}
web3 = new Web3(App.web3Provider);
return App.initContract(web3);
}, 500);
Dies funktioniert jedoch nicht gut mit der Truffle-Contract js-Bibliothek, die die web3-Instanz sofort benötigt . Hat jemand einen Workaround oder einen Fix dafür? Danke.
Die truffle-contract
Bibliothek benötigt die web3-Instanz nicht sofort. Es muss nur vorhanden sein, wenn Sie versuchen, es zu verwenden.
var contract = require("truffle-contract");
var my_contract_json = require('../../../truffle/build/contracts/MyContract.json');
var MyContract;
var my_contract_deployed;
window.onload = function() {
if (typeof web3 !== 'undefined') {
window.web3 = new Web3(web3.currentProvider);
MyContract = contract(my_contract_json);
MyContract.setProvider(web3.currentProvider);
MyContract.deployed().then(function(instance) {
my_contract_deployed = instance;
// doWhateverUIStuff();
});
}
}
Sehen Sie hier ein funktionierendes Beispiel: https://github.com/realitykeys/realitycheck/blob/8674f7b07cef2c4c96762c30befd549138bdb8e4/assets/js/scripts/main.js#L3398
Alex