Advertisement
George_Ivanov05

Solidity First smart Contract

Oct 2nd, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | Cryptocurrency | 0 0
  1. // SPDX-License-Identifier: Unlicensed
  2.  
  3. pragma solidity ^0.8.7;
  4.  
  5. // Data Types
  6. // uint amountOfMoney (int)
  7. // bool isTotal
  8. // string 'Hello'
  9. // address walletAddress
  10.  
  11. // uint[5] names; // declaring array of five
  12. // uint[] names;
  13. // names.push('Travis')
  14. // names[0] = 'Phil'
  15. // delete names
  16.  
  17. // Dictionaries/ Maps
  18. // mapping (string => uint) public kids
  19.  
  20. // Functions
  21. // function increment(uint x) returns(uint) {
  22. // x ++;
  23. // return x
  24. // }
  25. // increment(10)
  26.  
  27. contract CryptoKids {
  28. // owner DAD
  29. address owner;
  30.  
  31. event LogKidFundingReceived(address addr, uint amount, uint contractBalance);
  32.  
  33. constructor() {
  34. owner = msg.sender;
  35. }
  36.  
  37. // define Kid
  38. struct Kid {
  39. address payable walletAddress;
  40. string firstName;
  41. string lastName;
  42. uint releaseTime;
  43. uint amount;
  44. bool canWithdraw;
  45. }
  46.  
  47. Kid[] public kids;
  48.  
  49. modifier onlyOwner() {
  50. require(msg.sender == owner, "Only the owner can add kids");
  51. _;
  52. }
  53.  
  54. // add kid to contract
  55. function addKid(address payable walletAddress, string memory firstName, string memory lastName, uint releaseTime, uint amount, bool canWithdraw) public onlyOwner {
  56. kids.push(Kid(
  57. walletAddress,
  58. firstName,
  59. lastName,
  60. releaseTime,
  61. amount,
  62. canWithdraw
  63. ));
  64. }
  65.  
  66. function balanceOf() public view returns(uint) { // !!! returns !!! / !!! View !!! (instead of view you can use pure, but pure is more strict than view)
  67. return address(this).balance;
  68. } // BalanceOf appears on the Depliyed Contract
  69.  
  70. // deposit funds to contract, specifical to a kid's account // deposit button in the contract appears
  71. function deposit(address walletAddress) payable public {
  72. addToKidsBalance(walletAddress);
  73. }
  74.  
  75. function addToKidsBalance(address walletAddress) private {
  76. for(uint i = 0; i < kids.length; i++) {
  77. if(kids[i].walletAddress == walletAddress) {
  78. kids[i].amount += msg.value;
  79. emit LogKidFundingReceived(walletAddress, msg.value, balanceOf());
  80. }
  81. }
  82. }
  83.  
  84. function getIndex(address walletAddress) view private returns(uint) {
  85. for(uint i = 0; i < kids.length; i++) {
  86. if (kids[i].walletAddress == walletAddress) {
  87. return i;
  88. }
  89. }
  90. return 999;
  91. }
  92.  
  93. // kid checks if able to withdraw
  94. function availableToWithdraw(address walletAddress) public returns(bool) {
  95. uint i = getIndex(walletAddress);
  96. require(block.timestamp > kids[i].releaseTime, "You cannot withdraw yet");
  97. if (block.timestamp > kids[i].releaseTime) {
  98. kids[i].canWithdraw = true;
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. }
  104.  
  105. // withdraw money
  106. function withdraw(address payable walletAddress) payable public {
  107. uint i = getIndex(walletAddress);
  108. require(msg.sender == kids[i].walletAddress, "You must be the kid to withdraw");
  109. require(kids[i].canWithdraw == true, "You are not able to withdraw at this time");
  110. kids[i].walletAddress.transfer(kids[i].amount);
  111. }
  112.  
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement