Answer the question
In order to leave comments, you need to log in
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
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
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 addr1
and 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.transfer
not 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 questionAsk a Question
731 491 924 answers to any question