Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.5.1;
- contract WorkName {
- enum State { Empty, Initiator, Participant }
- struct Swap {
- //-----------------отдача залога-----------------
- uint initTimestampP1;
- uint refundTimeP1;
- //---------------отдача "самоката"---------------
- uint initTimestampP2;
- uint refundTimeP2;
- //---------ключи для первой части сделки---------
- bytes20 hashedSecretA;
- bytes secretA;
- //---------ключи для второй части сделки---------
- bytes20 hashedSecretB;
- bytes secretB;
- //-----------------все остальное-----------------
- address payable initiator;
- address payable participant;
- uint256 value;
- bool emptied;
- State state;
- }
- mapping(bytes20 => Swap) public swaps;
- event Refunded(uint _refundTime);
- event Redeemed(uint _redeemTime);
- event Participated(
- address payable _initiator,
- address payable _participator,
- bytes20 _hashedSecretA,
- bytes20 _hashedSecretB,
- uint256 _value
- );
- event Initiated(
- uint _initTimestampP1,
- uint _refundTimeP1,
- uint _refundTimeP2,
- bytes20 _hashedSecretA,
- bytes20 _hashedSecretB,
- address payable _participant,
- address payable _initiator,
- uint256 _funds
- );
- constructor() public {}
- modifier isRefundableP1(bytes20 _hashedSecret) { // определение того, что время сделки прошло
- require(block.timestamp > swaps[_hashedSecret].initTimestampP1 + swaps[_hashedSecret].refundTimeP1);
- require(swaps[_hashedSecret].emptied == false);
- _;
- }
- modifier isRedeemableP1(bytes20 _hashedSecret, bytes memory _secret) {//определение завершение сделки и сравнения секретного значения
- require(ripemd160(_secret) == swaps[_hashedSecret].hashedSecretA);
- require(block.timestamp < swaps[_hashedSecret].initTimestampP1 + swaps[_hashedSecret].refundTimeP1);
- require(swaps[_hashedSecret].emptied == false);
- _;
- }
- //------------------------------------------------
- modifier isRefundableP2(bytes20 _hashedSecret, bytes memory _secret) { // определение того, что время сделки прошло
- require(ripemd160(_secret) == swaps[_hashedSecret].hashedSecretA);
- require(block.timestamp > swaps[_hashedSecret].initTimestampP2 + swaps[_hashedSecret].refundTimeP2);
- require(swaps[_hashedSecret].emptied == false);
- _;
- }
- modifier isRedeemableP2(bytes20 _hashedSecret, bytes memory _secret) {//определение завершение сделки и сравнения секретного значения
- require(ripemd160(_secret) == swaps[_hashedSecret].hashedSecretB);
- require(block.timestamp < swaps[_hashedSecret].initTimestampP2 + swaps[_hashedSecret].refundTimeP2);
- require(swaps[_hashedSecret].emptied == false);
- _;
- }
- modifier isInitiator(bytes20 _hashedSecret) {
- require(msg.sender == swaps[_hashedSecret].initiator);
- _;
- }
- modifier isNotInitiated(bytes20 _hashedSecret) {
- require(swaps[_hashedSecret].state == State.Empty);
- _;
- }
- function initiate (uint _refundTimeP1,uint _refundTimeP2,bytes20 _hashedSecretA,bytes20 _hashedSecretB,address payable _participant) //
- payable
- isNotInitiated(_hashedSecretA)
- public
- {
- swaps[_hashedSecretA].refundTimeP1 = _refundTimeP1;
- swaps[_hashedSecretA].refundTimeP2 = _refundTimeP2;
- swaps[_hashedSecretA].initTimestampP1 = block.timestamp;
- swaps[_hashedSecretA].hashedSecretA = _hashedSecretA;
- swaps[_hashedSecretA].hashedSecretB = _hashedSecretB;
- swaps[_hashedSecretA].participant = _participant;
- swaps[_hashedSecretA].initiator = msg.sender;
- swaps[_hashedSecretA].state = State.Initiator;
- swaps[_hashedSecretA].value = msg.value;
- emit Initiated(
- swaps[_hashedSecretA].initTimestampP1,
- _refundTimeP1,
- _refundTimeP2,
- _hashedSecretA,
- _hashedSecretB,
- _participant,
- msg.sender,
- msg.value
- );
- }
- function participate(uint _refundTimeP1,uint _refundTimeP2,bytes20 _hashedSecretA,bytes20 _hashedSecretB,address payable _initiator) //
- payable
- isNotInitiated(_hashedSecretA)
- public
- {
- swaps[_hashedSecretA].refundTimeP1 = _refundTimeP1;
- swaps[_hashedSecretA].refundTimeP2 = _refundTimeP2;
- swaps[_hashedSecretA].initTimestampP1 = block.timestamp;
- swaps[_hashedSecretA].participant = msg.sender;
- swaps[_hashedSecretA].initiator = _initiator;
- swaps[_hashedSecretA].value = msg.value;
- swaps[_hashedSecretA].hashedSecretA = _hashedSecretA;
- swaps[_hashedSecretA].hashedSecretB = _hashedSecretB;
- swaps[_hashedSecretA].state = State.Participant;
- emit Participated(_initiator,msg.sender,_hashedSecretA,_hashedSecretB,msg.value);
- }
- function redeemPartOne(bytes memory _secretA, bytes20 _hashedSecretA) // успешное завершение сделки
- isRedeemableP1(_hashedSecretA, _secretA)
- public
- {
- if(swaps[_hashedSecretB].state == State.Initiator){
- //Initiator gets Access
- }
- emit Redeemed(block.timestamp);
- swaps[_hashedSecretA].secretA = _secretA;
- swaps[_hashedSecretA].initTimestampP2 = block.timestamp;
- }
- function refundPartOne(bytes20 _hashedSecret)
- isRefundableP1(_hashedSecret)
- public
- {
- if(swaps[_hashedSecret].state == State.Initiator){
- swaps[_hashedSecret].initiator.transfer(swaps[_hashedSecret].value);
- }
- emit Refunded(block.timestamp);
- }
- ////-----------------------------------------------
- function redeemPartTwo(bytes memory _secretB, bytes20 _hashedSecretB) // успешное завершение сделки
- isRedeemableP2(_hashedSecretB, _secretB)
- public
- {
- if(swaps[_hashedSecretB].state == State.Participant){
- //Participant get aссess back
- }
- if(swaps[_hashedSecretB].state == State.Initiator){
- swaps[_hashedSecretB].initiator.transfer(swaps[_hashedSecretB].value);
- }
- swaps[_hashedSecretB].emptied = true;
- emit Refunded(block.timestamp);
- swaps[_hashedSecretB].secretB = _secretB;
- }
- function refundPartTwo(bytes memory _secretA, bytes20 _hashedSecretA)
- isRefundableP2(_hashedSecretA, _secretA)
- public
- {
- if (swaps[_hashedSecretA].state == State.Participant){
- swaps[_hashedSecretA].participant.transfer(swaps[_hashedSecretA].value);
- }
- swaps[_hashedSecretA].emptied = true;//
- emit Redeemed(block.timestamp);
- }
- }
- //1. Алиса отправляет Бобу большой залог (Атомик своп залог-самокат)
- //2. Залог лочится в контракте и не отдается Бобу, Он сможет его забрать по истечении времени
- //3. Боб отправляет Алисе самокат
- //4. Алиса катается на самокате n часов
- //5. Алиса отдает самокат обратно Бобу
- //6. В контракте из залога вычитается стоимость аренды и отправляется Бобу вместе с самокатом
- //7. Остаток залога отправлется обратно Алисе
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement