如何使用solidity和web3将ether存入帐户?

时间:2019-05-31 15:21:40

标签: blockchain ethereum solidity smartcontracts web3js

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

这是我的合同代码:

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代码:

 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 个答案:

答案 0 :(得分:0)

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

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