How to deposit ether to an account using solidity and web3?

孤人 提交于 2021-01-29 05:49:21

问题


I am building a sample smart contract . I am trying to deposit ether from 1 account to another but the issue is amount is deducted from the sender's account but not able to deposit to receiver's account.

Here is my contract code :

pragma solidity ^0.5.0;

 contract ApprovalContract{
address public sender;
address public receiver;

function deposit(address _receiver) external payable {
    require(msg.value > 0);
    sender = msg.sender;
    receiver = _receiver;
     address payable _rec = receiver.make_payable();
     _rec.transfer(address(this).balance);
}



using address_make_payable for address;

}
 library address_make_payable {
function make_payable(address x) internal pure returns (address payable) {
  return address(uint160(x));
 }
 }

Here is my web3 code :

 let ApprovalContract = new web3.eth.Contract(
    abi,
    "0xABd4495e3afc9E61Bbbf54620587aB4B48CEd7d3" //contract address
  );

ApprovalContract.methods
      .deposit("0xF33ca58CbD25bC0fFe5546Dc494E5c61C8D7fFc3")
      .send(
        {
          from: "0x7186ffcf5ea764257fdbaefccb9472f054495d11",
          gas: 100000,
          value: web3.utils.toWei("1", "ether")
        },
        (err, res) =>
          err
            ? console.log(`error: ${err}`)
            : console.log(`Success: ${res}`)
      );

回答1:


Just add some mapping in your smart contract, like this mapping( address => uint256 ) balance and this is fixed smart contract for you :

pragma solidity ^0.5.0;
contract ApprovalContract{
    address payable owner = msg.sender;
    mapping(address => uint256) balance;
    function deposit() external payable {
            require(msg.value > 0);
            sender = msg.sender;
            deposited = msg.value;
            receiver = owner;
            balance[sender] = deposited;
            receiver.transfer(deposited);
    }
}


来源:https://stackoverflow.com/questions/56397786/how-to-deposit-ether-to-an-account-using-solidity-and-web3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!