PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How to use transfer function in solidity

 July 28, 2022     blockchain, ether, ethereum, smartcontracts, solidity     No comments   

Issue

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

Solution

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


Answered By - Petr Hejda
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing