Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. pragma solidity >=0.4.22 <0.6.0;
  2.  
  3. contract BottleDepositSystem {
  4.  
  5. struct Person {
  6. uint amountOfBottles;
  7. mapping(uint => uint) bottleIds;
  8. }
  9.  
  10. struct Bottle {
  11. uint id;
  12. uint deposit;
  13. address payable originalPurchasor;
  14. address owner;
  15. }
  16.  
  17. mapping(address => Person) public personList;
  18. mapping(uint => Bottle) public bottleList;
  19. uint amountOfOverallBottles;
  20.  
  21. modifier onlyWithBottles() {
  22. require(
  23. personList[msg.sender].amountOfBottles > 0,
  24. "Sender has no bottles to return/delegate."
  25. );
  26. _;
  27. }
  28.  
  29. modifier onlyIfSenderIsNotReceiver(address to) {
  30. require(
  31. msg.sender != to,
  32. "Sender is not allowed to be the receiver"
  33. );
  34. _;
  35. }
  36.  
  37. constructor() public{
  38. }
  39.  
  40. function buyBottle(uint256 amount) payable public {
  41. require(msg.value == amount,
  42. "Parameter equals not sent value.");
  43. require(msg.value > 0 ,
  44. "Bottledeposit needs to be above 0.");
  45.  
  46. Person storage buyer = personList[msg.sender];
  47. Bottle memory bottle = Bottle(amountOfOverallBottles, amount, msg.sender, msg.sender);
  48.  
  49. amountOfOverallBottles++;
  50. bottleList[bottle.id] = bottle;
  51. buyer.bottleIds[buyer.amountOfBottles++] = bottle.id;
  52. }
  53.  
  54. function giveBottleTo(address to) public onlyWithBottles onlyIfSenderIsNotReceiver(to){
  55. Person storage owner = personList[msg.sender];
  56. Person storage newOwner = personList[to];
  57. Bottle storage bottle = bottleList[owner.bottleIds[owner.amountOfBottles-1]];
  58.  
  59. newOwner.bottleIds[newOwner.amountOfBottles] = bottle.id;
  60. newOwner.amountOfBottles++;
  61.  
  62. bottle.owner = to;
  63. owner.amountOfBottles--;
  64. }
  65.  
  66. function returnBottle() public payable onlyWithBottles returns (uint amount){
  67. Person storage sender = personList[msg.sender];
  68. Bottle memory returnedBottle = bottleList[sender.bottleIds[sender.amountOfBottles-1]];
  69. sender.amountOfBottles--;
  70.  
  71. if(returnedBottle.owner != returnedBottle.originalPurchasor){
  72. msg.sender.transfer(returnedBottle.deposit/2);
  73. returnedBottle.originalPurchasor.transfer(returnedBottle.deposit/2);
  74. }else{
  75. msg.sender.transfer(returnedBottle.deposit);
  76. }
  77. amount = returnedBottle.deposit;
  78. }
  79.  
  80. function returnAllBottles() public payable onlyWithBottles returns (uint total){
  81. Person storage sender = personList[msg.sender];
  82. uint tempAmount = sender.amountOfBottles;
  83. uint tempTotal;
  84. for(uint i = 0; i < tempAmount; i++){
  85. tempTotal += returnBottle();
  86. }
  87. total = tempTotal;
  88. }
  89.  
  90. function getBalance() public view returns (uint256) {
  91. return address(this).balance;
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement