Skip to content

Instantly share code, notes, and snippets.

@RoxasShadow
Last active January 28, 2018 00:07
Show Gist options
  • Save RoxasShadow/2a15b6f019490ea88cf814f191d634fd to your computer and use it in GitHub Desktop.
Save RoxasShadow/2a15b6f019490ea88cf814f191d634fd to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.19;
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract TalEmplo {
using SafeMath for uint256;
modifier onlyEmployer {
require(msg.sender == employer);
_;
}
modifier onlyTalent {
require(msg.sender == talent);
_;
}
modifier notFrozenYet {
require(freezeTime == 0);
_;
}
modifier contractFrozen {
require(freezeTime > 0);
_;
}
modifier deadlineNotReached {
require(now < deadline);
_;
}
modifier deadlineIsReached {
require(now >= deadline);
_;
}
modifier fundsNotWithdrawn {
require(!fundsWithdrawn);
_;
}
// who store and freeze the funds in the contract
address public employer;
// who withdraw the funds from the contract
address public talent;
// funds sent by the employer that eventually will be send to the talent
uint256 public funds;
// the cap that is needed to the contract to start
uint256 public amountToStore;
// true if funds have been sent to the talent
bool public fundsWithdrawn = false;
// when the freeze period starts after that the cap is reached
uint public freezeTime;
// when the funds get unfrozen
uint public deadline;
function TalEmplo(address _employer, address _talent, uint256 _amountToStore, uint _deadline) public {
// employer address is defined
require(_employer != address(0));
// talent address is defined
require(_talent != address(0));
// the cap to reach is greater than 0
require(_amountToStore > 0);
// deadline is reached (in days)
require(_deadline > 0);
employer = _employer;
talent = _talent;
amountToStore = _amountToStore;
deadline = now + (deadline * 1 days); // TODO: set along to freezeTime?
}
// Default function. Called when a user sends ETH to the contract.
function () public payable onlyEmployer notFrozenYet fundsNotWithdrawn deadlineNotReached {
// the cap has not been reached yet
require(funds < amountToStore);
// the amount won't exceed the cap
uint256 amount = funds.add(msg.value);
require(amount <= amountToStore);
funds = amount;
// freeze the contract if the cap is reached
if(funds == amountToStore) {
freezeTime = now;
}
}
// Allows the employer to withdraw the funds when the contract has not started yet
function refund() public onlyEmployer notFrozenYet fundsNotWithdrawn {
// the cap has not been reached
require(funds < amountToStore);
// send the funds to the employer
employer.transfer(funds);
}
// Allows the talent to withdraw the amount that the employer sent to the contract
function withdraw() public onlyTalent contractFrozen fundsNotWithdrawn deadlineIsReached {
// send the funds and throw an error if fail
talent.transfer(funds);
// close the contract
fundsWithdrawn = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment