Hi activeweb3provider doesnât exists after we reload the page? So how do we keep it per session? I can not store it in a session as it gives circular error! What is the use of authenticate if authenticate is no more there after refresh happens? How do i solve it?
Also if i do not refresh , it executes the transaction but it doesnât take me to the wallet to approve the transaction, i need to go to wallet manually from my browser . It works seemless on pancakeswap , how do i make it like pancakeswap?
Thank you for your patience @Son
The user session is not equal web3 provider session. You need to call web3provider after reloading the page. Could you post the code for reproducing the smart contract call issue? Thank you
I do this inside my login session
And store web3 globally or in window.web3
Then in my buy method which is called after the refresh has happened -
I call
web3.eth.sendTransaction
Now the problem is web3 is no more available as it is refreshed.
And if i donât refresh it works
But it doesnât take me to my trust wallet on click of buy, i need to go there manually!
Web3 activating and user session do not depend on each other. You need to enable the web3 provider not during authorization, but during page loading.
How can you activate web3 without authenticating via wallet connect, web3 itself is not available on load as it is wallet connect then how can we activate it?
You can call every time when the page is loaded this:
if (window.localStorage.walletconnect) {
await Moralis.enable({
provider: "walletconnect"
});
}
After connecting via WC in the localstorage
the walletconnect
object is written. So you can easily check if it is there
This helped , finally i am able to adjust everything! Thankyou so much.
I am doing the check on page load for wallet connect then enabling it and then on buy i am checking the chain id and activating current provider , then doing transaction. So it works fine for wallet connect and from inside metamask browser.
There are few issues like if i go through metamask via walletconnect - buy doesnât work.
For trust wallet it works but i need to open trust wallet app manually and approve transaction!
Do you know any way of opening it directly with transaction - just like pancakeswap does?
There are few issues like if i go through metamask via walletconnect - buy doesnât work.
If I remember correctly there was an issue with non-trustwallet wallets. Iâll let you know when it will be fixed.
For trust wallet it works but i need to open trust wallet app manually and approve transaction!
Iâm not sure if I got your question right. Before sending any transactions you need to approve it from your wallet. What is the problem, please describe it in more detail. I only use pancake using metamask.
So the problem is when i click buy, it triggers a transaction for approval but i need to manually open the trustwallet app and then approve it, it is not automatically going to trustwallet as it does it in pancakeswap. This is in mobile chrome/safari browser.
it is not automatically going to trustwallet as it does it in pancakeswap
What it does automatically? Opens the app?
Do you connect on pancakeswap via TWT or wallectconnect+TWT?
Yes it should open the app automatically once i click on buy. So i can directly approve transaction.
I connect via walletconnect and then TWT
There is another problem only in metamask app browser - Authenticate doesnt work- try testing connect feature at scamornot.io
It works in iOS but android is having some issues.
window.addEventListener('load', function () {
$.ajax(
{
type: 'GET',
dataType: 'JSON',
url: '/GetMoralisConfig',
success:
function (response) {
Moralis.initialize(response.appId);
Moralis.serverURL = response.server;
},
error:
function (response) {
alertMessage("alertDanger", "Error : Please contact admin! Code:01");
}
});
if (window.localStorage.walletconnect) {
Moralis.enable({
provider: "walletconnect"
});
} else {
Moralis.enable();
}
});
const ethereumButton = document.querySelector('.enableEthereumButton');
const showAccount = document.querySelector('.showAccount');
Moralis.Web3.onAccountsChanged(function (accounts) {
login();
});
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
window.localStorage.removeItem('WALLETCONNECT_DEEPLINK_CHOICE');
}
});
async function login() {
let message = 'Hello, You are logging in : ' + (Math.random() + 1).toString(36).substring(2);
var walletProvider = "walletconnect";
// Check if Web3 has been injected by the browser (MetaMask).
if (window.ethereum) {
walletProvider = "metamask";
}
$.LoadingOverlay("show");
Moralis.Web3.authenticate({ signingMessage: message, provider: walletProvider, chainId: 56 }).then(function (user) {
authenticate(user.id, user.get('ethAddress'));
}).catch((err) => {
alert("Error : Please contact admin! Code:02 - " + err.message);
$.LoadingOverlay("hide");
});
}
Could you try to hardcode the
Moralis.initialize(response.appId);
Moralis.serverURL = response.server;
Also please check if the response sends correct data ( or even sends it). Because that error is shown only in case when initialization is not correct.(not initializated)
I can not check anything as it is on android metamask app. It works perfectly fine with iOS metamask app, desktops. The code is fine, valueâs are there!
I mean if you hard code it - it will work on the android correctly too. Is there a real need to hide this info? appId and serverUrl - public info
Iâm not sure if window.addEventListener('load')
works in all mobile browsers correctly
appid and serverUrl are not the problem, they are filled in properly. According to me it should not be public, if they are public, anyone can copy it and use our servers to take bandwidth.
window.addEventListener should be there, i will check with otherwise authenticate will not trigger.
ok figured it out, it is happening because of the network delay, user is clicking the connect before everything is loaded.