K
K
konstantinst132022-03-27 23:37:22
Cryptocurrency
konstantinst13, 2022-03-27 23:37:22

Why is the method in receive() external payable{this.withdrawFunds();} not called in Solidity?

I wrote a smart contract, which should automatically transfer 50% to the 1st wallet address, 25% to the second one, 25% to the third one, upon receipt of money to my address. For some reason it doesn't do it automatically.

I am using Remix IDE. I clicked Compile in the left tab of the Solidity Compiler, then went to Deploy & Run transactions, deleted the deployed contracts, wrote at the top in value 1 (selected ether instead of wei on the side) and deployed this contract on the new one.
1 ether was debited from the selected wallet and transferred to the contract account (I found out using the getBalance function), but the money was not transferred to the required wallet addresses. In order for the money to be transferred, you have to click on the withdrawFunds button below (that is, you need to call this function).
I wrote receive() external payable{this.withdrawFunds();}, expecting that because of this line, the contract itself will transfer money when received.
But for some reason it doesn't work. I need the contract to transfer the money itself.

Contract:

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

contract Demo {
        constructor() payable{}
        receive() external payable{this.withdrawFunds();} //почему-то не работает            

        function sendValue(address payable recipient, uint256 amount) internal {
                require(address(this).balance >= amount, "Address: insufficient balance");

                (bool success, ) = recipient.call{value: amount}("");
                require(success, "Address: unable to send value, recipient may have reverted");
            }

        function withdrawFunds() external payable{
            address addr1 = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;
            address addr2 = 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db;
            address addr3 = 0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB;

            //address target = payable(_to);
            address payable wallet1 = payable(addr1); //первый адрес для Украины
            address payable wallet2 = payable(addr2); //второй адрес для Украины
            address payable wallet3 = payable(addr3); //мой другой адрес

            this.getBalance();

            uint _50 = address(this).balance*50/100; //50% of balance of contract
            uint _25 = address(this).balance*25/100; //25% of balance of contract
            uint s_25 = address(this).balance*25/100; //25% of balance of contract

            //wallet1.transfer(_50);
            //wallet2.transfer(_25);
            //wallet3.transfer(s_25);

      sendValue(wallet1, _50);
      sendValue(wallet2, _25);
      sendValue(wallet3, s_25);	
        }

        function receiveFunds() external payable {}

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

        function getMsgValue() public payable returns(uint){
            return msg.value;
        }
}


Thanks for reading.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wonderingpeanut, 2022-03-28
@wonderingpeanut

Hello. You are welcome.
Solidity does not need the this keyword to call functions.

J
Julia Bedrosova, 2022-03-28
@Bedrosova

What makes you think that your receive function should work automatically? It is not an event handler.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question