AKRA
AKRA

Reputation: 286

Sending eth from contract to wallet address

I have this contract deployed that is meant to send ether from itself to an another account


pragma solidity ^0.8.0;

contract Contract {

    address public owner;
    address public admin;

    constructor(address _admin) public {
        owner = msg.sender;
        admin = _admin;
    }

    modifier onlyOwner(address sender) {
        require(
            sender == admin,
            "Only the admin of the contract can perform this operation."
        );_;
    }

    function sendTo (
        address toAddress
    ) public payable onlyOwner(msg.sender) {
        payable(toAddress).transfer(msg.value);
    }
}

and I try to interact with it like so on the client side:

var contract = new web3.eth.Contract(abi, Address, null);

const transaction = {
            from: mainAcct,
            to: dummyAcct,
            value: '10000000000000000',
            gas: 30000
};

await contract.methods.sendTo(dummyAcct).send(
            transaction , function(err, hash){
            if(!err){
                console.log("Transaction hash:", hash);
            }else{
                console.log(err);
            }
});
} catch (e) {
                console.log(e);
}

why do I get this error in the console:

Error: Transaction has been reverted by the EVM

Upvotes: 1

Views: 2046

Answers (2)

jhonny
jhonny

Reputation: 858

The problem could be with transfer, transfer limit the gas and that could be the reason of the error, try using call instead of transfer, check this web to see how to use it https://solidity-by-example.org/sending-ether/

Upvotes: 1

Kartik Puri
Kartik Puri

Reputation: 467

Looks like you need to fund the contract before sending the amount out I did something like below hre is hardhat object, but there will be way to add funds while deploying

const waveContractFactory = await hre.ethers.getContractFactory('contract_name');
  const waveContract = await waveContractFactory.deploy({
    value: hre.ethers.utils.parseEther('0.1'),
  });

Upvotes: 2

Related Questions