[英]Not able to send ether to msg.sender from smart contract
This is the simple contract in which I am allowing user to open an account ie smart contract acts as a account.这是我允许用户开立账户的简单合约,即智能合约充当账户。 I have kept an initial balance limit of 1 ether ie user is bond to pay 1 ether at the time of opening of the account.
我保持了 1 个以太币的初始余额限制,即用户在开户时需要支付 1 个以太币。 Moreover, user has to set the secret key which will used for withdrawing the balance.
此外,用户必须设置用于提取余额的密钥。 In withdraw function I am unable to transfer ether back to the user.
在提款功能中,我无法将以太币转回给用户。 I am checking the balance of the user and after verifying balance condition.
我正在检查用户的余额并在验证余额状况后。 I am sending back the ether to the depositor.
我将把以太币寄回给存款人。 Can anyone help me I have commented the line on which I am facing the issue.
谁能帮助我我已经评论了我面临的问题。
// 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];
}
}
I think it's a bit strange because Msg.value is the ETH sent to the contract when the user calls the contract, you should want to send the balance on the contract to the user, right?我觉得有点奇怪,因为 Msg.value 是用户调用合约时发送给合约的 ETH,你应该想把合约上的余额发送给用户吧? Then you should change this code
然后你应该改变这个代码
receiver.transfer(msg.value); // issue seems to be on this line
to this对此
receiver.transfer(address(this).balance); // issue seems to be on this line
UPDATE:更新:
// 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];
}
}
I just modified all the code and it should run fine :)我刚刚修改了所有代码,它应该可以正常运行:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.