2

I am relatively new to programming in solidity and trying to build my own auction platform for demonstrational purposes. What I am trying to do is, to send ETH to a contract and whenever the auction ends, the funds will get re transferred to the auctioneer. I am trying to make a small test contract which only does the payment to the smart contract and allows refund to users. The process of making a payment to the smart contract using a function however, does not work. This is the code I am using:

    pragma solidity ^0.5.6;

contract testContract {

    constructor () public payable{
    }

    function transferFunds () external payable {
      // this method supposed to send ether to the contract address, however it doesnt
        address(this).transfer(1 ether);

    }
    function refund () external payable {
        msg.sender.transfer(address(this).balance);
    }

    function showBalance ()  public returns  (uint256){
        return  address(this).balance;
    }
    function returnContractAdress () public returns (address){
       return address(this);
    }
}

The error I get is that address(this) is not a payable address, however I read online that it supposed to be a payable address. Whenever I try to convert it to a payable address it doesnt work. I spend so much time reading post on stackexchange and overflow but cant seem to figure out a solution. Can someone help me out?

To clarify, this is what I am trying to build:

enter image description here

0

Transferring Ether is always implicit - you don't have to (and you can't) write code for it. As long as you have some function which is marked payable and its execution doesn't revert the function can receive Ether without any code. All received Ether is automatically stored in the contract's balance without any code.

So you can either just return all code from inside your function transferFunds or use what is called a payable fallback function which receives any Ether sent to the contract if no specific function is called. More about this for example here: https://www.bitdegree.org/learn/solidity-fallback-functions .

So all received Ethers are always automatically stored and you can't write code to handle that - unless the execution reverts in which case the Ethers are returned.

  • Thank you for your answer! Is this the reason why so many people use their own currency / tokens in order to do transactions on their DAPP? – Nils Apr 3 at 12:09
  • No, this is just a syntax/semantics issue. The reasons for using custom tokens are something completely different. – Lauri Peltonen Apr 3 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.