Skip to content

Instantly share code, notes, and snippets.

@pelle
Last active January 8, 2017 22:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pelle/c642c79e1750a7ad5262 to your computer and use it in GitHub Desktop.
Save pelle/c642c79e1750a7ad5262 to your computer and use it in GitHub Desktop.
contract EscrowContract {
address buyer;
address seller;
address agent;
// Each party has an entry here with the timestamp of their acceptance
uint[3] acceptances;
bool active;
function EscrowContract(address _agent, address _seller) {
// In this simple example, the person sending money is the buyer and
// sets up the initial contract
buyer = msg.sender;
agent = _agent;
seller = _seller;
active = false;
acceptances[0] = now; // As part of the creation of the contract the buyer accepts
}
// This only allows function if contract is active
modifier onlywhenactive { if (!active) throw; _ }
// This only allows agent to perform function
modifier onlyagent { if (msg.sender != agent) throw; _ }
// This only allows parties of the contract to perform function
modifier onlyparties {
if ((msg.sender != buyer) && (msg.sender != seller) && (msg.sender != agent))
throw; _
}
// Any party to the contract can accept the contract
function accept() onlyparties returns(bool) {
uint party_index;
// First find the index in the acceptances array
if (msg.sender == seller)
party_index = 1;
else if (msg.sender == agent)
party_index = 2;
if (acceptances[party_index] > 0)
throw;
else
acceptances[party_index] = now;
if (acceptances[0]>0 && acceptances[1]>0 && acceptances[2]>0)
active = true;
return active;
}
function release() onlywhenactive onlyagent {
suicide(seller); // Send all funds to seller
}
function cancel() onlyparties {
// Any party can cancel the contract before it is active
if (!active || (msg.sender == agent))
suicide(buyer); // Cancel escrow and return all funds to buyer
else
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment