issues678.sol 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.7.0;
  3. contract Shares {
  4. event Transfer(address to, uint amount, uint balance);
  5. struct Share {
  6. address payable shareOwner;
  7. uint amount;
  8. }
  9. Share[] private _shares;
  10. /// @notice Create the shares object that gives the shares to every body
  11. constructor(address payable sender) {
  12. _shares.push(Share(sender, 1000));
  13. }
  14. function getShares() external view returns(address[] memory, uint[] memory) {
  15. address[] memory retAddress = new address[](uint32(_shares.length));
  16. uint[] memory retShare = new uint[](uint32(_shares.length));
  17. for (uint i = 0; i < _shares.length; i++) {
  18. retAddress[i] = _shares[i].shareOwner;
  19. retShare[i] = _shares[i].amount;
  20. }
  21. return (retAddress, retShare);
  22. }
  23. function _senderIsAShareOwner(address sender) private view returns (bool) {
  24. for (uint i = 0; i < _shares.length; i++) {
  25. if (_shares[i].shareOwner == sender) {
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31. /**
  32. ** @dev Allow a share owner to retrieve his money. It empty the money contained inside of the smart contract to give it to owners.
  33. */
  34. function withdraw(address sender) external {
  35. require(_senderIsAShareOwner(sender), "You can't withdraw if you are not a share owner");
  36. uint curr_balance = address(this).balance;
  37. require(curr_balance > 0, "There is nothing to withdraw");
  38. for (uint i = 0; i < _shares.length; i++) {
  39. uint to_transfer = curr_balance * _shares[i].amount / 1000;
  40. _shares[i].shareOwner.transfer(uint64(to_transfer));
  41. emit Transfer(_shares[i].shareOwner, to_transfer, curr_balance);
  42. }
  43. if (address(this).balance > 0) {
  44. // Send the remaining money to the one who withdraw so there is nothing left on
  45. // the contract
  46. payable(sender).transfer(address(this).balance);
  47. }
  48. }
  49. }
  50. // ---- Expect: diagnostics ----