Kevin:
What does the isconnectedMetamask() do? From what you posted, addNetwork() only adds a network, but does not actually check if the user is connected to that network (it basically asks "Do you want to add network X to MetaMask" and a few warnings). If isconnectedMetamask() only checks if the user has MetaMask (as the name suggest), but not if the user uses the testnet, then that's the fault.
Cainuriel:
I did not share all the code. The code above was to show that if we detected the user network. As you well indicate, something else must be done:
useEffect(function () {
console.log('Dentro del useEffect network')
if(network !== BINANCENETWORK && network !== 'no-net' ){
Swal.fire({
title: t('red'),
//text: `Cambia a BSC si la tienes o sigue el siguiente tutorial para configurarla`,
text: t('cambia-red')+network,
showCancelButton: true,
confirmButtonText: t('btn-configurar-red'),
//imageUrl: 'https://i2.wp.com/criptotendencia.com/wp-content/uploads/2020/04/binance-smart-chain.jpg?fit=1200%2C674&ssl=1',
imageUrl: 'https://cryptodaily.io/wp-content/uploads/2021/07/p-2.png',
imageWidth: 300,
imageAlt: 'Red Binance Smart Chain',
}).then((result) => {
/* Read more about isConfirmed, isDenied below */
if (result.isConfirmed) {
addNetwork();
//window.open('https://academy.binance.com/es/articles/connecting-metamask-to-binance-smart-chain', '_blank');
}
})
} else {
fetchrate();
getBalance();
}
console.log('useEffect de control de network. resultado', network );
},[network]);
We have this useEffect that is activated in each change of variable network
, which is in charge of controlling the user's network.
If we detect that it is not in the network it touches, in the tests the bnbt network, a modal appears with a button that, if the user touches it, will change to the test network if it has it installed, or otherwise, it will automatically install this network so you can interact with the contract.
We have this useEffect that is activated in each change of variable network
, which is in charge of controlling the user's network.
Obviously we do not force the user to do something that he considered excessive. And above all I would never imagine that, from the maintet, the contract deployed in the testnet could be activated
Kevin:
The transaction implies that the user was in fact on mainnet but calling a function provided by the dApp.
Cainuriel:
but in other network.
That function is the Openzeppelin Crowdsale buyTokens function.
I still don't understand how the function can be activated in testnet being in another chain.
Kevin:
Functions are converted to bytecode and an average user does not know neither the exact function parameters (the user would have to take the input, convert it to bytecode, create a custom function call), nor is there a contract on the mainnet and even the contract on testnet is not verified. Almost impossible that the user did all of that work manually. So the dApp definetly is responsible for this imo. If isconnectedMetamask() does check the testnet, then there can be an other issue too (like the check not working properly or so).
Cainuriel:
The isConnectedMetamask function works perfectly for what it is, to acquire the provider and the user's account. However, it would not be bad to review it for what you comment.