Search code examples
solidityweb3js

How to avoid metamask confirmation in web3js


I am trying to call following function from my solidity contract using web3js frontend.

function playerData(address player) public {      
   gameStatus[player] = false;      
}

webjs calls this method using following line of code.

 await myContract.methods.playerData(playerAddress)
                .send(
                    { 
                        from: playerAddress,
                    }
                )

Now when this line is executed, metamask window opens up and waits for confirmation. As there is no token transfer involved in this method, I want to remove confirmation step of metamask.

Please help me understand if there is a way to call this method without confirmation popup from metamask.

Thanks!


Solution

  • If you are going to change the state of the blockchain ( modify a State variable ), you'll need to create a transaction, and since transactions are signed by a user you'll need a wallet ( in this case metamask ) to confirm that transaction.

    Metamask is a wallet, a simple app ( be it browser extension or physical USB ) that allows someone to interact with a blockchain account using a private key, in this case you use the wallet to sign the transaction and paying for the gas fees.

    If you are NOT changing any State variable, just make your functions view or pure and you won't get the pop-up for metamask.

    Now, if you DO change a State variable, the only way you could do it without using metamask is by getting a web3js account object from a private key, and manually signing it with this account ( check out this ), now I heavily advise against this unless you know what you're doing, because if you publish your private key somewhere, anyone will be able to access your account.