我正在为合同添加价值,但收到此错误 "VM error revert"
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];
}}
在部署前插入值时出现此错误,如果我不这样做,余额为 0,为什么? (抱歉这个菜鸟问题)
此错误是因为您无法使用智能合约中的地址以太币余额。我的意思是智能合约不能将以太币从一个地址转移到另一个地址,因为这真的很危险。
你可以做的是转移 msg.value,这是作为交易价值发送的以太币。
顺便说一句,您应该检查正确的缩进和符号位置。这些也可能导致错误。
问题在这里:
payable(admin).transfer(balance[msg.sender]);
您想向管理员转账,但管理员没有余额。所以你需要汇款。为此写这个函数:
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!");
}
避免使用 transfer
.
https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/
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];
}}
在部署前插入值时出现此错误,如果我不这样做,余额为 0,为什么? (抱歉这个菜鸟问题)
此错误是因为您无法使用智能合约中的地址以太币余额。我的意思是智能合约不能将以太币从一个地址转移到另一个地址,因为这真的很危险。
你可以做的是转移 msg.value,这是作为交易价值发送的以太币。
顺便说一句,您应该检查正确的缩进和符号位置。这些也可能导致错误。
问题在这里:
payable(admin).transfer(balance[msg.sender]);
您想向管理员转账,但管理员没有余额。所以你需要汇款。为此写这个函数:
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!");
}
避免使用 transfer
.
https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/