S
S
solidussnake2022-03-25 15:07:01
Cryptocurrency
solidussnake, 2022-03-25 15:07:01

CODE REVIEW of SmartContract Solidity. The contract transfers money to 2 wallets. Did you write everything correctly?

I wrote a smart contract on Solidity for Ethereum and Polygon. The contract should receive money to its address and automatically transfer 50% to the first wallet address, transfer 25% to the second wallet. I ask you to make a code review of my contract and point out my mistakes and how they can be corrected. I'm especially interested in how secure this contract is.

PS Thank you for reading.

Smart contract:

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vicky, 2022-03-25
_

Here are the two lines

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

will be executed while the constructor is running. Those. if nothing is sent to the address of the contract (before it is created), then address(this).balance will be equal to 0. These variables must be calculated every time you need to transfer eth.
I also recommend that you first do all the multiplication, and then all the division: less problems with rounding.
For the contract to work automatically, the funds transfer method must be in receive() external payable{}. And it's better not to use balance, because. past remnants will fall into it. In this regard, msg.value is suitable (if we talk about automatic translation).
address payable addr1and the second variable should be made constant if you do not plan to update these addresses in the future. And if we are talking about industrial code with testing, then a good option is to make them immutable and set them in the constructor.
In addition, a method is needed to withdraw the remaining 25%, otherwise they will be stuck at the contract address for good.
And I’ll probably add one more trifle: transfernot the most ideal way to transfer funds (if we are talking about a real contract), due to gas restrictions on such a method, I recommend looking at Address.sol (it seems the sendValue method) from the openzeppelin library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question