简体   繁体   English

无法从智能合约向 msg.sender 发送以太币

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

相关问题 在solidity和ether.js中更改`msg.sender` - change `msg.sender` in solidity and ether.js 如何将令牌发送到智能合约调用? - How to send tokens to smart contract call? 接受JavaScript中的参数以发送到Solidity智能合约 - Accepting parameters in javascript to send to solidity smart contract 如何在智能合约中将地址连接到午餐 function? | TypeError:无法读取未定义的属性(读取“连接”)ether.js - how can i connect an address to lunch a function in the smart contract ? | TypeError: Cannot read properties of undefined (reading 'connect') ether.js 从 web3 将数组传递给智能合约 - Passing array to smart contract from web3 如何将 ERC20 代币发送到智能合约余额? - How to send ERC20 token to smart contract balance? 从我的智能合约中调用已部署的智能合约的功能 - Call function of a already deployed smart contract from within my smart contract 如何从js访问智能合约的公共地址? - How to access public address of smart contract from js? 映射动态数组从智能合约起反应,其值包括地址和字符串 - map dynamic array in react from a smart contract with values an address and a string 如何从 Web 应用程序签署和调用锚 (solana) 智能合约 - How to Sign and call anchor (solana) smart contract from web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM