简体   繁体   English

我正在为合同添加价值,但我收到此错误“VM 错误恢复”

[英]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?在部署之前插入值时,我收到此错误,如果我不这样做,余额为 0,为什么? (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.你可以做的是转移 msg.value,这是作为交易价值发送的以太币。

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:

  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 .避免使用transfer

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚为什么在处理事务时出现 VM 异常:恢复错误 - Cannot figure out why I am getting VM Exception while processing transaction: revert error 我正在使用 RIDE 测试多签合同,但出现以下错误? - I'm testing a multi-sig contract using RIDE but I have got the following error? Openzepplin众包合同获得:处理事务时VM异常:恢复错误 - Openzepplin crowdsale contract got: VM Exception while processing transaction: revert error 松露错误:错误:处理事务时VM异常:恢复 - Truffle error: Error: VM Exception while processing transaction: revert 向以太坊智能合约发送价值时出错 - Error sending value to Ethereum smart contract Chainlink 返回错误:处理事务时出现 VM 异常:还原 - Chainlink Returned error: VM Exception while processing transaction: revert 我尝试使用一些 eth 在 rinkeby tes.net 上部署合约,但出现错误 - I try to deploy a contract on rinkeby testnet with some eth but there is an error occurred 在智能合约 Fundme 中出现错误,错误是“VM 执行错误。恢复为 0x” - Getting an error in smart contract Fundme, The error is "VM execution error. Reverted to 0x" 错误:返回错误:处理事务时出现 VM 异常:恢复余额不足 — 给出的原因:余额不足 - Error: Returned error: VM Exception while processing transaction: revert Not Enough Balance — Reason given: Not Enough Balance 我在为我的 Solidity 合约编写测试时遇到问题? - I'm having problems writing test for my Solidity contract?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM