无法向msg发送以太.来自智能合约的发送方



这是一个简单的合约,我允许用户开设一个账户,即智能合约作为一个账户。我保留了1个以太币的初始余额限制,即用户在开设账户时必须支付1个以太币。此外,用户必须设置用于提取余额的密钥。在退出功能中,我无法将以太币转移回用户。我正在检查用户的余额,验证余额情况后。我把以太币寄回给存款人。有谁能帮助我吗?我已经评论了我所面临的问题。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract ActualBank{
uint minAccountBalance=1 ether;
address payable owner;
mapping(address => uint256) balance ;
mapping(address => uint256) secretKey;
constructor(){
owner=payable(msg.sender);
}
function openAccount(uint256 _secretKey) payable public returns(uint256) {
require(msg.value >= minAccountBalance,"There must a minimum balance of 1 ether");
balance[msg.sender]+=msg.value;
secretKey[msg.sender]=_secretKey;
return balance[msg.sender];
}
function withDraw(uint256 _secretKey) payable public returns(uint256) {
require(msg.value <= balance[msg.sender],"With drawal value not correct");
require(secretKey[msg.sender] == _secretKey, "Secret key didn't matched");
balance[msg.sender]-=msg.value;
address payable receiver= payable(msg.sender);
receiver.transfer(msg.value);  // issue seems to be on this line 
return balance[msg.sender];
}
function getAccountBalance() public view returns(uint256){
return balance[msg.sender];
}
}

我觉得这有点奇怪,因为Msg。value是用户调用合约时发送给合约的ETH,你应该要将合约上的余额发送给用户,对吗?然后你应该修改这个代码

receiver.transfer(msg.value);  // issue seems to be on this line 

receiver.transfer(address(this).balance);  // issue seems to be on this line 

更新:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract ActualBank{
uint minAccountBalance=1 ether;
address payable owner;
mapping(address => uint256) balance ;
mapping(address => uint256) secretKey;
constructor(){
owner=payable(msg.sender);
}
function openAccount(uint256 _secretKey) payable public returns(uint256) {
require(msg.value >= minAccountBalance,"There must a minimum balance of 1 ether");
balance[msg.sender]+=msg.value;
secretKey[msg.sender]=_secretKey;
return balance[msg.sender];
}
function withDraw(uint256 _secretKey) payable public returns(uint256) {
require(address(this).balance >= balance[msg.sender],"With drawal value not correct");
require(secretKey[msg.sender] == _secretKey, "Secret key didn't matched");
balance[msg.sender] = 0;
address payable receiver= payable(msg.sender);
receiver.transfer(address(this).balance);  // issue seems to be on this line 
return balance[msg.sender];
}
function getAccountBalance() public view returns(uint256){
return balance[msg.sender];
}
}

我刚刚修改了所有的代码,它应该运行良好:)

相关内容

  • 没有找到相关文章

最新更新