1

I was making a smart contract in which I wanted to send and receive ether.

My code:

contract coin_transfer_to {
    function() public payable {
    }
    function get_balance() public returns (uint)
    {
        returns address(this).balance;
    }
}

contract coin_transfer_from{
    coin_transfer_to private _instance;
    function coin_transfer_from () public
    {
        _instance = new coin_transfer_to();
    }
    
    function get_balance() public returns (uint)
    {
        return address(this).balance;
    }
    
    function get_balance_of_instance() public returns (uint)
    {
       return _instance.get_balance();
    }
    
    function () public payable 
    {
        address(_instance).send(msg.value);
    }
}

I got the following error in line 2 of above code which is function() public payable:

error in line 2 of above code which is function() public payable{

1
  • It would be helpful if you could edit your question to display the code properly. The contract definition is unreadable.
    – Undead8
    Apr 18, 2021 at 0:00

2 Answers 2

0

In solidity v6 and above payable functions have to be written like this receive() public payable

2
  • it now gives error in the same line as , ParserError : Expected identifier but got '(' recieve() public payable { ^ Apr 17, 2021 at 23:38
  • 1
    Oh yes instead of public payable has to be declared external. So write it like receive() external payable
    – mzaidi
    Apr 18, 2021 at 6:18
0

You can use the latest version of the Solidity compiler.

I refactored your code to add a function to withdraw the funds easily from both contracts so you make sure to not lose funds. Notice that only the owner, the one that deployed the contract, is able to withdraw all the contract balances:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.6;

contract coin_transfer_to {

    receive() external payable {}

    address public owner;

    modifier onlyOwner() {
        require(owner == msg.sender, "Not owner");
        _;
    }

    function get_balance() public view returns(uint) {
        return address(this).balance;
    }

    function withdraw(address payable recipient) public onlyOwner {
        (bool success, ) =  recipient.call{value: address(this).balance}("");
        require(success, "Sending the balance was not successful");
    }

}

contract coin_transfer_from {

    coin_transfer_to private _instance;
    address public owner;

    modifier onlyOwner() {
        require(owner == msg.sender, "Not owner");
        _;
    }

    constructor() {
        _instance = new coin_transfer_to();
        owner = msg.sender;
    }
    
    function get_balance() public view returns (uint) {
        return address(this).balance;
    }
    
    function get_balance_of_instance() public view returns (uint) {
       return _instance.get_balance();
    }

    function withdraw(address payable recipient) public onlyOwner {
        (bool success, ) =  recipient.call{value: address(this).balance}("");
        require(success, "Sending the balance was not successful");
    }
    
    receive() external payable {
        bool success = payable(address(_instance)).send(msg.value);
        require(success, "Sending the balance was not successful");
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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