Demo entry 6751006

smart contracs

   

Submitted by anonymous on Jun 20, 2018 at 22:20
Language: Python. Code size: 2.1 kB.

pragma solidity ^0.4.22; 

  contract Purchase {         
    uint public value;      
    address public seller;  
    address public buyer;   
    enum State { Created, Locked, Inactive } 
    State public state;                  

    function Purchase() public payable { 
        seller = msg.sender;          
        value = msg.value / 2;
        require((2 * value) == msg.value, "Value has to be even."); 
    }
    
    modifier condition(bool _condition) {  
        require(_condition);                 
        _;
    }
    
    modifier onlyBuyer() {
        require(
            msg.sender == buyer,
            "Only buyer can call this."      
        );                                              
        _;
    }
    
    modifier onlySeller() {
        require(
            msg.sender == seller,
            
        );                                                            
        _;
    }
    
    modifier inState(State _state) {
        require(
            state == _state,
            "Invalid state."            
        );
        _;
    }
    event Aborted();                
    event PurchaseConfirmed();   
    event ItemReceived();             

    function abort()                           
        public
        onlySeller                              
        inState(State.Created)
    {
        emit Aborted();                      
        state = State.Inactive;
        seller.transfer(this.balance);
    }
    function confirmPurchase()   
        public
        inState(State.Created)             
        condition(msg.value == (2 * value))   
        payable                             
    {
        emit PurchaseConfirmed();     
        buyer = msg.sender;
        state = State.Locked;          
    }
        function confirmReceived()       
        public
        onlyBuyer
        inState(State.Locked)
    {
        emit ItemReceived();           
                state = State.Inactive;
           buyer.transfer(value);
        seller.transfer(this.balance);
    }

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).