I am working on a react application and just can't catch the web3 object of mobile wallets. Catching web3 in Metamask works like a charm.
Here's my code so far:
componentDidMount() {
window.addEventListener("load", () => {
let web3 = window.web3;
if (web3) {
web3 = new Web3(web3.currentProvider);
web3.eth.getAccounts().then(accts => {
web3.eth.net.getNetworkType().then(network => {
this.setState({
web3,
isConnected: true,
network: network,
user: accts[0]
});
});
});
}
});
}
So far I've tried Trust Wallet and Status Wallet and none of both works with the existing code.
I have also tried to user web3.currentProvider.isTrust
but that doesn't seem to work either.
Edit: I also tried to set an interval to check for the web3 object every few seconds. But that didn't work either.
Edit 2: Since I'm using web3js 1.x I tried the following:
componentDidMount() {
window.addEventListener("load", () => {
if (Web3.givenProvider) {
let web3 = new Web3(Web3.givenProvider);
alert(web3.currentProvider.host);
console.log(web3.currentProvider);
web3.eth.getAccounts((err, accts) => {
if (!err) {
web3.eth.net.getNetworkType((err, network) => {
if (!err) {
this.setState({
web3,
isConnected: true,
network: network,
user: accts[0]
});
} else {
alert(err);
}
});
} else {
alert(err);
}
});
}
});
}
As you can see I'm trying to get error messages with the alerts
. Now status responds with You tried to send "undefined" synchronously. Synchronous requests are not supported, sorry
. I don't get it. This is not a synchronous request?!
Trust also tells me Error: Trust does not support calling undefined synchronously without a callback parameter to call undefined asynchronously.
.