Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. pragma solidity ^0.5.1;
  2.  
  3. contract WorkName {
  4.  
  5. enum State { Empty, Initiator, Participant }
  6.  
  7. struct Swap {
  8. //-----------------отдача залога-----------------
  9. uint initTimestampP1;
  10. uint refundTimeP1;
  11. //---------------отдача "самоката"---------------
  12. uint initTimestampP2;
  13. uint refundTimeP2;
  14. //---------ключи для первой части сделки---------
  15. bytes20 hashedSecretA;
  16. bytes secretA;
  17. //---------ключи для второй части сделки---------
  18. bytes20 hashedSecretB;
  19. bytes secretB;
  20. //-----------------все остальное-----------------
  21. address payable initiator;
  22. address payable participant;
  23. uint256 value;
  24. bool emptied;
  25. State state;
  26. }
  27.  
  28. mapping(bytes20 => Swap) public swaps;
  29.  
  30. event Refunded(uint _refundTime);
  31. event Redeemed(uint _redeemTime);
  32. event Participated(
  33. address payable _initiator,
  34. address payable _participator,
  35. bytes20 _hashedSecretA,
  36. bytes20 _hashedSecretB,
  37. uint256 _value
  38. );
  39. event Initiated(
  40. uint _initTimestampP1,
  41. uint _refundTimeP1,
  42. uint _refundTimeP2,
  43. bytes20 _hashedSecretA,
  44. bytes20 _hashedSecretB,
  45. address payable _participant,
  46. address payable _initiator,
  47. uint256 _funds
  48. );
  49.  
  50. constructor() public {}
  51.  
  52. modifier isRefundableP1(bytes20 _hashedSecret) { // определение того, что время сделки прошло
  53. require(block.timestamp > swaps[_hashedSecret].initTimestampP1 + swaps[_hashedSecret].refundTimeP1);
  54. require(swaps[_hashedSecret].emptied == false);
  55. _;
  56. }
  57.  
  58. modifier isRedeemableP1(bytes20 _hashedSecret, bytes memory _secret) {//определение завершение сделки и сравнения секретного значения
  59. require(ripemd160(_secret) == swaps[_hashedSecret].hashedSecretA);
  60. require(block.timestamp < swaps[_hashedSecret].initTimestampP1 + swaps[_hashedSecret].refundTimeP1);
  61. require(swaps[_hashedSecret].emptied == false);
  62. _;
  63. }
  64. //------------------------------------------------
  65. modifier isRefundableP2(bytes20 _hashedSecret, bytes memory _secret) { // определение того, что время сделки прошло
  66. require(ripemd160(_secret) == swaps[_hashedSecret].hashedSecretA);
  67. require(block.timestamp > swaps[_hashedSecret].initTimestampP2 + swaps[_hashedSecret].refundTimeP2);
  68. require(swaps[_hashedSecret].emptied == false);
  69. _;
  70. }
  71.  
  72. modifier isRedeemableP2(bytes20 _hashedSecret, bytes memory _secret) {//определение завершение сделки и сравнения секретного значения
  73. require(ripemd160(_secret) == swaps[_hashedSecret].hashedSecretB);
  74. require(block.timestamp < swaps[_hashedSecret].initTimestampP2 + swaps[_hashedSecret].refundTimeP2);
  75. require(swaps[_hashedSecret].emptied == false);
  76. _;
  77. }
  78.  
  79. modifier isInitiator(bytes20 _hashedSecret) {
  80. require(msg.sender == swaps[_hashedSecret].initiator);
  81. _;
  82. }
  83.  
  84. modifier isNotInitiated(bytes20 _hashedSecret) {
  85. require(swaps[_hashedSecret].state == State.Empty);
  86. _;
  87. }
  88.  
  89. function initiate (uint _refundTimeP1,uint _refundTimeP2,bytes20 _hashedSecretA,bytes20 _hashedSecretB,address payable _participant) //
  90. payable
  91. isNotInitiated(_hashedSecretA)
  92. public
  93. {
  94. swaps[_hashedSecretA].refundTimeP1 = _refundTimeP1;
  95. swaps[_hashedSecretA].refundTimeP2 = _refundTimeP2;
  96. swaps[_hashedSecretA].initTimestampP1 = block.timestamp;
  97. swaps[_hashedSecretA].hashedSecretA = _hashedSecretA;
  98. swaps[_hashedSecretA].hashedSecretB = _hashedSecretB;
  99. swaps[_hashedSecretA].participant = _participant;
  100. swaps[_hashedSecretA].initiator = msg.sender;
  101. swaps[_hashedSecretA].state = State.Initiator;
  102. swaps[_hashedSecretA].value = msg.value;
  103. emit Initiated(
  104. swaps[_hashedSecretA].initTimestampP1,
  105. _refundTimeP1,
  106. _refundTimeP2,
  107. _hashedSecretA,
  108. _hashedSecretB,
  109. _participant,
  110. msg.sender,
  111. msg.value
  112. );
  113. }
  114.  
  115. function participate(uint _refundTimeP1,uint _refundTimeP2,bytes20 _hashedSecretA,bytes20 _hashedSecretB,address payable _initiator) //
  116. payable
  117. isNotInitiated(_hashedSecretA)
  118. public
  119. {
  120. swaps[_hashedSecretA].refundTimeP1 = _refundTimeP1;
  121. swaps[_hashedSecretA].refundTimeP2 = _refundTimeP2;
  122. swaps[_hashedSecretA].initTimestampP1 = block.timestamp;
  123. swaps[_hashedSecretA].participant = msg.sender;
  124. swaps[_hashedSecretA].initiator = _initiator;
  125. swaps[_hashedSecretA].value = msg.value;
  126. swaps[_hashedSecretA].hashedSecretA = _hashedSecretA;
  127. swaps[_hashedSecretA].hashedSecretB = _hashedSecretB;
  128. swaps[_hashedSecretA].state = State.Participant;
  129. emit Participated(_initiator,msg.sender,_hashedSecretA,_hashedSecretB,msg.value);
  130. }
  131.  
  132. function redeemPartOne(bytes memory _secretA, bytes20 _hashedSecretA) // успешное завершение сделки
  133. isRedeemableP1(_hashedSecretA, _secretA)
  134. public
  135. {
  136. if(swaps[_hashedSecretB].state == State.Initiator){
  137. //Initiator gets Access
  138. }
  139.  
  140. emit Redeemed(block.timestamp);
  141. swaps[_hashedSecretA].secretA = _secretA;
  142. swaps[_hashedSecretA].initTimestampP2 = block.timestamp;
  143. }
  144.  
  145. function refundPartOne(bytes20 _hashedSecret)
  146. isRefundableP1(_hashedSecret)
  147. public
  148. {
  149. if(swaps[_hashedSecret].state == State.Initiator){
  150. swaps[_hashedSecret].initiator.transfer(swaps[_hashedSecret].value);
  151. }
  152. emit Refunded(block.timestamp);
  153. }
  154.  
  155. ////-----------------------------------------------
  156.  
  157. function redeemPartTwo(bytes memory _secretB, bytes20 _hashedSecretB) // успешное завершение сделки
  158. isRedeemableP2(_hashedSecretB, _secretB)
  159. public
  160. {
  161. if(swaps[_hashedSecretB].state == State.Participant){
  162. //Participant get aссess back
  163. }
  164. if(swaps[_hashedSecretB].state == State.Initiator){
  165. swaps[_hashedSecretB].initiator.transfer(swaps[_hashedSecretB].value);
  166. }
  167. swaps[_hashedSecretB].emptied = true;
  168. emit Refunded(block.timestamp);
  169. swaps[_hashedSecretB].secretB = _secretB;
  170. }
  171.  
  172. function refundPartTwo(bytes memory _secretA, bytes20 _hashedSecretA)
  173. isRefundableP2(_hashedSecretA, _secretA)
  174. public
  175. {
  176. if (swaps[_hashedSecretA].state == State.Participant){
  177. swaps[_hashedSecretA].participant.transfer(swaps[_hashedSecretA].value);
  178. }
  179. swaps[_hashedSecretA].emptied = true;//
  180. emit Redeemed(block.timestamp);
  181. }
  182. }
  183.  
  184.  
  185. //1. Алиса отправляет Бобу большой залог (Атомик своп залог-самокат)
  186. //2. Залог лочится в контракте и не отдается Бобу, Он сможет его забрать по истечении времени
  187. //3. Боб отправляет Алисе самокат
  188. //4. Алиса катается на самокате n часов
  189. //5. Алиса отдает самокат обратно Бобу
  190. //6. В контракте из залога вычитается стоимость аренды и отправляется Бобу вместе с самокатом
  191. //7. Остаток залога отправлется обратно Алисе
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement