简体   繁体   中英

i'm addming a value to the contract but i recevie this error "VM error revert"

在此处输入图像描述

contract Bank {


    address public admin;

    constructor() {
       admin = msg.sender;
    }

   mapping (address => uint) balance;
   mapping (address => bool) AccountActive;

    function closeAccount() public  payable{
    AccountActive[msg.sender] = false;
        //trasfer all the money from an account closed to the admin
        payable(admin).transfer(balance[msg.sender]);
        
    }

    function viewbalance() public view returns(uint) {
        return balance[msg.sender];
        
    }}

when inserting a value before deployment, I get this error, and if I don't do it in this way the balance is 0, why? (sorry for this noob question)

This error is because you can not use an address ether balance from a smart contract. What I mean is that a smart contract cannot transfer ether from one address to another, because it would be really dangerous.

What you can do is to transfer the msg.value, which is the ether sent as the value of the transaction.

By the way, you should check for correct indentation and symbols placement. Those can lead to errors too.

The issue is here:

payable(admin).transfer(balance[msg.sender]);

You want to transfer money from the admin but the admin has no balance. So you need to send money. For this write this function:

  function depositMoneyToAdmin() payable public {
        // since it is payable, the money that you send would be stored in msg.value
        (bool success,) = admin.call{value: msg.value}("");
        // then add the owner's balance to the mapping so when u call viewBalance, you get the balance of owner
        balance[admin]+=msg.value;
         require(success,"Transfer failed!");
    }

在此处输入图像描述

Avoid using transfer .

https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM