如何使用solidity和web3将ether存入帐户? [英] How to deposit ether to an account using solidity and web3?

查看:122
本文介绍了如何使用solidity和web3将ether存入帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个示例智能合约.我试图将以太币从1个帐户存入另一个帐户,但问题是从发件人的帐户中扣除了金额,但无法存入收款人的帐户.

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));
 }
 }

这是我的web3代码:

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}`)
      );

推荐答案

只需在您的智能合约中添加一些映射,例如 mapping(address => uint256)balance ,这是固定的智能合约为您:

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);
    }
}

这篇关于如何使用solidity和web3将ether存入帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆