EatTheBlocks Forum

Auto send ether from contract to someone directly without confirming it with MetaMask

Hello, I just joined today, and I really have an urgent question to ask. Thanks for your time.

Actually, I posted this on https://ethereum.stackexchange.com/questions/101534/auto-send-ether-from-contract-to-someone-directly-without-confirming-it-with-met but no one answered there. I’m hoping you can help me. This is my situation:


Here is the method inside the solidity contract

function payUser(address payable _to, uint _amount) public {
        _to.transfer(_amount);

        // (bool sent, bytes memory data) = _to.call{value: _amount}("");
        // require(sent, "Failed to send Ether");
    }

The concept of this dapp is that whenever a user wins a game the smart contract should automatically send the won amount of ether.

The problem I’m facing right now is that when I trigger this code using Web3 it will always open the metamask for confirmation. That’s not what I want to happen.

const payUserHelper = async () => {
        try {
            const contractAddress = await getContractAddressHelper();
            if (contractAddress) {
                await myContract.methods.payUser('0x9b5e257A1e282768E81A5520B151c8Fb96cD126a', web3.utils.toWei(`${0.0997}`)).send({ from: contractAddress });
            }
        } catch (e) {
            console.log(e)
        }
    }

NOTE: I just hard coded the receiver for testing purposes, that is an account inside a Ganache. By the way, the smart contract is deployed locally in Ganache. And all accounts for testing is within that Ganache setup.

THE GOAL HERE IS:

  • whenever a user wins a game the smart contract should automatically send the won amount of ether WITHOUT opening the metamask for confirmation.

THE CURRENT PROBLEM:

  • If I use the contract address inside the .send({ from: contractAddress }), it would log an error that address is invalid ethereum address
  • If I use the current Metamask selected account (Ganache network) then the Metamask would pop up and ask for confirmation, which is not ideal for this application

I read in the Web3 docs that in order to change something in the smart contract like, it should use the methods’ .send() function but the problem here is that it requires a ‘from’ property. That would always trigger the Metamask to open and confirm.

Thank you. I really someone could give me some light on this one.