Solidity Transactions – How to Transfer Ethers from a Contract to Another Specific Address

accountsaddressesbalancessoliditytransactions

I want to write a smart contract that, when receiving money, transfers 50% to a specific address of the first wallet (0x583031D1113aD414F02576BD6afaBfb302140225)

and transfers 25% to a specific address of the second wallet (0xdD870fA1b7C4700F2BD7f44238821C26f7392148).

I wrote it like, but it doesn't work and Remix IDE swears at these lines:

address payable addr1 = address(0x583031D1113aD414F02576BD6afaBfb302140225);
address payable addr2 = address(0xdD870fA1b7C4700F2BD7f44238821C26f7392148);

Remix IDE writes: type address is not implicitly convertible

P.S.
I watched tutorials on YouTube, in which the authors talk about creating a function in which you need to enter the wallet address for the transaction in the Remix IDE menu.
Example:
enter image description here

It doesn't suit me.

I want 50% to be sent to the first wallet and 25% to the second one.

SmartContract:

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

contract Demo {
        constructor() payable{}
        receive() external payable{}

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

        address payable addr1 = address(0x583031D1113aD414F02576BD6afaBfb302140225);
        address payable addr2 = address(0xdD870fA1b7C4700F2BD7f44238821C26f7392148);

        function withdrawFunds() external {
            //address target = payable(_to);
            addr1.transfer(_50);
            addr2.transfer(_25);
        }

        function receiveFunds() external payable {}

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

Best Answer

You need to cast the address type to address payable type. There's a shortcut for it (https://docs.soliditylang.org/en/v0.6.0/types.html#address): address payable addr1 = payable(0x583031D1113aD414F02576BD6afaBfb302140225);

So just change the address casting to payable casting and it should work.

Related Topic