0

I am deploying a contract to Ethereum EVM environment with Ropsten testing network. It sends 1 ether to another account which is passed from the method paratemter.

After deploy, I set the target account's address 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 when call transfer method. But I see below error:

transact to Leger.transfer errored: VM error: revert.
    The transaction has been reverted to the initial state.
Reason provided by the contract: "Transfer failed.".
Debug the transaction to get more information.

My account has 99 ethers and why I got above error?

// SPDX-License-Identifier: MIT
pragma solidity 0.7.4;
pragma experimental ABIEncoderV2;

contract Leger {

    address payable owner;

    constructor(){
        owner = msg.sender;
    }

    function transfer(address payable _recipient) public payable {
        
        bool success = _recipient.send(1);
        require(success, "Transfer failed.");
    }


    function getBalance() public view returns (uint256) {
        return owner.balance;
    }
    

}

I tired to fund first by setting up the 4th parameter when deploy (10 ether) but I got this error on deploying:

enter image description here

enter image description here

0

2 Answers 2

0

The problem is that your smart contract is not funded. You can verify this by changing the code of your getBalance function like this:

function getBalance() public view returns (uint256) {
    return address(this).balance;
}

Now, if you call getBalance it will return 0.

If you want to transfer ether from your smart contract you have to fund it first. If you are using remix you can do this by setting a value (4th input field from the top in the "DEPLOY & RUN TRANSACTIONS" tab) and then call the transfer function.

7
  • I have tried to set 4 ether when deploy but got an error. I updated the info in the question. Nov 29, 2021 at 22:16
  • Deploy the smart contract with value=0. Afterwards you set the value to 4 ether and call the transfer function and it should work. Be aware that you are sending 1 wei in the transfer function and not 1 ether.
    – GongFu
    Nov 29, 2021 at 23:45
  • If you want to send ether to your smart contract during the deployment you have to make your constructor payable like this: constructor() payable {...}
    – GongFu
    Nov 30, 2021 at 0:09
  • yes, you are right. That works. but no doc mention that Nov 30, 2021 at 0:29
  • If I deploy the contract via truffle, where should I specify the 4th parameter? Nov 30, 2021 at 9:33
0

the error you are getting during deployment is caz your constructor is not payable and you are trying to send 10 ETH when deploying the contract.

The Account balance screenshot which you have provided is the external account balance and not the smart contract balance.

To transfer the balance from smart contract first you should deposit the funds inside it .. for that you can implement a fallback or receive function.

Here you can find how a fallback should be written.

https://docs.soliditylang.org/en/v0.8.16/contracts.html?highlight=fallback#fallback-function

the below code should work for you.

// SPDX-License-Identifier: MIT
pragma solidity 0.7.4;
pragma experimental ABIEncoderV2;

contract Leger {

address payable owner;

constructor() payable {
    owner = msg.sender;
}

function transfer(address payable _recipient) public payable {
    
    bool success = _recipient.send(1);
    require(success, "Transfer failed.");
}


function getBalance() public view returns (uint256) {
    return owner.balance;
}

fallback() external payable {}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.