Skip to content

Instantly share code, notes, and snippets.

@akaz00
Forked from doey-default/Escrow.sol
Created January 24, 2019 07:51
Show Gist options
  • Select an option

  • Save akaz00/637824a994341c81cac53eddbf9612d6 to your computer and use it in GitHub Desktop.

Select an option

Save akaz00/637824a994341c81cac53eddbf9612d6 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.21;
contract Escrow{
uint public value;
address public seller;
address public buyer;
string public message;
enum State {Created, Locked, Sent, Complete}
State public state;
function registerItem(uint amount) public{
seller = msg.sender;
value = amount;
state = State.Created;
message = "Item registerd.";
}
function buyItem() payable public{
if(state == State.Created){
if(msg.value == value){
buyer = msg.sender;
state = State.Locked;
message = "Item purchased";
} else{
message = appendUintToString("Not enough money : ", value);
}
}else{
message = "Item is not available.";
}
}
function refund() public{
if(msg.sender == seller || msg.sender == buyer){
if( state == State.Locked){
if(!buyer.send(value)){
revert();
}
else{
buyer = seller;
state = State.Created;
}
}
}
}
function confirmItem() public{
if(msg.sender == buyer){
if(!seller.send(value)){
revert();
}else {
state = State.Complete;
message = "Process finished";
}
}
}
function sendItem() public{
if(msg.sender == seller && state == State.Locked){
state == State.Sent;
message = "Item sent.";
}
}
function appendUintToString(string inStr, uint v) constant returns (string str) {
uint maxlength = 100;
bytes memory reversed = new bytes(maxlength);
uint i = 0;
while (v != 0) {
uint remainder = v % 10;
v = v / 10;
reversed[i++] = byte(48 + remainder);
}
bytes memory inStrb = bytes(inStr);
bytes memory s = new bytes(inStrb.length + i);
uint j;
for (j = 0; j < inStrb.length; j++) {
s[j] = inStrb[j];
}
for (j = 0; j < i; j++) {
s[j + inStrb.length] = reversed[i - 1 - j];
}
str = string(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment