简体   繁体   English

如何在solidity中使用传递函数

[英]How to use transfer function in solidity

I created smart contract to transfer ETH from one account to another, ETH deduction occurs but send to other address(which is not specified).我创建了智能合约将 ETH 从一个账户转移到另一个账户,发生 ETH 扣除但发送到另一个地址(未指定)。 Please help me to resolve this problem.请帮我解决这个问题。

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.12;

contract ETH{
    address public  buyer;
    uint public amt;
    address public seller;
    constructor(){
        seller = msg.sender;
    }
    function add(address payable _buyer) payable public{
        buyer = _buyer;
        payable(buyer).transfer(amt);
    }
    function bal() view public returns(uint){
        return buyer.balance;
    }
    function s() payable public returns(uint){
        return msg.value;
    }
}

The default value of amt is 0 and your code does not set this property (to a non-zero value) anywhere. amt的默认值为 0,并且您的代码不会在任何地方设置此属性(设置为非零值)。

Which effectively makes your .transfer(amt) function to send 0 ETH to the buyer and keep all the ETH, that you sent along with the transaction, in the contract.这有效地使您的.transfer(amt)函数向buyer发送 0 ETH,并将您与交易一起发送的所有 ETH 保留在合同中。

If you want to redirect the sent amount, there's the msg.value global variable, which reflects the current value sent with the transaction.如果要重定向发送的金额,则有msg.value全局变量,它反映了与交易一起发送的当前值。

payable(buyer).transfer(msg.value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM