0

This is the simple contract in which I am allowing user to open an account i.e smart contract acts as a account. I have kept an initial balance limit of 1 ether i.e user is bond to pay 1 ether at the time of opening of the account. 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];
}
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Shamoon97
  • 332
  • 2
  • 12

1 Answers1

0

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? 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 :)

Crazy Punch Man
  • 116
  • 1
  • 5