简体   繁体   中英

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). 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.

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.

If you want to redirect the sent amount, there's the msg.value global variable, which reflects the current value sent with the transaction.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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