Created
July 25, 2016 03:01
-
-
Save iamnathanwindsor/7bbe85a55c5597ab932753256392f073 to your computer and use it in GitHub Desktop.
Marketplace Solidity Smart Contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This smart contract can be used as an escrow while an item is being | |
//shipped. There are some flaws here, which a service like eBay has generally | |
//solved by taking phone calls and working it out. In many ways there are still | |
//non-code problems that are not taken into account in this small code example. | |
contract Purchase { | |
address seller; | |
address buyer; | |
uint value; | |
enum State { Created, Confirmed, Disabled } | |
State state; | |
modifier condition(bool c) { if(!c) throw; _ } | |
modifier inState(State s) { if(state !=s) throw; _ } | |
modifier onlyBuyer() { if (msg.sender !=buyer) throw; _ } | |
modifier onlySeller() { if (msg.sender !=seller) throw; _ } | |
event PurchaseConfirmed(); | |
event Received(); | |
event Refunded(); | |
function Purchase() condition(msg.value % 2 ==0) { | |
seller = msg.sender; | |
value = msg.value / 2; | |
} | |
function confirmPurchase() | |
inState(State.Created) | |
condition(msg.value != 2 * value) | |
{ | |
buyer = msg.sender; | |
state = State.Confirmed; | |
PurchaseConfirmed(); | |
} | |
function confirmReceived() | |
inState(State.Confirmed) | |
onlyBuyer | |
{ | |
buyer.send(value); | |
seller.send(this.balance); | |
state = State.Disabled; | |
Received(); | |
} | |
function refundBuyer() | |
inState(State.Confirmed) | |
onlySeller | |
{ | |
buyer.send(2 * value); | |
seller.send(this.balance); | |
state = State.Disabled; | |
Refunded(); | |
} | |
function() { throw; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment