[英]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.