Skip to content

Instantly share code, notes, and snippets.

Created June 11, 2017 16:59
Show Gist options
  • Save anonymous/5ba57c2705b8265481cbb658dc3bccd9 to your computer and use it in GitHub Desktop.
Save anonymous/5ba57c2705b8265481cbb658dc3bccd9 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.11+commit.68ef5810.js&optimize=undefined&gist=
pragma solidity ^0.4.11;
contract Lease {
uint public value;
address public owner;
address public tenant;
enum State { Created, Locked, Inuse, Inactive }
State public state;
function Lease() payable {
owner = msg.sender;
value = msg.value / 3;
require((3 * value) == msg.value);
}
modifier condition(bool _condition) {
require(_condition);
_;
}
modifier onlyTenant() {
require(msg.sender == tenant);
_;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
modifier inState(State _state) {
require(state == _state);
_;
}
event Aborted();
event LeaseConfirmed();
event KeyReceived();
event KeyReturned();
/// Abort the lease and reclaim the ether.
/// Can only be called by the seller before
/// the contract is locked.
function abort()
onlyOwner
inState(State.Created)
{
Aborted();
state = State.Inactive;
owner.transfer(this.balance);
}
/// Confirm the lease as tenant.
/// Transaction has to include `3 * value` ether.
/// The ether will be locked until confirmReceived
/// is called.
function confirmLease()
inState(State.Created)
condition(msg.value == (3 * value))
payable
{
LeaseConfirmed();
tenant = msg.sender;
state = State.Locked;
}
/// Confirm that you (the tenant) received the key.
/// This will release 2/3 of the locked ether.
function confirmReceived()
onlyTenant
inState(State.Locked)
{
KeyReceived();
state = State.Inuse;
owner.transfer(2 * value);
}
/// Confirm that you (the owner) received the key.
/// This will release 1/3 of the locked ether.
function confirmReturned()
onlyOwner
inState(State.Inuse)
{
KeyReturned();
state = State.Inactive;
tenant.transfer(this.balance);
}
}
pragma solidity ^0.4.11;
contract Purchase {
uint public value;
address public seller;
address public buyer;
enum State { Created, Locked, Inactive }
State public state;
function Purchase() payable {
seller = msg.sender;
value = msg.value / 2;
require((2 * value) == msg.value);
}
modifier condition(bool _condition) {
require(_condition);
_;
}
modifier onlyBuyer() {
require(msg.sender == buyer);
_;
}
modifier onlySeller() {
require(msg.sender == seller);
_;
}
modifier inState(State _state) {
require(state == _state);
_;
}
event Aborted();
event PurchaseConfirmed();
event ItemReceived();
/// Abort the purchase and reclaim the ether.
/// Can only be called by the seller before
/// the contract is locked.
function abort()
onlySeller
inState(State.Created)
{
Aborted();
state = State.Inactive;
seller.transfer(this.balance);
}
/// Confirm the purchase as buyer.
/// Transaction has to include `2 * value` ether.
/// The ether will be locked until confirmReceived
/// is called.
function confirmPurchase()
inState(State.Created)
condition(msg.value == (2 * value))
payable
{
PurchaseConfirmed();
buyer = msg.sender;
state = State.Locked;
}
/// Confirm that you (the buyer) received the item.
/// This will release the locked ether.
function confirmReceived()
onlyBuyer
inState(State.Locked)
{
ItemReceived();
// It is important to change the state first because
// otherwise, the contracts called using `send` below
// can call in again here.
state = State.Inactive;
// NOTE: This actually allows both the buyer and the seller to
// block the refund - the withdraw pattern should be used.
buyer.transfer(value);
seller.transfer(this.balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment