Estoy tratando de implementar la funcionalidad alternativa para mi DApp, lo que significa que cuando no se encuentra un proveedor web3 (por ejemplo, metamask/trustwallet, etc.), todavía puedo hacer llamadas a la red ethereum, pero parece que siempre tengo el mismo problema.
Esta es mi lógica alternativa:
let bootStrappedWeb3;
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
bootStrappedWeb3 = new Web3(web3.currentProvider);
} else {
console.log('No Web3 Detected... falling back to using default mainnet HTTP Provider');
bootStrappedWeb3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/<MY_API_KEY>"));
}
window.web3 = bootStrappedWeb3;
// Listen for when web3 is connected and then bootstrap the app
window.web3.eth.net.isListening()
.then(() => {
console.log('is connected');
// Bootstrap the full app
this.$store.dispatch(actions.INIT_APP, bootStrappedWeb3);
})
.catch(e => console.log('Something went wrong', e));
Veo is connected
y luego INIT_APP
se llama.
INIT_APP
luego establece el proveedor en mi contrato de trufas de esta manera:
MyContract.setProvider(web3.currentProvider);
Puedo leer correctamente la red actual que informa como en Mainnet
, ID 1, pero luego, cada vez que trato de usar trufa, esto falla.
La llamada a la trufa se ve así:
MyContract.deployed()
.then((contract) => {
// Do stuff
}).catch((error) => console.log("Something went bang!", error));
Una excepción es throw y asalto el error que es el siguiente:
Something went bang! TypeError: Cannot read property 'apply' of undefined
at Provider.sendAsync (contract.js?6b6f:24)
at RequestManager.sendAsync (requestmanager.js?e4d9:80)
at Object.get [as getNetwork] (property.js?7a8c:116)
at eval (contract.js?6b6f:512)
at new Promise (<anonymous>)
at Function.detectNetwork (contract.js?6b6f:503)
at Function.deployed (contract.js?6b6f:451)
at Store.eval (index.js?e3b1:444)
at Array.wrappedActionHandler (vuex.esm.js?edaa:704)
at Store.dispatch (vuex.esm.js?edaa:426)
at boundDispatch (vuex.esm.js?edaa:332)
at eval (index.js?e3b1:273)
at tryCatcher (bluebird.js?e531:5063)
at Promise._settlePromiseFromHandler (bluebird.js?e531:3095)
at Promise._settlePromise (bluebird.js?e531:3153)
at Promise._settlePromise0 (bluebird.js?e531:3198)
at Promise._settlePromises (bluebird.js?e531:3281)
at eval (bluebird.js?e531:162)
at MutationObserver.eval (bluebird.js?e531:4330)
¿Alguien tiene alguna idea? De la documentación y los ejemplos que he leído, creo que esto debería funcionar, ¡y se agradece toda la ayuda!
Gracias.
Descubrí que usar el trabajo descrito en este ticket resolvió el problema para mí: https://github.com/trufflesuite/truffle-contract/issues/57
p.ej
MyContract.setProvider(web3.currentProvider);
if (typeof MyContract.currentProvider.sendAsync !== "function") {
MyContract.currentProvider.sendAsync = function() {
return MyContract.currentProvider.send.apply(
MyContract.currentProvider, arguments
);
};
}
Jimbob1987
Jimbob1987
Jimbob1987