Skip to content

Instantly share code, notes, and snippets.

@p3c-bot
Created November 24, 2018 17:13
Show Gist options
  • Save p3c-bot/79d3e8eeb4a76e4f7f36c274100a0954 to your computer and use it in GitHub Desktop.
Save p3c-bot/79d3e8eeb4a76e4f7f36c274100a0954 to your computer and use it in GitHub Desktop.
pragma solidity 0.4.21;
contract Hourglass {
function transfer(address, uint256) public returns(bool){}
function buy(address) public payable returns(uint256) {}
}
contract Escrow {
struct Job {
bool started;
bool complete;
bytes32 passwordHash;
uint256 timeout;
uint256 value;
uint8 percentP3C;
address labor;
address customer;
bytes32 oraclePasswordHash;
}
mapping(bytes32 => Job) public jobs;
Hourglass p3c;
address public p3cAddress = 0xDF9AaC76b722B08511A4C561607A9bf3AfA62E49;
event CreatedJob(bytes32 jobId);
event ExpiredJob(bytes32 jobId);
event SuccessfulJob(bytes32 jobId);
event OverrideJob(bytes32 jobId, bool payLabor);
function Escrow() public {
p3c = Hourglass(p3cAddress);
}
function createJob(bytes32 jobId, bytes32 passwordHash, uint256 expirationSeconds, uint8 percentP3C, address labor, address customer, bytes32 oraclePasswordHash)
public
payable
returns(bytes32) {
require(jobs[jobId].started == false);
uint256 timeout = SafeMath.add(now, expirationSeconds);
// started = true, complete = false, expirationTime is seconds from now, percentP3C is int 5 = 5%
Job memory newJob = Job(true, false, passwordHash, timeout, msg.value, percentP3C, labor, customer, oraclePasswordHash);
jobs[jobId] = newJob;
emit CreatedJob(jobId);
}
// given a hash and the password, evaluate whether the job was completed.
function evaluate(bytes32 jobId, string password)
public {
Job storage currentJob = jobs[jobId];
require(currentJob.started == true);
require(currentJob.complete == false);
require(keccak256(password) == currentJob.passwordHash);
// job has timed out
if(currentJob.timeout < now){
(currentJob.customer).transfer(currentJob.value);
currentJob.complete = true;
emit ExpiredJob(jobId);
} else {
// collect the community tax, turn to p3c, and pass it to labor
// uint256 taxedValue = buyAndTransferP3C(currentJob.value, 5, currentJob.labor, currentJob.labor);
// (currentJob.labor).transfer(taxedValue);
(currentJob.labor).transfer(currentJob.value);
// job was completed successfully
currentJob.complete = true;
emit SuccessfulJob(jobId);
}
}
function oracleOverride(bytes32 jobId, bool payLabor, string oraclePassword)
public {
Job storage currentJob = jobs[jobId];
require(currentJob.started == true);
require(keccak256(oraclePassword) == currentJob.oraclePasswordHash);
if(payLabor){
// collect the community tax, turn to p3c, and pass it to labor
// uint256 taxedValue = buyAndTransferP3C(currentJob.value, 5, currentJob.labor, currentJob.labor);
// (currentJob.labor).transfer(taxedValue);
(currentJob.labor).transfer(currentJob.value);
// job was completed successfully
currentJob.complete = true;
} else {
// refund the user
(currentJob.customer).transfer(currentJob.value);
currentJob.complete = true;
}
emit OverrideJob(jobId, payLabor);
}
function buyAndTransferP3C(uint256 value, uint8 p3cTax, address destination, address masternode)
private
returns (uint256) {
uint256 taxedEtc = SafeMath.div(SafeMath.mul(value, p3cTax), 100);
uint256 tokensBought = p3c.buy.value(taxedEtc)(masternode);
p3c.transfer(destination, tokensBought);
return SafeMath.sub(value, taxedEtc);
}
}
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment